From 21bfcc3e397961b804241cc056a4c64f214c71f7 Mon Sep 17 00:00:00 2001 From: Ruben Laguna Date: Wed, 25 Sep 2024 03:47:39 +0200 Subject: [PATCH 1/5] feat(tags): add support for displaying tag kind (#3235) * Add kind to builtin.tags picker * Add show_kind option * Make show_kind true by default * [docgen] Update doc/telescope.txt skip-checks: true * lint --------- Co-authored-by: Ruben Laguna Co-authored-by: Github Actions --- lua/telescope/make_entry.lua | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 3f42d176f1..3920e2623d 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -1017,10 +1017,12 @@ end function make_entry.gen_from_ctags(opts) opts = opts or {} + local show_kind = vim.F.if_nil(opts.show_kind, true) local cwd = utils.path_expand(opts.cwd or vim.loop.cwd()) local current_file = Path:new(vim.api.nvim_buf_get_name(opts.bufnr)):normalize(cwd) local display_items = { + { width = 16 }, { remaining = true }, } @@ -1062,6 +1064,7 @@ function make_entry.gen_from_ctags(opts) end, }, entry.tag, + entry.kind, scode, } end @@ -1089,13 +1092,14 @@ function make_entry.gen_from_ctags(opts) return nil end - local tag, file, scode, lnum - -- ctags gives us: 'tags\tfile\tsource' - tag, file, scode = string.match(line, '([^\t]+)\t([^\t]+)\t/^?\t?(.*)/;"\t+.*') + local tag, file, scode, lnum, extension_fields + -- ctags gives us: 'tags\tfile\tsource;"extension_fields' + tag, file, scode, extension_fields = string.match(line, '([^\t]+)\t([^\t]+)\t/^?\t?(.*)/;"\t+(.*)') if not tag then -- hasktags gives us: 'tags\tfile\tlnum' tag, file, lnum = string.match(line, "([^\t]+)\t([^\t]+)\t(%d+).*") end + local kind = string.match(extension_fields or "", "kind:(%S+)") if Path.path.sep == "\\" then file = string.gsub(file, "/", "\\") @@ -1124,6 +1128,9 @@ function make_entry.gen_from_ctags(opts) tag_entry.filename = file tag_entry.col = 1 tag_entry.lnum = lnum and tonumber(lnum) or 1 + if show_kind then + tag_entry.kind = kind + end return setmetatable(tag_entry, mt) end From 6b08cdcc22a630d19172b9db55f7b8db5f81d761 Mon Sep 17 00:00:00 2001 From: James Trew <66286082+jamestrew@users.noreply.github.com> Date: Wed, 25 Sep 2024 02:44:41 +0000 Subject: [PATCH 2/5] docs(builtin.tags): add `show_kind` option (#3304) * docs(builtin.tags): add `show_kind` option * [docgen] Update doc/telescope.txt skip-checks: true --------- Co-authored-by: Github Actions --- doc/telescope.txt | 4 ++++ lua/telescope/builtin/init.lua | 2 ++ 2 files changed, 6 insertions(+) diff --git a/doc/telescope.txt b/doc/telescope.txt index edfbb5c1b4..3aad0466ab 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -1012,6 +1012,8 @@ builtin.tags({opts}) *telescope.builtin.tags()* {show_line} (boolean) if true, shows the content of the line the tag is found on in the picker (default: true) + {show_kind} (boolean) if true and kind info is available, show + the kind of the tag (default: true) {only_sort_tags} (boolean) if true we will only sort tags (default: false) {fname_width} (number) defines the width of the filename section @@ -1033,6 +1035,8 @@ builtin.current_buffer_tags({opts}) *telescope.builtin.current_buffer_tags()* {show_line} (boolean) if true, shows the content of the line the tag is found on in the picker (default: true) + {show_kind} (boolean) if true and kind info is available, show + the kind of the tag (default: true) {only_sort_tags} (boolean) if true we will only sort tags (default: false) {fname_width} (number) defines the width of the filename section diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 9abd53c7ef..6e27c27ab3 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -114,6 +114,7 @@ builtin.current_buffer_fuzzy_find = require_on_exported_call("telescope.builtin. ---@field cwd string: root dir to search from (default: cwd, use utils.buffer_dir() to search relative to open buffer) ---@field ctags_file string: specify a particular ctags file to use ---@field show_line boolean: if true, shows the content of the line the tag is found on in the picker (default: true) +---@field show_kind boolean: if true and kind info is available, show the kind of the tag (default: true) ---@field only_sort_tags boolean: if true we will only sort tags (default: false) ---@field fname_width number: defines the width of the filename section (default: 30) builtin.tags = require_on_exported_call("telescope.builtin.__files").tags @@ -123,6 +124,7 @@ builtin.tags = require_on_exported_call("telescope.builtin.__files").tags ---@field cwd string: root dir to search from (default: cwd, use utils.buffer_dir() to search relative to open buffer) ---@field ctags_file string: specify a particular ctags file to use ---@field show_line boolean: if true, shows the content of the line the tag is found on in the picker (default: true) +---@field show_kind boolean: if true and kind info is available, show the kind of the tag (default: true) ---@field only_sort_tags boolean: if true we will only sort tags (default: false) ---@field fname_width number: defines the width of the filename section (default: 30) builtin.current_buffer_tags = require_on_exported_call("telescope.builtin.__files").current_buffer_tags From cb3f98d935842836cc115e8c9e4b38c1380fbb6b Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Wed, 25 Sep 2024 17:22:16 -0700 Subject: [PATCH 3/5] fix(highlights): previewer show correct highlight group (#3305) Fixes a minor bug in the builtin highlights picker where having `Comment` selected in the picker shows `SpecialComment` in the previewer. Only happens when the selected highlight is a suffix of another highlight and the other highlight occurs first. --- lua/telescope/previewers/buffer_previewer.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/telescope/previewers/buffer_previewer.lua b/lua/telescope/previewers/buffer_previewer.lua index d0756ff69e..da11227e9e 100644 --- a/lua/telescope/previewers/buffer_previewer.lua +++ b/lua/telescope/previewers/buffer_previewer.lua @@ -1116,7 +1116,7 @@ previewers.highlights = defaulter(function(_) vim.schedule(function() vim.api.nvim_buf_call(self.state.bufnr, function() vim.cmd "keepjumps norm! gg" - vim.fn.search(entry.value .. " ") + vim.fn.search("^" .. entry.value .. " ") local lnum = vim.api.nvim_win_get_cursor(self.state.winid)[1] -- That one is actually a match but its better to use it like that then matchadd pcall(vim.api.nvim_buf_clear_namespace, self.state.bufnr, ns_previewer, 0, -1) From eae0d8fbde590b0eaa2f9481948cd6fd7dd21656 Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Mon, 30 Sep 2024 08:56:14 +0200 Subject: [PATCH 4/5] fix(treesitter): adapt to upstream change (#3308) In Nvim 0.11, `vim.treesitter.language.add()` returns `true` on success or `nil,errmsg` on failure instead of throwing an error. --- lua/telescope/utils.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index 44ab5d9b08..145b7f3aae 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -662,7 +662,11 @@ 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) + if vim.fn.has "nvim-0.11" == 1 then + return vim.treesitter.language.add(lang) + else + return pcall(vim.treesitter.language.add, lang) + end end --- Telescope Wrapper around vim.notify From dc6fc321a5ba076697cca89c9d7ea43153276d81 Mon Sep 17 00:00:00 2001 From: James Trew <66286082+jamestrew@users.noreply.github.com> Date: Sun, 6 Oct 2024 02:11:05 +0000 Subject: [PATCH 5/5] fix(grep_string): cast `search` value to string (#3319) When using the vim command API and passing a number to the `search` option of `grep_string` (eg. `:Telescope grep_string search=3`), the number is passed as a number. This eventually causes a type error at `string.gsub` as the `search` value is expected to be a string. We'll just cast `search` value to a string. --- lua/telescope/builtin/__files.lua | 53 ++++++++++++++++--------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/lua/telescope/builtin/__files.lua b/lua/telescope/builtin/__files.lua index cc5f905c0f..97ee6a3def 100644 --- a/lua/telescope/builtin/__files.lua +++ b/lua/telescope/builtin/__files.lua @@ -17,23 +17,27 @@ local filter = vim.tbl_filter local files = {} -local escape_chars = function(string) - return string.gsub(string, "[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%.]", { - ["\\"] = "\\\\", - ["-"] = "\\-", - ["("] = "\\(", - [")"] = "\\)", - ["["] = "\\[", - ["]"] = "\\]", - ["{"] = "\\{", - ["}"] = "\\}", - ["?"] = "\\?", - ["+"] = "\\+", - ["*"] = "\\*", - ["^"] = "\\^", - ["$"] = "\\$", - ["."] = "\\.", - }) +---@param s string +---@return string +local escape_chars = function(s) + return ( + s:gsub("[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%.]", { + ["\\"] = "\\\\", + ["-"] = "\\-", + ["("] = "\\(", + [")"] = "\\)", + ["["] = "\\[", + ["]"] = "\\]", + ["{"] = "\\{", + ["}"] = "\\}", + ["?"] = "\\?", + ["+"] = "\\+", + ["*"] = "\\*", + ["^"] = "\\^", + ["$"] = "\\$", + ["."] = "\\.", + }) + ) end local has_rg_program = function(picker_name, program) @@ -202,7 +206,10 @@ files.grep_string = function(opts) else word = vim.F.if_nil(opts.search, vim.fn.expand "") end + + word = tostring(word) local search = opts.use_regex and word or escape_chars(word) + local search_args = search == "" and { "-v", "--", "^[[:space:]]*$" } or { "--", search } local additional_args = {} if opts.additional_args ~= nil then @@ -217,32 +224,26 @@ files.grep_string = function(opts) additional_args[#additional_args + 1] = "--encoding=" .. opts.file_encoding end - if search == "" then - search = { "-v", "--", "^[[:space:]]*$" } - else - search = { "--", search } - end - local args if visual == true then args = flatten { vimgrep_arguments, additional_args, - search, + search_args, } else args = flatten { vimgrep_arguments, additional_args, opts.word_match, - search, + search_args, } end opts.__inverted, opts.__matches = opts_contain_invert(args) if opts.grep_open_files then - for _, file in ipairs(get_open_filelist(opts.grep_open_files, opts.cwd)) do + for _, file in ipairs(get_open_filelist(opts.grep_open_files, opts.cwd) or {}) do table.insert(args, file) end elseif opts.search_dirs then