From cffcfca318584948df405634a96ee2c1051df60c Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Mon, 11 Nov 2024 08:56:28 +0100 Subject: [PATCH] style(nvim): lua formatting --- vim/lua/lsp_config.lua | 256 ++++++++++++++++++++-------------------- vim/lua/test_runner.lua | 154 ++++++++++++------------ 2 files changed, 208 insertions(+), 202 deletions(-) diff --git a/vim/lua/lsp_config.lua b/vim/lua/lsp_config.lua index 21a03a0..77733da 100644 --- a/vim/lua/lsp_config.lua +++ b/vim/lua/lsp_config.lua @@ -1,42 +1,44 @@ local nvim_lsp = require("lspconfig") +vim.lsp.set_log_level("debug") + 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 }) + -- 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", "k", "lua vim.diagnostic.open_float()", 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) + 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", "k", "lua vim.diagnostic.open_float()", 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) + -- 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("lsp_signature").on_attach({ + hint_enable = false, + }) - require("copilot_cmp").setup() + require("copilot_cmp").setup() end -- Go @@ -44,42 +46,46 @@ end local capabilities = require("cmp_nvim_lsp").default_capabilities() capabilities.textDocument.completion.completionItem.snippetSupport = false capabilities.textDocument.completion.completionItem.resolveSupport = { - properties = { - "documentation", - "detail", - "additionalTextEdits", - }, + properties = { + "documentation", + "detail", + "additionalTextEdits", + }, } nvim_lsp.gopls.setup({ - settings = { - gopls = { - staticcheck = true, - -- Disabling linksInHover may not be working as expected: - linksInHover = false, - usePlaceholders = true, - experimentalPostfixCompletions = true, - analyses = { - unusedparams = true, - shadow = true, - unusedwrite = true, - unusedresult = true, - nilness = true, - }, - buildFlags = { "-tags=acceptance" }, - }, - }, - init_options = { - usePlaceholders = true, - }, - capabilities = vim.tbl_extend("error", capabilities, { - workspace = { - didChangeWatchedFiles = { - dynamicRegistration = true, - }, - }, - }), - on_attach = on_attach, + settings = { + gopls = { + staticcheck = true, + -- Disabling linksInHover may not be working as expected: + linksInHover = false, + usePlaceholders = true, + experimentalPostfixCompletions = true, + analyses = { + unusedparams = true, + shadow = true, + unusedwrite = true, + unusedresult = true, + nilness = true, + }, + buildFlags = { "-tags=acceptance,basketapitest,graphqlapitest" } + }, + }, + init_options = { + usePlaceholders = true, + }, + capabilities = vim.tbl_extend( + "error", + capabilities, + { + workspace = { + didChangeWatchedFiles = { + dynamicRegistration = true + } + } + } + ), + on_attach = on_attach, }) -- Rust @@ -112,58 +118,58 @@ nvim_lsp.rust_analyzer.setup({ -- Ruby nvim_lsp.solargraph.setup({ - on_attach = on_attach, - settings = { - solargraph = { - autoformat = false, - formatting = false, - }, - }, + on_attach = on_attach, + settings = { + solargraph = { + autoformat = false, + formatting = false, + }, + }, }) -- Typescript -- https://jose-elias-alvarez.medium.com/configuring-neovims-lsp-client-for-typescript-development-5789d58ea9c nvim_lsp.ts_ls.setup({ - on_attach = on_attach, + on_attach = on_attach, }) local filetypes = { - typescript = "eslint", - typescriptreact = "eslint", + 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" }, - }, + 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, - }, + on_attach = on_attach, + filetypes = vim.tbl_keys(filetypes), + init_options = { + linters = linters, + filetypes = filetypes, + }, }) -- Lua @@ -173,35 +179,35 @@ table.insert(runtime_path, "lua/?.lua") table.insert(runtime_path, "lua/?/init.lua") nvim_lsp.lua_ls.setup({ - 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, + 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, }) -- Bash language server -- https://github.com/bash-lsp/bash-language-server#neovim vim.api.nvim_create_autocmd("FileType", { - pattern = "sh", - callback = function() - vim.lsp.start({ - name = "bash-language-server", - cmd = { "bash-language-server", "start" }, - }) - end, + pattern = "sh", + callback = function() + vim.lsp.start({ + name = "bash-language-server", + cmd = { "bash-language-server", "start" }, + }) + end, }) diff --git a/vim/lua/test_runner.lua b/vim/lua/test_runner.lua index a49d809..e35d788 100644 --- a/vim/lua/test_runner.lua +++ b/vim/lua/test_runner.lua @@ -1,108 +1,108 @@ _G._test_cmd_to_wins = {} _G._build_test_cmd = function() - local bufnr = vim.fn.bufnr("%") - local filetype = vim.fn.getbufvar(bufnr, "&filetype") - if filetype == "" then - vim.api.nvim_err_writeln("cannot build test command for filetype:" .. filetype) - return nil - end + local bufnr = vim.fn.bufnr("%") + local filetype = vim.fn.getbufvar(bufnr, "&filetype") + if filetype == "" then + vim.api.nvim_err_writeln("cannot build test command for filetype:" .. filetype) + return nil + end - local path = vim.fn.expand("%:p") + local path = vim.fn.expand("%:p") - -- TODO: allow line number to be passed - -- TODO: check file exists before running command - if filetype == "ruby" then - if not path:find("_spec.rb") then - path = path:gsub("/app/", "/spec/"):gsub([[.rb$]], "_spec.rb") - end + -- TODO: allow line number to be passed + -- TODO: check file exists before running command + if filetype == "ruby" then + if not path:find("_spec.rb") then + path = path:gsub("/app/", "/spec/"):gsub([[.rb$]], "_spec.rb") + end - return "bundle exec rspec --format=progress --no-profile " .. path - elseif filetype == "go" then - return "go test -v " .. path:gsub("^(.*)/(.*go)$", "%1/...") - elseif filetype == "typescript" or filetype == "javascript" then - return "pnpm test -- " .. path - else - vim.api.nvim_err_writeln("filetype not supported: " .. filetype) - return nil - end + return "bundle exec rspec --format=progress --no-profile " .. path + elseif filetype == "go" then + return "go test -v " .. path:gsub("^(.*)/(.*go)$", "%1/...") + elseif filetype == "typescript" or filetype == "javascript" then + return "pnpm test -- " .. path + else + vim.api.nvim_err_writeln("filetype not supported: " .. filetype) + return nil + end end _G.run_tests = function() - local cmd = _G._build_test_cmd() - if cmd == nil then - return - end + local cmd = _G._build_test_cmd() + if cmd == nil then + return + end - vim.api.nvim_command([[silent up]]) + vim.api.nvim_command([[silent up]]) - local winid = _G._test_cmd_to_wins[cmd] - local current_winid = vim.fn.win_getid() - if - winid == nil - or not vim.api.nvim_win_is_valid(winid) - or winid == current_winid - or not vim.fn.win_gotoid(winid) == -1 - then - vim.api.nvim_command([[10split]]) - winid = vim.fn.win_getid() - _G._test_cmd_to_wins[cmd] = winid - end + local winid = _G._test_cmd_to_wins[cmd] + local current_winid = vim.fn.win_getid() + if + winid == nil + or not vim.api.nvim_win_is_valid(winid) + or winid == current_winid + or not vim.fn.win_gotoid(winid) == -1 + then + vim.api.nvim_command([[10split]]) + winid = vim.fn.win_getid() + _G._test_cmd_to_wins[cmd] = winid + end - local buf = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_option(buf, "bufhidden", "delete") - vim.api.nvim_win_set_buf(winid, buf) + local buf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_option(buf, "bufhidden", "delete") + vim.api.nvim_win_set_buf(winid, buf) - vim.fn.termopen(cmd) - vim.api.nvim_command([[normal! G]]) - vim.api.nvim_command([[wincmd p]]) + vim.fn.termopen(cmd) + vim.api.nvim_command([[normal! G]]) + vim.api.nvim_command([[wincmd p]]) end _G.focus_tests = function() - local cmd = _G._build_test_cmd() - if cmd == nil then - return - end + local cmd = _G._build_test_cmd() + if cmd == nil then + return + end - local winid = _G._test_cmd_to_wins[cmd] - if winid == nil or not vim.api.nvim_win_is_valid(winid) then - return - end + local winid = _G._test_cmd_to_wins[cmd] + if winid == nil or not vim.api.nvim_win_is_valid(winid) then + return + end - vim.fn.win_gotoid(winid) - vim.api.nvim_command([[wincmd =]]) + vim.fn.win_gotoid(winid) + vim.api.nvim_command([[wincmd =]]) end _G.close_tests = function() - local current_winid = vim.api.nvim_get_current_win() - for _, winid in pairs(_G._test_cmd_to_wins) do - if current_winid == winid and vim.api.nvim_win_is_valid(winid) then - vim.api.nvim_win_close(winid, false) - return - end - end + local current_winid = vim.api.nvim_get_current_win() + for _, winid in pairs(_G._test_cmd_to_wins) do + if current_winid == winid and vim.api.nvim_win_is_valid(winid) then + vim.api.nvim_win_close(winid, false) + return + end + end - local cmd = _G._build_test_cmd() - if cmd == nil then - return - end + local cmd = _G._build_test_cmd() + if cmd == nil then + return + end - local winid = _G._test_cmd_to_wins[cmd] - if winid == nil or not vim.api.nvim_win_is_valid(winid) then - return - end + local winid = _G._test_cmd_to_wins[cmd] + if winid == nil or not vim.api.nvim_win_is_valid(winid) then + return + end - vim.api.nvim_win_close(winid, false) + vim.api.nvim_win_close(winid, false) end _G.copy_test_cmd = function() - local cmd = _G._build_test_cmd() - if cmd == nil then - return - end + local cmd = _G._build_test_cmd() + if cmd == nil then + return + end - vim.fn.setreg("+", cmd) - print("Copied: " .. cmd) + vim.fn.setreg("+", cmd) + print("Copied: " .. cmd) end vim.api.nvim_set_keymap("n", "cr", [[:lua _G.run_tests()]], { noremap = true, silent = true })