summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorMaksym Klishevych <m@klishevy.ch>2023-03-22 15:49:55 +0200
committerGitHub <noreply@github.com>2023-03-22 09:49:55 -0400
commit942fe5faef47b21241e970551eba407bc10d9547 (patch)
treed3b695b0196f367cec44729e02eeef0d344bc0a9 /lua
parente504cf03c24119ee024c4182bee7ab31276cd684 (diff)
feat(treesitter): symbols & ignore symbols options (#2338)HEADmaster
* feat(treesitter): symbols & ignore symbols options * renamed the function passed to utils.filter_symbols --------- Co-authored-by: Maksym Klishevych <max@klishevy.ch>
Diffstat (limited to 'lua')
-rw-r--r--lua/telescope/builtin/__files.lua6
-rw-r--r--lua/telescope/builtin/__lsp.lua38
-rw-r--r--lua/telescope/builtin/init.lua2
-rw-r--r--lua/telescope/utils.lua30
4 files changed, 48 insertions, 28 deletions
diff --git a/lua/telescope/builtin/__files.lua b/lua/telescope/builtin/__files.lua
index 3c2d259..dc885a3 100644
--- a/lua/telescope/builtin/__files.lua
+++ b/lua/telescope/builtin/__files.lua
@@ -408,6 +408,12 @@ files.treesitter = function(opts)
end
end
+ results = utils.filter_symbols(results, opts)
+ if results == nil then
+ -- error message already printed in `utils.filter_symbols`
+ return
+ end
+
if vim.tbl_isempty(results) then
return
end
diff --git a/lua/telescope/builtin/__lsp.lua b/lua/telescope/builtin/__lsp.lua
index 96b7cfa..17cf430 100644
--- a/lua/telescope/builtin/__lsp.lua
+++ b/lua/telescope/builtin/__lsp.lua
@@ -227,6 +227,38 @@ lsp.implementations = function(opts)
return list_or_jump("textDocument/implementation", "LSP Implementations", opts)
end
+local symbols_sorter = function(symbols)
+ if vim.tbl_isempty(symbols) then
+ return symbols
+ end
+
+ local current_buf = vim.api.nvim_get_current_buf()
+
+ -- sort adequately for workspace symbols
+ local filename_to_bufnr = {}
+ for _, symbol in ipairs(symbols) do
+ if filename_to_bufnr[symbol.filename] == nil then
+ filename_to_bufnr[symbol.filename] = vim.uri_to_bufnr(vim.uri_from_fname(symbol.filename))
+ end
+ symbol.bufnr = filename_to_bufnr[symbol.filename]
+ end
+
+ table.sort(symbols, function(a, b)
+ if a.bufnr == b.bufnr then
+ return a.lnum < b.lnum
+ end
+ if a.bufnr == current_buf then
+ return true
+ end
+ if b.bufnr == current_buf then
+ return false
+ end
+ return a.bufnr < b.bufnr
+ end)
+
+ return symbols
+end
+
lsp.document_symbols = function(opts)
local params = vim.lsp.util.make_position_params(opts.winnr)
vim.lsp.buf_request(opts.bufnr, "textDocument/documentSymbol", params, function(err, result, _, _)
@@ -244,7 +276,7 @@ lsp.document_symbols = function(opts)
end
local locations = vim.lsp.util.symbols_to_items(result or {}, opts.bufnr) or {}
- locations = utils.filter_symbols(locations, opts)
+ locations = utils.filter_symbols(locations, opts, symbols_sorter)
if locations == nil then
-- error message already printed in `utils.filter_symbols`
return
@@ -287,7 +319,7 @@ lsp.workspace_symbols = function(opts)
end
local locations = vim.lsp.util.symbols_to_items(server_result or {}, opts.bufnr) or {}
- locations = utils.filter_symbols(locations, opts)
+ locations = utils.filter_symbols(locations, opts, symbols_sorter)
if locations == nil then
-- error message already printed in `utils.filter_symbols`
return
@@ -335,7 +367,7 @@ local function get_workspace_symbols_requester(bufnr, opts)
local locations = vim.lsp.util.symbols_to_items(res or {}, bufnr) or {}
if not vim.tbl_isempty(locations) then
- locations = utils.filter_symbols(locations, opts) or {}
+ locations = utils.filter_symbols(locations, opts, symbols_sorter) or {}
end
return locations
end
diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua
index d0d669d..922aea4 100644
--- a/lua/telescope/builtin/init.lua
+++ b/lua/telescope/builtin/init.lua
@@ -91,6 +91,8 @@ builtin.fd = builtin.find_files
--- - `<C-l>`: show autocompletion menu to prefilter your query by kind of ts node you want to see (i.e. `:var:`)
---@field show_line boolean: if true, shows the row:column that the result is found at (default: true)
---@field bufnr number: specify the buffer number where treesitter should run. (default: current buffer)
+---@field symbols string|table: filter results by symbol kind(s)
+---@field ignore_symbols string|table: list of symbols to ignore
---@field symbol_highlights table: string -> string. Matches symbol with hl_group
builtin.treesitter = require_on_exported_call("telescope.builtin.__files").treesitter
diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua
index 0e2eb06..0ef0e06 100644
--- a/lua/telescope/utils.lua
+++ b/lua/telescope/utils.lua
@@ -39,7 +39,7 @@ utils.repeated_table = function(n, val)
return empty_lines
end
-utils.filter_symbols = function(results, opts)
+utils.filter_symbols = function(results, opts, post_filter)
local has_ignore = opts.ignore_symbols ~= nil
local has_symbols = opts.symbols ~= nil
local filtered_symbols
@@ -86,31 +86,11 @@ utils.filter_symbols = function(results, opts)
end, results)
end
- -- TODO(conni2461): If you understand this correctly then we sort the results table based on the bufnr
- -- If you ask me this should be its own function, that happens after the filtering part and should be
- -- called in the lsp function directly
- local current_buf = vim.api.nvim_get_current_buf()
+ if type(post_filter) == "function" then
+ filtered_symbols = post_filter(filtered_symbols)
+ end
+
if not vim.tbl_isempty(filtered_symbols) then
- -- filter adequately for workspace symbols
- local filename_to_bufnr = {}
- for _, symbol in ipairs(filtered_symbols) do
- if filename_to_bufnr[symbol.filename] == nil then
- filename_to_bufnr[symbol.filename] = vim.uri_to_bufnr(vim.uri_from_fname(symbol.filename))
- end
- symbol["bufnr"] = filename_to_bufnr[symbol.filename]
- end
- table.sort(filtered_symbols, function(a, b)
- if a.bufnr == b.bufnr then
- return a.lnum < b.lnum
- end
- if a.bufnr == current_buf then
- return true
- end
- if b.bufnr == current_buf then
- return false
- end
- return a.bufnr < b.bufnr
- end)
return filtered_symbols
end