From 5c7ccf57e8b534e4bea7dbe505a9592957e42a97 Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Sat, 23 Dec 2023 00:06:39 +0100 Subject: [PATCH] [diagnosticls] Add php-cs-fixer --- lua/lsp.lua | 26 ++++++++++++++++++++++---- lua/lsp/diagnosticls.lua | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lua/lsp.lua b/lua/lsp.lua index da999db..5a591be 100644 --- a/lua/lsp.lua +++ b/lua/lsp.lua @@ -38,7 +38,7 @@ P.config = { } for server, _ in pairs(P.config) do - utils.try_require("lsp." .. server, package_name, function(mod) + utils.try_require("lsp." .. server, package_name, function (mod) P.config[server] = mod end) end @@ -108,7 +108,25 @@ function P.on_attach(client, bufnr) { "n", "x", }, "lf", function () - vim.lsp.buf.format({ async = false, }) + if vim.bo.filetype ~= "php" then + return vim.lsp.buf.format() + end + + local dls = require("lsp.diagnosticls") + local formatters = dls.lspconfig.init_options.formatFiletypes.php + for _, fmt in ipairs(formatters) do + if fmt == "php_cs_fixer" then + ---@type table + local winview = vim.fn.winsaveview() + vim.cmd.write({ bang = true, }) + vim.lsp.buf.format() + vim.cmd.write({ bang = true, }) + vim.fn.winrestview(winview) + return + end + end + + return vim.lsp.buf.format() end, opts ) @@ -199,8 +217,8 @@ function P.language_servers() if #not_installed > 0 then utils.warn( ("Disabling %s " - + "because the following required package(s) " - + "are not installed: %s") + .. "because the following required package(s) " + .. "are not installed: %s") :format( server, table.concat(not_installed, ", ") diff --git a/lua/lsp/diagnosticls.lua b/lua/lsp/diagnosticls.lua index a219708..2b9e3a5 100644 --- a/lua/lsp/diagnosticls.lua +++ b/lua/lsp/diagnosticls.lua @@ -126,6 +126,7 @@ return { sh = { "shfmt", }, bash = { "shfmt", }, zsh = { "shfmt", }, + php = { "php_cs_fixer", }, }, formatters = { autopep8 = { @@ -185,6 +186,27 @@ return { isStderr = false, ignoreExitCode = true, }, + php_cs_fixer = { + command = "./vendor/bin/php-cs-fixer", + args = { + "fix", + "--no-ansi", + "--using-cache=no", + "--quiet", + "--no-interaction", + "%file", + }, + isStdout = false, + isStderr = false, + doesWriteToFile = true, + ignoreExitCode = true, + rootPatterns = { + "composer.json", + "composer.lock", + "vendor", + ".git", + }, + }, }, }, },