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:
Thomas Vigouroux 2020-04-19 09:53:44 +02:00
parent f784a0adda
commit b706c4e8ed
2 changed files with 90 additions and 8 deletions

View 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

View file

@ -3,19 +3,19 @@
;; Variable and field declarations
((variable_declarator
(identifier) @definition)
(set! kind "v"))
(set! definition.kind "v"))
((variable_declarator
(field_expression object:(*) @definition.associated (property_identifier) @definition))
(set! kind "v"))
(set! difinition.kind "v"))
;; Parameters
((local_function
(parameters (identifier) @definition))
(set! kind "v"))
(set! definition.kind "v"))
((function
(parameters (identifier) @definition))
(set! kind "v"))
(set! definition.kind "v"))
;; Function definitions
;; Functions definitions creates both a definition and a new scope
@ -23,15 +23,15 @@
(function_name_field
object: (identifier) @definition.associated
(property_identifier) @definition)) @scope
(set! kind "m"))
(set! definition.kind "m"))
((function
(function_name (identifier) @definition)) @scope
(set! kind "f"))
(set! definition.kind "f"))
((local_function
(identifier) @definition) @scope
(set! kind "f"))
(set! definition.kind "f"))
((if_statement) @scope)
((for_in_statement) @scope)
@ -39,7 +39,7 @@
;; Loops
((loop_expression
(identifier) @definition)
(set! kind "v"))
(set! definition.kind "v"))
;;; REFERENCES
((identifier) @reference)