Skip to content

Commit dc2eb65

Browse files
committed
Refactor swap top level methods
They kinda became a dumping ground
1 parent c7ef54b commit dc2eb65

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

lua/treewalker/strategies.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ end
171171
-- Use this to get the whole string from inside of a string
172172
-- returns nils if the passed in node is not a string node
173173
---@param node TSNode
174-
---@return TSNode | nil, TSNode | nil
174+
---@return TSNode | nil
175175
function M.get_highest_string_node(node)
176176
---@type TSNode | nil
177177
local highest = nil

lua/treewalker/swap.lua

+28-34
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ local function is_supported_ft()
2828
return not unsupported_filetypes[ft]
2929
end
3030

31+
---@param node TSNode
32+
---@return TSNode
33+
local function get_highest_coincident_pipe(node)
34+
local candidate = nodes.get_highest_coincident(node)
35+
return candidate or node
36+
end
37+
38+
---@param node TSNode
39+
---@return TSNode
40+
local function get_highest_string_node_pipe(node)
41+
local candidate = strategies.get_highest_string_node(node)
42+
return candidate or node
43+
end
44+
3145
function M.swap_down()
3246
if not is_on_target_node() then return end
3347
if not is_supported_ft() then return end
@@ -39,7 +53,7 @@ function M.swap_down()
3953
end
4054

4155
local current = nodes.get_current()
42-
current = nodes.get_highest_coincident(current)
56+
current = get_highest_coincident_pipe(current)
4357
local current_augments = augment.get_node_augments(current)
4458
local current_all = { current, unpack(current_augments) }
4559
local current_all_rows = nodes.whole_range(current_all)
@@ -73,7 +87,7 @@ function M.swap_up()
7387
end
7488

7589
local current = nodes.get_current()
76-
current = nodes.get_highest_coincident(current)
90+
current = get_highest_coincident_pipe(current)
7791
local current_augments = augment.get_node_augments(current)
7892
local current_all = { current, unpack(current_augments) }
7993
local current_all_rows = nodes.whole_range(current_all)
@@ -105,60 +119,40 @@ end
105119
function M.swap_right()
106120
if not is_supported_ft() then return end
107121

108-
-- Least desirable strategies first
109-
110-
-- most naive next sibling
122+
-- Iteratively more desirable
111123
local current = nodes.get_current()
112-
current = nodes.get_highest_coincident(current)
113-
local target = nodes.next_sib(current)
124+
current = get_highest_string_node_pipe(current)
125+
current = get_highest_coincident_pipe(current)
114126

115-
-- strings
116-
local candidate = strategies.get_highest_string_node(nodes.get_current())
117-
if candidate then candidate = nodes.get_highest_coincident(candidate) end
118-
local candidate_target = nodes.next_sib(candidate)
119-
if candidate and candidate_target then
120-
current = candidate
121-
target = candidate_target
122-
end
127+
local target = nodes.next_sib(current)
123128

124-
-- No candidates found
125129
if not current or not target then return end
126130

127131
operations.swap_nodes(current, target)
128132

129133
-- Place cursor
130-
local next = nodes.next_sib(current)
134+
local new_current = nodes.next_sib(current)
131135

132136
-- Now next will be the same node as current is,
133137
-- but with an updated range
134-
if not next then return end
138+
if not new_current then return end
135139

136140
vim.fn.cursor(
137-
nodes.get_srow(next),
138-
nodes.get_scol(next)
141+
nodes.get_srow(new_current),
142+
nodes.get_scol(new_current)
139143
)
140144
end
141145

142146
function M.swap_left()
143147
if not is_supported_ft() then return end
144148

145-
-- Least desirable strategies first
146-
147-
-- most naive next sibling
149+
-- Iteratively more desirable
148150
local current = nodes.get_current()
149-
current = nodes.get_highest_coincident(current)
150-
local target = nodes.prev_sib(current)
151+
current = get_highest_string_node_pipe(current)
152+
current = get_highest_coincident_pipe(current)
151153

152-
-- strings
153-
local candidate = strategies.get_highest_string_node(nodes.get_current())
154-
if candidate then candidate = nodes.get_highest_coincident(candidate) end
155-
local candidate_target = nodes.prev_sib(candidate)
156-
if candidate and candidate_target then
157-
current = candidate
158-
target = candidate_target
159-
end
154+
local target = nodes.prev_sib(current)
160155

161-
-- No candidates found
162156
if not current or not target then return end
163157

164158
operations.swap_nodes(target, current)

lua/treewalker/targets.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ function M.out()
2020
local node = nodes.get_current()
2121
local target = strategies.get_first_ancestor_with_diff_scol(node)
2222
if not target then return end
23-
local row = target:range()
24-
row = row + 1
23+
local row = nodes.get_srow(target)
2524
local line = lines.get_line(row)
2625
return target, row, line
2726
end

0 commit comments

Comments
 (0)