fix(lsp): disable php formatting workaround

This commit is contained in:
2024-02-26 18:45:53 +01:00
parent c785fe4e8c
commit 936cfc9096
16 changed files with 199 additions and 194 deletions
+10 -4
View File
@@ -63,7 +63,11 @@ when needed.
### Language servers ### Language servers
Language servers are installed automatically to the nvim data directory Language servers are installed automatically to the nvim data directory
(`:echo stdpath('data') .. '/mason'`). The following are some noted requirements (`:echo stdpath('data') .. '/mason'`) upon entering a buffer of related
filetype. Automatic installation can be turned off, see the end of this section
for instructions.
The following are some noted requirements
for the installations themselves: for the installations themselves:
- **diagnostic-languageserver**: npm - **diagnostic-languageserver**: npm
@@ -78,9 +82,11 @@ Some servers have additional runtime dependencies:
- **bash-language-server**: shellcheck (optional, used for linting) - **bash-language-server**: shellcheck (optional, used for linting)
If you don't need some specific language server, and want to get rid of any If you don't need some specific language server, you may either remove them from
warning messages, you may either remove them from the top of `lua/lsp.lua` the top of `lua/lsp.lua` or disable them in `lua/lsp/<server>.lua`.
or disable them in `lua/lsp/<server>.lua`.
To disable automatic installation of a selected language server, remove or
comment out the mason part of the configuration at `lua/lsp/<server>.lua`.
### Nerd Font ### Nerd Font
It's recommended to use a [Nerd Font](https://www.nerdfonts.com/), It's recommended to use a [Nerd Font](https://www.nerdfonts.com/),
+115 -147
View File
@@ -1,14 +1,14 @@
local package_name = "lsp" local module_name = "lsp"
local utils = require("utils") local utils = require("utils")
local P = {} local M = {}
P._filetypes = nil local _filetypes = nil
P._language_servers = nil -- local auto_installed_servers = nil
P.capabilities = {} local capabilities = {}
P.config = { local config = {
bashls = {}, bashls = {},
clangd = {}, clangd = {},
cmake = {}, cmake = {},
@@ -23,9 +23,9 @@ P.config = {
gopls = {}, gopls = {},
} }
for server, _ in pairs(P.config) do for server, _ in pairs(config) do
utils.try_require("lsp." .. server, package_name, function (mod) utils.try_require("lsp." .. server, module_name, function (mod)
P.config[server] = mod config[server] = mod
end) end)
end end
@@ -43,7 +43,7 @@ local function ca_rename()
end end
end end
function P._setup_diagnostics() local function setup_diagnostics()
-- https://github.com/neovim/nvim-lspconfig/wiki/UI-Customization#customizing-how-diagnostics-are-displayed -- https://github.com/neovim/nvim-lspconfig/wiki/UI-Customization#customizing-how-diagnostics-are-displayed
vim.diagnostic.config({ vim.diagnostic.config({
underline = true, underline = true,
@@ -73,7 +73,7 @@ function P._setup_diagnostics()
end end
end end
function P.on_attach(client, bufnr) local function on_attach(client, bufnr)
-- Mappings. -- Mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions -- See `:help vim.lsp.*` for documentation on any of the below functions
local opts = { buffer = bufnr, } local opts = { buffer = bufnr, }
@@ -93,27 +93,7 @@ function P.on_attach(client, bufnr)
vim.keymap.set( vim.keymap.set(
{ "n", "x", }, { "n", "x", },
"<leader>lf", "<leader>lf",
function () vim.lsp.buf.format,
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 opts
) )
@@ -146,20 +126,20 @@ function P.on_attach(client, bufnr)
require("lsp-inlayhints").on_attach(client, bufnr, false) require("lsp-inlayhints").on_attach(client, bufnr, false)
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with( vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(
vim.lsp.handlers.hover, { vim.lsp.handlers.hover, {
border = "single" border = "single",
} }
) )
vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with( vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(
vim.lsp.handlers.signature_help, { vim.lsp.handlers.signature_help, {
border = "single" border = "single",
} }
) )
end end
function P.reload_server_buf(name) local function reload_server_buf(name)
local server = P.config[name] local server = config[name]
local ft_map = {} local ft_map = {}
for _, ft in ipairs(server.lspconfig.filetypes) do for _, ft in ipairs(server.lspconfig.filetypes) do
ft_map[ft] = true ft_map[ft] = true
@@ -180,101 +160,15 @@ function P.reload_server_buf(name)
end end
end end
function P.filetypes()
if not P._filetypes then
P._filetypes = {}
local unique = {}
for _, server in pairs(P.config) do
for _, ft in ipairs(server.lspconfig.filetypes) do
if not unique[ft] then
table.insert(P._filetypes, ft)
unique[ft] = true
end
end
end
end
return P._filetypes local function configure_server(name, server)
end local ok, ret = pcall(require, "lspconfig")
function P.language_servers()
if not P._language_servers then
P._language_servers = {}
for server, opts in pairs(P.config) do
if opts.enabled ~= true then
goto next_server
end
if opts.dependencies ~= nil then
local not_installed = {}
for _, dep in ipairs(opts.dependencies) do
if not utils.is_installed(dep) then
table.insert(not_installed, dep)
end
end
if #not_installed > 0 then
utils.warn(
("Disabling %s "
.. "because the following required package(s) "
.. "are not installed: %s")
:format(
server,
table.concat(not_installed, ", ")
),
package_name
)
opts.enabled = false
goto next_server
end
end
if opts.py_module_deps ~= nil then
local not_installed = {}
for _, mod in ipairs(opts.py_module_deps) do
if not utils.python3_module_is_installed(mod) then
table.insert(not_installed, mod)
end
end
if #not_installed > 0 then
utils.warn(
("Disabling %s "
+ "because the following required python3 "
+ "module(s) are not installed: %s")
:format(
server,
table.concat(not_installed, ", ")
),
package_name
)
opts.enabled = false
goto next_server
end
end
table.insert(P._language_servers, server)
::next_server::
end
end
return P._language_servers
end
function P.setup_server(name)
local server = P.config[name]
if not server or server.enabled ~= true then
return
end
local ok, lspconfig = pcall(require, "lspconfig")
if not ok then if not ok then
utils.err("Missing required plugin lspconfig", package_name) utils.err("Missing required plugin lspconfig", module_name)
return return
end end
local lspconfig = ret
-- server.lspconfig.root_dir = function () return vim.fn.getcwd() end
if server.root_pattern then if server.root_pattern then
server.lspconfig.root_dir = lspconfig.util.root_pattern( server.lspconfig.root_dir = lspconfig.util.root_pattern(
unpack(server.root_pattern) unpack(server.root_pattern)
@@ -282,39 +176,113 @@ function P.setup_server(name)
else else
server.lspconfig.root_dir = lspconfig.util.find_git_ancestor server.lspconfig.root_dir = lspconfig.util.find_git_ancestor
end end
server.lspconfig.capabilities = P.capabilities server.lspconfig.capabilities = capabilities
server.lspconfig.on_attach = function (...) server.lspconfig.on_attach = function (...)
local resp ok, ret = pcall(on_attach, ...)
ok, resp = pcall(P.on_attach, ...)
if not ok then if not ok then
utils.err( utils.err(
("Failed to load on_attach for %s:\n%s"):format(name, resp) ("Failed to load on_attach for %s:\n%s"):format(name, ret),
module_name
) )
end end
end end
if not pcall(lspconfig[name].setup, server.lspconfig) then ok, ret = pcall(lspconfig[name].setup, server.lspconfig)
utils.err("Unknown LSP server for lspconfig: " .. name, package_name) if not ok then
utils.err(
("Failed to setup LSP server %s with lspconfig: %s"):format(
name,
ret
),
module_name
)
return return
end end
P.reload_server_buf(name) reload_server_buf(name)
end end
function P.setup() local function setup_server(name, server)
P._setup_diagnostics() local registry = require("mason-registry")
local pkg_name
utils.try_require("cmp_nvim_lsp", package_name, function (mod) if server.mason then
P.capabilities = mod.default_capabilities() pkg_name = server.mason.name
end) end
utils.try_require("mason-lspconfig", package_name, function (mod) if (pkg_name and not registry.is_installed(pkg_name)) then
mod.setup_handlers({ local pkg = registry.get_package(pkg_name)
function (name) local handle = pkg:install({ version = server.mason.version, })
P.setup_server(name) utils.info("Installing " .. pkg_name)
end, local err
handle:on("stderr", vim.schedule_wrap(function (msg)
err = (err or "") .. msg
end))
handle:once("closed", vim.schedule_wrap(function ()
if err then
utils.err(err, module_name)
end
if pkg:is_installed() then
utils.info("Installation finished for " .. pkg_name)
configure_server(name, server)
else
utils.err("Installation failed for " .. pkg_name)
server.enable = false
end
end))
else
if vim.fn.executable(server.lspconfig.cmd[1]) == 1 then
configure_server(name, server)
else
utils.info(name .. " not installed, disabling", module_name)
server.enable = false
end
end
end
local function register_server(name, server)
local augroup = vim.api.nvim_create_augroup("LSP-" .. name, {})
vim.api.nvim_create_autocmd("FileType", {
once = true,
pattern = table.concat(server.lspconfig.filetypes, ","),
callback = vim.schedule_wrap(function ()
setup_server(name, server)
vim.api.nvim_del_augroup_by_id(augroup)
end),
group = augroup,
}) })
end)
end end
return P function M.filetypes()
if not _filetypes then
_filetypes = {}
local unique = {}
for _, server in pairs(config) do
for _, ft in ipairs(server.lspconfig.filetypes) do
if not unique[ft] then
table.insert(_filetypes, ft)
unique[ft] = true
end
end
end
end
return _filetypes
end
function M.setup()
setup_diagnostics()
utils.try_require("cmp_nvim_lsp", module_name, function (mod)
capabilities = mod.default_capabilities()
end)
for name, server in pairs(config) do
if server.enable then
register_server(name, server)
end
end
end
return M
+5 -1
View File
@@ -1,8 +1,12 @@
return { return {
enabled = true, enable = true,
dependencies = { dependencies = {
"npm", "npm",
}, },
mason = {
name = "bash-language-server",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"sh", "sh",
+5 -1
View File
@@ -1,5 +1,9 @@
return { return {
enabled = true, enable = true,
mason = {
name = "clangd",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"c", "c",
+5 -1
View File
@@ -1,11 +1,15 @@
return { return {
enabled = true, enable = true,
dependencies = { dependencies = {
"python3", "python3",
}, },
py_module_deps = { py_module_deps = {
"venv", "venv",
}, },
mason = {
name = "cmake-language-server",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"cmake", "cmake",
+8 -4
View File
@@ -5,10 +5,14 @@
-- https://github.com/iamcco/coc-diagnostic/blob/master/src/config.ts -- https://github.com/iamcco/coc-diagnostic/blob/master/src/config.ts
return { return {
enabled = true, enable = true,
dependencies = { dependencies = {
"npm", "npm",
}, },
mason = {
name = "diagnostic-languageserver",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"python", "python",
@@ -110,16 +114,16 @@ return {
sh = { "shfmt", }, sh = { "shfmt", },
bash = { "shfmt", }, bash = { "shfmt", },
zsh = { "shfmt", }, zsh = { "shfmt", },
php = { "php_cs_fixer", }, -- php = { "php_cs_fixer", },
}, },
formatters = { formatters = {
autopep8 = { autopep8 = {
command = "autopep8", command = "autopep8",
args = { args = {
"--aggressive", "--aggressive",
"-" "-",
}, },
rootPatterns = {"Pipfile", "tox.ini", ".git"}, rootPatterns = { "Pipfile", "tox.ini", ".git", },
isStdout = true, isStdout = true,
isStderr = false, isStderr = false,
ignoreExitCode = false, ignoreExitCode = false,
+5 -1
View File
@@ -1,7 +1,11 @@
-- spec: https://rust-analyzer.github.io/manual.html#configuration -- spec: https://rust-analyzer.github.io/manual.html#configuration
return { return {
enabled = true, enable = true,
mason = {
name = "gopls",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"go", "go",
+5 -1
View File
@@ -1,7 +1,7 @@
-- spec: https://github.com/bmewburn/intelephense-docs/blob/master/installation.md -- spec: https://github.com/bmewburn/intelephense-docs/blob/master/installation.md
return { return {
enabled = true, enable = true,
dependencies = { dependencies = {
"npm", "npm",
}, },
@@ -10,6 +10,10 @@ return {
"composer.lock", "composer.lock",
"vendor", "vendor",
}, },
mason = {
name = "intelephense",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"php", "php",
+5 -1
View File
@@ -1,11 +1,15 @@
return { return {
enabled = true, enable = true,
dependencies = { dependencies = {
"python3", "python3",
}, },
py_module_deps = { py_module_deps = {
"venv", "venv",
}, },
mason = {
name = "jedi-language-server",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"python", "python",
+4
View File
@@ -2,6 +2,10 @@
return { return {
enabled = true, enabled = true,
mason = {
name = "lemminx",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"xml", "xml",
+5 -1
View File
@@ -1,7 +1,11 @@
-- spec: https://luals.github.io/wiki/settings/ -- spec: https://luals.github.io/wiki/settings/
return { return {
enabled = true, enable = true,
mason = {
name = "lua-language-server",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"lua", "lua",
+5 -1
View File
@@ -1,7 +1,11 @@
-- spec: https://rust-analyzer.github.io/manual.html#configuration -- spec: https://rust-analyzer.github.io/manual.html#configuration
return { return {
enabled = true, enable = true,
mason = {
name = "rust-analyzer",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"rust", "rust",
+7 -1
View File
@@ -1,8 +1,14 @@
-- spec: https://github.com/zigtools/zls#configuration-options
return { return {
enabled = true, enable = true,
dependencies = { dependencies = {
"zig", "zig",
}, },
-- mason = {
-- name = "zls",
-- -- version = "",
-- },
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"zig", "zig",
+14 -10
View File
@@ -8,6 +8,20 @@ local plugins = {
name = "moonfly", name = "moonfly",
config = require("plugins.moonfly"), config = require("plugins.moonfly"),
}, },
--[[ {
"catppuccin/nvim",
name = "catppuccin",
priority = 100,
lazy = false,
config = require("plugins.catppuccin"),
}, ]]
--[[ {
"navarasu/onedark.nvim",
priority = 1000,
lazy = false,
-- name = "moonfly",
config = require("plugins.onedark"),
}, ]]
{ {
"rcarriga/nvim-notify", "rcarriga/nvim-notify",
priority = 900, priority = 900,
@@ -60,12 +74,6 @@ local plugins = {
lazy = true, lazy = true,
event = "VimEnter", event = "VimEnter",
}, },
{
"williamboman/mason-lspconfig.nvim",
config = require("plugins.mason_lspconfig"),
lazy = true,
event = "VimEnter",
},
{ {
"neovim/nvim-lspconfig", "neovim/nvim-lspconfig",
config = require("lsp").setup, config = require("lsp").setup,
@@ -156,10 +164,6 @@ local plugins = {
lazy = true, lazy = true,
event = "VimEnter", event = "VimEnter",
}, },
{
"RubixDev/mason-update-all",
config = require("plugins.mason_update_all"),
},
{ {
"famiu/bufdelete.nvim", "famiu/bufdelete.nvim",
config = require("plugins.bufdelete"), config = require("plugins.bufdelete"),
-12
View File
@@ -1,12 +0,0 @@
-- https://github.com/williamboman/mason-lspconfig.nvim
local function setup()
require("mason-lspconfig").setup({
-- A list of servers to automatically install if they're not already installed. Example: { "rust_analyzer@nightly", "lua_ls" }
-- This setting has no relation with the `automatic_installation` setting.
---@type string[]
ensure_installed = require("lsp").language_servers(),
})
end
return setup
-7
View File
@@ -1,7 +0,0 @@
-- https://github.com/RubixDev/mason-update-all
local function setup()
require("mason-update-all").setup()
end
return setup