Revert "fix(lsp): disable php formatting workaround"

This reverts commit 7ce88bb7c1d904a628634c0d96ef05100866f50e.
This commit is contained in:
2024-02-27 16:49:03 +01:00
parent 35e77a950b
commit 68bf1acad0
15 changed files with 194 additions and 195 deletions
+4 -10
View File
@@ -63,11 +63,7 @@ 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'`) upon entering a buffer of related (`:echo stdpath('data') .. '/mason'`). The following are some noted requirements
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
@@ -81,11 +77,9 @@ 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, you may either remove them from If you don't need some specific language server, and want to get rid of any
the top of `lua/lsp.lua` or disable them in `lua/lsp/<server>.lua`. warning messages, you may either remove them from the top of `lua/lsp.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/),
+148 -116
View File
@@ -1,14 +1,14 @@
local module_name = "lsp" local package_name = "lsp"
local utils = require("utils") local utils = require("utils")
local M = {} local P = {}
local _filetypes = nil P._filetypes = nil
-- local auto_installed_servers = nil P._language_servers = nil
local capabilities = {} P.capabilities = {}
local config = { P.config = {
bashls = {}, bashls = {},
clangd = {}, clangd = {},
cmake = {}, cmake = {},
@@ -23,9 +23,9 @@ local config = {
zls = {}, zls = {},
} }
for server, _ in pairs(config) do for server, _ in pairs(P.config) do
utils.try_require("lsp." .. server, module_name, function (mod) utils.try_require("lsp." .. server, package_name, function (mod)
config[server] = mod P.config[server] = mod
end) end)
end end
@@ -43,7 +43,7 @@ local function ca_rename()
end end
end end
local function setup_diagnostics() function P._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 @@ local function setup_diagnostics()
end end
end end
local function on_attach(client, bufnr) function P.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,7 +93,27 @@ local function on_attach(client, bufnr)
vim.keymap.set( vim.keymap.set(
{ "n", "x", }, { "n", "x", },
"<leader>lf", "<leader>lf",
vim.lsp.buf.format, function ()
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
) )
@@ -126,20 +146,20 @@ local function 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
local function reload_server_buf(name) function P.reload_server_buf(name)
local server = config[name] local server = P.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
@@ -160,15 +180,101 @@ local function 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
local function configure_server(name, server) return P._filetypes
local ok, ret = pcall(require, "lspconfig") end
if not ok then
utils.err("Missing required plugin lspconfig", module_name) 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 return
end end
local lspconfig = ret
local ok, lspconfig = pcall(require, "lspconfig")
if not ok then
utils.err("Missing required plugin lspconfig", package_name)
return
end
-- 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)
@@ -176,113 +282,39 @@ local function configure_server(name, server)
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 = capabilities server.lspconfig.capabilities = P.capabilities
server.lspconfig.on_attach = function (...) server.lspconfig.on_attach = function (...)
ok, ret = pcall(on_attach, ...) local resp
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, ret), ("Failed to load on_attach for %s:\n%s"):format(name, resp)
module_name
) )
end end
end end
ok, ret = pcall(lspconfig[name].setup, server.lspconfig) if not pcall(lspconfig[name].setup, server.lspconfig) then
if not ok then utils.err("Unknown LSP server for lspconfig: " .. name, package_name)
utils.err(
("Failed to setup LSP server %s with lspconfig: %s"):format(
name,
ret
),
module_name
)
return return
end end
reload_server_buf(name) P.reload_server_buf(name)
end end
local function setup_server(name, server) function P.setup()
local registry = require("mason-registry") P._setup_diagnostics()
local pkg_name
if server.mason then utils.try_require("cmp_nvim_lsp", package_name, function (mod)
pkg_name = server.mason.name P.capabilities = mod.default_capabilities()
end
if (pkg_name and not registry.is_installed(pkg_name)) then
local pkg = registry.get_package(pkg_name)
local handle = pkg:install({ version = server.mason.version, })
utils.info("Installing " .. pkg_name)
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
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) end)
for name, server in pairs(config) do utils.try_require("mason-lspconfig", package_name, function (mod)
if server.enable then mod.setup_handlers({
register_server(name, server) function (name)
end P.setup_server(name)
end end,
})
end)
end end
return M return P
+1 -5
View File
@@ -1,12 +1,8 @@
return { return {
enable = true, enabled = true,
dependencies = { dependencies = {
"npm", "npm",
}, },
mason = {
name = "bash-language-server",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"sh", "sh",
+1 -5
View File
@@ -1,9 +1,5 @@
return { return {
enable = true, enabled = true,
mason = {
name = "clangd",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"c", "c",
+1 -5
View File
@@ -1,15 +1,11 @@
return { return {
enable = true, enabled = 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",
+4 -8
View File
@@ -5,14 +5,10 @@
-- https://github.com/iamcco/coc-diagnostic/blob/master/src/config.ts -- https://github.com/iamcco/coc-diagnostic/blob/master/src/config.ts
return { return {
enable = true, enabled = true,
dependencies = { dependencies = {
"npm", "npm",
}, },
mason = {
name = "diagnostic-languageserver",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"python", "python",
@@ -114,16 +110,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,
+1 -5
View File
@@ -1,11 +1,7 @@
-- spec: https://rust-analyzer.github.io/manual.html#configuration -- spec: https://rust-analyzer.github.io/manual.html#configuration
return { return {
enable = true, enabled = true,
mason = {
name = "gopls",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"go", "go",
+1 -5
View File
@@ -3,7 +3,7 @@
-- https://github.com/bmewburn/vscode-intelephense/blob/master/package.json -- https://github.com/bmewburn/vscode-intelephense/blob/master/package.json
return { return {
enable = true, enabled = true,
dependencies = { dependencies = {
"npm", "npm",
}, },
@@ -12,10 +12,6 @@ return {
"composer.lock", "composer.lock",
"vendor", "vendor",
}, },
mason = {
name = "intelephense",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"php", "php",
+1 -5
View File
@@ -1,15 +1,11 @@
return { return {
enable = true, enabled = 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",
+1 -5
View File
@@ -1,11 +1,7 @@
-- spec: https://luals.github.io/wiki/settings/ -- spec: https://luals.github.io/wiki/settings/
return { return {
enable = true, enabled = true,
mason = {
name = "lua-language-server",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"lua", "lua",
+1 -5
View File
@@ -1,11 +1,7 @@
-- spec: https://rust-analyzer.github.io/manual.html#configuration -- spec: https://rust-analyzer.github.io/manual.html#configuration
return { return {
enable = true, enabled = true,
mason = {
name = "rust-analyzer",
-- version = "",
},
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"rust", "rust",
+1 -7
View File
@@ -1,14 +1,8 @@
-- spec: https://github.com/zigtools/zls#configuration-options
return { return {
enable = true, enabled = true,
dependencies = { dependencies = {
"zig", "zig",
}, },
-- mason = {
-- name = "zls",
-- -- version = "",
-- },
lspconfig = { lspconfig = {
filetypes = { filetypes = {
"zig", "zig",
+10 -14
View File
@@ -8,20 +8,6 @@ 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,
@@ -74,6 +60,12 @@ 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,
@@ -164,6 +156,10 @@ 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
@@ -0,0 +1,12 @@
-- 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
@@ -0,0 +1,7 @@
-- https://github.com/RubixDev/mason-update-all
local function setup()
require("mason-update-all").setup()
end
return setup