summaryrefslogtreecommitdiff
path: root/lua/telescope/builtin/__lsp.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/telescope/builtin/__lsp.lua')
-rw-r--r--lua/telescope/builtin/__lsp.lua38
1 files changed, 35 insertions, 3 deletions
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