Skip to content

Commit

Permalink
feat(lsp): ignore_symbols option (nvim-telescope#1745)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Hauser <[email protected]>
  • Loading branch information
kkharji and Conni2461 authored Mar 11, 2022
1 parent c5bf83d commit ddb9e56
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 36 deletions.
3 changes: 3 additions & 0 deletions doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,7 @@ builtin.lsp_document_symbols({opts}) *builtin.lsp_document_symbols()*
line the tag is found on (default:
false)
{symbols} (string|table) filter results by symbol kind(s)
{ignore_symbols} (string|table) list of symbols to ignore
{symbol_highlights} (table) string -> string. Matches symbol
with hl_group

Expand All @@ -1432,6 +1433,7 @@ builtin.lsp_workspace_symbols({opts}) *builtin.lsp_workspace_symbols()*
line the tag is found on (default:
false)
{symbols} (string|table) filter results by symbol kind(s)
{ignore_symbols} (string|table) list of symbols to ignore
{symbol_highlights} (table) string -> string. Matches symbol
with hl_group

Expand All @@ -1453,6 +1455,7 @@ builtin.lsp_dynamic_workspace_symbols({opts}) *builtin.lsp_dynamic_workspace_sym
line the symbol is found on
(default: false)
{symbols} (string|table) filter results by symbol kind(s)
{ignore_symbols} (string|table) list of symbols to ignore
{symbol_highlights} (table) string -> string. Matches symbol
with hl_group

Expand Down
3 changes: 3 additions & 0 deletions lua/telescope/builtin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ builtin.lsp_range_code_actions = require_on_exported_call("telescope.builtin.lsp
---@field ignore_filename boolean: dont show filenames (default: true)
---@field show_line boolean: if true, shows the content of the line the tag is found on (default: false)
---@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.lsp_document_symbols = require_on_exported_call("telescope.builtin.lsp").document_symbols

Expand All @@ -404,6 +405,7 @@ builtin.lsp_document_symbols = require_on_exported_call("telescope.builtin.lsp")
---@field ignore_filename boolean: dont show filenames (default: false)
---@field show_line boolean: if true, shows the content of the line the tag is found on (default: false)
---@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.lsp_workspace_symbols = require_on_exported_call("telescope.builtin.lsp").workspace_symbols

Expand All @@ -414,6 +416,7 @@ builtin.lsp_workspace_symbols = require_on_exported_call("telescope.builtin.lsp"
---@field ignore_filename boolean: dont show filenames (default: false)
---@field show_line boolean: if true, shows the content of the line the symbol is found on (default: false)
---@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.lsp_dynamic_workspace_symbols = require_on_exported_call("telescope.builtin.lsp").dynamic_workspace_symbols

Expand Down
1 change: 1 addition & 0 deletions lua/telescope/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ local split_keywords = {
["sections"] = true,
["search_dirs"] = true,
["symbols"] = true,
["ignore_symbols"] = true,
}

-- convert command line string arguments to
Expand Down
80 changes: 44 additions & 36 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,44 +91,46 @@ utils.quickfix_items_to_entries = function(locations)
end

utils.filter_symbols = function(results, opts)
if opts.symbols == nil then
local has_ignore = opts.ignore_symbols ~= nil
local has_symbols = opts.symbols ~= nil
local filtered_symbols

if has_symbols and has_ignore then
error "Either opts.symbols or opts.ignore_symbols, can't process opposing options at the same time!"
return
elseif not (has_ignore or has_symbols) then
return results
end
local valid_symbols = vim.tbl_map(string.lower, vim.lsp.protocol.SymbolKind)

local filtered_symbols = {}
if type(opts.symbols) == "string" then
opts.symbols = string.lower(opts.symbols)
if vim.tbl_contains(valid_symbols, opts.symbols) then
for _, result in ipairs(results) do
if string.lower(result.kind) == opts.symbols then
table.insert(filtered_symbols, result)
end
end
else
print(string.format("%s is not a valid symbol per `vim.lsp.protocol.SymbolKind`", opts.symbols))
elseif has_ignore then
if type(opts.ignore_symbols) == "string" then
opts.ignore_symbols = { opts.ignore_symbols }
end
elseif type(opts.symbols) == "table" then
opts.symbols = vim.tbl_map(string.lower, opts.symbols)
local mismatched_symbols = {}
for _, symbol in ipairs(opts.symbols) do
if vim.tbl_contains(valid_symbols, symbol) then
for _, result in ipairs(results) do
if string.lower(result.kind) == symbol then
table.insert(filtered_symbols, result)
end
end
else
table.insert(mismatched_symbols, symbol)
mismatched_symbols = table.concat(mismatched_symbols, ", ")
print(string.format("%s are not valid symbols per `vim.lsp.protocol.SymbolKind`", mismatched_symbols))
end
if type(opts.ignore_symbols) ~= "table" then
print "Please pass ignore_symbols as either a string or a list of strings"
return
end
else
print "Please pass filtering symbols as either a string or a list of strings"
return

opts.ignore_symbols = vim.tbl_map(string.lower, opts.ignore_symbols)
filtered_symbols = vim.tbl_filter(function(item)
return not vim.tbl_contains(opts.ignore_symbols, string.lower(item.kind))
end, results)
elseif has_symbols then
if type(opts.symbols) == "string" then
opts.symbols = { opts.symbols }
end
if type(opts.symbols) ~= "table" then
print "Please pass filtering symbols as either a string or a list of strings"
return
end

opts.symbols = vim.tbl_map(string.lower, opts.symbols)
filtered_symbols = vim.tbl_filter(function(item)
return vim.tbl_contains(opts.symbols, string.lower(item.kind))
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 not vim.tbl_isempty(filtered_symbols) then
-- filter adequately for workspace symbols
Expand All @@ -153,9 +155,15 @@ utils.filter_symbols = function(results, opts)
end)
return filtered_symbols
end
-- only account for string|table as function otherwise already printed message and returned nil
local symbols = type(opts.symbols) == "string" and opts.symbols or table.concat(opts.symbols, ", ")
print(string.format("%s symbol(s) were not part of the query results", symbols))

-- print message that filtered_symbols is now empty
if has_symbols then
local symbols = table.concat(opts.symbols, ", ")
print(string.format("%s symbol(s) were not part of the query results", symbols))
elseif has_ignore then
local symbols = table.concat(opts.ignore_symbols, ", ")
print(string.format("%s ignore_symbol(s) have removed everything from the query result", symbols))
end
end

utils.path_smart = (function()
Expand Down

0 comments on commit ddb9e56

Please sign in to comment.