use phpantom_lsp for php and format with bin/cs-fix
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>
This commit is contained in:
1 parent
a21331d225
commit
c057c995fd
1 file changed
+76
-4
@@ -30,6 +30,42 @@ vim.diagnostic.config({
|
||||
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 }
|
||||
@@ -37,9 +73,7 @@ local on_attach = function(_, 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", function()
|
||||
vim.lsp.buf.format()
|
||||
end, { desc = "Format code function" })
|
||||
keymap.set("n", "<leader>l", format_buffer, { desc = "Format code function" })
|
||||
keymap.set(
|
||||
"n",
|
||||
"<leader>cbf",
|
||||
@@ -123,6 +157,44 @@ setup_server("lua_ls", {
|
||||
}
|
||||
})
|
||||
|
||||
-- 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,
|
||||
@@ -171,6 +243,6 @@ end
|
||||
local enabled_servers = {
|
||||
"bashls", "lemminx", "yamlls", "jsonls", "pyright", "html", "cssls",
|
||||
"eslint", "rust_analyzer", "lua_ls",
|
||||
"intelephense"
|
||||
php_lsp_server,
|
||||
}
|
||||
vim.lsp.enable(enabled_servers)
|
||||
Reference in new issue
Block a user