feat(ts)!: use upstream treesitter implementation (#2499)

bumps minimum required neovim version to 0.9, see `help telescope.changelog-2499`
This commit is contained in:
Christian Clason
2023-05-24 10:43:04 +02:00
committed by GitHub
parent 057ee0f878
commit d8c5ed4e40
11 changed files with 62 additions and 99 deletions

View File

@@ -383,7 +383,7 @@ files.treesitter = function(opts)
local has_nvim_treesitter, _ = pcall(require, "nvim-treesitter")
if not has_nvim_treesitter then
utils.notify("builtin.treesitter", {
msg = "User need to install nvim-treesitter needs to be installed",
msg = "This picker requires nvim-treesitter",
level = "ERROR",
})
return
@@ -451,16 +451,10 @@ files.current_buffer_fuzzy_find = function(opts)
})
end
local ts_ok, ts_parsers = pcall(require, "nvim-treesitter.parsers")
if ts_ok then
filetype = ts_parsers.ft_to_lang(filetype)
end
local _, ts_configs = pcall(require, "nvim-treesitter.configs")
local parser_ok, parser = pcall(vim.treesitter.get_parser, opts.bufnr, filetype)
local get_query = vim.treesitter.query.get or vim.treesitter.get_query
local query_ok, query = pcall(get_query, filetype, "highlights")
if parser_ok and query_ok and ts_ok and ts_configs.is_enabled("highlight", filetype, opts.bufnr) then
local lang = vim.treesitter.language.get_lang(filetype)
if lang and utils.has_ts_parser(lang) then
local parser = vim.treesitter.get_parser(opts.bufnr, lang)
local query = vim.treesitter.query.get(lang, "highlights")
local root = parser:parse()[1]:root()
local line_highlights = setmetatable({}, {
@@ -471,25 +465,8 @@ files.current_buffer_fuzzy_find = function(opts)
end,
})
-- update to changes on Neovim master, see https://github.com/neovim/neovim/pull/19931
-- TODO(clason): remove when dropping support for Neovim 0.7
local get_hl_from_capture = (function()
if vim.fn.has "nvim-0.8" == 1 then
return function(q, id)
return "@" .. q.captures[id]
end
else
local highlighter = vim.treesitter.highlighter.new(parser)
local highlighter_query = highlighter:get_query(filetype)
return function(_, id)
return highlighter_query:_get_hl_from_capture(id)
end
end
end)()
for id, node in query:iter_captures(root, opts.bufnr, 0, -1) do
local hl = get_hl_from_capture(query, id)
local hl = "@" .. query.captures[id]
if hl and type(hl) ~= "number" then
local row1, col1, row2, col2 = node:range()

View File

@@ -604,17 +604,14 @@ append(
highlighting, which falls back to regex-based highlighting.
`true`: treesitter highlighting for all available filetypes
`false`: regex-based highlighting for all filetypes
`table`: following nvim-treesitters highlighting options:
It contains two keys:
- enable boolean|table: if boolean, enable all ts
highlighing with that flag,
disable still considered.
Containing a list of filetypes,
that are enabled, disabled
ignored because it doesnt make
any sense in this case.
- disable table: containing a list of filetypes
that are disabled
`table`: may contain the following keys:
- enable boolean|table: if boolean, enable ts
highlighting for all supported
filetypes.
if table, ts highlighting is only
enabled for given filetypes.
- disable table: list of filetypes for which ts highlighting
is not used if `enable = true`.
Default: true
- msg_bg_fillchar: Character to fill background of unpreviewable buffers with
Default: ""

View File

@@ -38,7 +38,7 @@ local required_plugins = {
{
lib = "nvim-treesitter",
optional = true,
info = "",
info = "(Required for `:Telescope treesitter`.)",
},
}

View File

@@ -3,10 +3,6 @@ local ts_utils = require "telescope.utils"
local strings = require "plenary.strings"
local conf = require("telescope.config").values
local has_ts, _ = pcall(require, "nvim-treesitter")
local _, ts_configs = pcall(require, "nvim-treesitter.configs")
local _, ts_parsers = pcall(require, "nvim-treesitter.parsers")
local Job = require "plenary.job"
local utils = {}
@@ -131,37 +127,14 @@ utils.regex_highlighter = function(bufnr, ft)
return false
end
local treesitter_attach = function(bufnr, ft)
local lang = ts_parsers.ft_to_lang(ft)
if not ts_configs.is_enabled("highlight", lang, bufnr) then
return false
end
local config = ts_configs.get_module "highlight"
vim.treesitter.highlighter.new(ts_parsers.get_parser(bufnr, lang))
local is_table = type(config.additional_vim_regex_highlighting) == "table"
if
config.additional_vim_regex_highlighting
and (not is_table or vim.tbl_contains(config.additional_vim_regex_highlighting, lang))
then
vim.api.nvim_buf_set_option(bufnr, "syntax", ft)
end
return true
end
-- Attach ts highlighter
utils.ts_highlighter = function(bufnr, ft)
if not has_ts then
has_ts, _ = pcall(require, "nvim-treesitter")
if has_ts then
_, ts_configs = pcall(require, "nvim-treesitter.configs")
_, ts_parsers = pcall(require, "nvim-treesitter.parsers")
if has_filetype(ft) then
local lang = vim.treesitter.language.get_lang(ft)
if lang and ts_utils.has_ts_parser(lang) then
return vim.treesitter.start(bufnr, lang)
end
end
if has_ts and has_filetype(ft) then
return treesitter_attach(bufnr, ft)
end
return false
end

View File

@@ -489,6 +489,12 @@ utils.get_devicons = load_once(function()
end
end)
--- Checks if treesitter parser for language is installed
---@param lang string
utils.has_ts_parser = function(lang)
return pcall(vim.treesitter.language.add, lang)
end
--- Telescope Wrapper around vim.notify
---@param funname string: name of the function that will be
---@param opts table: opts.level string, opts.msg string, opts.once bool