From e6fb8cc47839763cfdaa18af995bf96db9191041 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Tue, 19 Sep 2023 06:51:33 +0200 Subject: [PATCH] refactor(nvim): complete lua extraction --- nvimrc | 271 ++++------------------------------ vim/lua/copilot_config.lua | 2 - vim/lua/formatter_config.lua | 3 +- vim/lua/lsp_config.lua | 179 ++++++++++++++++++++++ vim/lua/main.lua | 6 - vim/lua/nvim_cmp_config.lua | 47 ++++++ vim/lua/treesitter_config.lua | 6 - 7 files changed, 252 insertions(+), 262 deletions(-) create mode 100644 vim/lua/lsp_config.lua delete mode 100644 vim/lua/main.lua create mode 100644 vim/lua/nvim_cmp_config.lua diff --git a/nvimrc b/nvimrc index 9e9e6bb..59b85d9 100644 --- a/nvimrc +++ b/nvimrc @@ -146,8 +146,10 @@ nnoremap dq ggVG"_d:wq iabbrev esdebug debugger; // eslint-disable-line no-debugger iabbrev bashstrict set -euo pipefailIFS=$'\n\t' -" include all lua -lua require("main") +" include lua +lua require "helpers" +lua require "commands" +lua require "test_runner" " Function to eat trailing character when applying iabbrevs. " https://stackoverflow.com/questions/11858927/preventing-trailing-whitespace-when-using-vim-abbreviations @@ -306,6 +308,9 @@ omap ah (GitGutterTextObjectOuterPending) xmap ih (GitGutterTextObjectInnerVisual) xmap ah (GitGutterTextObjectOuterVisual) +" enable inline vim highlighting: +let g:vimsyn_embed= 'lPr' + " vim-fugitive packadd! vim-fugitive " https://github.com/tpope/vim-fugitive.git @@ -333,24 +338,26 @@ packadd! vim-rhubarb " https://github.com/tpope/vim-rhubarb.git packadd! editorconfig-vim " https://github.com/editorconfig/editorconfig-vim.git packadd! vim-wordmotion " https://github.com/chaoren/vim-wordmotion.git -" formatter.nvim: see lua/formatter.lua -" packadd! formatter.nvim " https://github.com/mhartington/formatter.nvim.git +" formatter.nvim: +packadd! formatter.nvim " https://github.com/mhartington/formatter.nvim.git -" enable inline vim highlighting: -let g:vimsyn_embed= 'lPr' +lua require "formatter_config" -" Treesitter: see lua/treesitter_config.lua -" packadd! nvim-treesitter " https://github.com/nvim-treesitter/nvim-treesitter.git -" packadd! nvim-treesitter-textobjects " https://github.com/nvim-treesitter/nvim-treesitter-textobjects.git -" packadd! nvim-treesitter-refactor " https://github.com/nvim-treesitter/nvim-treesitter-refactor.git -" packadd! nvim-treesitter-context " https://github.com/nvim-treesitter/nvim-treesitter-context.git -" packadd! playground " https://github.com/nvim-treesitter/playground.git +" treesitter: +packadd! nvim-treesitter " https://github.com/nvim-treesitter/nvim-treesitter.git +packadd! nvim-treesitter-textobjects " https://github.com/nvim-treesitter/nvim-treesitter-textobjects.git +packadd! nvim-treesitter-refactor " https://github.com/nvim-treesitter/nvim-treesitter-refactor.git +packadd! nvim-treesitter-context " https://github.com/nvim-treesitter/nvim-treesitter-context.git +packadd! playground " https://github.com/nvim-treesitter/playground.git -" copilot: see lua/copilot_config.lua -" packadd! copilot.lua " https://github.com/zbirenbaum/copilot.lua.git +lua require "treesitter_config" + +" copilot: +packadd! copilot.lua " https://github.com/zbirenbaum/copilot.lua.git + +lua require "copilot_config" " nvim-cmp: - packadd! nvim-cmp " https://github.com/hrsh7th/nvim-cmp.git packadd! copilot-cmp " https://github.com/zbirenbaum/copilot-cmp.git packadd! vim-vsnip " https://github.com/hrsh7th/vim-vsnip.git @@ -360,239 +367,11 @@ packadd! cmp-buffer " https://github.com/hrsh7th/cmp-buffer.git packadd! cmp-path " https://github.com/hrsh7th/cmp-path.git packadd! cmp-calc " https://github.com/hrsh7th/cmp-calc -lua <'] = cmp.mapping.scroll_docs(-4), - [''] = cmp.mapping.scroll_docs(4), - [''] = cmp.mapping.complete(), - [''] = cmp.mapping.close(), - ['C-y'] = cmp.mapping.confirm({ select = true, behaviour = cmp.ConfirmBehavior.Replace }), - [''] = cmp.mapping.confirm({ select = true, behaviour = cmp.ConfirmBehavior.Replace }), - }), - sources = cmp.config.sources({ - { name = 'copilot', keyword_length = 0 }, - { name = 'nvim_lsp', keyword_length = 3 }, - { - name = 'buffer', - option = { - get_bufnrs = function() - return vim.api.nvim_list_bufs() - end - }, - }, - { name = 'path' }, - { name = 'calc' }, - }), - sorting = { - comparators = { - function(...) return cmp_buffer:compare_locality(...) end, - } - }, - view = { - entries = 'native', - }, - experimental = { - ghost_text = true, - }, - }) -EOF - -" LSP +lua require "nvim_cmp_config" +" LSP: packadd! nvim-lspconfig " https://github.com/neovim/nvim-lspconfig.git packadd! lsp_signature.nvim " https://github.com/ray-x/lsp_signature.nvim.git packadd! fzf-lsp.nvim " https://github.com/gfanto/fzf-lsp.nvim.git -lua <lua vim.lsp.buf.definition()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', 'lua vim.lsp.buf.type_definition()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', 'lua vim.lsp.buf.definition()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', 'lua vim.lsp.buf.type_definition()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'e', 'lua vim.lsp.buf.rename()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'r', 'lua vim.lsp.buf.references()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'i', 'lua vim.lsp.buf.implementation()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'ca', 'lua vim.lsp.buf.code_action()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'ci', 'lua vim.lsp.buf.incoming_calls()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'co', 'lua vim.lsp.buf.outgoing_calls()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', 'lua vim.lsp.buf.hover()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', ']e', 'lua vim.diagnostic.goto_next({severity="Error"})', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', '[e', 'lua vim.diagnostic.goto_prev({severity="Error"})', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', ']d', 'lua vim.diagnostic.goto_next()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', '[d', 'lua vim.diagnostic.goto_prev()', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'cd', 'lua vim.diagnostic.hide(nil, 0)', opts) - - -- fzf triggers: - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fr', 'References', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fi', 'Implementations', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fs', 'DocumentSymbols', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fw', 'WorkspaceSymbols', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fc', 'CodeActions', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fci', 'IncomingCalls', opts) - vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fco', 'OutgoingCalls', opts) - - require 'lsp_signature'.on_attach({ - hint_enable = false, - }) - - require 'copilot_cmp'.setup(); -end - --- Go - -local capabilities = require('cmp_nvim_lsp').default_capabilities() -capabilities.textDocument.completion.completionItem.snippetSupport = true -capabilities.textDocument.completion.completionItem.resolveSupport = { - properties = { - 'documentation', - 'detail', - 'additionalTextEdits', - } -} - -nvim_lsp.gopls.setup{ - settings = { - gopls = { - staticcheck = true, - -- Disalbing linksInHover may not be working as expected: - linksInHover = false, - usePlaceholders = true, - analyses = { - unusedparams = true, - shadow = true, - unusedwrite = true, - unusedresult = true, - nilness = true, - }, - }, - }, - capabilities = capabilities, - on_attach = on_attach, -} - --- Rust -nvim_lsp.rust_analyzer.setup{ - capabilities = capabilities, - on_attach = on_attach, - settings = { - ["rust-analyzer"] = { - imports = { - granularity = { - group = "module", - }, - prefix = "self", - }, - cargo = { - buildScripts = { - enable = true, - }, - }, - procMacro = { - enable = false, - }, - diagnostics = { - disabled = { "unresolved-proc-macro" }, - } - } - } -} - --- Ruby - -nvim_lsp.solargraph.setup{ - on_attach = on_attach, -} - --- Typescript --- https://jose-elias-alvarez.medium.com/configuring-neovims-lsp-client-for-typescript-development-5789d58ea9c - -nvim_lsp.tsserver.setup{ - on_attach = on_attach, -} - -local filetypes = { - typescript = "eslint", - typescriptreact = "eslint", -} - --- diagnosticls - -local linters = { - eslint = { - sourceName = "eslint", - -- fallback to global eslint if the local is not found? - -- https://github.com/creativenull/diagnosticls-configs-nvim/blob/e7d6f7e99f6b416d2aeee89314bc46fc36df7b22/lua/diagnosticls-configs/fs.lua#L20 - command = "./node_modules/.bin/eslint", - rootPatterns = {".eslintrc", ".eslintrc.js"}, - debounce = 100, - args = {"--stdin", "--stdin-filename", "%filepath", "--format", "json"}, - parseJson = { - errorsRoot = "[0].messages", - line = "line", - column = "column", - endLine = "endLine", - endColumn = "endColumn", - message = "${message} [${ruleId}]", - security = "severity" - }, - securities = {[1] = "error", [2] = "warning"} - } -} - -nvim_lsp.diagnosticls.setup{ - on_attach = on_attach, - filetypes = vim.tbl_keys(filetypes), - init_options = { - linters = linters, - filetypes = filetypes, - } -} - --- Lua - -local lua_ls_root_path = os.getenv("HOME").."/dev/lua-language-server" -local lua_ls_binary = lua_ls_root_path.."/bin/lua-language-server" - -local runtime_path = vim.split(package.path, ';') -table.insert(runtime_path, "lua/?.lua") -table.insert(runtime_path, "lua/?/init.lua") - -nvim_lsp.lua_ls.setup { - cmd = {lua_ls_binary, "-E", lua_ls_root_path .. "/main.lua"}; - settings = { - Lua = { - runtime = { - version = 'LuaJIT', - path = runtime_path, - }, - diagnostics = { - globals = {'vim'}, - }, - workspace = { - library = vim.api.nvim_get_runtime_file("", true), - }, - telemetry = { - enable = false, - }, - }, - }, - on_attach = on_attach, -} -EOF +lua require "lsp_config" diff --git a/vim/lua/copilot_config.lua b/vim/lua/copilot_config.lua index 710d171..9d9cfa3 100644 --- a/vim/lua/copilot_config.lua +++ b/vim/lua/copilot_config.lua @@ -1,5 +1,3 @@ -vim.api.nvim_command("packadd copilot.lua") - vim.defer_fn(function() require('copilot').setup({ panel = { diff --git a/vim/lua/formatter_config.lua b/vim/lua/formatter_config.lua index e02909b..104781d 100644 --- a/vim/lua/formatter_config.lua +++ b/vim/lua/formatter_config.lua @@ -1,5 +1,3 @@ -vim.api.nvim_command("packadd formatter.nvim") - local prettier = function() return { exe = Get_project_node_modules_path() .. "/.bin/prettier", @@ -16,6 +14,7 @@ require('formatter').setup({ } }) +-- TODO: use vim.api.nvim_create_augroup() vim.api.nvim_exec2([[ augroup FormatAutogroup autocmd! diff --git a/vim/lua/lsp_config.lua b/vim/lua/lsp_config.lua new file mode 100644 index 0000000..b04323d --- /dev/null +++ b/vim/lua/lsp_config.lua @@ -0,0 +1,179 @@ +local nvim_lsp = require('lspconfig') +local on_attach = function(client, bufnr) + -- TODO: use vim.api.nvim_create_autocmd + vim.api.nvim_exec2([[autocmd User LspDiagnosticsChanged call lightline#update()]], { output = false }) + + local opts = { noremap=true, silent=false } + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', 'lua vim.lsp.buf.definition()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', 'lua vim.lsp.buf.type_definition()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', 'lua vim.lsp.buf.definition()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', 'lua vim.lsp.buf.type_definition()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'e', 'lua vim.lsp.buf.rename()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'r', 'lua vim.lsp.buf.references()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'i', 'lua vim.lsp.buf.implementation()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'ca', 'lua vim.lsp.buf.code_action()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'ci', 'lua vim.lsp.buf.incoming_calls()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'co', 'lua vim.lsp.buf.outgoing_calls()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', 'lua vim.lsp.buf.hover()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', ']e', 'lua vim.diagnostic.goto_next({severity="Error"})', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', '[e', 'lua vim.diagnostic.goto_prev({severity="Error"})', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', ']d', 'lua vim.diagnostic.goto_next()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', '[d', 'lua vim.diagnostic.goto_prev()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'cd', 'lua vim.diagnostic.hide(nil, 0)', opts) + + -- fzf triggers: + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fr', 'References', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fi', 'Implementations', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fs', 'DocumentSymbols', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fw', 'WorkspaceSymbols', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fc', 'CodeActions', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fci', 'IncomingCalls', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'fco', 'OutgoingCalls', opts) + + require 'lsp_signature'.on_attach({ + hint_enable = false, + }) + + require 'copilot_cmp'.setup(); +end + +-- Go + +local capabilities = require('cmp_nvim_lsp').default_capabilities() +capabilities.textDocument.completion.completionItem.snippetSupport = true +capabilities.textDocument.completion.completionItem.resolveSupport = { + properties = { + 'documentation', + 'detail', + 'additionalTextEdits', + } +} + +nvim_lsp.gopls.setup{ + settings = { + gopls = { + staticcheck = true, + -- Disalbing linksInHover may not be working as expected: + linksInHover = false, + usePlaceholders = true, + analyses = { + unusedparams = true, + shadow = true, + unusedwrite = true, + unusedresult = true, + nilness = true, + }, + }, + }, + capabilities = capabilities, + on_attach = on_attach, +} + +-- Rust +nvim_lsp.rust_analyzer.setup{ + capabilities = capabilities, + on_attach = on_attach, + settings = { + ["rust-analyzer"] = { + imports = { + granularity = { + group = "module", + }, + prefix = "self", + }, + cargo = { + buildScripts = { + enable = true, + }, + }, + procMacro = { + enable = false, + }, + diagnostics = { + disabled = { "unresolved-proc-macro" }, + } + } + } +} + +-- Ruby + +nvim_lsp.solargraph.setup{ + on_attach = on_attach, +} + +-- Typescript +-- https://jose-elias-alvarez.medium.com/configuring-neovims-lsp-client-for-typescript-development-5789d58ea9c + +nvim_lsp.tsserver.setup{ + on_attach = on_attach, +} + +local filetypes = { + typescript = "eslint", + typescriptreact = "eslint", +} + +-- diagnosticls + +local linters = { + eslint = { + sourceName = "eslint", + -- fallback to global eslint if the local is not found? + -- https://github.com/creativenull/diagnosticls-configs-nvim/blob/e7d6f7e99f6b416d2aeee89314bc46fc36df7b22/lua/diagnosticls-configs/fs.lua#L20 + command = "./node_modules/.bin/eslint", + rootPatterns = {".eslintrc", ".eslintrc.js"}, + debounce = 100, + args = {"--stdin", "--stdin-filename", "%filepath", "--format", "json"}, + parseJson = { + errorsRoot = "[0].messages", + line = "line", + column = "column", + endLine = "endLine", + endColumn = "endColumn", + message = "${message} [${ruleId}]", + security = "severity" + }, + securities = {[1] = "error", [2] = "warning"} + } +} + +nvim_lsp.diagnosticls.setup{ + on_attach = on_attach, + filetypes = vim.tbl_keys(filetypes), + init_options = { + linters = linters, + filetypes = filetypes, + } +} + +-- Lua + +local lua_ls_root_path = os.getenv("HOME").."/dev/lua-language-server" +local lua_ls_binary = lua_ls_root_path.."/bin/lua-language-server" + +local runtime_path = vim.split(package.path, ';') +table.insert(runtime_path, "lua/?.lua") +table.insert(runtime_path, "lua/?/init.lua") + +nvim_lsp.lua_ls.setup { + cmd = {lua_ls_binary, "-E", lua_ls_root_path .. "/main.lua"}; + settings = { + Lua = { + runtime = { + version = 'LuaJIT', + path = runtime_path, + }, + diagnostics = { + globals = {'vim'}, + }, + workspace = { + library = vim.api.nvim_get_runtime_file("", true), + }, + telemetry = { + enable = false, + }, + }, + }, + on_attach = on_attach, +} diff --git a/vim/lua/main.lua b/vim/lua/main.lua deleted file mode 100644 index 6f1ce1d..0000000 --- a/vim/lua/main.lua +++ /dev/null @@ -1,6 +0,0 @@ -require "helpers" -require "commands" -require "test_runner" -require "formatter_config" -require "treesitter_config" -require "copilot_config" diff --git a/vim/lua/nvim_cmp_config.lua b/vim/lua/nvim_cmp_config.lua new file mode 100644 index 0000000..91ff9e2 --- /dev/null +++ b/vim/lua/nvim_cmp_config.lua @@ -0,0 +1,47 @@ +local cmp = require('cmp') +local cmp_buffer = require('cmp_buffer') + +cmp.setup({ + completion = { + completeopt = 'menu,menuone,noinsert', + }, + snippet = { + expand = function(args) + vim.fn["vsnip#anonymous"](args.body) + end, + }, + mapping = cmp.mapping.preset.insert({ + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.close(), + ['C-y'] = cmp.mapping.confirm({ select = true, behaviour = cmp.ConfirmBehavior.Replace }), + [''] = cmp.mapping.confirm({ select = true, behaviour = cmp.ConfirmBehavior.Replace }), + }), + sources = cmp.config.sources({ + { name = 'copilot', keyword_length = 0 }, + { name = 'nvim_lsp', keyword_length = 3 }, + { + name = 'buffer', + option = { + get_bufnrs = function() + return vim.api.nvim_list_bufs() + end + }, + }, + { name = 'path' }, + { name = 'calc' }, + }), + sorting = { + comparators = { + function(...) return cmp_buffer:compare_locality(...) end, + } + }, + view = { + entries = 'native', + }, + experimental = { + ghost_text = true, + }, +}) + diff --git a/vim/lua/treesitter_config.lua b/vim/lua/treesitter_config.lua index f39bf61..6305f06 100644 --- a/vim/lua/treesitter_config.lua +++ b/vim/lua/treesitter_config.lua @@ -1,9 +1,3 @@ -vim.api.nvim_command("packadd nvim-treesitter") -vim.api.nvim_command("packadd nvim-treesitter-textobjects") -vim.api.nvim_command("packadd nvim-treesitter-refactor") -vim.api.nvim_command("packadd nvim-treesitter-context") -vim.api.nvim_command("packadd playground") - require 'nvim-treesitter.configs'.setup { ensure_installed = "all", highlight = {