mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-02 11:36:54 -04:00
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
This commit is contained in:
parent
f784a0adda
commit
b706c4e8ed
2 changed files with 90 additions and 8 deletions
82
lua/nvim-treesitter/locals.lua
Normal file
82
lua/nvim-treesitter/locals.lua
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue