Mirrors the ~/repos/vim setup: vim.g.php_lsp_server picks phpantom_lsp (default) or intelephense, phpantom gets a generated .phpantom.toml that leaves phpstan/phpcs to none-ls, and <leader>l prefers a project's bin/cs-fix for php buffers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
249 lines
9.2 KiB
Lua
249 lines
9.2 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" } })
|
|
setup_server("eslint", {
|
|
filetypes = { "html", "typescriptreact", "javascriptreact", "css", "sass", "scss", "less", "svelte" },
|
|
})
|
|
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)
|