From 6a82a8638dc7b9e40026502a3755bb4770235bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Wed, 6 Mar 2024 19:58:59 +0100 Subject: [PATCH] Improve editor config --- home/editor/neovim/keybinds.lua | 5 +++++ home/editor/neovim/plugins.lua | 34 +++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/home/editor/neovim/keybinds.lua b/home/editor/neovim/keybinds.lua index a6d4378..19562e9 100644 --- a/home/editor/neovim/keybinds.lua +++ b/home/editor/neovim/keybinds.lua @@ -21,6 +21,11 @@ vim.keymap.set('n', 'ff', require('telescope.builtin').find_files, vim.keymap.set('n', 'fg', require('telescope.builtin').live_grep, { desc = '[F]ind by [G]rep' }) +-- Tabs vim.keymap.set('n', 'tt', ':tabnew') vim.keymap.set('n', 'tn', ':tabnext') vim.keymap.set('n', 'tp', ':tabprevious') + +-- LSP +vim.keymap.set('n', 'gD', vim.lsp.buf.declaration) +vim.keymap.set('n', 'gd', vim.lsp.buf.definition) diff --git a/home/editor/neovim/plugins.lua b/home/editor/neovim/plugins.lua index f852fdf..bcae021 100644 --- a/home/editor/neovim/plugins.lua +++ b/home/editor/neovim/plugins.lua @@ -20,7 +20,7 @@ lsp_zero.format_on_save({ ['tsserver'] = { 'typescript', 'javascript' }, }, }) - +-- LSP: go local lspconfig = require('lspconfig') lspconfig.gopls.setup({ settings = { @@ -33,7 +33,30 @@ lspconfig.gopls.setup({ } } }) +vim.api.nvim_create_autocmd("BufWritePre", { + pattern = "*.go", + callback = function() + local params = vim.lsp.util.make_range_params() + params.context = { only = { "source.organizeImports" } } + -- buf_request_sync defaults to a 1000ms timeout. Depending on your + -- machine and codebase, you may want longer. Add an additional + -- argument after params if you find that you have to write the file + -- twice for changes to be saved. + -- E.g., vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 3000) + local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params) + for cid, res in pairs(result or {}) do + for _, r in pairs(res.result or {}) do + if r.edit then + local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or "utf-16" + vim.lsp.util.apply_workspace_edit(r.edit, enc) + end + end + end + vim.lsp.buf.format({ async = false }) + end +}) +-- LSP: nix lspconfig.nil_ls.setup({ autostart = true, settings = { @@ -45,6 +68,7 @@ lspconfig.nil_ls.setup({ }, }) +-- LSP: lua lspconfig.lua_ls.setup({ on_init = function(client) local path = client.workspace_folders[1].name @@ -85,6 +109,7 @@ lspconfig.lua_ls.setup({ } }) +-- LSP: ts/js lspconfig.tsserver.setup({ init_options = { plugins = { @@ -206,9 +231,10 @@ cmp.setup({ }), sources = cmp.config.sources({ }, { - { name = "copilot", group_index = 2 }, - { name = "luasnip" }, - { name = "buffer" }, + { name = "copilot", group_index = 2 }, + { name = "nvim_lsp", group_index = 2 }, + -- { name = "luasnip" }, + -- { name = "buffer" }, }) })