aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lua/nvim-treesitter/locals.lua82
-rw-r--r--queries/lua/locals.scm16
2 files changed, 90 insertions, 8 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
diff --git a/queries/lua/locals.scm b/queries/lua/locals.scm
index a7b15d1e5..4764c021c 100644
--- a/queries/lua/locals.scm
+++ b/queries/lua/locals.scm
@@ -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)