From 4d27b3cabb7162444be8dfa63d94428db6fedfd4 Mon Sep 17 00:00:00 2001 From: logrusx Date: Wed, 23 Jul 2025 12:42:32 +0300 Subject: [PATCH 1/5] feat: Mason 2.0 migration --- lazy.lua | 2 +- lua/java/startup/mason-registry-check.lua | 2 +- lua/java/utils/mason.lua | 40 +++++++++-------------- tests/prepare-config.lua | 2 +- 4 files changed, 18 insertions(+), 28 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-registry-check.lua b/lua/java/startup/mason-registry-check.lua index 76e3e8d..033c155 100644 --- a/lua/java/startup/mason-registry-check.lua +++ b/lua/java/startup/mason-registry-check.lua @@ -7,7 +7,7 @@ local M = { function M.is_valid() local has_reg = false - for reg in mason_source.iter() do + for reg in mason_source:iterate() do if reg.id == M.JAVA_REG_ID then has_reg = true goto continue diff --git a/lua/java/utils/mason.lua b/lua/java/utils/mason.lua index 6e239a2..d1c340a 100644 --- a/lua/java/utils/mason.lua +++ b/lua/java/utils/mason.lua @@ -6,40 +6,27 @@ local await = async.wait_handle_ok 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.0 + local has_pkg, pkg = pcall(mason_reg.get_package, package_name) if not has_pkg then return false end - local has_version = false + local installed_version = pkg:get_installed_version() - 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) - - 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.0 + 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) + local installed_version = pkg:get_installed_version() return installed_version == package_version end @@ -69,10 +56,13 @@ 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.0 + if not pkg:is_installing() 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 cecd82a58afaa243f65c083adb7774687ddb9e1b Mon Sep 17 00:00:00 2001 From: logrusx Date: Tue, 5 Aug 2025 14:01:49 +0300 Subject: [PATCH 2/5] feat: Mason 2.0 migration Pt. 2: backward compatibility with Mason 1.x --- lua/java/startup/mason-dep.lua | 19 ++++++++- lua/java/startup/mason-registry-check.lua | 47 +++++++++++++-------- lua/java/utils/mason.lua | 50 ++++++++++++++++++++--- 3 files changed, 92 insertions(+), 24 deletions(-) diff --git a/lua/java/startup/mason-dep.lua b/lua/java/startup/mason-dep.lua index c424b04..5882a4f 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,20 @@ 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 + 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) diff --git a/lua/java/startup/mason-registry-check.lua b/lua/java/startup/mason-registry-check.lua index 033c155..c271def 100644 --- a/lua/java/startup/mason-registry-check.lua +++ b/lua/java/startup/mason-registry-check.lua @@ -1,26 +1,41 @@ -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 + 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:iterate() 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 d1c340a..0085bc5 100644 --- a/lua/java/utils/mason.lua +++ b/lua/java/utils/mason.lua @@ -2,31 +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) - -- get_package errors if the package is not available in Mason 2.0 + -- 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 installed_version = pkg:get_installed_version() + 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 installed_version == package_version end function M.is_installed(package_name, package_version) - -- get_package errors if the package is not available in Mason 2.0 + -- 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 found or not pkg:is_installed() then return false end - local installed_version = pkg:get_installed_version() + 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 installed_version == package_version end @@ -56,8 +85,17 @@ function M.install_pkgs(packages) if not M.is_installed(dep.name, dep.version) then local pkg = mason_reg.get_package(dep.name) - -- install errors if installation is already running in Mason 2.0 - if not pkg:is_installing() then + -- 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, From abf09e0ff9a31f492567f8014146a2d401fb6980 Mon Sep 17 00:00:00 2001 From: logrusx Date: Tue, 5 Aug 2025 14:56:40 +0300 Subject: [PATCH 3/5] feat: Mason 2.0 migration Pt. 3: fixed formatting and typos, added missing diagnostic disable's --- lua/java/startup/mason-dep.lua | 9 +++++---- lua/java/startup/mason-registry-check.lua | 6 ++++-- lua/java/utils/mason.lua | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lua/java/startup/mason-dep.lua b/lua/java/startup/mason-dep.lua index 5882a4f..129c777 100644 --- a/lua/java/startup/mason-dep.lua +++ b/lua/java/startup/mason-dep.lua @@ -6,7 +6,7 @@ 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 mason_v2 = require('mason.version').MAJOR_VERSION == 2 local List = require('java-core.utils.list') @@ -29,11 +29,12 @@ end ---@param registries java.Config local function add_custom_registries_v2(registries) for _, reg in ipairs(registries) do - require("mason-registry").sources:prepend(reg) + ---@diagnostic disable-next-line: undefined-field + require('mason-registry').sources:prepend(reg) end end -if (mason_v2) then +if mason_v2 then M.add_custom_registries = add_custom_registries_v2 else M.add_custom_registries = add_custom_registries_v1 @@ -66,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 c271def..60097cf 100644 --- a/lua/java/startup/mason-registry-check.lua +++ b/lua/java/startup/mason-registry-check.lua @@ -1,8 +1,10 @@ -local mason_v2 = require('mason.version').MAJOR_VERSION==2 +local mason_v2 = require('mason.version').MAJOR_VERSION == 2 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') @@ -33,7 +35,7 @@ function M.is_valid() if reg.id == M.JAVA_REG_ID then return { success = true, - continue = true + continue = true, } end end diff --git a/lua/java/utils/mason.lua b/lua/java/utils/mason.lua index 0085bc5..824d425 100644 --- a/lua/java/utils/mason.lua +++ b/lua/java/utils/mason.lua @@ -23,7 +23,7 @@ function M.is_available(package_name, package_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) + pkg:get_installed_version(function(success, version) if success then installed_version = version end @@ -50,7 +50,7 @@ function M.is_installed(package_name, package_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) + pkg:get_installed_version(function(success, version) if success then installed_version = version end From 047331bdeb1ab6653d4784f73f9e89fb269cdaa0 Mon Sep 17 00:00:00 2001 From: logrusx Date: Tue, 5 Aug 2025 14:59:46 +0300 Subject: [PATCH 4/5] feat: Mason 2.0 migration Pt. 4: fixed formatting --- lua/java/startup/mason-registry-check.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/java/startup/mason-registry-check.lua b/lua/java/startup/mason-registry-check.lua index 60097cf..ce8db28 100644 --- a/lua/java/startup/mason-registry-check.lua +++ b/lua/java/startup/mason-registry-check.lua @@ -30,7 +30,6 @@ function M.is_valid() iterator = mason_sources.iter end - for reg in iterator(mason_sources) do if reg.id == M.JAVA_REG_ID then return { From 82333335b90f97d5e3f28550faa3ec95b299e2d5 Mon Sep 17 00:00:00 2001 From: s1n7ax Date: Wed, 6 Aug 2025 12:04:38 +0530 Subject: [PATCH 5/5] chore: release 3.0.0 Release-As: 3.0.0