diff --git a/lua/plugins/none-ls.lua b/lua/plugins/none-ls.lua index 96736bb..2383379 100644 --- a/lua/plugins/none-ls.lua +++ b/lua/plugins/none-ls.lua @@ -54,28 +54,9 @@ return { prefer_local = "node_modules/.bin", }), - -- PHP CodeSniffer (phpcbf) - WordPress standards - formatting.phpcbf.with({ - command = find_executable({ "phpcbf" }), - prefer_local = "vendor/bin", - -- Respects phpcs.xml in project root or uses WordPress standard - extra_args = function(params) - local root = params.root or vim.fn.getcwd() - - -- Check for project ruleset - local has_project_ruleset = - vim.loop.fs_stat(root .. "/phpcs.xml") - or vim.loop.fs_stat(root .. "/phpcs.xml.dist") - - if has_project_ruleset then - return {} -- Let project ruleset control standard - end - - -- No project ruleset: use WordPress to match nvim-lint - return { "--standard=WordPress" } - end, - }), - + -- PHP: phpcbf DISABLED - using direct autocmd approach instead (see bottom of file) + -- formatting.phpcbf causes blank line bug even with custom formatters + -- stylua (Lua) formatting.stylua.with({ command = find_executable({ "stylua" }), @@ -124,5 +105,54 @@ return { vim.keymap.set("v", "lf", function() vim.lsp.buf.format({ async = false }) end, { desc = "Formatting: Format selection", silent = true, noremap = true }) + + -- PHP: Direct phpcbf formatting (bypasses none-ls entirely) + vim.api.nvim_create_autocmd("BufWritePre", { + pattern = "*.php", + callback = function() + if vim.g.format_on_save == false then + return + end + + local bufnr = vim.api.nvim_get_current_buf() + local filepath = vim.api.nvim_buf_get_name(bufnr) + + -- Find phpcbf + local phpcbf = find_executable({ "phpcbf" }) + if not phpcbf then + return + end + + -- Determine standard + local root = vim.fn.getcwd() + local has_project_ruleset = + vim.loop.fs_stat(root .. "/phpcs.xml") + or vim.loop.fs_stat(root .. "/phpcs.xml.dist") + + local cmd = { phpcbf, "-q", "--stdin-path=" .. filepath } + if not has_project_ruleset then + table.insert(cmd, "--standard=WordPress") + end + table.insert(cmd, "-") + + -- Get buffer content + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + local input = table.concat(lines, "\n") + + -- Run phpcbf + local result = vim.fn.system(cmd, input) + local exit_code = vim.v.shell_error + + -- Apply result if successful (exit code 0 or 1) + if exit_code == 0 or exit_code == 1 then + local output_lines = vim.split(result, "\n", { plain = true }) + -- Remove trailing empty line if present + if output_lines[#output_lines] == "" then + table.remove(output_lines) + end + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, output_lines) + end + end, + }) end, }