From ce7fed242ec58da78d37767d9d4bf9f1fc432141 Mon Sep 17 00:00:00 2001 From: Evie Litherland-Smith Date: Mon, 20 Feb 2023 11:45:08 +0000 Subject: [PATCH] Split LSP settings into more specific files in lsp directory --- config/nvim/init.lua | 8 ++-- config/nvim/lua/lsp/attach.lua | 23 +++++++++++ config/nvim/lua/lsp/flags.lua | 5 +++ config/nvim/lua/lsp/formatting.lua | 9 ++++ config/nvim/lua/lsp/init.lua | 55 ++----------------------- config/nvim/lua/lsp/null_ls_sources.lua | 13 ++++++ 6 files changed, 57 insertions(+), 56 deletions(-) create mode 100644 config/nvim/lua/lsp/attach.lua create mode 100644 config/nvim/lua/lsp/flags.lua create mode 100644 config/nvim/lua/lsp/formatting.lua create mode 100644 config/nvim/lua/lsp/null_ls_sources.lua diff --git a/config/nvim/init.lua b/config/nvim/init.lua index 2bd9cad6..14ba0ad1 100644 --- a/config/nvim/init.lua +++ b/config/nvim/init.lua @@ -1,3 +1,5 @@ +vim.g.mapleader = " " + -- bootstrap lazy.nvim local lazypath = vim.fn.stdpath "data" .. "/lazy/lazy.nvim" if not vim.loop.fs_stat(lazypath) then @@ -12,14 +14,10 @@ if not vim.loop.fs_stat(lazypath) then end vim.opt.rtp:prepend(lazypath) -vim.g.mapleader = " " -require("lazy").setup("plugins") - +require("lazy").setup "plugins" -- Set vim options require "options" - -- Define custom keymappings require "keymaps" - -- Remaining vim commands to be converted to lua require "vimcommands" diff --git a/config/nvim/lua/lsp/attach.lua b/config/nvim/lua/lsp/attach.lua new file mode 100644 index 00000000..589b6387 --- /dev/null +++ b/config/nvim/lua/lsp/attach.lua @@ -0,0 +1,23 @@ +-- Use an on_attach function to only map the following keys +-- after the language server attaches to the current buffer +local on_attach = function(_, bufnr) + -- Mappings. + -- See `:help vim.lsp.*` for documentation on any of the below functions + local bufopts = { noremap = true, silent = true, buffer = bufnr } + vim.keymap.set("n", "gD", vim.lsp.buf.definition, bufopts) + vim.keymap.set("n", "gd", "Trouble lsp_definitions", bufopts) + vim.keymap.set("n", "gR", vim.lsp.buf.references, bufopts) + vim.keymap.set("n", "gr", "Trouble lsp_references", bufopts) + vim.keymap.set("n", "gI", vim.lsp.buf.implementation, bufopts) + vim.keymap.set("n", "gi", "Trouble lsp_implementations", bufopts) + vim.keymap.set("n", "K", vim.lsp.buf.hover, bufopts) + vim.keymap.set("n", "", vim.lsp.buf.signature_help, bufopts) + vim.keymap.set("n", "wa", vim.lsp.buf.add_workspace_folder, bufopts) + vim.keymap.set("n", "wr", vim.lsp.buf.remove_workspace_folder, bufopts) + vim.keymap.set("n", "wl", function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, bufopts) + vim.keymap.set("n", "D", vim.lsp.buf.type_definition, bufopts) + vim.keymap.set("n", "rn", vim.lsp.buf.rename, bufopts) + vim.keymap.set("n", "ca", vim.lsp.buf.code_action, bufopts) + vim.keymap.set("n", "i", require "lsp.formatting", bufopts) +end +return on_attach diff --git a/config/nvim/lua/lsp/flags.lua b/config/nvim/lua/lsp/flags.lua new file mode 100644 index 00000000..3f61fcac --- /dev/null +++ b/config/nvim/lua/lsp/flags.lua @@ -0,0 +1,5 @@ +local lsp_flags = { + -- This is the default in Nvim 0.7+ + debounce_text_changes = 150, +} +return lsp_flags diff --git a/config/nvim/lua/lsp/formatting.lua b/config/nvim/lua/lsp/formatting.lua new file mode 100644 index 00000000..0f368e01 --- /dev/null +++ b/config/nvim/lua/lsp/formatting.lua @@ -0,0 +1,9 @@ +local lsp_formatting = function(bufnr) + vim.lsp.buf.format { + filter = function(client) return client.name == "null-ls" end, + bufnr = bufnr, + timeout_ms = 2000, + -- async = true, + } +end +return lsp_formatting diff --git a/config/nvim/lua/lsp/init.lua b/config/nvim/lua/lsp/init.lua index 362db1c6..b22b778f 100644 --- a/config/nvim/lua/lsp/init.lua +++ b/config/nvim/lua/lsp/init.lua @@ -12,45 +12,10 @@ for type, icon in pairs(signs) do vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl }) end -local lsp_formatting = function(bufnr) - vim.lsp.buf.format { - filter = function(client) return client.name == "null-ls" end, - bufnr = bufnr, - timeout_ms = 2000, - -- async = true, - } -end - --- Use an on_attach function to only map the following keys --- after the language server attaches to the current buffer -local on_attach = function(_, bufnr) - -- Mappings. - -- See `:help vim.lsp.*` for documentation on any of the below functions - local bufopts = { noremap = true, silent = true, buffer = bufnr } - vim.keymap.set("n", "gD", vim.lsp.buf.definition, bufopts) - vim.keymap.set("n", "gd", "Trouble lsp_definitions", bufopts) - vim.keymap.set("n", "gR", vim.lsp.buf.references, bufopts) - vim.keymap.set("n", "gr", "Trouble lsp_references", bufopts) - vim.keymap.set("n", "gI", vim.lsp.buf.implementation, bufopts) - vim.keymap.set("n", "gi", "Trouble lsp_implementations", bufopts) - vim.keymap.set("n", "K", vim.lsp.buf.hover, bufopts) - vim.keymap.set("n", "", vim.lsp.buf.signature_help, bufopts) - vim.keymap.set("n", "wa", vim.lsp.buf.add_workspace_folder, bufopts) - vim.keymap.set("n", "wr", vim.lsp.buf.remove_workspace_folder, bufopts) - vim.keymap.set("n", "wl", function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, bufopts) - vim.keymap.set("n", "D", vim.lsp.buf.type_definition, bufopts) - vim.keymap.set("n", "rn", vim.lsp.buf.rename, bufopts) - vim.keymap.set("n", "ca", vim.lsp.buf.code_action, bufopts) - vim.keymap.set("n", "i", lsp_formatting, bufopts) -end - -local lsp_flags = { - -- This is the default in Nvim 0.7+ - debounce_text_changes = 150, -} - require("neodev").setup() +local on_attach = require "lsp.attach" +local lsp_flags = require "lsp.flags" local lspconfig = require "lspconfig" local capabilities = require("cmp_nvim_lsp").default_capabilities() @@ -78,20 +43,8 @@ lspconfig.texlab.setup { capabilities = capabilities, } -local null_ls = require "null-ls" -null_ls.setup { - sources = { - null_ls.builtins.code_actions.gitsigns, - null_ls.builtins.diagnostics.mypy, - null_ls.builtins.diagnostics.zsh, - null_ls.builtins.formatting.black, - null_ls.builtins.formatting.isort, - null_ls.builtins.formatting.trim_whitespace, - null_ls.builtins.formatting.stylua, - null_ls.builtins.formatting.shfmt, - null_ls.builtins.formatting.prettier, - null_ls.builtins.hover.dictionary, - }, +require("null-ls").setup { + sources = require "lsp.null_ls_sources", on_attach = on_attach, flags = lsp_flags, capabilities = capabilities, diff --git a/config/nvim/lua/lsp/null_ls_sources.lua b/config/nvim/lua/lsp/null_ls_sources.lua new file mode 100644 index 00000000..3c39fb19 --- /dev/null +++ b/config/nvim/lua/lsp/null_ls_sources.lua @@ -0,0 +1,13 @@ +local null_ls = require "null-ls" +return { + null_ls.builtins.code_actions.gitsigns, + null_ls.builtins.diagnostics.mypy, + null_ls.builtins.diagnostics.zsh, + null_ls.builtins.formatting.black, + null_ls.builtins.formatting.isort, + null_ls.builtins.formatting.trim_whitespace, + null_ls.builtins.formatting.stylua, + null_ls.builtins.formatting.shfmt, + null_ls.builtins.formatting.prettier, + null_ls.builtins.hover.dictionary, +}