mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-14 01:10:02 -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
|
||||||
|
|
@ -3,19 +3,19 @@
|
||||||
;; Variable and field declarations
|
;; Variable and field declarations
|
||||||
((variable_declarator
|
((variable_declarator
|
||||||
(identifier) @definition)
|
(identifier) @definition)
|
||||||
(set! kind "v"))
|
(set! definition.kind "v"))
|
||||||
|
|
||||||
((variable_declarator
|
((variable_declarator
|
||||||
(field_expression object:(*) @definition.associated (property_identifier) @definition))
|
(field_expression object:(*) @definition.associated (property_identifier) @definition))
|
||||||
(set! kind "v"))
|
(set! difinition.kind "v"))
|
||||||
|
|
||||||
;; Parameters
|
;; Parameters
|
||||||
((local_function
|
((local_function
|
||||||
(parameters (identifier) @definition))
|
(parameters (identifier) @definition))
|
||||||
(set! kind "v"))
|
(set! definition.kind "v"))
|
||||||
((function
|
((function
|
||||||
(parameters (identifier) @definition))
|
(parameters (identifier) @definition))
|
||||||
(set! kind "v"))
|
(set! definition.kind "v"))
|
||||||
|
|
||||||
;; Function definitions
|
;; Function definitions
|
||||||
;; Functions definitions creates both a definition and a new scope
|
;; Functions definitions creates both a definition and a new scope
|
||||||
|
|
@ -23,15 +23,15 @@
|
||||||
(function_name_field
|
(function_name_field
|
||||||
object: (identifier) @definition.associated
|
object: (identifier) @definition.associated
|
||||||
(property_identifier) @definition)) @scope
|
(property_identifier) @definition)) @scope
|
||||||
(set! kind "m"))
|
(set! definition.kind "m"))
|
||||||
|
|
||||||
((function
|
((function
|
||||||
(function_name (identifier) @definition)) @scope
|
(function_name (identifier) @definition)) @scope
|
||||||
(set! kind "f"))
|
(set! definition.kind "f"))
|
||||||
|
|
||||||
((local_function
|
((local_function
|
||||||
(identifier) @definition) @scope
|
(identifier) @definition) @scope
|
||||||
(set! kind "f"))
|
(set! definition.kind "f"))
|
||||||
|
|
||||||
((if_statement) @scope)
|
((if_statement) @scope)
|
||||||
((for_in_statement) @scope)
|
((for_in_statement) @scope)
|
||||||
|
|
@ -39,7 +39,7 @@
|
||||||
;; Loops
|
;; Loops
|
||||||
((loop_expression
|
((loop_expression
|
||||||
(identifier) @definition)
|
(identifier) @definition)
|
||||||
(set! kind "v"))
|
(set! definition.kind "v"))
|
||||||
|
|
||||||
;;; REFERENCES
|
;;; REFERENCES
|
||||||
((identifier) @reference)
|
((identifier) @reference)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue