Skip to content

Commit 63a82dc

Browse files
committed
fix(treesitter): standalone implementation
Drop hard nvim-treesitter requirement, but assumes parser and `locals` query (using the captures documented here: https://github.com/nvim-treesitter/nvim-treesitter/blob/main/CONTRIBUTING.md#locals) is available on `runtimepath`.
1 parent d143e80 commit 63a82dc

File tree

3 files changed

+19
-33
lines changed

3 files changed

+19
-33
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ wiki.
6868
### Optional dependencies
6969

7070
- [sharkdp/fd](https://github.com/sharkdp/fd) (finder)
71-
- [nvim-treesitter/nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) (finder/preview)
7271
- [neovim LSP](https://neovim.io/doc/user/lsp.html) (picker)
7372
- [devicons](https://github.com/nvim-tree/nvim-web-devicons) (icons)
7473

lua/telescope/builtin/__files.lua

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -397,49 +397,41 @@ files.find_files = function(opts)
397397
:find()
398398
end
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
415401
files.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

lua/telescope/health.lua

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ local optional_dependencies = {
3030

3131
local required_plugins = {
3232
{ lib = "plenary", optional = false },
33-
{
34-
lib = "nvim-treesitter",
35-
optional = true,
36-
info = "(Required for `:Telescope treesitter`.)",
37-
},
3833
}
3934

4035
local check_binary_installed = function(package)

0 commit comments

Comments
 (0)