tsql.nvim/repl.md

118 lines
4.5 KiB
Markdown

---
this_is: front-matter
---
# REPL
Walks through how I develop tsql interactively.
Within neovim, I just use `V"+y:<ctrl-v><enter>` to execute these one-liners
```lua
:lua local ts = require('tsql'); ts.t(ts.buf_match.filetype("lua"), ts.ts_query.from_scm("(string) @_"), ts.sink_by.print()):do_nvim(ts.store);
:lua local ts = require('tsql'); ts.t(ts.buf_match.filetype("lua"), ts.ts_query.from_scm("(string) @_"), ts.sink_by.highlight()):do_nvim(ts.store);
:Noh
-- QNodes from current buffer
:lua local ts = require('tsql'); print(vim.inspect(ts.ts_query.from_scm("(string) @_"):find_nodes({ts.QBuf.from_nvim_bufnr(0)})))
```
`do_nvim` only
```lua
:lua local ts = require('tsql'); ts.t(ts.buf_match.filetype("lua"), ts.ts_query.from_scm("(string) @_"), ts.sink_by.highlight()):do_nvim(ts.store);
:lua local ts = require('tsql'); ts.t(ts.buf_match.filetype("lua"), ts.ts_query.from_scm("(identifier) @_"), ts.sink_by.highlight()):do_nvim(ts.store);
-- Highlight all function calls `hello.world("what the heck!")`
:lua local ts = require('tsql'); ts.t(ts.buf_match.filetype("lua"), ts.ts_query.from_scm("(function_call) @_"), ts.sink_by.highlight()):do_nvim(ts.store);
-- Highlight only function calls that are "pure": Not `hello.world` but `world`
:lua local ts = require('tsql'); ts.t(ts.buf_match.filetype("lua"), ts.ts_query.from_scm("(function_call (identifier) @_)"), ts.sink_by.highlight()):do_nvim(ts.store);
-- Highlight function calls, but only the function identifier
:lua local ts = require('tsql'); ts.t(ts.buf_match.filetype("lua"), ts.ts_query.from_scm("[(function_call name: (dot_index_expression) @_) (function_call name: (identifier) @_) (function_call name: (method_index_expression) @_)]"), ts.sink_by.highlight()):do_nvim(ts.store);
-- remove all highlights by this plugin
:Noh
```
Non-tsql poc
```lua
:lua local parser = vim.treesitter.get_parser(0, 'lua'); local tree = parser:parse()[1]; local query = vim.treesitter.parse_query('lua', '(identifier) @name'); for id, node in query:iter_captures(tree:root(), 0) do local name = query.captures[id]; if name == 'name' and vim.treesitter.get_node_text(node, 0) == 'TSQuery' then local sr, sc, er, ec = node:range(); print(string.format("TSQuery Start: (%d, %d), End: (%d, %d)", sr, sc, er, ec)); end; end
```
## Injection features
TL;DR:
- Buffer-oriented parser
- `local buf_parser = vim.treesitter.parser(bufnr: number): LanguageTree`: Buffer-specific parser
- `buf_parser:trees()`: Gets into the nodes within the buffer "host" language (usually based on filetype)
- `buf_parser:children(): table<language(string), LanguageTree>`:
```lua
-- Parser for current buffer
:lua print(vim.inspect(vim.treesitter.get_parser(0)))
-- Using nvim-treesitter (what nvim-treesitter-playground uses)
:lua print(vim.inspect(require('nvim-treesitter.parsers').get_parser(0)))
-- NOTE: currently,
-- `nvim-treesitter.parsers.get_parser(
-- buf_nr: number = factory(vim.api.nvim_get_current_buf),
-- lang: string = factory(get_buf_lang))
-- is just a thin wrapper that has some backwards compatibilty. Not so much of our concern atm.
```
### NOTE: language from vim api
```lua
vim.treesitter.language.get_lang(ft: string)
-- what we're currently using:
vim.treesitter.get_parser(bufnr: number):lang
```
### Parsing with language injection
The current file should be a good tester
```lua
:lua print(vim.inspect(vim.treesitter.get_parser(0):lang())) -- "markdown"
:lua print(vim.inspect(vim.treesitter.language.get_lang(vim.api.nvim_buf_get_option(0, "filetype")))) -- "markdown"
-- lang tree. We can see there are js, css, lua, markdown "children"
-- within each are trees. There is also a `lang_tree._trees`
:lua local lang_tree = vim.treesitter.get_parser(0); print(vim.inspect(lang_tree))
-- This is our main language's tree
:lua local lang_tree = vim.treesitter.get_parser(0); print(vim.inspect(lang_tree:trees()))
-- This is our injected language trees. At the time of writing, `[:markdown-inline, :javascript, ]`
:lua local lang_tree = vim.treesitter.get_parser(0); print(vim.inspect(lang_tree:children()))
-- Content inside injection (markdown)
:lua local
```
## Corpus
just ignore this maybe, these are for upper blocks where it may need language injection
### css-in-js-in-markdown
If your highlighter doesn't support this, maybe think about switching to tree-sitter :)
```js
const my_class = css`
h1, h2, p {
text-align: center;
color: red;
}
`;
const js_ts_limitations = lang.css`
h1, h2, p {
text-align: center;
color: red;
}
`
```
```js some more cases
const my_class = css`h1, h2, p { text-align: center; color: red; }`
```