-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Smooth going until one major problem: In order to swap nodes, we need to know how big they are. We have the jump targets down pat, but knowing how big the current node is is not easy. Because it's not obvious what the current node is, only where its start position is. - This is the commit message #2: Iteration. Not working, but code structure is there - This is the commit message #3: Continuing iteration Currently it's looking at lines. It's not working fully, something wrong in the swap logic. But it's looking at lines. It might be nice to do this by nodes, so instead of highest_coincident starting on the same line, looking at the highest coincident on the same line and col. And swapping like that as well. - This is the commit message #4: Pilfer ts_util's swap_nodes
- Loading branch information
Showing
15 changed files
with
572 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
local ops = require "treewalker.ops" | ||
local targets = require "treewalker.targets" | ||
|
||
local M = {} | ||
|
||
---@return nil | ||
function M.move_out() | ||
local target, row, line = targets.out() | ||
if target and row and line then | ||
--util.log("no out candidate") | ||
ops.jump(row, target) | ||
return | ||
end | ||
end | ||
|
||
---@return nil | ||
function M.move_in() | ||
local target, row, line = targets.inn() | ||
|
||
if target and row and line then | ||
--util.log("no in candidate") | ||
ops.jump(row, target) | ||
end | ||
end | ||
|
||
---@return nil | ||
function M.move_up() | ||
local target, row, line = targets.up() | ||
|
||
if target and row and line then | ||
--util.log("no up candidate") | ||
ops.jump(row, target) | ||
end | ||
end | ||
|
||
---@return nil | ||
function M.move_down() | ||
local target, row, line = targets.down() | ||
|
||
if target and row and line then | ||
--util.log("no down candidate") | ||
ops.jump(row, target) | ||
end | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
local nodes = require "treewalker.nodes" | ||
local ops = require "treewalker.ops" | ||
local targets = require "treewalker.targets" | ||
|
||
local M = {} | ||
|
||
---@param target TSNode | ||
local function swap_with_current(target) | ||
local current = nodes.get_current() | ||
current = nodes.get_highest_coincident(current) | ||
target = nodes.get_highest_coincident(target) | ||
|
||
local target_range = nodes.range(target) | ||
local current_range = nodes.range(current) | ||
|
||
ops.swap( | ||
{ current_range[1], current_range[3] }, | ||
{ target_range[1], target_range[3] } | ||
) | ||
|
||
-- cursor should follow current node | ||
-- TODO This should be factored out into the swap_down and swap_up | ||
if current_range[1] < target_range[1] then | ||
-- for down swaps | ||
local node_length_diff = ((current_range[3] - current_range[1]) + 1) - ((target_range[3] - target_range[1]) + 1) | ||
vim.fn.cursor(target_range[1] - node_length_diff + 1, target_range[2] + 1) | ||
elseif current_range[1] > target_range[1] then | ||
-- up swaps | ||
vim.fn.cursor(target_range[1] + 1, target_range[2] + 1) | ||
end | ||
end | ||
|
||
---@return nil | ||
function M.swap_down() | ||
local target, row, line = targets.down() | ||
|
||
if not target or not row or not line then | ||
--util.log("no down candidate") | ||
return | ||
end | ||
|
||
swap_with_current(target) | ||
end | ||
|
||
---@return nil | ||
function M.swap_up() | ||
local target, row, line = targets.up() | ||
|
||
if not target or not row or not line then | ||
--util.log("no down candidate") | ||
return | ||
end | ||
|
||
swap_with_current(target) | ||
end | ||
|
||
return M |
Oops, something went wrong.