From 58c25cd45d867fc512af48a457c71bb26d9d778d Mon Sep 17 00:00:00 2001 From: logrusx <45463882+logrusx@users.noreply.github.com> Date: Wed, 6 Aug 2025 09:38:48 +0300 Subject: [PATCH 1/3] feat: adds Mason 2.0 support (#402) * feat: Mason 2.0 migration * feat: Mason 2.0 migration Pt. 2: backward compatibility with Mason 1.x * feat: Mason 2.0 migration Pt. 3: fixed formatting and typos, added missing diagnostic disable's * feat: Mason 2.0 migration Pt. 4: fixed formatting * chore: release 3.0.0 Release-As: 3.0.0 --------- Co-authored-by: s1n7ax --- lazy.lua | 2 +- lua/java/startup/mason-dep.lua | 22 ++++++- lua/java/startup/mason-registry-check.lua | 50 ++++++++++----- lua/java/utils/mason.lua | 76 ++++++++++++++++------- tests/prepare-config.lua | 2 +- 5 files changed, 106 insertions(+), 46 deletions(-) diff --git a/lazy.lua b/lazy.lua index a352b8d..03ef45a 100644 --- a/lazy.lua +++ b/lazy.lua @@ -14,7 +14,7 @@ return { commit = '218c0c26c14d99feca778e4d13f5ec3e8b1b60f0', }, { - 'williamboman/mason.nvim', + 'mason-org/mason.nvim', -- opts = { -- registries = { -- 'github:nvim-java/mason-registry', diff --git a/lua/java/startup/mason-dep.lua b/lua/java/startup/mason-dep.lua index c424b04..129c777 100644 --- a/lua/java/startup/mason-dep.lua +++ b/lua/java/startup/mason-dep.lua @@ -6,14 +6,15 @@ local notify = require('java-core.utils.notify') local async = require('java-core.utils.async') local lazy = require('java.ui.lazy') local sync = async.sync +local mason_v2 = require('mason.version').MAJOR_VERSION == 2 local List = require('java-core.utils.list') local M = {} ----Add custom registries to mason +---Add custom registries to Mason 1.x ---@param registries java.Config -function M.add_custom_registries(registries) +local function add_custom_registries_v1(registries) local mason_default_config = require('mason.settings').current local new_registries = @@ -24,6 +25,21 @@ function M.add_custom_registries(registries) }) end +---Add custom registries to Mason 2.x +---@param registries java.Config +local function add_custom_registries_v2(registries) + for _, reg in ipairs(registries) do + ---@diagnostic disable-next-line: undefined-field + require('mason-registry').sources:prepend(reg) + end +end + +if mason_v2 then + M.add_custom_registries = add_custom_registries_v2 +else + M.add_custom_registries = add_custom_registries_v1 +end + ---Install mason package dependencies for nvim-java ---@param config java.Config function M.install(config) @@ -51,7 +67,7 @@ function M.refresh_and_install(packages) lazy.close_lazy_if_opened() mason_ui.open() - notify.warn('Please close and re-open after dependecies are installed') + notify.warn('Please close and re-open after dependencies are installed') end) mason_util.refresh_registry() diff --git a/lua/java/startup/mason-registry-check.lua b/lua/java/startup/mason-registry-check.lua index 76e3e8d..ce8db28 100644 --- a/lua/java/startup/mason-registry-check.lua +++ b/lua/java/startup/mason-registry-check.lua @@ -1,26 +1,42 @@ -local mason_source = require('mason-registry.sources') +local mason_v2 = require('mason.version').MAJOR_VERSION == 2 -local M = { - JAVA_REG_ID = 'github:nvim-java/mason-registry', -} +local mason_sources + +if mason_v2 then + -- compiler will complain when Mason 1.x is used + ---@diagnostic disable-next-line: undefined-field + mason_sources = require('mason-registry').sources +else + mason_sources = require('mason-registry.sources') +end + +local M = {} +if mason_v2 then + M.JAVA_REG_ID = 'nvim-java/mason-registry' +else + M.JAVA_REG_ID = 'github:nvim-java/mason-registry' +end function M.is_valid() - local has_reg = false + local iterator - for reg in mason_source.iter() do - if reg.id == M.JAVA_REG_ID then - has_reg = true - goto continue - end + if mason_v2 then + -- the compiler will complain when Mason 1.x is in use + ---@diagnostic disable-next-line: undefined-field + iterator = mason_sources.iterate + else + -- the compiler will complain when Mason 2.x is in use + ---@diagnostic disable-next-line: undefined-field + iterator = mason_sources.iter end - ::continue:: - - if has_reg then - return { - success = true, - continue = true, - } + for reg in iterator(mason_sources) do + if reg.id == M.JAVA_REG_ID then + return { + success = true, + continue = true, + } + end end return { diff --git a/lua/java/utils/mason.lua b/lua/java/utils/mason.lua index 6e239a2..824d425 100644 --- a/lua/java/utils/mason.lua +++ b/lua/java/utils/mason.lua @@ -2,44 +2,60 @@ local log = require('java.utils.log') local mason_reg = require('mason-registry') local async = require('java-core.utils.async') local await = async.wait_handle_ok +local mason_v2 = require('mason.version').MAJOR_VERSION == 2 local M = {} function M.is_available(package_name, package_version) - local has_pkg = mason_reg.has_package(package_name) + -- get_package errors if the package is not available in Mason 2.x + -- it works fine in Mason 1.x this way too. + local has_pkg, pkg = pcall(mason_reg.get_package, package_name) if not has_pkg then return false end - local has_version = false - - local pkg = mason_reg.get_package(package_name) - pkg:get_installed_version(function(success, version) - if success and version == package_version then - has_version = true - end - end) + local installed_version + if mason_v2 then + -- the compiler will complain when Mason 1.x is in use + ---@diagnostic disable-next-line: missing-parameter + installed_version = pkg:get_installed_version() + else + -- the compiler will complain when mason 2.x is in use + ---@diagnostic disable-next-line: param-type-mismatch + pkg:get_installed_version(function(success, version) + if success then + installed_version = version + end + end) + end - return has_version + return installed_version == package_version end function M.is_installed(package_name, package_version) - local pkg = mason_reg.get_package(package_name) - local is_installed = pkg:is_installed() + -- get_package errors if the package is not available in Mason 2.x + -- it works fine in Mason 1.x this way too. + local found, pkg = pcall(mason_reg.get_package, package_name) - if not is_installed then + if not found or not pkg:is_installed() then return false end local installed_version - pkg:get_installed_version(function(ok, version) - if not ok then - return - end - - installed_version = version - end) + if mason_v2 then + -- the compiler will complain when Mason 1.x is in use + ---@diagnostic disable-next-line: missing-parameter + installed_version = pkg:get_installed_version() + else + -- the compiler will complain when Mason 2.x is in use + ---@diagnostic disable-next-line: param-type-mismatch + pkg:get_installed_version(function(success, version) + if success then + installed_version = version + end + end) + end return installed_version == package_version end @@ -69,10 +85,22 @@ function M.install_pkgs(packages) if not M.is_installed(dep.name, dep.version) then local pkg = mason_reg.get_package(dep.name) - pkg:install({ - version = dep.version, - force = true, - }) + -- install errors if installation is already running in Mason 2.x + local guard + if mason_v2 then + -- guard if the package is already installing in Mason 2.x + -- the compiler will complain about the following line with Mason 1.x + ---@diagnostic disable-next-line: undefined-field + guard = pkg:is_installing() + else + guard = false + end + if not guard then + pkg:install({ + version = dep.version, + force = true, + }) + end end end end diff --git a/tests/prepare-config.lua b/tests/prepare-config.lua index f2dafac..cf2d89e 100644 --- a/tests/prepare-config.lua +++ b/tests/prepare-config.lua @@ -50,7 +50,7 @@ require('lazy').setup({ lazy = false, }, { - 'williamboman/mason.nvim', + 'mason-org/mason.nvim', lazy = false, }, { From 3cecf7362e5f83e4a34e84e4f14c65bad81968f1 Mon Sep 17 00:00:00 2001 From: s1n7ax Date: Wed, 6 Aug 2025 12:12:31 +0530 Subject: [PATCH 2/3] chore: release 3.0.0 Release-As: 3.0.0 From 3d56b7461facb42f135e25b2636bf220a7f0ed42 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 6 Aug 2025 12:31:31 +0530 Subject: [PATCH 3/3] chore(main): release 3.0.0 (#408) * chore(main): release 3.0.0 * Update CHANGELOG.md --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Srinesh Nisala --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cda89f..7b11fa0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## [3.0.0](https://github.com/nvim-java/nvim-java/compare/v2.1.2...v3.0.0) (2025-08-06) + + +### Features + +* [@logrusx](https://github.com/logrusx) adds Mason 2.0 support ([#402](https://github.com/nvim-java/nvim-java/issues/402)) ([58c25cd](https://github.com/nvim-java/nvim-java/commit/58c25cd45d867fc512af48a457c71bb26d9d778d)) + + +### Miscellaneous Chores + +* release 3.0.0 ([3cecf73](https://github.com/nvim-java/nvim-java/commit/3cecf7362e5f83e4a34e84e4f14c65bad81968f1)) + ## [2.1.2](https://github.com/nvim-java/nvim-java/compare/v2.1.1...v2.1.2) (2025-08-04)