Separate LSP config and nvim-cmp

Make nvim-cmp standalone, lazy lone on InsertEnter and be used by neorg
to enable treesitter integration (no LSP available)

Move LSP config into lspconfig.lua

Optimise some startup conditions
This commit is contained in:
Evie Litherland-Smith 2023-05-20 12:44:21 +01:00
parent 2a3c867b11
commit 7d3109f60e
13 changed files with 184 additions and 180 deletions

View file

@ -1,31 +0,0 @@
-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, 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", "gr", vim.lsp.buf.references, bufopts)
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, bufopts)
vim.keymap.set("n", "K", vim.lsp.buf.hover, bufopts)
vim.keymap.set("n", "<leader>D", vim.lsp.buf.type_definition, bufopts)
vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, bufopts)
vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, bufopts)
vim.keymap.set(
"n",
"<leader>i",
function() vim.lsp.buf.format { bufnr = bufnr, timeout_ms = 2000, async = true } end,
bufopts
)
-- specific client tweaks
if client.name ~= "null-ls" then client.server_capabilities.documentFormattingProvider = false end
if client.name == "ruff_lsp" then client.server_capabilities.hoverProvider = false end
-- Attach navic and navbuddy if applicable
if client.server_capabilities.documentSymbolProvider then
require("nvim-navic").attach(client, bufnr)
require("nvim-navbuddy").attach(client, bufnr)
end
end
return on_attach

View file

@ -1,53 +0,0 @@
local cmp = require "cmp"
local luasnip = require "luasnip"
local cmp_autopairs = require "nvim-autopairs.completion.cmp"
cmp.setup {
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body) -- For `luasnip` users.
end,
},
mapping = cmp.mapping.preset.insert {
["<C-u>"] = cmp.mapping.scroll_docs(-4), -- Up
["<C-d>"] = cmp.mapping.scroll_docs(4), -- Down
["<C-Space>"] = cmp.mapping.complete(),
["<CR>"] = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = true,
},
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
},
sources = {
{ name = "nvim_lsp" },
{ name = "luasnip" },
},
}
-- Set configuration for specific filetype.
-- cmp.setup.filetype("python", { sources = { { name = "nvim_lsp" } } })
-- cmp.setup.filetype("nix", { sources = { { name = "nvim_lsp" } } })
cmp.setup.cmdline("/", { mapping = cmp.mapping.preset.cmdline(), sources = { { name = "buffer" } } })
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = { { name = "cmdline" }, { name = "path" } },
})
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())

View file

@ -1,39 +0,0 @@
local M = {}
-- Mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
local opts = { noremap = true, silent = true }
vim.keymap.set("n", "<leader>e", vim.diagnostic.open_float, opts)
vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
vim.keymap.set("n", "<leader>q", vim.diagnostic.setloclist, opts)
local signs = { Error = "", Warn = "", Hint = "", Info = "" }
for type, icon in pairs(signs) do
local hl = "DiagnosticSign" .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
end
require("neodev").setup()
M.on_attach = require "lsp.attach"
M.lsp_flags = require "lsp.flags"
M.capabilities = require("cmp_nvim_lsp").default_capabilities()
local servers = { "pyright", "ruff_lsp", "nil_ls", "lua_ls", "fortls", "yamlls", "vimls", "bashls" }
for _, name in ipairs(servers) do
require("lspconfig")[name].setup {
on_attach = M.on_attach,
flags = M.lsp_flags,
capabilities = M.capabilities,
}
end
require("null-ls").setup {
sources = require "lsp.null_ls_sources",
on_attach = M.on_attach,
flags = M.lsp_flags,
capabilities = M.capabilities,
}
return M

View file

@ -1,5 +0,0 @@
local lsp_flags = {
-- This is the default in Nvim 0.7+
debounce_text_changes = 150,
}
return lsp_flags

View file

@ -1,19 +0,0 @@
local null_ls = require "null-ls"
return {
null_ls.builtins.code_actions.gitsigns,
null_ls.builtins.formatting.alejandra,
null_ls.builtins.formatting.autoflake,
null_ls.builtins.formatting.beautysh,
null_ls.builtins.formatting.black,
null_ls.builtins.formatting.fixjson,
-- null_ls.builtins.formatting.fprettify,
null_ls.builtins.formatting.isort,
null_ls.builtins.formatting.mdformat,
-- null_ls.builtins.formatting.nixfmt,
-- null_ls.builtins.formatting.nixpkgs_fmt,
null_ls.builtins.formatting.shellharden,
null_ls.builtins.formatting.stylua,
null_ls.builtins.hover.dictionary,
}

View file

@ -1,7 +1,6 @@
return {
"numToStr/Comment.nvim",
keys = { "gcc", "gcb" },
event = { "BufReadPre" },
config = true,
opts = { ignore = "^$" },
}

View file

@ -1,18 +1,93 @@
local on_attach = function(client, 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", "gr", vim.lsp.buf.references, bufopts)
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, bufopts)
vim.keymap.set("n", "K", vim.lsp.buf.hover, bufopts)
vim.keymap.set("n", "<leader>D", vim.lsp.buf.type_definition, bufopts)
vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, bufopts)
vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, bufopts)
vim.keymap.set(
"n",
"<leader>i",
function() vim.lsp.buf.format { bufnr = bufnr, timeout_ms = 2000, async = true } end,
bufopts
)
-- specific client tweaks
if client.name ~= "null-ls" then client.server_capabilities.documentFormattingProvider = false end
if client.name == "ruff_lsp" then client.server_capabilities.hoverProvider = false end
-- Attach navic and navbuddy if applicable
if client.server_capabilities.documentSymbolProvider then
require("nvim-navic").attach(client, bufnr)
require("nvim-navbuddy").attach(client, bufnr)
end
end
local config = function()
require("neodev").setup()
-- Mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
local opts = { noremap = true, silent = true }
vim.keymap.set("n", "<leader>e", vim.diagnostic.open_float, opts)
vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
vim.keymap.set("n", "<leader>q", vim.diagnostic.setloclist, opts)
local signs = { Error = "", Warn = "", Hint = "", Info = "" }
for type, icon in pairs(signs) do
local hl = "DiagnosticSign" .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
end
local lsp_flags = { debounce_text_changes = 150 }
local capabilities = require("cmp_nvim_lsp").default_capabilities()
local servers = {
"pyright",
"ruff_lsp",
"nil_ls",
"lua_ls",
"fortls",
"yamlls",
"vimls",
"bashls",
}
for _, name in ipairs(servers) do
require("lspconfig")[name].setup {
on_attach = on_attach,
flags = lsp_flags,
capabilities = capabilities,
}
end
local builtins = require "null-ls.builtins"
require("null-ls").setup {
sources = {
builtins.code_actions.gitsigns,
builtins.formatting.alejandra,
builtins.formatting.autoflake,
builtins.formatting.beautysh,
builtins.formatting.black,
builtins.formatting.fixjson,
builtins.formatting.isort,
builtins.formatting.mdformat,
builtins.formatting.shellharden,
builtins.formatting.stylua,
builtins.hover.dictionary,
},
on_attach = on_attach,
flags = lsp_flags,
capabilities = capabilities,
}
end
return {
"neovim/nvim-lspconfig",
event = { "BufReadPre", "BufNewFile", "CmdlineEnter" },
dependencies = {
{ "hrsh7th/cmp-nvim-lsp", dependencies = { "hrsh7th/nvim-cmp" } },
{ "hrsh7th/cmp-buffer", dependencies = { "hrsh7th/nvim-cmp" } },
{ "hrsh7th/cmp-path", dependencies = { "hrsh7th/nvim-cmp" } },
{ "hrsh7th/cmp-cmdline", dependencies = { "hrsh7th/nvim-cmp" } },
{ "L3MON4D3/LuaSnip", dependencies = { "hrsh7th/nvim-cmp", "saadparwaiz1/cmp_luasnip" } },
{ "windwp/nvim-autopairs", dependencies = { "hrsh7th/nvim-cmp" } },
{ "folke/neodev.nvim", dependencies = { "hrsh7th/nvim-cmp" } },
{ "jose-elias-alvarez/null-ls.nvim", dependencies = { "davidmh/cspell.nvim" } },
},
config = function()
require "lsp.config"
require "lsp.cmp"
end,
event = { "BufReadPre", "BufNewFile" },
dependencies = { "cmp-nvim-lsp", "folke/neodev.nvim", "jose-elias-alvarez/null-ls.nvim" },
config = config,
}

9
lua/plugins/luasnip.lua Normal file
View file

@ -0,0 +1,9 @@
return {
"L3MON4D3/LuaSnip",
lazy = true,
dependencies = {
"rafamadriz/friendly-snippets",
config = function() require("luasnip.loaders.from_vscode").lazy_load() end,
},
-- build = "make install_jsregexp",
}

View file

@ -1,4 +1,4 @@
return {
"dccsillag/magma-nvim",
event = { "BufReadPre", "BufNewFile" },
event = { "FileType python" },
}

View file

@ -10,10 +10,6 @@ return {
opts = {
disable_commit_confirmation = true,
disable_builtin_notifications = true,
-- kind = "floating",
-- commit_popup = { kind = "floating" },
-- preview_buffer = { kind = "floating" },
-- popup = { kind = "floating" },
integrations = { diffview = true },
},
}

View file

@ -1,10 +1,8 @@
return {
"rcarriga/nvim-notify",
lazy = true,
-- dependencies = { "catppuccin" },
config = true,
opts = {
-- background_colour = require("catppuccin.palettes").get_palette("macchiato").base,
fps = 60,
render = "default", -- "minimal", "simple"
},

66
lua/plugins/nvim_cmp.lua Normal file
View file

@ -0,0 +1,66 @@
local config = function()
local cmp = require "cmp"
local luasnip = require "luasnip"
local cmp_autopairs = require "nvim-autopairs.completion.cmp"
cmp.setup {
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body) -- For `luasnip` users.
end,
},
mapping = cmp.mapping.preset.insert {
["<C-u>"] = cmp.mapping.scroll_docs(-4), -- Up
["<C-d>"] = cmp.mapping.scroll_docs(4), -- Down
["<C-Space>"] = cmp.mapping.complete(),
["<CR>"] = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = true,
},
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
},
sources = { { name = "nvim_lsp" }, { name = "luasnip" } },
}
-- Set configuration for specific filetype.
cmp.setup.filetype("norg", { sources = { { name = "neorg" } } })
cmp.setup.cmdline({ "/", "?" }, { mapping = cmp.mapping.preset.cmdline(), sources = { { name = "buffer" } } })
cmp.setup.cmdline(
":",
{ mapping = cmp.mapping.preset.cmdline(), sources = { { name = "path" }, { name = "cmdline" } } }
)
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
end
return {
"hrsh7th/nvim-cmp",
event = { "InsertEnter", "CmdlineEnter" },
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-path",
"hrsh7th/cmp-cmdline",
"windwp/nvim-autopairs",
"saadparwaiz1/cmp_luasnip",
"L3MON4D3/LuaSnip",
},
config = config,
}

View file

@ -1,18 +1,26 @@
return {
"nvim-neorg/neorg",
build = ":Neorg sync-parsers",
dependencies = { { "nvim-lua/plenary.nvim" } },
event = { "FileType norg" },
cmd = "Neorg",
lazy = true,
dependencies = { "nvim-lua/plenary.nvim", "nvim-treesitter/nvim-treesitter" },
init = function()
vim.api.nvim_create_autocmd({ "BufNewFile", "BufReadPost" }, {
pattern = { "*.norg" },
command = "set filetype=norg",
})
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
pattern = { "*.norg" },
command = "Neorg update-metadata",
})
end,
opts = {
load = {
["core.defaults"] = {}, -- Loads default behaviour
["core.concealer"] = {}, -- Adds pretty icons to your documents
["core.dirman"] = { -- Manages Neorg workspaces
config = {
workspaces = {
notes = "~/notes",
},
},
},
["core.defaults"] = {},
["core.concealer"] = {},
["core.dirman"] = { config = { workspaces = { notes = "~/notes" } } },
["core.completion"] = { config = { engine = "nvim-cmp" } },
},
},
}