@@ -397,49 +397,41 @@ files.find_files = function(opts)
397397 :find ()
398398end
399399
400- local function prepare_match (entry , kind )
401- local entries = {}
402-
403- if entry .node then
404- table.insert (entries , entry )
405- else
406- for name , item in pairs (entry ) do
407- vim .list_extend (entries , prepare_match (item , name ))
408- end
409- end
410-
411- return entries
412- end
413-
414400-- TODO: finish docs for opts.show_line
415401files .treesitter = function (opts )
416402 opts .show_line = vim .F .if_nil (opts .show_line , true )
403+ local ts = vim .treesitter
404+ local ft = vim .bo [opts .bufnr ].filetype
405+ local lang = ts .language .get_lang (ft )
417406
418- local has_nvim_treesitter , _ = pcall (require , " nvim-treesitter" )
419- if not has_nvim_treesitter then
407+ if not (lang and ts .language .add (lang )) then
420408 utils .notify (" builtin.treesitter" , {
421- msg = " This picker requires nvim-treesitter " ,
409+ msg = " No parser for the current buffer " ,
422410 level = " ERROR" ,
423411 })
424412 return
425413 end
426-
427- local parsers = require " nvim-treesitter.parsers"
428- if not parsers .has_parser (parsers .get_buf_lang (opts .bufnr )) then
414+ local query = ts .query .get (lang , " locals" )
415+ if not query then
429416 utils .notify (" builtin.treesitter" , {
430- msg = " No parser for the current buffer" ,
417+ msg = " No locals query for the current buffer" ,
431418 level = " ERROR" ,
432419 })
433420 return
434421 end
435422
436- local ts_locals = require " nvim-treesitter.locals"
423+ local parser = assert (ts .get_parser (opts .bufnr ))
424+ parser :parse ()
425+ local root = parser :trees ()[1 ]:root ()
426+
437427 local results = {}
438- for _ , definition in ipairs (ts_locals .get_definitions (opts .bufnr )) do
439- local entries = prepare_match (ts_locals .get_local_nodes (definition ))
440- for _ , entry in ipairs (entries ) do
441- entry .kind = vim .F .if_nil (entry .kind , " " )
442- table.insert (results , entry )
428+ for id , node , _ in query :iter_captures (root , opts .bufnr ) do
429+ local kind = query .captures [id ]
430+
431+ if node and vim .startswith (kind , " local.definition" ) then
432+ kind = kind :gsub (" ^local%.definition" , " " )
433+ kind = kind :gsub (" ^%." , " " )
434+ table.insert (results , { kind = kind , node = node })
443435 end
444436 end
445437
0 commit comments