From 847d1b71906ed42fa40bdff6bd0e3f9a9d18d0f9 Mon Sep 17 00:00:00 2001 From: Oleksandr Smirnov Date: Wed, 29 Oct 2025 15:40:05 +0200 Subject: [PATCH] refactor: make it work on neovim version below 0.12 --- lua/gopher/_utils/init.lua | 19 +++++++++++++++++++ lua/gopher/_utils/struct_tags.lua | 9 ++++----- spec/unit/utils_test.lua | 10 ++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lua/gopher/_utils/init.lua b/lua/gopher/_utils/init.lua index 345c670..58a17b2 100644 --- a/lua/gopher/_utils/init.lua +++ b/lua/gopher/_utils/init.lua @@ -50,4 +50,23 @@ function utils.indent(line, indent) return string.rep(char, indent) end +---@generic T +---@param tbl T[] +---@return T[] +function utils.list_unique(tbl) + if vim.fn.has "nvim-0.12" == 1 then + return vim.list.unique(tbl) + end + + for i = #tbl, 1, -1 do + for j = 1, i - 1 do + if tbl[i] == tbl[j] then + table.remove(tbl, i) + break + end + end + end + return tbl +end + return utils diff --git a/lua/gopher/_utils/struct_tags.lua b/lua/gopher/_utils/struct_tags.lua index 5522840..f3f49d2 100644 --- a/lua/gopher/_utils/struct_tags.lua +++ b/lua/gopher/_utils/struct_tags.lua @@ -1,3 +1,5 @@ +local u = require "gopher._utils" + ---@param option string local function option_to_tag(option) return option:match "^(.-)=" @@ -32,12 +34,9 @@ function struct_tags.parse_args(args) end end - -- todo: it's incompatible with vim lower 0.12 - tags = vim.list.unique(tags) - options = vim.list.unique(options) return { - tags = table.concat(tags, ","), - options = table.concat(options, ","), + tags = table.concat(u.list_unique(tags), ","), + options = table.concat(u.list_unique(options), ","), } end diff --git a/spec/unit/utils_test.lua b/spec/unit/utils_test.lua index 2d8f0bc..0b71c93 100644 --- a/spec/unit/utils_test.lua +++ b/spec/unit/utils_test.lua @@ -46,4 +46,14 @@ utils["should add .indent() 2 tabs"] = function() t.eq("\t\t", u.indent(line, indent)) end +utils["should .list_unique on list with duplicates"] = function() + local u = require "gopher._utils" + t.eq({ "json", "xml" }, u.list_unique { "json", "xml", "json" }) +end + +utils["should .list_unique on list with no duplicates"] = function() + local u = require "gopher._utils" + t.eq({ "json", "xml" }, u.list_unique { "json", "xml" }) +end + return T