diff --git a/lua/custom/lspconfig.lua b/lua/custom/lspconfig.lua index 5fe97c8..b26ca73 100644 --- a/lua/custom/lspconfig.lua +++ b/lua/custom/lspconfig.lua @@ -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", "lua vim.lsp.buf.document_symbol()", opts) keymap.set("n", "gd", "lua vim.lsp.buf.definition()", opts) keymap.set("n", "gi", "lua vim.lsp.buf.implementation()", opts) - keymap.set("n", "l", function() - vim.lsp.buf.format() - end, { desc = "Format code function" }) + keymap.set("n", "l", format_buffer, { desc = "Format code function" }) keymap.set( "n", "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)