From 48516377705855f280cd6b495fde7d68a447837e Mon Sep 17 00:00:00 2001 From: Noah Masur <7386960+nmasur@users.noreply.github.com> Date: Sun, 10 Jul 2022 14:15:41 +0000 Subject: [PATCH] conditional lsp servers --- modules/editor/neovim/lua/functions.lua | 11 --- modules/editor/neovim/lua/keybinds.lua | 20 ++-- modules/editor/neovim/lua/packer/lsp.lua | 91 ++++++++++++++----- modules/editor/neovim/lua/packer/syntax.lua | 1 + .../editor/neovim/lua/packer/telescope.lua | 13 +++ 5 files changed, 90 insertions(+), 46 deletions(-) diff --git a/modules/editor/neovim/lua/functions.lua b/modules/editor/neovim/lua/functions.lua index 4aa4082..5731ba6 100644 --- a/modules/editor/neovim/lua/functions.lua +++ b/modules/editor/neovim/lua/functions.lua @@ -26,17 +26,6 @@ find_downloads = function() require("telescope").extensions.file_browser.file_browser(opts) end -choose_project = function() - local opts = require("telescope.themes").get_ivy({ - layout_config = { - bottom_pane = { - height = 10, - }, - }, - }) - require("telescope").extensions.project.project(opts) -end - command_history = function() local opts = require("telescope.themes").get_ivy({ layout_config = { diff --git a/modules/editor/neovim/lua/keybinds.lua b/modules/editor/neovim/lua/keybinds.lua index 73cb615..89a5cfa 100644 --- a/modules/editor/neovim/lua/keybinds.lua +++ b/modules/editor/neovim/lua/keybinds.lua @@ -5,8 +5,7 @@ -- Function to cut down config boilerplate local key = function(mode, key_sequence, action, params) params = params or {} - params["noremap"] = true - vim.api.nvim_set_keymap(mode, key_sequence, action, params) + vim.keymap.set(mode, key_sequence, action, params) end -- Remap space as leader key @@ -34,9 +33,9 @@ key("n", "k", ":Telescope keymaps") key("n", "/", ":Telescope live_grep") key("n", "ff", ":Telescope find_files") key("n", "fp", ":Telescope git_files") -key("n", "fN", "lua find_notes()") -key("n", "N", "lua grep_notes()") -key("n", "fD", "lua find_downloads()") +key("n", "fN", find_notes) +key("n", "N", grep_notes) +key("n", "fD", find_downloads) key("n", "fa", ":Telescope file_browser") key("n", "fw", ":Telescope grep_string") key("n", "wt", ":Telescope tmux sessions") @@ -48,14 +47,11 @@ key("n", "hh", ":Telescope help_tags") key("n", "fr", ":Telescope oldfiles") key("n", "cc", ":Telescope commands") key("n", "cr", "lua command_history()") -key("n", "y", "lua clipboard_history()") -key("i", "", "lua clipboard_history()") key("n", "s", ":Telescope current_buffer_fuzzy_find") key("n", "gc", ":Telescope git_commits") key("n", "gf", ":Telescope git_bcommits") key("n", "gb", ":Telescope git_branches") key("n", "gs", ":Telescope git_status") -key("n", "", "lua choose_project()") -- Buffer tabs (tmux interferes) -- key("n", "", "gt") @@ -64,10 +60,10 @@ key("n", "", "lua choose_project()") -- key("i", "", "gT") -- LSP -key("n", "gd", "lua vim.lsp.buf.definition()", { silent = true }) -key("n", "gT", "lua vim.lsp.buf.type_definition()", { silent = true }) -key("n", "gi", "lua vim.lsp.buf.implementation()", { silent = true }) -key("n", "gh", "lua vim.lsp.buf.hover()", { silent = true }) +key("n", "gd", vim.lsp.buf.definition, { silent = true }) +key("n", "gT", vim.lsp.buf.type_definition, { silent = true }) +key("n", "gi", vim.lsp.buf.implementation, { silent = true }) +key("n", "gh", vim.lsp.buf.hover, { silent = true }) key("n", "gr", "Telescope lsp_references", { silent = true }) key("n", "R", "lua vim.lsp.buf.rename()", { silent = true }) key("n", "]e", "lua vim.diagnostic.goto_next()", { silent = true }) diff --git a/modules/editor/neovim/lua/packer/lsp.lua b/modules/editor/neovim/lua/packer/lsp.lua index 47501ab..2ab913a 100644 --- a/modules/editor/neovim/lua/packer/lsp.lua +++ b/modules/editor/neovim/lua/packer/lsp.lua @@ -4,6 +4,10 @@ local M = {} +function on_path(program) + return vim.fn.executable(program) == 1 +end + M.packer = function(use) -- Language server engine use({ @@ -13,16 +17,24 @@ M.packer = function(use) local capabilities = require("cmp_nvim_lsp").update_capabilities( vim.lsp.protocol.make_client_capabilities() ) - require("lspconfig").rust_analyzer.setup({ capabilities = capabilities }) - require("lspconfig").tflint.setup({ capabilities = capabilities }) - require("lspconfig").terraformls.setup({ capabilities = capabilities }) - require("lspconfig").pyright.setup({ - on_attach = function() - -- set keymaps (requires 0.7.0) - -- vim.keymap.set("n", "", "", {buffer=0}) - end, - capabilities = capabilities, - }) + if on_path("rust-analyzer") then + require("lspconfig").rust_analyzer.setup({ capabilities = capabilities }) + end + if on_path("tflint") then + require("lspconfig").tflint.setup({ capabilities = capabilities }) + end + if on_path("terraform-ls") then + require("lspconfig").terraformls.setup({ capabilities = capabilities }) + end + if on_path("pyright") then + require("lspconfig").pyright.setup({ + on_attach = function() + -- set keymaps (requires 0.7.0) + -- vim.keymap.set("n", "", "", {buffer=0}) + end, + capabilities = capabilities, + }) + end end, }) @@ -40,16 +52,47 @@ M.packer = function(use) config = function() require("null-ls").setup({ sources = { - require("null-ls").builtins.formatting.stylua, - require("null-ls").builtins.formatting.black, - require("null-ls").builtins.formatting.fish_indent, - require("null-ls").builtins.formatting.nixfmt, - require("null-ls").builtins.formatting.rustfmt, - require("null-ls").builtins.diagnostics.shellcheck, + require("null-ls").builtins.formatting.stylua.with({ + condition = function() + return on_path("stylua") + end, + }), + require("null-ls").builtins.formatting.black.with({ + condition = function() + return on_path("black") + end, + }), + require("null-ls").builtins.formatting.fish_indent.with({ + condition = function() + return on_path("fish_indent") + end, + }), + require("null-ls").builtins.formatting.nixfmt.with({ + condition = function() + return on_path("nixfmt") + end, + }), + require("null-ls").builtins.formatting.rustfmt.with({ + condition = function() + return on_path("rustfmt") + end, + }), + require("null-ls").builtins.diagnostics.shellcheck.with({ + condition = function() + return on_path("shellcheck") + end, + }), require("null-ls").builtins.formatting.shfmt.with({ extra_args = { "-i", "4", "-ci" }, + condition = function() + return on_path("shfmt") + end, + }), + require("null-ls").builtins.formatting.terraform_fmt.with({ + condition = function() + return on_path("terraform") + end, }), - require("null-ls").builtins.formatting.terraform_fmt, -- require("null-ls").builtins.diagnostics.luacheck, -- require("null-ls").builtins.diagnostics.markdownlint, -- require("null-ls").builtins.diagnostics.pylint, @@ -57,12 +100,14 @@ M.packer = function(use) -- Format on save on_attach = function(client) if client.resolved_capabilities.document_formatting then - vim.cmd([[ - augroup LspFormatting - autocmd! * - autocmd BufWritePre lua vim.lsp.buf.formatting_seq_sync() - augroup END - ]]) + local id = vim.api.nvim_create_augroup("LspFormatting", { + clear = true, + }) + vim.api.nvim_create_autocmd("BufWritePre", { + group = id, + pattern = "*", + callback = vim.lsp.buf.formatting_seq_sync, + }) end end, }) diff --git a/modules/editor/neovim/lua/packer/syntax.lua b/modules/editor/neovim/lua/packer/syntax.lua index d068f80..5a44ec0 100644 --- a/modules/editor/neovim/lua/packer/syntax.lua +++ b/modules/editor/neovim/lua/packer/syntax.lua @@ -21,6 +21,7 @@ M.packer = function(use) "yaml", "json", }, + auto_install = true, highlight = { enable = true }, indent = { enable = true }, }) diff --git a/modules/editor/neovim/lua/packer/telescope.lua b/modules/editor/neovim/lua/packer/telescope.lua index f28f96e..06b18f7 100644 --- a/modules/editor/neovim/lua/packer/telescope.lua +++ b/modules/editor/neovim/lua/packer/telescope.lua @@ -57,6 +57,19 @@ M.packer = function(use) requires = { "nvim-telescope/telescope.nvim" }, config = function() require("telescope").load_extension("project") + + choose_project = function() + local opts = require("telescope.themes").get_ivy({ + layout_config = { + bottom_pane = { + height = 10, + }, + }, + }) + require("telescope").extensions.project.project(opts) + end + + vim.keymap.set("n", "", choose_project) end, })