From 23442b59f347b2838397a5eaff7a404f11e50588 Mon Sep 17 00:00:00 2001 From: Einherjar Date: Wed, 20 Sep 2023 11:59:22 -0300 Subject: [PATCH 1/3] feat(lsp): inlay hints Depends on neovim 0.10. Ideas taken from https://youtu.be/DYaTzkw3zqQ and https://github.com/LazyVim/LazyVim/commit/01c7eeebd0641bd952e3c518dd28b178aed8e148. Closes #1. --- lua/plugins/lsp.lua | 53 +++++++++++--- lua/plugins/rust.lua | 161 +++++++++++++++++-------------------------- 2 files changed, 109 insertions(+), 105 deletions(-) diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index e35090f..cfad5b3 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -102,6 +102,14 @@ return { vim.keymap.set("n", "cwl", function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, { desc = "[L]ist Folders" }) + -- Enable inlay hints (depends on neovim 0.10) + if vim.lsp.inlay_hint then + vim.lsp.buf.inlay_hint(0, true) -- toggle on by default + -- set the keymap to toggle on/off + vim.keymap.set("n", "ch", function() + vim.lsp.inlay_hint(0, nil) + end, { desc = "Toggle Inlay [H]ints" }) + end end, }) -- nvim-cmp supports additional completion capabilities, so broadcast that to servers @@ -218,19 +226,46 @@ return { }, }) -- Enable some language servers with the additional completion capabilities offered by nvim-cmp - lsp.pyright.setup({ capabilities = capabilities }) -- requires pyright to be installed - lsp.tsserver.setup({ capabilities = capabilities }) -- requires typescript-language-server to be installed - lsp.bashls.setup({ capabilities = capabilities }) -- requires bash-language-server to be installed - lsp.html.setup({ capabilities = capabilities }) -- requires vscode-langservers-extracted to be installed - lsp.cssls.setup({ capabilities = capabilities }) -- requires vscode-langservers-extracted to be installed - lsp.jsonls.setup({ capabilities = capabilities }) -- requires vscode-langservers-extracted to be installed - lsp.eslint.setup({ capabilities = capabilities }) -- requires vscode-langservers-extracted to be installed - lsp.rnix.setup({ capabilities = capabilities }) -- requires rnix-lsp to be installed - lsp.lua_ls.setup({ -- requires lua-language-server to be installed + lsp.pyright.setup({ capabilities = capabilities }) -- requires pyright to be installed + lsp.tsserver.setup({ -- requires typescript-language-server to be installed + capabilities = capabilities, + -- taken from https://github.com/typescript-language-server/typescript-language-server#workspacedidchangeconfiguration + javascript = { + inlayHints = { + includeInlayEnumMemberValueHints = true, + includeInlayFunctionLikeReturnTypeHints = true, + includeInlayFunctionParameterTypeHints = true, + includeInlayParameterNameHints = "all", + includeInlayParameterNameHintsWhenArgumentMatchesName = true, + includeInlayPropertyDeclarationTypeHints = true, + includeInlayVariableTypeHints = true, + }, + }, + typescript = { + inlayHints = { + includeInlayEnumMemberValueHints = true, + includeInlayFunctionLikeReturnTypeHints = true, + includeInlayFunctionParameterTypeHints = true, + includeInlayParameterNameHints = "all", + includeInlayParameterNameHintsWhenArgumentMatchesName = true, + includeInlayPropertyDeclarationTypeHints = true, + includeInlayVariableTypeHints = true, + }, + }, + }) + lsp.bashls.setup({ capabilities = capabilities }) -- requires bash-language-server to be installed + lsp.html.setup({ capabilities = capabilities }) -- requires vscode-langservers-extracted to be installed + lsp.cssls.setup({ capabilities = capabilities }) -- requires vscode-langservers-extracted to be installed + lsp.jsonls.setup({ capabilities = capabilities }) -- requires vscode-langservers-extracted to be installed + lsp.eslint.setup({ capabilities = capabilities }) -- requires vscode-langservers-extracted to be installed + lsp.rnix.setup({ capabilities = capabilities }) -- requires rnix-lsp to be installed + lsp.lua_ls.setup({ -- requires lua-language-server to be installed capabilities = capabilities, settings = { Lua = { + workspace = { checkThirdParty = false }, telemetry = { enable = false }, + hint = { enable = true }, }, }, }) diff --git a/lua/plugins/rust.lua b/lua/plugins/rust.lua index 61289f4..64e05e6 100644 --- a/lua/plugins/rust.lua +++ b/lua/plugins/rust.lua @@ -1,104 +1,73 @@ return { - { - "simrat39/rust-tools.nvim", - event = { "BufReadPre", "BufNewFile" }, - lazy = true, - opts = { - on_initialized = function() - vim.cmd([[ - augroup RustLSP - autocmd CursorHold *.rs silent! lua vim.lsp.buf.document_highlight() - autocmd CursorMoved,InsertEnter *.rs silent! lua vim.lsp.buf.clear_references() - autocmd BufEnter,CursorHold,InsertLeave *.rs silent! lua vim.lsp.codelens.refresh() - augroup END - ]]) - end, + "nvim-neotest/neotest", + dependencies = { + { + "rouge8/neotest-rust", + dependencies = { + "nvim-lua/plenary.nvim", + "nvim-treesitter/nvim-treesitter", + "antoinemadec/FixCursorHold.nvim", + }, }, - config = function() - local rt = require("rust-tools") - rt.setup({ - server = { - on_attach = function(_, bufnr) - -- Hover actions - vim.keymap.set("n", "K", "RustHoverActions", { buffer = bufnr, desc = "Hover Documentation" }) - -- Code action groups - vim.keymap.set("n", "ca", "RustCodeAction", { buffer = bufnr, desc = "Code [A]ction" }) - end, - }, - }) - end, }, - { - "nvim-neotest/neotest", - dependencies = { - { - "rouge8/neotest-rust", - dependencies = { - "nvim-lua/plenary.nvim", - "nvim-treesitter/nvim-treesitter", - "antoinemadec/FixCursorHold.nvim", - }, + config = function() + require("neotest").setup({ + status = { virtual_text = true }, + output = { open_on_run = true }, + adapters = { + require("neotest-rust"), }, + }) + end, + keys = { + { + "tt", + function() + require("neotest").run.run(vim.fn.expand("%")) + end, + desc = "Run File", }, - config = function() - require("neotest").setup({ - status = { virtual_text = true }, - output = { open_on_run = true }, - adapters = { - require("neotest-rust"), - }, - }) - end, - keys = { - { - "tt", - function() - require("neotest").run.run(vim.fn.expand("%")) - end, - desc = "Run File", - }, - { - "tT", - function() - require("neotest").run.run(vim.loop.cwd()) - end, - desc = "Run All Test Files", - }, - { - "tr", - function() - require("neotest").run.run() - end, - desc = "Run Nearest", - }, - { - "ts", - function() - require("neotest").summary.toggle() - end, - desc = "Toggle Summary", - }, - { - "to", - function() - require("neotest").output.open({ enter = true, auto_close = true }) - end, - desc = "Show Output", - }, - { - "tO", - function() - require("neotest").output_panel.toggle() - end, - desc = "Toggle Output Panel", - }, - { - "tS", - function() - require("neotest").run.stop() - end, - desc = "Stop", - }, + { + "tT", + function() + require("neotest").run.run(vim.loop.cwd()) + end, + desc = "Run All Test Files", + }, + { + "tr", + function() + require("neotest").run.run() + end, + desc = "Run Nearest", + }, + { + "ts", + function() + require("neotest").summary.toggle() + end, + desc = "Toggle Summary", + }, + { + "to", + function() + require("neotest").output.open({ enter = true, auto_close = true }) + end, + desc = "Show Output", + }, + { + "tO", + function() + require("neotest").output_panel.toggle() + end, + desc = "Toggle Output Panel", + }, + { + "tS", + function() + require("neotest").run.stop() + end, + desc = "Stop", }, }, } From 04c1f542c94f3a997e4b9dd0fee5aa3790b303e4 Mon Sep 17 00:00:00 2001 From: Einherjar Date: Wed, 20 Sep 2023 12:06:28 -0300 Subject: [PATCH 2/3] feat(lsp): add `opts` as well just in case --- lua/plugins/lsp.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index cfad5b3..7c26990 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -53,6 +53,9 @@ return { config = true, }, }, + opts = function(_, opts) + opts.inlay_hints.enabled = true + end, config = function() -- IMPORTANT: make sure to setup neodev BEFORE lspconfig require("neodev").setup() From a2f689281b892332599737439ec80f91ed8c7a69 Mon Sep 17 00:00:00 2001 From: Einherjar Date: Thu, 21 Sep 2023 06:24:23 -0300 Subject: [PATCH 3/3] fix: tested with neovim 0.10 --- README.md | 2 +- lua/plugins/lsp.lua | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 24b9cdf..a0721e6 100644 --- a/README.md +++ b/README.md @@ -17,5 +17,5 @@ vscode-langservers-extracted pyright lua-language-server` ## Tmux -Additinally you can use my tmux config to have a nice status bar. +Additionally you can use my tmux config to have a nice status bar. They are located in `tmux.conf`. diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index 7c26990..e048182 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -53,9 +53,6 @@ return { config = true, }, }, - opts = function(_, opts) - opts.inlay_hints.enabled = true - end, config = function() -- IMPORTANT: make sure to setup neodev BEFORE lspconfig require("neodev").setup() @@ -105,12 +102,14 @@ return { vim.keymap.set("n", "cwl", function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, { desc = "[L]ist Folders" }) - -- Enable inlay hints (depends on neovim 0.10) - if vim.lsp.inlay_hint then - vim.lsp.buf.inlay_hint(0, true) -- toggle on by default + -- Enable inlay hints + local client = vim.lsp.get_client_by_id(ev.data.client_id) + if client.supports_method("textDocument/inlayHint") then + vim.g.inlay_hints_visible = true + vim.lsp.inlay_hint(ev.buf, true) -- set the keymap to toggle on/off vim.keymap.set("n", "ch", function() - vim.lsp.inlay_hint(0, nil) + vim.lsp.inlay_hint(ev.buf, nil) end, { desc = "Toggle Inlay [H]ints" }) end end,