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

@@ -15,11 +15,7 @@ jobs:
manager: sudo apt-get
packages: -y ripgrep
- os: ubuntu-22.04
rev: v0.7.2/nvim-linux64.tar.gz
manager: sudo apt-get
packages: -y ripgrep
- os: ubuntu-22.04
rev: v0.8.1/nvim-linux64.tar.gz
rev: v0.9.0/nvim-linux64.tar.gz
manager: sudo apt-get
packages: -y ripgrep
- os: macos-12
@@ -27,11 +23,7 @@ jobs:
manager: brew
packages: ripgrep
- os: macos-12
rev: v0.7.2/nvim-macos.tar.gz
manager: brew
packages: ripgrep
- os: macos-12
rev: v0.8.1/nvim-macos.tar.gz
rev: v0.9.0/nvim-macos.tar.gz
manager: brew
packages: ripgrep
steps:

View File

@@ -5,7 +5,7 @@ on: [push, pull_request]
jobs:
luacheck:
name: Luacheck
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
@@ -20,7 +20,7 @@ jobs:
stylua:
name: stylua
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: JohnnyMorganz/stylua-action@v2

View File

@@ -42,7 +42,7 @@ Telescope Wiki</sub>
This section should guide you to run your first builtin pickers.
[Neovim (v0.7.0)](https://github.com/neovim/neovim/releases/tag/v0.7.0) or the
[Neovim (v0.9.0)](https://github.com/neovim/neovim/releases/tag/v0.9.0) or the
latest neovim nightly commit is required for `telescope.nvim` to work.
### Required dependencies

View File

@@ -506,17 +506,14 @@ telescope.setup({opts}) *telescope.setup()*
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: ""
@@ -2295,6 +2292,14 @@ utils.transform_path({opts}, {path}) *telescope.utils.transform_path()*
string: The transformed path ready to be displayed
utils.has_ts_parser({lang}) *telescope.utils.has_ts_parser()*
Checks if treesitter parser for language is installed
Parameters: ~
{lang} (string)
utils.notify({funname}, {opts}) *telescope.utils.notify()*
Telescope Wrapper around vim.notify

View File

@@ -255,4 +255,17 @@ following breaking changes are included in this PR:
<
*telescope.changelog-2499*
Date: May 24, 2023
PR: https://github.com/nvim-telescope/telescope.nvim/pull/2499
We decided to bump the minimum Neovim version to 0.9.0, in order to remove a
couple of no longer required workarounds. That includes using upstream
treesitter implementation in favor of nvim-treesitter.
If you still have a requirement for Neovim 0.7 or 0.8, we also have a stable
branch 0.1.x (or version, currently 0.1.1) which will not receive this version
bump and will continue to offer support for older Neovim versions.
vim:tw=78:ts=8:ft=help:norl:

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

View File

@@ -1,5 +1,5 @@
if 1 ~= vim.fn.has "nvim-0.7.0" then
vim.api.nvim_err_writeln "Telescope.nvim requires at least nvim-0.7.0. See `:h telescope.changelog-1851`"
if 1 ~= vim.fn.has "nvim-0.9.0" then
vim.api.nvim_err_writeln "Telescope.nvim requires at least nvim-0.9.0. See `:h telescope.changelog-2499`"
return
end
@@ -118,7 +118,7 @@ end, {
if n == 0 then
return vim.tbl_filter(function(val)
return vim.startswith(val, l[2])
end, vim.tbl_flatten({builtin_list, extensions_list}))
end, vim.tbl_flatten { builtin_list, extensions_list })
end
if n == 1 then