From 74d7afcc015a5f936729be1bab9974690e3914bd Mon Sep 17 00:00:00 2001 From: Oleksandr Smirnov Date: Sat, 22 Mar 2025 23:00:14 +0200 Subject: [PATCH] refactor: move thing out to utils --- lua/gopher/_utils/init.lua | 7 +++++++ lua/gopher/struct_tags.lua | 3 ++- spec/unit/utils_test.lua | 15 ++++++--------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lua/gopher/_utils/init.lua b/lua/gopher/_utils/init.lua index 0d9f5e2..2a5e987 100644 --- a/lua/gopher/_utils/init.lua +++ b/lua/gopher/_utils/init.lua @@ -30,4 +30,11 @@ function utils.remove_empty_lines(t) return res end +---@param s string +---@return string +function utils.trimend(s) + local r, _ = string.gsub(s, "%s+$", "") + return r +end + return utils diff --git a/lua/gopher/struct_tags.lua b/lua/gopher/struct_tags.lua index 3f2e51b..cf990bf 100644 --- a/lua/gopher/struct_tags.lua +++ b/lua/gopher/struct_tags.lua @@ -33,6 +33,7 @@ local ts = require "gopher._utils.ts" local r = require "gopher._utils.runner" local c = require "gopher.config" +local u = require "gopher._utils" local log = require "gopher._utils.log" local struct_tags = {} @@ -77,7 +78,7 @@ local function handle_tags(fpath, bufnr, user_args) end for i, v in ipairs(res["lines"]) do - res["lines"][i] = string.gsub(v, "%s+$", "") + res["lines"][i] = u.trimend(v) end vim.api.nvim_buf_set_lines( diff --git a/spec/unit/utils_test.lua b/spec/unit/utils_test.lua index 7001261..72d89dc 100644 --- a/spec/unit/utils_test.lua +++ b/spec/unit/utils_test.lua @@ -1,13 +1,5 @@ local t = require "spec.testutils" -local child = MiniTest.new_child_neovim() -local T = MiniTest.new_set { - hooks = { - post_once = child.stop, - pre_case = function() - child.restart { "-u", t.mininit_path } - end, - }, -} +local _, T = t.setup "utils" T["utils"] = MiniTest.new_set() T["utils"]["should .remove_empty_lines()"] = function() @@ -26,4 +18,9 @@ T["utils"]["should .readfile_joined()"] = function() t.eq(u.readfile_joined(tmp), data) end +T["utils"]["should .trimend()"] = function() + local u = require "gopher._utils" + t.eq(u.trimend " hi ", " hi") +end + return T