add: custom mason ensure installed

This commit is contained in:
asep.komarudin 2024-06-28 12:01:06 +07:00
parent fc32c1b69c
commit 6da9ec51d8
9 changed files with 100 additions and 17 deletions

View file

@ -45,7 +45,7 @@ pcode.format_timeout_ms = 5000
-- https://github.com/mfussenegger/nvim-lint
-- https://github.com/stevearc/conform.nvim
-- use conform and nvim-lint if set true
pcode.disable_null_ls = false
pcode.disable_null_ls = true
pcode.treesitter_ensure_installed = {}
pcode.tscontext = false
@ -70,7 +70,7 @@ pcode.mason_ensure_installed = { -- sebelumnya register_lsp
-- tambahkan di bawah sini setelah melakukan :masoninstall
}
pcode.unregister_lsp = {
-- "jdtls", -- tambahkan di bawah ini
"jdtls", -- tambahkan di bawah ini
}
-- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/doc/BUILTINS.md
@ -172,6 +172,6 @@ pcode.active_golang_config = false
pcode.active_python_config = false
pcode.active_cpp_config = false
pcode.active_java_config = {
active = false,
active = true,
project = "gradle", -- gradle or maven
}

View file

@ -1,11 +1,11 @@
return {
{
"williamboman/mason-lspconfig.nvim",
event = "VeryLazy",
dependencies = {
{
"neovim/nvim-lspconfig",
lazy = true,
event = "BufRead",
cmd = {
"LspInfo",
"LspInstall",
@ -62,7 +62,6 @@ return {
end,
},
},
event = "BufReadPre",
opts = function()
local servers = { "lua_ls" }
local mason_install = pcode.mason_ensure_installed or {}

View file

@ -9,9 +9,10 @@ end
return {
{
"nvim-treesitter/nvim-treesitter",
opts = {
ensure_installed = ts_list,
},
opts = function(_, opts)
opts.ensure_installed = opts.ensure_installed or {}
vim.list_extend(opts.ensure_installed, ts_list)
end,
config = function(_, opts)
local parser_config = require("nvim-treesitter.parsers").get_parser_configs()

View file

@ -4,6 +4,29 @@ if pcode.active_java_config.active then
-- {
-- "mfussenegger/nvim-jdtls",
-- },
{
"nvim-treesitter/nvim-treesitter",
opts = function(_, opts)
opts.ensure_installed = opts.ensure_installed or {}
vim.list_extend(opts.ensure_installed, { "java" })
end,
},
{
"williamboman/mason-lspconfig.nvim",
opts = function(_, opts)
opts.ensure_installed = opts.ensure_installed or {}
vim.list_extend(opts.ensure_installed, { "jdtls" })
end,
},
{
"stevearc/conform.nvim",
event = "VeryLazy",
opts = function(_, opts)
local package="google-java-format"
require("user.utils.mason").try_install(package)
opts.formatters_by_ft.java = { package }
end,
},
{
"nvim-neotest/neotest",
dependencies = {

View file

@ -6,6 +6,7 @@ return {
dependencies = {
{
"jayp0521/mason-null-ls.nvim",
event = "VeryLazy",
opts = {
ensure_installed = pcode.null_ls_ensure_installed or {},
automatic_setup = true,

View file

@ -110,11 +110,11 @@ if pcode.active_cpp_config then
pcode.nvim_dap = true
end
-- run if java config true
if pcode.active_java_config.active then
table.insert(pcode.treesitter_ensure_installed, "java")
table.insert(pcode.mason_ensure_installed, "jdtls")
table.insert(pcode.null_ls_ensure_installed, "google_java_format")
-- table.insert(pcode.dap_ensure_installed, "javadbg")
-- table.insert(pcode.unregister_lsp, "jdtls")
end
-- if pcode.active_java_config.active then
-- table.insert(pcode.treesitter_ensure_installed, "java")
-- table.insert(pcode.mason_ensure_installed, "jdtls")
-- table.insert(pcode.null_ls_ensure_installed, "google_java_format")
-- table.insert(pcode.dap_ensure_installed, "javadbg")
-- table.insert(pcode.unregister_lsp, "jdtls")
-- end
return {}

56
lua/user/utils/mason.lua Normal file
View file

@ -0,0 +1,56 @@
local registry = require("mason-registry")
local M = {}
local function resolve_package(mason_package_name)
local Optional = require("mason-core.optional")
local ok, pkg = pcall(registry.get_package, mason_package_name)
if ok then
return Optional.of_nilable(pkg)
end
end
local function install_package(pkg, version)
local linter_name = pkg.name
vim.notify(("[mason-conform] installing %s"):format(linter_name))
return pkg:install({ version = version }):once(
"closed",
vim.schedule_wrap(function()
if pkg:is_installed() then
vim.notify(("[mason-conform] %s was successfully installed"):format(linter_name))
else
vim.notify(
("[mason-conform] failed to install %s. Installation logs are available in :Mason and :MasonLog"):format(
linter_name
),
vim.log.levels.ERROR
)
end
end)
)
end
function M.try_install(mason_package_name)
local Package = require("mason-core.package")
local package_name, version = Package.Parse(mason_package_name)
resolve_package(package_name)
:if_present(function(pkg)
if not pkg:is_installed() then
install_package(pkg, version)
end
end)
:if_not_present(function()
vim.notify(
("[mason-conform] Formatter %q is not a valid entry in ensure_installed. Make sure to only provide valid formatter names."):format(
package_name
),
vim.log.levels.WARN
)
end)
end
return M