feat: add IvyWorkspaceSymbol command
This is a command that will search the lsp workspace symbols on a search term.
This commit is contained in:
parent
2188552724
commit
c930fb1594
3 changed files with 44 additions and 0 deletions
40
lua/ivy/backends/lsp-workspace-symbols.lua
Normal file
40
lua/ivy/backends/lsp-workspace-symbols.lua
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
local window = require "ivy.window"
|
||||
local utils = require "ivy.utils"
|
||||
|
||||
local previous_results = {}
|
||||
local function set_items(items)
|
||||
window.set_items(items)
|
||||
previous_results = items
|
||||
end
|
||||
|
||||
local function items(input)
|
||||
local buffer_number = window.origin_buffer
|
||||
local cwd = vim.fn.getcwd()
|
||||
local results = {}
|
||||
vim.lsp.buf_request(buffer_number, "workspace/symbol", { query = input }, function(err, server_result, _, _)
|
||||
if err ~= nil then
|
||||
set_items { content = "-- There was an error with workspace/symbol --" }
|
||||
return
|
||||
end
|
||||
local locations = vim.lsp.util.symbols_to_items(server_result or {}, buffer_number) or {}
|
||||
for index = 1, #locations do
|
||||
local item = locations[index]
|
||||
local relative_path = item.filename:sub(#cwd + 2, -1)
|
||||
table.insert(results, { content = relative_path .. ":" .. item.lnum .. ": " .. item.text })
|
||||
end
|
||||
|
||||
set_items(results)
|
||||
end)
|
||||
|
||||
return previous_results
|
||||
end
|
||||
|
||||
local lsp_workspace_symbols = {
|
||||
name = "WorkspaceSymbol",
|
||||
command = "IvyWorkspaceSymbol",
|
||||
description = "Search for workspace ymbol using the lsp workspace/symbol",
|
||||
items = items,
|
||||
callback = utils.vimgrep_action(),
|
||||
}
|
||||
|
||||
return lsp_workspace_symbols
|
||||
|
|
@ -27,6 +27,7 @@ window.index = 0
|
|||
window.origin = nil
|
||||
window.window = nil
|
||||
window.buffer = nil
|
||||
window.origin_buffer = nil
|
||||
|
||||
window.initialize = function()
|
||||
window.make_buffer()
|
||||
|
|
@ -34,6 +35,7 @@ end
|
|||
|
||||
window.make_buffer = function()
|
||||
window.origin = vim.api.nvim_get_current_win()
|
||||
window.origin_buffer = vim.api.nvim_win_get_buf(0)
|
||||
|
||||
vim.api.nvim_command "botright split new"
|
||||
window.buffer = vim.api.nvim_win_get_buf(0)
|
||||
|
|
@ -138,6 +140,7 @@ window.destroy = function()
|
|||
window.buffer = nil
|
||||
window.window = nil
|
||||
window.origin = nil
|
||||
window.origin_buffer = nil
|
||||
window.index = 0
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -29,5 +29,6 @@ register_backend(require "ivy.backends.ag")
|
|||
register_backend(require "ivy.backends.buffers")
|
||||
register_backend(require "ivy.backends.files")
|
||||
register_backend(require "ivy.backends.lines")
|
||||
register_backend(require "ivy.backends.lsp-workspace-symbols")
|
||||
|
||||
vim.cmd "highlight IvyMatch cterm=bold gui=bold"
|
||||
|
|
|
|||
Loading…
Reference in a new issue