bypass none-ls when formatting php

This commit is contained in:
Ray Elliott 2026-01-12 18:26:51 +00:00
parent 9916849e01
commit 1530564117
1 changed files with 52 additions and 22 deletions

View File

@ -54,27 +54,8 @@ 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({
@ -124,5 +105,54 @@ return {
vim.keymap.set("v", "<leader>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,
}