From 340249e8b8fdabcb2a5f4310c9a8e9fad8016eaa Mon Sep 17 00:00:00 2001 From: Mystical-C <64483945+MysticalDevil@users.noreply.github.com> Date: Tue, 17 Oct 2023 08:36:33 +0800 Subject: [PATCH] Fix nvim-jdtls config. Fix inlay hints function. Add inlay hints toggle keymap --- lua/configs/core/keybindings.lua | 21 ++++++++++++--------- lua/configs/plugin/flutter-tools.lua | 17 +++++++++++++++++ lua/configs/plugin/jdtls.lua | 18 ++++++++++-------- lua/configs/plugin/rust-tools.lua | 4 +++- lua/lsp/setup.lua | 4 ++-- lua/lsp/util.lua | 12 +++++------- 6 files changed, 49 insertions(+), 27 deletions(-) diff --git a/lua/configs/core/keybindings.lua b/lua/configs/core/keybindings.lua index bcd66a5..06df566 100644 --- a/lua/configs/core/keybindings.lua +++ b/lua/configs/core/keybindings.lua @@ -8,11 +8,11 @@ local utils = require("utils") -- term_mode = 't', -- command_mode = 'c', -local map = vim.api.nvim_set_keymap -local opt = { - noremap = true, - silent = true, -} +-- local map = vim.api.nvim_set_keymap +-- local opt = { +-- noremap = true, +-- silent = true, +-- } ----------------------------------------------------------- -- leader key is null @@ -83,7 +83,7 @@ utils.keymap("t", "", "") local plugin_keys = {} -- LSP callback function shortcut key setting -plugin_keys.map_LSP = function(mapbuf) +plugin_keys.map_LSP = function(mapbuf, bufnr) -- rename -- Lspsaga replace rn -- mapbuf("n", "rn", "Lspsaga rename", opt) @@ -103,6 +103,8 @@ plugin_keys.map_LSP = function(mapbuf) -- ignore_filename = false, }) end, { desc = "Go to definition" }) + mapbuf("n", "gD", vim.lsp.buf.declaration, { desc = "Go to declaration" }) + mapbuf("n", "gi", vim.lsp.buf.implementation, { desc = "Go to implementation" }) -- hover document -- Lspsaga replace gh @@ -123,9 +125,10 @@ plugin_keys.map_LSP = function(mapbuf) mapbuf("n", "gj", "Lspsaga diagnostic_jump_next", { desc = "Jump to next diagnostic" }) mapbuf("n", "gk", "Lspsaga diagnostic_jump_prev", { desc = "Jump to previous diagnostic" }) - -- unused - -- mapbuf("n", "gD", "lua vim.lsp.buf.declaration()", opt) - -- mapbuf("n", "gi", "lua vim.lsp.buf.implementation()", opt) + -- toggle inlay hints + mapbuf("n", "L", function() + vim.lsp.inlay_hint(bufnr) + end, { desc = "Toggle LSP inlay hints" }) end -- gitsigns diff --git a/lua/configs/plugin/flutter-tools.lua b/lua/configs/plugin/flutter-tools.lua index 5a7ca59..577d280 100644 --- a/lua/configs/plugin/flutter-tools.lua +++ b/lua/configs/plugin/flutter-tools.lua @@ -75,6 +75,23 @@ local opts = { virtual_text = true, -- show the highlight using virtual text virtual_text_str = "■", -- the virtual text character to highlight }, + on_attach = require("lsp.util").default_on_attach, + settings = { + showTodos = true, + completeFunctionCalls = true, + analysisExcludedFolders = {}, + renameFilesWithClasses = "prompt", -- "always" + enableSnippets = true, + updateImportsOnRename = true, -- Whether to update imports and other directives when files are renamed. Required for `FlutterRename` command. + enableSdkFormatter = true, + }, + init_options = { + closingLabels = true, + flutterOutline = true, + onlyAnalyzeProjectsWithOpenFiles = true, + outline = true, + suggestFromUnimportedLibraries = true, + }, }, } diff --git a/lua/configs/plugin/jdtls.lua b/lua/configs/plugin/jdtls.lua index 607f58c..bf5d4bb 100644 --- a/lua/configs/plugin/jdtls.lua +++ b/lua/configs/plugin/jdtls.lua @@ -139,21 +139,23 @@ local config = { extendedClientCapabilities = extendedClientCapabilities, }, handlers = { - ["language/status"] = function() end, - ["workspace/diagnostic/refresh"] = function() end, - ["textDocument/codeAction"] = function() end, - ["textDocument/rename"] = function() end, - ["workspace/applyEdit"] = function() end, - ["textDocument/documentHighlight"] = function() end, + -- ["language/status"] = function() end, + -- ["workspace/diagnostic/refresh"] = function() end, + -- ["textDocument/codeAction"] = function() end, + -- ["textDocument/rename"] = function() end, + -- ["workspace/applyEdit"] = function() end, + -- ["textDocument/documentHighlight"] = function() end, }, - on_attach = function(_, bufnr) + on_attach = function(client, bufnr) if vim.fn.has("nvim-0.10") == 1 then - vim.lsp.inlay_hint(bufnr) + vim.lsp.inlay_hint(bufnr, true) end jdtls.setup_dap({ hotcodereplace = "auto" }) require("jdtls.dap").setup_dap_main_class_configs() require("jdtls.setup").add_commands() + require("lsp.util").default_on_attach(client, bufnr) end, + filetypes = { "java" }, } jdtls.start_or_attach(config) diff --git a/lua/configs/plugin/rust-tools.lua b/lua/configs/plugin/rust-tools.lua index 661ec84..9c2eb77 100644 --- a/lua/configs/plugin/rust-tools.lua +++ b/lua/configs/plugin/rust-tools.lua @@ -168,11 +168,13 @@ local opts = { -- these override the defaults set by rust-tools.nvim -- see https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#rust_analyzer server = { - on_attach = function(_, bufnr) + on_attach = function(client, bufnr) -- Hover actions vim.keymap.set("n", "rh", rt.hover_actions.hover_actions, { buffer = bufnr }) -- Code action groups vim.keymap.set("n", "ra", rt.code_action_group.code_action_group, { buffer = bufnr }) + + require("lsp.util").default_on_attach(client, bufnr) end, -- standalone file support -- setting it to false may improve startup time diff --git a/lua/lsp/setup.lua b/lua/lsp/setup.lua index 10fe4d5..e174263 100644 --- a/lua/lsp/setup.lua +++ b/lua/lsp/setup.lua @@ -27,7 +27,7 @@ end local lsp_servers = require("lsp.check") lsp_servers = arr_extend(lsp_servers, { "csharp_ls", - "dartls", + -- "dartls", "perlpls", "racket_langserver", "rust_analyzer", @@ -61,7 +61,7 @@ local servers = { clangd = require("lsp.config.clangd"), clojure_lsp = require("lsp.config.clojure_lsp"), cssls = require("lsp.config.cssls"), - dartls = require("lsp.config.dartls"), + -- dartls = require("lsp.config.dartls"), denols = require("lsp.config.denols"), emmet_ls = require("lsp.config.emmet_ls"), eslint = require("lsp.config.eslint"), diff --git a/lua/lsp/util.lua b/lua/lsp/util.lua index c154722..a025092 100644 --- a/lua/lsp/util.lua +++ b/lua/lsp/util.lua @@ -15,7 +15,9 @@ function M.key_attach(bufnr) vim.keymap.set(mode, lhs, rhs, vim.tbl_extend("force", default_opts, opts)) end -- keybingings - require("configs.core.keybindings").map_LSP(buf_set_keymap) + require("configs.core.keybindings").map_LSP(buf_set_keymap, bufnr) + + vim.notify("The LSP key settings is attached", vim.log.levels.INFO) end -- disable format, handle it to a dedicated plugin @@ -45,11 +47,11 @@ function M.default_on_attach(client, bufnr) M.disable_format(client) M.key_attach(bufnr) + M.set_inlay_hints(client, bufnr) + vim.api.nvim_set_option_value("formatexpr", "v:lua.vim.lsp.formatexpr()") vim.api.nvim_set_option_value("omnifunc", "v:lua.vim.lsp.omnifunc") vim.api.nvim_set_option_value("tagfunc", "v:lua.vim.lsp.tagfunc") - - M.set_inlay_hints(client, bufnr) end ---@return table @@ -62,10 +64,6 @@ function M.default_configs() end function M.set_inlay_hints(client, bufnr) - if client.name == "null-ls" then - return - end - if not client then vim.notify_once("LSP inlay hints attached failed: nil client.", vim.log.levels.ERROR) return