@@ -38,6 +38,24 @@ local function is_supported_ft()
3838 return not unsupported_filetypes [ft ]
3939end
4040
41+ --- @param current_node TSNode
42+ --- @param candidate TSNode
43+ --- @return boolean
44+ local function should_confine_swap (current_node , candidate )
45+ local opts = require (' treewalker' ).opts
46+ if opts .scope_confined ~= true then
47+ return false
48+ end
49+
50+ local current_parent = nodes .scope_parent (current_node )
51+ if not current_parent then
52+ return false
53+ end
54+
55+ local candidate_anchor = nodes .get_highest_row_coincident (candidate )
56+ return not nodes .is_descendant_of (current_parent , candidate_anchor )
57+ end
58+
4159function M .swap_down ()
4260 vim .cmd (" normal! ^" )
4361 if not is_supported_ft () then return end
@@ -50,6 +68,10 @@ function M.swap_down()
5068 local target = targets .down (current , row )
5169 if not target then return end
5270
71+ if should_confine_swap (current , target ) then
72+ return
73+ end
74+
5375 local current_augments = augment .get_node_augments (current )
5476 local current_all = { current , unpack (current_augments ) }
5577 local current_srow = nodes .get_srow (current )
@@ -82,6 +104,10 @@ function M.swap_up()
82104 local target = targets .up (current , row )
83105 if not target then return end
84106
107+ if should_confine_swap (current , target ) then
108+ return
109+ end
110+
85111 local current_augments = augment .get_node_augments (current )
86112 local current_all = { current , unpack (current_augments ) }
87113 local current_srow = nodes .get_srow (current )
@@ -121,6 +147,10 @@ function M.swap_right()
121147
122148 if not current or not target then return end
123149
150+ if should_confine_swap (current , target ) then
151+ return
152+ end
153+
124154 -- set a mark to track where the target started, so we may later go there after the swap
125155 local ns_id = vim .api .nvim_create_namespace (" treewalker#swap_right" )
126156 local ext_id = vim .api .nvim_buf_set_extmark (
@@ -158,6 +188,10 @@ function M.swap_left()
158188
159189 if not current or not target then return end
160190
191+ if should_confine_swap (current , target ) then
192+ return
193+ end
194+
161195 operations .swap_nodes (target , current )
162196
163197 -- Place cursor
0 commit comments