From 72ecc858425dc84495f5fc979345e2890d6cdd0d Mon Sep 17 00:00:00 2001 From: ray-x Date: Fri, 18 Jun 2021 01:39:41 +1000 Subject: [PATCH] add rename --- lua/navigator/lspclient/clients.lua | 25 +++++++++-------- lua/navigator/lspclient/mapping.lua | 1 + lua/navigator/rename.lua | 43 +++++++++++++++++++++++++++++ lua/navigator/util.lua | 25 +++++++++++++++++ 4 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 lua/navigator/rename.lua diff --git a/lua/navigator/lspclient/clients.lua b/lua/navigator/lspclient/clients.lua index 6263f4c..a277fec 100644 --- a/lua/navigator/lspclient/clients.lua +++ b/lua/navigator/lspclient/clients.lua @@ -103,7 +103,7 @@ local setups = { }, settings = { gopls = { - flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, + flags = {allow_incremental_sync = true, debounce_text_changes = 500}, analyses = {unusedparams = true, unreachable = false}, codelenses = { generate = true, -- show the `go generate` lens. @@ -125,7 +125,7 @@ local setups = { end }, clangd = { - flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, + flags = {allow_incremental_sync = true, debounce_text_changes = 500}, cmd = { "clangd", "--background-index", "--suggest-missing-includes", "--clang-tidy", "--header-insertion=iwyu" @@ -151,7 +151,7 @@ local setups = { procMacro = {enable = true} } }, - flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, + flags = {allow_incremental_sync = true, debounce_text_changes = 500} }, sqls = { filetypes = {"sql"}, @@ -160,7 +160,7 @@ local setups = { highlight.diagnositc_config_sign() require"sqls".setup {picker = "telescope"} -- or default end, - flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, + flags = {allow_incremental_sync = true, debounce_text_changes = 500}, settings = { cmd = {"sqls", "-config", "$HOME/.config/sqls/config.yml"} -- alterantively: @@ -176,7 +176,7 @@ local setups = { cmd = {"lua-language-server"}, filetypes = {"lua"}, on_attach = on_attach, - flags = { allow_incremental_sync = true, debounce_text_changes = 500 }, + flags = {allow_incremental_sync = true, debounce_text_changes = 500}, settings = { Lua = { runtime = { @@ -194,8 +194,8 @@ local setups = { workspace = { -- Make the server aware of Neovim runtime files library = library, - maxPreload = 256, - preloadFileSize = 50000 + maxPreload = 1000, + preloadFileSize = 10000 }, telemetry = {enable = false} } @@ -204,7 +204,7 @@ local setups = { pyright = { cmd = {"pyright-langserver", "--stdio"}, filetypes = {"python"}, - flags = { allow_incremental_sync = true, debounce_text_changes = 500}, + flags = {allow_incremental_sync = true, debounce_text_changes = 500}, settings = { python = { analysis = { @@ -222,7 +222,7 @@ local setups = { index = {threads = 2}, clang = {excludeArgs = {"-frounding-math"}} }, - flags = { allow_incremental_sync = true }, + flags = {allow_incremental_sync = true} } } @@ -234,7 +234,10 @@ local servers = { "terraformls" } -local default_cfg = {on_attach = on_attach, flags = { allow_incremental_sync = true, debounce_text_changes = 500 },} +local default_cfg = { + on_attach = on_attach, + flags = {allow_incremental_sync = true, debounce_text_changes = 500} +} -- check and load based on file type local function load_cfg(ft, client, cfg, loaded) @@ -294,10 +297,8 @@ local function wait_lsp_startup(ft, retry, lsp_opts) if lsp_opts[lspclient] ~= nil then -- log(lsp_opts[lspclient], cfg) cfg = vim.tbl_deep_extend("force", cfg, lsp_opts[lspclient]) - trace(cfg) end - load_cfg(ft, lspclient, cfg, loaded) ::continue:: end diff --git a/lua/navigator/lspclient/mapping.lua b/lua/navigator/lspclient/mapping.lua index 8e05bda..51f46ba 100644 --- a/lua/navigator/lspclient/mapping.lua +++ b/lua/navigator/lspclient/mapping.lua @@ -33,6 +33,7 @@ local key_maps = { {key = "K", func = "hover({ popup_opts = { border = single }})"}, {key = "ga", mode = "n", func = "code_action()"}, {key = "ga", mode = "v", func = "range_code_action()"}, {key = "re", func = "rename()"}, + {key = "rn", func = "require('navigator.rename').rename()"}, {key = "gi", func = "incoming_calls()"}, {key = "go", func = "outgoing_calls()"}, {key = "gi", func = "implementation()"}, {key = "gt", func = "type_definition()"}, {key = "gL", func = "diagnostic.show_line_diagnostics({ popup_opts = { border = single }})"}, diff --git a/lua/navigator/rename.lua b/lua/navigator/rename.lua new file mode 100644 index 0000000..b2c343b --- /dev/null +++ b/lua/navigator/rename.lua @@ -0,0 +1,43 @@ +-- https://github.com/lukas-reineke/dotfiles/blob/master/vim/lua/lsp/rename.lua +local M = {} +local util = require "navigator.util" +local rename_prompt = "Rename -> " + +M.rename = function() + local current_name = vim.fn.expand("") + local bufnr = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_option(bufnr, "buftype", "prompt") + vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe") + vim.api.nvim_buf_add_highlight(bufnr, -1, "RenamePrompt", 0, 0, #rename_prompt) + vim.fn.prompt_setprompt(bufnr, rename_prompt) + local winnr = vim.api.nvim_open_win(bufnr, true, { + relative = "cursor", + width = 50, + height = 1, + row = -3, + col = 1, + style = "minimal", + border = "single" + }) + vim.api.nvim_win_set_option(winnr, "winhl", "Normal:Floating") + util.map("n", "", "bd!", {silent = true, buffer = true}) + util.map({"n", "i"}, "", "lua require('navigator.rename').callback()", + {silent = true, buffer = true}) + util.map("i", "", "xi", {silent = true, buffer = true}) + vim.cmd(string.format("normal i%s", current_name)) +end + +M.callback = function() + local new_name = vim.trim(vim.fn.getline("."):sub(#rename_prompt + 1, -1)) + vim.cmd [[stopinsert]] + vim.cmd [[bd!]] + if #new_name == 0 or new_name == vim.fn.expand("") then + return + end + local params = vim.lsp.util.make_position_params() + params.newName = new_name + vim.lsp.buf_request(0, "textDocument/rename", params) +end +M.rename() +-- M.callback() +return M diff --git a/lua/navigator/util.lua b/lua/navigator/util.lua index 6e5dacf..e364190 100644 --- a/lua/navigator/util.lua +++ b/lua/navigator/util.lua @@ -28,6 +28,31 @@ function M.get_data_from_file(filename, startLine) return {data = data, line = displayLine} end +M.merge = function(t1, t2) + for k, v in pairs(t2) do + t1[k] = v + end + return t1 +end + +M.map = function(modes, key, result, options) + options = M.merge({noremap = true, silent = false, expr = false, nowait = false}, options or {}) + local buffer = options.buffer + options.buffer = nil + + if type(modes) ~= "table" then + modes = {modes} + end + + for i = 1, #modes do + if buffer then + vim.api.nvim_buf_set_keymap(0, modes[i], key, result, options) + else + vim.api.nvim_set_keymap(modes[i], key, result, options) + end + end +end + function M.get_base(path) local len = #path for i = len, 1, -1 do