How would I be able to check the category of a package ? #93
-
local M = {}
local vim = vim
local lspconfig = require("lspconfig")
require("mason").setup()
require("mason-lspconfig").setup({})
require("mason-tool-installer").setup({
ensure_installed = {'bash-language-server', 'shfmt', 'lua-language-server', 'stylua'},
auto_update = true
})
local Pkg = require("mason-core.package")
local mason_registry = require("mason-registry")
local null_ls = require("null-ls")
local default_lsp_on_attach = function(client)
require("mappings").keys_lsp()
client.resolved_capabilities.document_formatting = false
client.resolved_capabilities.document_range_formatting = false
end
local default_lsp_capabilities = require("cmp_nvim_lsp").update_capabilities(vim.lsp.protocol.make_client_capabilities
lspconfig.util.default_config = vim.tbl_extend("force", lspconfig.util.default_config, {
on_attach = default_lsp_on_attach,
capabilities = default_lsp_capabilities,
})
function M.setup_default()
require("mason-lspconfig").setup_handlers({
function(server_name)
lspconfig[server_name].setup({
})
end,
["sumneko_lua"] = function()
lspconfig.sumneko_lua.setup({
settings = {
Lua = {
runtime = {
version = "LuaJIT",
},
diagnostics = {
globals = { "vim", "use", "require" },
},
workspace = {
library = vim.api.nvim_get_runtime_file("", true),
},
telemetry = {
enable = false,
},
},
},
})
end,
})
local null_sources = {}
for _, package in ipairs(mason_registry.get_installed_packages()) do
local config = { command = package:get_install_path() }
if package.categories == { Pkg.Cat.Formatter} then
print(package.name)
print(package:get_install_path())
table.insert(null_sources, null_ls.builtins.formatting[package.name].with(config))
end
end
null_ls.setup({
sources = null_sources,
on_attach = function(client)
if client.resolved_capabilities.document_formatting then
vim.cmd([[
augroup LspFormatting
autocmd! * <buffer>
autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()
augroup END
]])
end
end,
})
end
return M I'm posting the whole file for context but what I'm really interested in is this piece. local null_sources = {}
for _, package in ipairs(mason_registry.get_installed_packages()) do
local config = { command = package:get_install_path() }
if package.categories == { Pkg.Cat.Formatter} then
print(package.name)
print(package:get_install_path())
table.insert(null_sources, null_ls.builtins.formatting[package.name].with(config))
end
end I choose not to go with the What I'm trying to do is loop over all installed packages and for certain categories insert them into |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hello! You can access the categories of a package via its |
Beta Was this translation helpful? Give feedback.
-
local M = {}
-- selene: allow(undefined_variable)
---@diagnostic disable-next-line
local vim = vim
local util = require("util")
local lspconfig = require("lspconfig")
local mason_package = require("mason-core.package")
local mason_registry = require("mason-registry")
local null_ls = require("null-ls")
require("mason").setup()
require("mason-lspconfig").setup({})
require("mason-tool-installer").setup({
ensure_installed = { "bash-language-server", "shellcheck", "shfmt", "lua-language-server", "stylua" },
auto_update = true,
})
local default_lsp_on_attach = function(client)
require("mappings").keys_lsp()
client.resolved_capabilities.document_formatting = false
client.resolved_capabilities.document_range_formatting = false
end
local default_lsp_capabilities =
require("cmp_nvim_lsp").update_capabilities(vim.lsp.protocol.make_client_capabilities())
lspconfig.util.default_config = vim.tbl_extend("force", lspconfig.util.default_config, {
on_attach = default_lsp_on_attach,
capabilities = default_lsp_capabilities,
})
function M.setup_default()
require("mason-lspconfig").setup_handlers({
function(server_name)
lspconfig[server_name].setup({})
end,
["sumneko_lua"] = function()
lspconfig.sumneko_lua.setup({
Lua = {
runtime = {
version = "LuaJIT",
},
diagnostics = {
globals = { "vim" },
},
workspace = {
library = vim.api.nvim_get_runtime_file("", true),
},
telemetry = {
enable = false,
},
},
})
end,
})
local null_sources = {}
table.insert(null_sources, null_ls.builtins.diagnostics.selene) -- lua
for _, package in ipairs(mason_registry.get_installed_packages()) do
if util.set_contains_val(package.spec.categories, mason_package.Cat.Formatter) then
table.insert(null_sources, null_ls.builtins.formatting[package.name])
end
if util.set_contains_val(package.spec.categories, mason_package.Cat.Linter) then
table.insert(null_sources, null_ls.builtins.diagnostics[package.name])
end
end
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
null_ls.setup({
sources = null_sources,
on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
callback = function()
vim.lsp.buf.formatting_sync()
end,
})
end
end,
})
end
return M Posting the end result. The following configuration might result in error in case null-ls doesn't have a BUILTIN for that package. There is currently a bug that happens when you use this config where try {
exit = yield proc;
} catch (e) {
if (e.code === 'ENOENT') {
// shellcheck path wasn't found, don't try to lint any more:
console.error(`shellcheck not available at path '${this.executablePath}'`);
this._canLint = false;
return {
comments: []
};
}
throw new Error(`shellcheck failed with code ${exit}: ${e}\nout:\n${out}\nerr:\n${err}`);
} Found it. I'm on a M1.
There don't seem to be any publicly available builds outside homebrew... |
Beta Was this translation helpful? Give feedback.
Hello! You can access the categories of a package via its
spec
field, which is of typePackageSpec
.