@@ -8,6 +8,7 @@ local ok, ts_locals = pcall(require, "nvim-treesitter.locals")
88
99if not ok then
1010 error (" treesitter not installed" )
11+ return nil
1112end
1213
1314local parsers = require " nvim-treesitter.parsers"
@@ -84,8 +85,7 @@ function M.find_definition(range, bufnr)
8485 if not root then
8586 return
8687 end
87- local node_at_point = root :named_descendant_for_range (symbolpos [1 ], symbolpos [2 ], symbolpos [1 ],
88- symbolpos [2 ])
88+ local node_at_point = root :named_descendant_for_range (symbolpos [1 ], symbolpos [2 ], symbolpos [1 ], symbolpos [2 ])
8989 if not node_at_point then
9090 lerr (" no node at cursor" )
9191 return
@@ -103,8 +103,7 @@ function M.find_definition(range, bufnr)
103103 local r , c = definition :range ()
104104 return {start = {line = r , character = c }}
105105 else
106- trace (" error: def not found in " , bufnr , definition :range (), definition :type (),
107- definition :parent ():type ())
106+ trace (" error: def not found in " , bufnr , definition :range (), definition :type (), definition :parent ():type ())
108107 end
109108end
110109
@@ -184,8 +183,7 @@ local function get_scope(type, source)
184183 end
185184
186185 if type == " var" and next ~= nil then
187- if next :type () == " function" or next :type () == " arrow_function" or next :type ()
188- == " function_definition" then
186+ if next :type () == " function" or next :type () == " arrow_function" or next :type () == " function_definition" then
189187 trace (current :type (), current :range ())
190188 return next , true
191189 elseif parent :type () == ' function_declaration' then
@@ -371,8 +369,7 @@ local function get_all_nodes(bufnr, filter, summary)
371369 if is_func then
372370 -- hack for lua and maybe other language aswell
373371 local parent = tsdata :parent ()
374- if parent ~= nil and parent :type () == ' function_name' or parent :type ()
375- == ' function_name_field' then
372+ if parent ~= nil and parent :type () == ' function_name' or parent :type () == ' function_name_field' then
376373 item .node_text = ts_utils .get_node_text (parent , bufnr )[1 ]
377374 log (parent :type (), item .node_text )
378375 end
@@ -403,8 +400,7 @@ local function get_all_nodes(bufnr, filter, summary)
403400 if item .node_text == " _" then
404401 goto continue
405402 end
406- item .full_text = vim .trim (api .nvim_buf_get_lines (bufnr , start_line_node , start_line_node + 1 ,
407- false )[1 ] or " " )
403+ item .full_text = vim .trim (api .nvim_buf_get_lines (bufnr , start_line_node , start_line_node + 1 , false )[1 ] or " " )
408404
409405 item .full_text = item .full_text :gsub (' %s*[%[%(%{]*%s*$' , ' ' )
410406 item .uri = uri
@@ -419,8 +415,7 @@ local function get_all_nodes(bufnr, filter, summary)
419415 indent = string.rep (" " , # parents - 1 ) .. " "
420416 end
421417
422- item .text = string.format (" %s %s%-10s\t %s" , item .kind , indent , item .node_text ,
423- item .full_text )
418+ item .text = string.format (" %s %s%-10s\t %s" , item .kind , indent , item .node_text , item .full_text )
424419 if # item .text > length then
425420 length = # item .text
426421 end
@@ -580,4 +575,34 @@ function M.get_node_at_line(lnum)
580575 return node
581576end
582577
578+ local usage_namespace = vim .api .nvim_create_namespace (" nvim-treesitter-usages" )
579+
580+ function M .highlight_usages (bufnr )
581+ M .clear_usage_highlights (bufnr )
582+
583+ local node_at_point = ts_utils .get_node_at_cursor ()
584+ local references = locals .get_references (bufnr )
585+
586+ if not node_at_point or not vim .tbl_contains (references , node_at_point ) then
587+ return
588+ end
589+
590+ local def_node , scope = locals .find_definition (node_at_point , bufnr )
591+ local usages = locals .find_usages (def_node , scope , bufnr )
592+
593+ for _ , usage_node in ipairs (usages ) do
594+ if usage_node ~= node_at_point then
595+ ts_utils .highlight_node (usage_node , bufnr , usage_namespace , " TSDefinitionUsage" )
596+ end
597+ end
598+
599+ if def_node ~= node_at_point then
600+ ts_utils .highlight_node (def_node , bufnr , usage_namespace , " TSDefinition" )
601+ end
602+ end
603+
604+ function M .clear_usage_highlights (bufnr )
605+ api .nvim_buf_clear_namespace (bufnr , usage_namespace , 0 , - 1 )
606+ end
607+
583608return M
0 commit comments