Skip to content

Commit

Permalink
refactor: replace util.contains & util.merge_tables with builtin func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
BBboy01 committed Dec 18, 2024
1 parent 6978a60 commit a828d3a
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 81 deletions.
3 changes: 1 addition & 2 deletions lua/treewalker/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local nodes = require('treewalker.nodes')
local util = require('treewalker.util')
local ops = require('treewalker.ops')
local lines = require('treewalker.lines')
local strategies = require('treewalker.strategies')
Expand All @@ -17,7 +16,7 @@ Treewalker.opts = {

---@param opts Opts | nil
function Treewalker.setup(opts)
Treewalker.opts = util.merge_tables(Treewalker.opts, opts)
Treewalker.opts = vim.tbl_deep_extend('force', Treewalker.opts, opts or {})
end

---@return nil
Expand Down
1 change: 0 additions & 1 deletion lua/treewalker/strategies.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local lines = require('treewalker.lines')
local nodes = require('treewalker.nodes')
local util = require('treewalker.util')

---@alias Dir "up" | "down"

Expand Down
36 changes: 0 additions & 36 deletions lua/treewalker/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,42 +55,6 @@ M.log = function(...)
log_file:close()
end

---@param lines string[]
---@param string string
M.contains = function(lines, string)
local found_line = false
for _, line in ipairs(lines) do
if line == string then
found_line = true
break
end
end
return found_line
end

--- Merging multiple tables into one. Works on both map like and array like tables.
--- This function accepts variadic arguments (multiple tables)
--- It merges keys from the provided tables into a new table.
--- Later tables get precedence
--- @generic T
--- @param ... T - Any number of tables to merge.
--- @return T - A new merged table of the same type as the input tables.
M.merge_tables = function(...)
local new_table = {}

for _, t in ipairs({ ... }) do
for k, v in pairs(t) do
if type(k) == "number" then
table.insert(new_table, v)
else
new_table[k] = v
end
end
end

return new_table
end

M.guid = function()
local template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function(c)
Expand Down
4 changes: 1 addition & 3 deletions tests/fixtures/lua.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local util = require('treewalker.util')

local M = {}

local NON_TARGET_NODE_MATCHERS = {
Expand Down Expand Up @@ -28,7 +26,7 @@ local function is_jump_target(node)
end

local function is_descendant_jump_target(node)
return util.contains(TARGET_DESCENDANT_TYPES, node:type())
return vim.list_contains(TARGET_DESCENDANT_TYPES, node:type())
end

---Do the nodes have the same starting point
Expand Down
39 changes: 0 additions & 39 deletions tests/treewalker/util_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ local stub = require('luassert.stub')
local assert = require("luassert")

describe("util", function()
describe("contains_line", function()
assert(util.contains({ "has", "also" }, "has"))
assert.False(util.contains({ "doesnt" }, "has"))
end)

describe("guid", function()
it("never repeats", function()
local guids = {}
Expand Down Expand Up @@ -57,40 +52,6 @@ describe("util", function()
end)
end)

describe("merge_tables", function()
it("merges N hash style tables", function()
local a = { a = true }
local b = { b = true }
local c = { c = true }
local d = util.merge_tables(a, b, c)
assert.same({ a = true, b = true, c = true }, d)
end)

it("merges multiple array style tables", function()
local a = { true }
local b = { false }
local c = { true, false }
local d = util.merge_tables(a, b, c)
assert.same({ true, false, true, false }, d)
end)

it("merges combo style tables", function()
local a = { a = true }
local b = { false, false }
local c = { true, true }
local d = util.merge_tables(a, b, c)
assert.same({ a = true, false, false, true, true }, d)
end)

it("does not overwrite arguments", function()
local a = { a = true }
local b = { b = true }
util.merge_tables(a, b)
assert.same({ a = true }, a)
assert.same({ b = true }, b)
end)
end)

describe("reverse", function()
it("reverses an array table", function()
local t = { 1, 2, 3, 4, 5 }
Expand Down

0 comments on commit a828d3a

Please sign in to comment.