From d8c5ed4e407c5c0a347d74ea091c3d56fbb2e7f2 Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Wed, 24 May 2023 10:43:04 +0200 Subject: [PATCH] feat(ts)!: use upstream treesitter implementation (#2499) bumps minimum required neovim version to 0.9, see `help telescope.changelog-2499` --- .github/workflows/ci.yml | 12 ++-------- .github/workflows/lint.yml | 4 ++-- README.md | 2 +- doc/telescope.txt | 27 +++++++++++++---------- doc/telescope_changelog.txt | 13 +++++++++++ lua/telescope/builtin/__files.lua | 35 +++++------------------------- lua/telescope/config.lua | 19 +++++++--------- lua/telescope/health.lua | 2 +- lua/telescope/previewers/utils.lua | 35 ++++-------------------------- lua/telescope/utils.lua | 6 +++++ plugin/telescope.lua | 6 ++--- 11 files changed, 62 insertions(+), 99 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5c8bb88..f8aa989 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 99a3072..bf781b4 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -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 diff --git a/README.md b/README.md index 060ceb0..43520f9 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Telescope Wiki 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 diff --git a/doc/telescope.txt b/doc/telescope.txt index 5e95c49..bf4c55c 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -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 diff --git a/doc/telescope_changelog.txt b/doc/telescope_changelog.txt index fbbb471..2d07c0d 100644 --- a/doc/telescope_changelog.txt +++ b/doc/telescope_changelog.txt @@ -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: diff --git a/lua/telescope/builtin/__files.lua b/lua/telescope/builtin/__files.lua index e949ccb..1a1757d 100644 --- a/lua/telescope/builtin/__files.lua +++ b/lua/telescope/builtin/__files.lua @@ -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() diff --git a/lua/telescope/config.lua b/lua/telescope/config.lua index 1e1f866..9d1b6ba 100644 --- a/lua/telescope/config.lua +++ b/lua/telescope/config.lua @@ -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: "╱" diff --git a/lua/telescope/health.lua b/lua/telescope/health.lua index d92c822..a5956f4 100644 --- a/lua/telescope/health.lua +++ b/lua/telescope/health.lua @@ -38,7 +38,7 @@ local required_plugins = { { lib = "nvim-treesitter", optional = true, - info = "", + info = "(Required for `:Telescope treesitter`.)", }, } diff --git a/lua/telescope/previewers/utils.lua b/lua/telescope/previewers/utils.lua index 2369305..6ca9bb2 100644 --- a/lua/telescope/previewers/utils.lua +++ b/lua/telescope/previewers/utils.lua @@ -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 diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index e0c5e24..20ae91c 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -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 diff --git a/plugin/telescope.lua b/plugin/telescope.lua index 5259d3a..add301c 100644 --- a/plugin/telescope.lua +++ b/plugin/telescope.lua @@ -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