From 51a72513f4203a55327c4b8199f6b6d38bb821d4 Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Wed, 1 Mar 2023 08:23:07 +0000 Subject: [PATCH] feat: add IvyWorkspaceSymbol command This is a command that will search the lsp workspace symbols on a search term. --- lua/ivy/backends/lsp-workspace-symbols.lua | 40 ++++++++++++++++++++++ lua/ivy/window.lua | 3 ++ plugin/ivy.lua | 1 + 3 files changed, 44 insertions(+) create mode 100644 lua/ivy/backends/lsp-workspace-symbols.lua diff --git a/lua/ivy/backends/lsp-workspace-symbols.lua b/lua/ivy/backends/lsp-workspace-symbols.lua new file mode 100644 index 0000000..c1c18d7 --- /dev/null +++ b/lua/ivy/backends/lsp-workspace-symbols.lua @@ -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 diff --git a/lua/ivy/window.lua b/lua/ivy/window.lua index 87c0856..c53d6e1 100644 --- a/lua/ivy/window.lua +++ b/lua/ivy/window.lua @@ -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 diff --git a/plugin/ivy.lua b/plugin/ivy.lua index 3bf9a67..2834681 100644 --- a/plugin/ivy.lua +++ b/plugin/ivy.lua @@ -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"