refactor: move everything under lazyvim

This commit is contained in:
Folke Lemaitre 2023-01-01 14:33:56 +01:00
parent 39ccddad5f
commit 8eb8d235c9
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
17 changed files with 10 additions and 10 deletions

View file

@ -0,0 +1,40 @@
local M = {}
M.autoformat = true
function M.toggle()
M.autoformat = not M.autoformat
vim.notify(M.autoformat and "Enabled format on save" or "Disabled format on save")
end
function M.format()
local buf = vim.api.nvim_get_current_buf()
local ft = vim.bo[buf].filetype
local have_nls = #require("null-ls.sources").get_available(ft, "NULL_LS_FORMATTING") > 0
vim.lsp.buf.format({
bufnr = buf,
filter = function(client)
if have_nls then
return client.name == "null-ls"
end
return client.name ~= "null-ls"
end,
})
end
function M.on_attach(client, buf)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_create_autocmd("BufWritePre", {
group = vim.api.nvim_create_augroup("LspFormat." .. buf, {}),
buffer = buf,
callback = function()
if M.autoformat then
M.format()
end
end,
})
end
end
return M

View file

@ -0,0 +1,64 @@
local servers = require("lazyvim.plugins.lsp.servers")
local function on_attach(client, bufnr)
require("lazyvim.plugins.lsp.format").on_attach(client, bufnr)
require("lazyvim.plugins.lsp.keymaps").on_attach(client, bufnr)
end
return {
-- lspconfig
{
"neovim/nvim-lspconfig",
event = "BufReadPre",
dependencies = {
{ "folke/neoconf.nvim", cmd = "Neoconf", config = true },
{ "folke/neodev.nvim", config = true },
{
"williamboman/mason.nvim",
config = true,
cmd = "Mason",
keys = { { "<leader>cm", "<cmd>Mason<cr>", desc = "Mason" } },
},
{ "williamboman/mason-lspconfig.nvim", config = { automatic_installation = true } },
"hrsh7th/cmp-nvim-lsp",
},
config = function()
-- diagnostics
for name, icon in pairs(require("lazyvim.config.icons").diagnostics) do
name = "DiagnosticSign" .. name
vim.fn.sign_define(name, { text = icon, texthl = name, numhl = "" })
end
vim.diagnostic.config({
underline = true,
update_in_insert = false,
virtual_text = { spacing = 4, prefix = "" },
severity_sort = true,
})
-- lspconfig
local capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities())
for server, opts in pairs(servers) do
opts.capabilities = capabilities
opts.on_attach = on_attach
require("lspconfig")[server].setup(opts)
end
end,
},
-- formatters
{
"jose-elias-alvarez/null-ls.nvim",
event = "BufReadPre",
config = function()
local nls = require("null-ls")
nls.setup({
on_attach = on_attach,
sources = {
-- nls.builtins.formatting.prettierd,
nls.builtins.formatting.stylua,
nls.builtins.diagnostics.flake8,
},
})
end,
},
}

View file

@ -0,0 +1,74 @@
local M = {}
function M.on_attach(client, buffer)
local self = M.new(client, buffer)
self:map("<leader>cd", vim.diagnostic.open_float, { desc = "Line Diagnostics" })
self:map("<leader>cl", "LspInfo", { desc = "Lsp Info" })
self:map("<leader>xd", "Telescope diagnostics", { desc = "Telescope Diagnostics" })
self:map("gd", "Telescope lsp_definitions", { desc = "Goto Definition" })
self:map("gr", "Telescope lsp_references", { desc = "References" })
self:map("gD", "Telescope lsp_declarations", { desc = "Goto Declaration" })
self:map("gI", "Telescope lsp_implementations", { desc = "Goto Implementation" })
self:map("gt", "Telescope lsp_type_definitions", { desc = "Goto Type Definition" })
self:map("K", vim.lsp.buf.hover, { desc = "Hover" })
self:map("[d", M.diagnostic_goto(true), { desc = "Next Diagnostic" })
self:map("]d", M.diagnostic_goto(false), { desc = "Prev Diagnostic" })
self:map("]e", M.diagnostic_goto(true, "ERROR"), { desc = "Next Error" })
self:map("[e", M.diagnostic_goto(false, "ERROR"), { desc = "Prev Error" })
self:map("]w", M.diagnostic_goto(true, "WARNING"), { desc = "Next Warning" })
self:map("[w", M.diagnostic_goto(false, "WARNING"), { desc = "Prev Warning" })
self:map("<C-k>", vim.lsp.buf.signature_help, { desc = "Signature Help", mode = { "i", "n" }, has = "signatureHelp" })
self:map("<leader>ca", vim.lsp.buf.code_action, { desc = "Code Action", mode = { "n", "v" }, has = "codeAction" })
local format = require("lazyvim.plugins.lsp.format").format
self:map("<leader>cf", format, { desc = "Format Document", has = "documentFormatting" })
self:map("<leader>cf", format, { desc = "Format Range", mode = "v", has = "documentRangeFormatting" })
self:map("<leader>cr", M.rename, { expr = true, desc = "Rename", has = "rename" })
if client.name == "tsserver" and pcall(require, "typescript") then
self:map("<leader>co", "TypescriptOrganizeImports", { desc = "Organize Imports" })
self:map("<leader>cR", "TypescriptRenameFile", { desc = "Rename File" })
end
end
function M.new(client, buffer)
return setmetatable({ client = client, buffer = buffer }, { __index = M })
end
function M:has(cap)
return self.client.server_capabilities[cap .. "Provider"]
end
function M:map(lhs, rhs, opts)
opts = opts or {}
if opts.has and not self:has(opts.has) then
return
end
vim.keymap.set(
opts.mode or "n",
lhs,
type(rhs) == "string" and ("<cmd>%s<cr>"):format(rhs) or rhs,
---@diagnostic disable-next-line: no-unknown
{ silent = true, buffer = self.buffer, expr = opts.expr, desc = opts.desc }
)
end
function M.rename()
if pcall(require, "inc_rename") then
return ":IncRename " .. vim.fn.expand("<cword>")
else
vim.lsp.buf.rename()
end
end
function M.diagnostic_goto(next, severity)
local go = next and vim.diagnostic.goto_next or vim.diagnostic.goto_prev
severity = severity and vim.diagnostic.severity[severity] or nil
return function()
go({ severity = severity })
end
end
return M

View file

@ -0,0 +1,26 @@
-- Add any servers here together with their settings
---@type lspconfig.options
local servers = {
bashls = {},
clangd = {},
cssls = {},
tsserver = {},
html = {},
jsonls = {},
pyright = {},
yamlls = {},
sumneko_lua = {
settings = {
Lua = {
workspace = {
checkThirdParty = false,
},
completion = {
callSnippet = "Replace",
},
},
},
},
}
return servers