From 0f1cb65f436b769733d18b41572f617a1fb41f62 Mon Sep 17 00:00:00 2001 From: William Boman Date: Sat, 11 May 2024 21:22:49 +0200 Subject: fix: fix usage of deprecated Neovim APIs (#1703) --- lua/mason-core/functional/init.lua | 1 + lua/mason-core/functional/list.lua | 20 +++++++++++++++++++- lua/mason-core/functional/type.lua | 4 ++++ lua/mason-core/installer/registry/init.lua | 2 +- lua/mason-core/installer/registry/util.lua | 2 +- lua/mason-core/spawn.lua | 4 +++- lua/mason/ui/components/json-schema.lua | 6 +++--- 7 files changed, 32 insertions(+), 7 deletions(-) (limited to 'lua') diff --git a/lua/mason-core/functional/init.lua b/lua/mason-core/functional/init.lua index 87a5f325..d377d2db 100644 --- a/lua/mason-core/functional/init.lua +++ b/lua/mason-core/functional/init.lua @@ -133,6 +133,7 @@ _.assoc = tbl.assoc local typ = lazy_require "mason-core.functional.type" _.is_nil = typ.is_nil _.is = typ.is +_.is_list = typ.is_list -- TODO do something else with these diff --git a/lua/mason-core/functional/list.lua b/lua/mason-core/functional/list.lua index ff2e18c2..b2c48293 100644 --- a/lua/mason-core/functional/list.lua +++ b/lua/mason-core/functional/list.lua @@ -74,7 +74,25 @@ _.filter = fun.curryN(vim.tbl_filter, 2) ---@type fun(map_fn: (fun(item: T): U), items: T[]): U[] _.map = fun.curryN(vim.tbl_map, 2) -_.flatten = fun.curryN(vim.tbl_flatten, 1) +---@param tbl table +---@return table +_.flatten = function(tbl) + local result = {} + --- @param _tbl table + local function _tbl_flatten(_tbl) + local n = #_tbl + for i = 1, n do + local v = _tbl[i] + if type(v) == "table" then + _tbl_flatten(v) + elseif v then + table.insert(result, v) + end + end + end + _tbl_flatten(tbl) + return result +end ---@generic T ---@param map_fn fun(item: T): Optional diff --git a/lua/mason-core/functional/type.lua b/lua/mason-core/functional/type.lua index e3bf5fe7..b15a2025 100644 --- a/lua/mason-core/functional/type.lua +++ b/lua/mason-core/functional/type.lua @@ -11,4 +11,8 @@ _.is = fun.curryN(function(typ, value) return type(value) == typ end, 2) +---@param value any +---@return boolean +_.is_list = vim.fn.has "nvim-0.10" and vim.islist or vim.tbl_islist + return _ diff --git a/lua/mason-core/installer/registry/init.lua b/lua/mason-core/installer/registry/init.lua index e97a8430..e7c796e9 100644 --- a/lua/mason-core/installer/registry/init.lua +++ b/lua/mason-core/installer/registry/init.lua @@ -54,7 +54,7 @@ end local function upsert(dst, src) for k, v in pairs(src) do if type(v) == "table" then - if vim.tbl_islist(v) then + if _.is_list(v) then dst[k] = _.concat(v, dst[k] or {}) else dst[k] = upsert(dst[k] or {}, src[k]) diff --git a/lua/mason-core/installer/registry/util.lua b/lua/mason-core/installer/registry/util.lua index d0045a3e..b3735c9c 100644 --- a/lua/mason-core/installer/registry/util.lua +++ b/lua/mason-core/installer/registry/util.lua @@ -12,7 +12,7 @@ local M = {} ---@param opts PackageInstallOpts ---@return Result # Result function M.coalesce_by_target(candidates, opts) - if not vim.tbl_islist(candidates) then + if not _.is_list(candidates) then return Result.success(candidates) end return Optional.of_nilable(_.find_first(function(asset) diff --git a/lua/mason-core/spawn.lua b/lua/mason-core/spawn.lua index c6f06d81..33af9ea4 100644 --- a/lua/mason-core/spawn.lua +++ b/lua/mason-core/spawn.lua @@ -5,6 +5,8 @@ local log = require "mason-core.log" local platform = require "mason-core.platform" local process = require "mason-core.process" +local is_not_nil = _.complement(_.equals(vim.NIL)) + ---@alias JobSpawn table ---@type JobSpawn local spawn = { @@ -17,7 +19,7 @@ local spawn = { luarocks = (platform.is.win and vim.fn.executable "luarocks.bat" == 1) and "luarocks.bat" or "luarocks", rebar3 = platform.is.win and "rebar3.cmd" or "rebar3", }, - _flatten_cmd_args = _.compose(_.filter(_.complement(_.equals(vim.NIL))), _.flatten), + _flatten_cmd_args = _.compose(_.filter(is_not_nil), _.flatten), } local function Failure(err, cmd) diff --git a/lua/mason/ui/components/json-schema.lua b/lua/mason/ui/components/json-schema.lua index 9430576c..0d24099e 100644 --- a/lua/mason/ui/components/json-schema.lua +++ b/lua/mason/ui/components/json-schema.lua @@ -14,7 +14,7 @@ local property_type_highlights = { } local function resolve_type(property_schema) - if vim.tbl_islist(property_schema.type) then + if _.is_list(property_schema.type) then return table.concat(property_schema.type, " | ") elseif property_schema.type == "array" then if property_schema.items then @@ -108,7 +108,7 @@ local function JsonSchema(pkg, schema_id, state, schema, key, level, key_width, ) end return Ui.Node(nodes) - elseif vim.tbl_islist(schema) then + elseif _.is_list(schema) then return Ui.Node(_.map(function(sub_schema) return JsonSchema(pkg, schema_id, state, sub_schema) end, schema)) @@ -155,7 +155,7 @@ local function JsonSchema(pkg, schema_id, state, schema, key, level, key_width, { { "type", "MasonMuted" }, { type, type_highlight } }, } - if vim.tbl_islist(schema.enum) then + if _.is_list(schema.enum) then for idx, enum in ipairs(schema.enum) do local enum_description = "" if schema.enumDescriptions and schema.enumDescriptions[idx] then -- cgit v1.2.3-70-g09d2