@@ -28,6 +28,20 @@ local function is_supported_ft()
28
28
return not unsupported_filetypes [ft ]
29
29
end
30
30
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
+
31
45
function M .swap_down ()
32
46
if not is_on_target_node () then return end
33
47
if not is_supported_ft () then return end
@@ -39,7 +53,7 @@ function M.swap_down()
39
53
end
40
54
41
55
local current = nodes .get_current ()
42
- current = nodes . get_highest_coincident (current )
56
+ current = get_highest_coincident_pipe (current )
43
57
local current_augments = augment .get_node_augments (current )
44
58
local current_all = { current , unpack (current_augments ) }
45
59
local current_all_rows = nodes .whole_range (current_all )
@@ -73,7 +87,7 @@ function M.swap_up()
73
87
end
74
88
75
89
local current = nodes .get_current ()
76
- current = nodes . get_highest_coincident (current )
90
+ current = get_highest_coincident_pipe (current )
77
91
local current_augments = augment .get_node_augments (current )
78
92
local current_all = { current , unpack (current_augments ) }
79
93
local current_all_rows = nodes .whole_range (current_all )
@@ -105,60 +119,40 @@ end
105
119
function M .swap_right ()
106
120
if not is_supported_ft () then return end
107
121
108
- -- Least desirable strategies first
109
-
110
- -- most naive next sibling
122
+ -- Iteratively more desirable
111
123
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 )
114
126
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 )
123
128
124
- -- No candidates found
125
129
if not current or not target then return end
126
130
127
131
operations .swap_nodes (current , target )
128
132
129
133
-- Place cursor
130
- local next = nodes .next_sib (current )
134
+ local new_current = nodes .next_sib (current )
131
135
132
136
-- Now next will be the same node as current is,
133
137
-- but with an updated range
134
- if not next then return end
138
+ if not new_current then return end
135
139
136
140
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 )
139
143
)
140
144
end
141
145
142
146
function M .swap_left ()
143
147
if not is_supported_ft () then return end
144
148
145
- -- Least desirable strategies first
146
-
147
- -- most naive next sibling
149
+ -- Iteratively more desirable
148
150
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 )
151
153
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 )
160
155
161
- -- No candidates found
162
156
if not current or not target then return end
163
157
164
158
operations .swap_nodes (target , current )
0 commit comments