refactor(nvim): complete lua extraction

This commit is contained in:
Rob Watson 2023-09-19 06:51:33 +02:00
parent d5d5ca6fa7
commit e6fb8cc478
7 changed files with 252 additions and 262 deletions

271
nvimrc
View File

@ -146,8 +146,10 @@ nnoremap <leader>dq ggVG"_d:wq<cr>
iabbrev esdebug debugger; // eslint-disable-line no-debugger
iabbrev bashstrict set -euo pipefail<cr>IFS=$'\n\t'<cr>
" 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 <Plug>(GitGutterTextObjectOuterPending)
xmap ih <Plug>(GitGutterTextObjectInnerVisual)
xmap ah <Plug>(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 <<EOF
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({
['<C-u>'] = cmp.mapping.scroll_docs(-4),
['<C-d>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.close(),
['C-y'] = cmp.mapping.confirm({ select = true, behaviour = cmp.ConfirmBehavior.Replace }),
['<CR>'] = 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 <<EOF
local nvim_lsp = require('lspconfig')
local on_attach = function(client, bufnr)
-- nvim does not expose autocmd via Lua API yet:
vim.api.nvim_exec([[autocmd User LspDiagnosticsChanged call lightline#update()]], false)
local opts = { noremap=true, silent=false }
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', '<Cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>gD', '<Cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>e', '<Cmd>lua vim.lsp.buf.rename()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>r', '<Cmd>lua vim.lsp.buf.references()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>i', '<Cmd>lua vim.lsp.buf.implementation()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>ca', '<Cmd>lua vim.lsp.buf.code_action()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>ci', '<Cmd>lua vim.lsp.buf.incoming_calls()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>co', '<Cmd>lua vim.lsp.buf.outgoing_calls()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', ']e', '<Cmd>lua vim.diagnostic.goto_next({severity="Error"})<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '[e', '<Cmd>lua vim.diagnostic.goto_prev({severity="Error"})<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', ']d', '<Cmd>lua vim.diagnostic.goto_next()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '[d', '<Cmd>lua vim.diagnostic.goto_prev()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'cd', '<Cmd>lua vim.diagnostic.hide(nil, 0)<CR>', opts)
-- fzf triggers:
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fr', '<Cmd>References<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fi', '<Cmd>Implementations<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fs', '<Cmd>DocumentSymbols<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fw', '<Cmd>WorkspaceSymbols<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fc', '<Cmd>CodeActions<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fci', '<Cmd>IncomingCalls<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fco', '<Cmd>OutgoingCalls<CR>', 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"

View File

@ -1,5 +1,3 @@
vim.api.nvim_command("packadd copilot.lua")
vim.defer_fn(function()
require('copilot').setup({
panel = {

View File

@ -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!

179
vim/lua/lsp_config.lua Normal file
View File

@ -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', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', '<Cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>gD', '<Cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>e', '<Cmd>lua vim.lsp.buf.rename()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>r', '<Cmd>lua vim.lsp.buf.references()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>i', '<Cmd>lua vim.lsp.buf.implementation()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>ca', '<Cmd>lua vim.lsp.buf.code_action()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>ci', '<Cmd>lua vim.lsp.buf.incoming_calls()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>co', '<Cmd>lua vim.lsp.buf.outgoing_calls()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', ']e', '<Cmd>lua vim.diagnostic.goto_next({severity="Error"})<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '[e', '<Cmd>lua vim.diagnostic.goto_prev({severity="Error"})<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', ']d', '<Cmd>lua vim.diagnostic.goto_next()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '[d', '<Cmd>lua vim.diagnostic.goto_prev()<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'cd', '<Cmd>lua vim.diagnostic.hide(nil, 0)<CR>', opts)
-- fzf triggers:
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fr', '<Cmd>References<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fi', '<Cmd>Implementations<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fs', '<Cmd>DocumentSymbols<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fw', '<Cmd>WorkspaceSymbols<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fc', '<Cmd>CodeActions<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fci', '<Cmd>IncomingCalls<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>fco', '<Cmd>OutgoingCalls<CR>', 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,
}

View File

@ -1,6 +0,0 @@
require "helpers"
require "commands"
require "test_runner"
require "formatter_config"
require "treesitter_config"
require "copilot_config"

View File

@ -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({
['<C-u>'] = cmp.mapping.scroll_docs(-4),
['<C-d>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.close(),
['C-y'] = cmp.mapping.confirm({ select = true, behaviour = cmp.ConfirmBehavior.Replace }),
['<CR>'] = 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,
},
})

View File

@ -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 = {