diff options
| author | Thomas Vigouroux <tomvig38@gmail.com> | 2020-04-19 09:53:44 +0200 |
|---|---|---|
| committer | Thomas Vigouroux <tomvig38@gmail.com> | 2020-04-19 17:28:52 +0200 |
| commit | b706c4e8edd20dfe5ec126479c3fc7308896f9c7 (patch) | |
| tree | 7e252146446d431ed5278745aaa1b802c9100585 /lua | |
| parent | Merge pull request #2 from kyazdani42/install-parser (diff) | |
| download | nvim-treesitter-b706c4e8edd20dfe5ec126479c3fc7308896f9c7.tar nvim-treesitter-b706c4e8edd20dfe5ec126479c3fc7308896f9c7.tar.gz nvim-treesitter-b706c4e8edd20dfe5ec126479c3fc7308896f9c7.tar.bz2 nvim-treesitter-b706c4e8edd20dfe5ec126479c3fc7308896f9c7.tar.lz nvim-treesitter-b706c4e8edd20dfe5ec126479c3fc7308896f9c7.tar.xz nvim-treesitter-b706c4e8edd20dfe5ec126479c3fc7308896f9c7.tar.zst nvim-treesitter-b706c4e8edd20dfe5ec126479c3fc7308896f9c7.zip | |
feat: first version of locals
Locals will be the main interface to treesitter, through some functions:
get_definitions(bufnr) : returns all the definitions in bufnr
get_scopes(bufnr): returns all definitions in bufnr
get_references(bufnr): returns all references in bufnr
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-treesitter/locals.lua | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua new file mode 100644 index 000000000..cd8a3e306 --- /dev/null +++ b/lua/nvim-treesitter/locals.lua @@ -0,0 +1,82 @@ +-- Functions to handle locals +-- Locals are a generalization of definition and scopes +-- its the way nvim-treesitter uses to "understand" the code +local api = vim.api +local ts = vim.treesitter +local queries = require'nvim-treesitter.query' +local parsers = require'nvim-treesitter.parsers' + +local M = { + locals={} +} + +function M.collect_locals(bufnr) + local ft = api.nvim_buf_get_option(bufnr, "ft") + local query = queries.get_query(ft, "locals") + + if ft then + local query = queries.get_query(ft, 'locals') + local parser = parsers.get_parser(bufnr) + + if parser then + local root = parser:parse():root() + local start_row, _, end_row, _ = root:range() + + local locals = {} + + for prepared_match in queries.iter_prepared_matches(query, root, bufnr, start_row, end_row) do + table.insert(locals, prepared_match) + end + + return locals + end + end +end + +function M.on_lines(_, buf, _, firstline, lastline, new_lastline, _) + M.locals[buf] = M.collect_locals(buf) +end + +function M.get_definitions(bufnr) + local locals = M.locals[bufnr] + + local defs = {} + + for _, loc in ipairs(locals) do + if loc.definition then + table.insert(defs, {definition=loc.definition, kind=loc.kind}) + end + end + + return defs +end + +function M.get_scopes(bufnr) + local locals = M.locals[bufnr] + + local scopes = {} + + for _, loc in ipairs(locals) do + if loc.scope then + table.insert(scopes, loc.scope) + end + end + + return scopes +end + +function M.get_references(bufnr) + local locals = M.locals[bufnr or api.nvim_get_current_buf()] + + local refs = {} + + for _, loc in ipairs(locals) do + if loc.reference then + table.insert(refs, loc.reference) + end + end + + return refs +end + +return M |
