Files
nvim-kickstart/lua/custom/lspconfig.lua
T
Mathias RothenhaeuslerandClaude Opus 5.5 3fed16e0b9 Run the eslint language server for JavaScript, TypeScript and Vue
Drop the filetypes copied from cssls so nvim-lspconfig's defaults apply,
fall back to the global eslint, and honour a project's ESLint baseline
(eslint-suppressions.json) via applySuppressions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 14:38:38 +01:00

269 lines
10 KiB
Lua

-- lsp.lua (replace your current file with this)
-- import cmp-nvim-lsp plugin safely
-- local cmp_nvim_lsp_status, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
-- if not cmp_nvim_lsp_status then
-- return
-- end
local keymap = vim.keymap -- for conciseness
---------- border >------------------------------
do
local orig_floating_preview = vim.lsp.util.open_floating_preview
vim.lsp.util.open_floating_preview = function(contents, syntax, opts, ...)
opts = opts or {}
opts.border = "rounded"
return orig_floating_preview(contents, syntax, opts, ...)
end
end
---------- </border ------------------------------
vim.diagnostic.config({
virtual_text = false, -- Disable inline virtual text
float = {
border = "rounded", -- You can choose "single", "double", etc.
source = "always", -- Show the source of the diagnostic (set to 'if_many' for less clutter)
header = "", -- Optional: add a header to the float window
prefix = "", -- Optional: prefix for each diagnostic (leave empty for no prefix)
},
underline = true, -- Optional: underline the lines with errors
signs = true, -- Optional: show signs in the sign column
})
-- A project's own bin/cs-fix (e.g. rcc's, wrapping php-cs-fixer with the
-- project config) wins over the LSP formatter for PHP buffers. It only
-- rewrites files on disk, so it runs on a temp copy of the buffer and the
-- result is put back with nvim_buf_set_lines() - the buffer needn't be saved
-- first, and the format stays a single undo step instead of a reload.
local function format_buffer()
local cs_fix = vim.bo.filetype == "php"
and vim.fs.find("bin/cs-fix", { upward = true, path = vim.fn.expand("%:p:h") })[1]
if not cs_fix or vim.fn.executable(cs_fix) == 0 then
vim.lsp.buf.format()
return
end
local tmp = vim.fn.tempname() .. ".php"
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
vim.fn.writefile(lines, tmp)
local result = vim.system({ cs_fix, tmp }, { text = true }):wait()
if result.code ~= 0 then
os.remove(tmp)
local output = vim.trim((result.stderr or "") .. (result.stdout or ""))
vim.notify("bin/cs-fix failed: " .. output, vim.log.levels.ERROR)
return
end
local formatted = vim.fn.readfile(tmp)
os.remove(tmp)
if vim.deep_equal(formatted, lines) then
vim.notify("bin/cs-fix: nothing to change")
return
end
local view = vim.fn.winsaveview()
vim.api.nvim_buf_set_lines(0, 0, -1, false, formatted)
vim.fn.winrestview(view)
vim.notify("Formatted with bin/cs-fix")
end
-- enable keybinds only for when lsp server available
local on_attach = function(_, bufnr)
local opts = { noremap = true, silent = true, buffer = bufnr }
keymap.set("n", "g0", "<cmd>lua vim.lsp.buf.document_symbol()<CR>", opts)
keymap.set("n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
keymap.set("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
keymap.set("n", "<leader>l", format_buffer, { desc = "Format code function" })
keymap.set(
"n",
"<leader>cbf",
":!phpcbf -w --standard=PSR12 %<CR>",
{ noremap = true, silent = false, buffer = bufnr }
)
keymap.set("n", "<leader>o", "<cmd>Lspsaga outline<CR>", opts)
keymap.set("n", "<leader>ss", "<cmd>Telescope lsp_dynamic_workspace_symbols<CR>", opts)
-- LSP Saga
keymap.set("n", "gsd", "<cmd>Lspsaga peek_definition<CR>", opts)
keymap.set("n", "gr", "<cmd>Lspsaga finder<CR>", opts)
keymap.set("n", "<leader>rn", "<cmd>Lspsaga rename<CR>", opts)
keymap.set("n", "<leader>ca", "<cmd>Lspsaga code_action<CR>", opts)
keymap.set("n", "ge", "<cmd>Lspsaga show_line_diagnostics<CR>", opts)
keymap.set("n", "<a-cr>", "<cmd>Lspsaga code_action<CR>", opts)
-- keymap.set("n", "<leader>d", "<cmd>Telescope diagnostics<CR>", opts)
keymap.set("n", "<leader>da", "<cmd>Lspsaga show_workspace_diagnostics ++float<CR>", opts)
keymap.set("n", "<leader>db", "<cmd>Lspsaga show_buf_diagnostics ++float<CR>", opts)
keymap.set("n", "<leader>dn", "<cmd>Lspsaga diagnostic_jump_next<CR>")
keymap.set("n", "<leader>dp", "<cmd>Lspsaga diagnostic_jump_prev<CR>")
keymap.set("n", "gj", "<cmd>lua require'nvim-treesitter-textobjects.move'.goto_next_start('@function.outer')<CR>",
opts)
keymap.set("n", "gk", "<cmd>lua require'nvim-treesitter-textobjects.move'.goto_previous_start('@function.outer')<CR>",
opts)
end
-- capabilities
local capabilities = vim.lsp.protocol.make_client_capabilities()
-- helper for registering server configs (lazy attach)
local function setup_server(name, config)
local cfg = vim.tbl_deep_extend("force", {
capabilities = capabilities,
on_attach = on_attach,
}, config or {})
-- register the config
-- use the function form so it works in latest nvim
vim.lsp.config(name, cfg)
end
-- servers
setup_server("bashls", { filetypes = { "sh", "bin" } })
setup_server("lemminx", { filetypes = { "xml" } })
setup_server("yamlls", { filetypes = { "yaml", "yml" } })
setup_server("jsonls", { filetypes = { "json" } })
setup_server("pyright", { filetypes = { "py" } })
setup_server("html", { filetypes = { "html" } })
setup_server("cssls",
{ filetypes = { "html", "typescriptreact", "javascriptreact", "css", "sass", "scss", "less", "svelte" } })
-- nvim-lspconfig's own filetypes (javascript, typescript, vue, the *react
-- variants, svelte, ...) - the server uses the project's node_modules eslint
-- and its config, like ALE does in ~/repos/vim. nodePath is the fallback
-- for projects without one: the global eslint (npm root -g), which the
-- server doesn't look in on its own.
--
-- applySuppressions honours a project's ESLint baseline (eslint-suppressions.json,
-- e.g. rcc's), which the server otherwise ignores. Only set when that file
-- exists: eslint before 9.24 doesn't know the option and refuses to start.
-- nvim-lspconfig's own before_init (workspaceFolder) still runs first.
local eslint_before_init = vim.lsp.config.eslint.before_init
setup_server("eslint", {
settings = { nodePath = "/usr/lib/node_modules" },
before_init = function(params, config)
if eslint_before_init then
eslint_before_init(params, config)
end
if config.root_dir and vim.uv.fs_stat(config.root_dir .. "/eslint-suppressions.json") then
config.settings.options = vim.tbl_extend("force", config.settings.options or {},
{ applySuppressions = true })
end
end,
})
setup_server("rust_analyzer", {
settings = {
["rust-analyzer"] = { checkOnSave = true, check = { command = "clippy" } },
},
})
-- PHP LSPs
-- start it manually :LspStart phpactor
-- setup_server("phpactor", {
-- filetypes = { "php" },
-- -- root_dir = fallback_root_dir,
-- init_options = {
-- ["language_server_phpstan.enabled"] = true,
-- ["language_server_psalm.enabled"] = false,
-- },
-- })
setup_server("lua_ls", {
filetypes = { "lua" },
settings = {
Lua = {
diagnostics = {
globals = { "vim" } },
format = { enable = true },
}
}
})
-- Which PHP LSP server gets enabled - "intelephense" (licensed, mature) or
-- "phpantom_lsp" (Rust, faster startup). Only one: running both doubles up
-- completion, hover and diagnostics. Mirrors g:pure_vim_php_lsp_server in
-- ~/repos/vim; override with `vim.g.php_lsp_server = "intelephense"` before
-- this file is loaded.
local php_lsp_server = vim.g.php_lsp_server or "phpantom_lsp"
-- phpantom runs PHPStan, PHPMD, PHPCS and Mago itself when a project has
-- them, but none-ls already runs phpstan/phpcs. Its only switch is a
-- per-project .phpantom.toml, so one turning those off is written to the
-- workspace root before the server initializes. An existing one is left alone.
local phpantom_config = {
"# generated by nvim-kickstart lspconfig.lua: phpstan, phpmd and phpcs",
"# run through none-ls, Mago is off. Edit freely - it is never overwritten.",
"[mago]",
'command = ""',
"[phpstan]",
'command = ""',
"[phpmd]",
'command = ""',
"[phpcs]",
'command = ""',
}
setup_server("phpantom_lsp", {
before_init = function(_, config)
local root = config.root_dir
if not root then
return
end
local file = root .. "/.phpantom.toml"
if vim.uv.fs_stat(file) or not vim.uv.fs_stat(root .. "/composer.json") then
return
end
vim.fn.writefile(phpantom_config, file)
end,
})
setup_server("intelephense", {
filetypes = { "php" },
-- root_dir = fallback_root_dir,
init_options = {
licenceKey = (function()
local license_path = vim.fn.expand("$HOME/.config/intelephense/license.txt")
if vim.fn.filereadable(license_path) == 1 then
return vim.fn.readfile(license_path)[1]
end
return nil
end)(),
},
settings = {
intelephense = {
telemetry = { enabled = false },
files = {
maxSize = 5000000,
associations = { "**/*.php" },
},
completion = {
fullyQualifyGlobalConstantsAndFunctions = false,
triggerParameterHints = true,
maxItems = 100,
},
phpdoc = { enabled = true },
diagnostics = { enable = true },
format = { enable = false },
},
},
})
-- phpcs / phpstan configs
vim.g.nvim_phpcs_config_phpcs_path = 'phpcs'
vim.g.nvim_phpcs_config_phpcbf_path = 'phpcbf'
local phpcs_config = "~/repos/configs/phpcs.xml"
local phpcs_config_new = "~/repos/dotfiles/phpcs.xml"
if vim.uv.fs_stat(phpcs_config_new) then
vim.g.nvim_phpcs_config_phpcs_standard = phpcs_config_new
elseif vim.uv.fs_stat(phpcs_config) then
vim.g.nvim_phpcs_config_phpcs_standard = phpcs_config
end
-- === ENABLE: tell Neovim to autostart these configs lazily (filetype + root_dir)
-- call this early in your init (so FileType autocommands are installed before opening files)
local enabled_servers = {
"bashls", "lemminx", "yamlls", "jsonls", "pyright", "html", "cssls",
"eslint", "rust_analyzer", "lua_ls",
php_lsp_server,
}
vim.lsp.enable(enabled_servers)