Skip to content

Commit

Permalink
cleanup and document
Browse files Browse the repository at this point in the history
  • Loading branch information
IndianBoy42 committed Jun 19, 2023
1 parent 801a54b commit 2837ab7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ will 'move' the node to the chosen position, rather than swapping. This is
equivalent to swapping the first node with all of the nodes up to and including
the second node.

For `I(Swap|Move)(With)?` you can bind it to a key in visual mode to influence node selection. The list node,
For `I(Swap|Move)(With)?` you can bind it to a key in visual mode to influence
node selection.

## configuration

Expand Down Expand Up @@ -80,9 +81,13 @@ require('iswap').setup{
move_cursor = true,

-- Automatically swap with only two arguments
-- default nil
-- default false
autoswap = true,

-- Keys that will use to expand the list/node that you are swapping within
-- Choose a key that is not in the `keys` config otherwise it won't work
expand_key = 'z'

-- Other default options you probably should not change:
debug = nil,
hl_grey_priority = '1000',
Expand Down
54 changes: 25 additions & 29 deletions lua/iswap/choose.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ local M = {}
local util = require('iswap.util')
local ts_utils = require('nvim-treesitter.ts_utils')
local internal = require('iswap.internal')
local ui = require('iswap.ui')
local err = util.err

local ui = require('iswap.ui')
function M.two_nodes_from_list(config)
local bufnr = vim.api.nvim_get_current_buf()
local winid = vim.api.nvim_get_current_win()

local lists = internal.get_list_nodes_at_cursor(winid, config, false)
if lists == nil then return end
err('found n lists: ' .. #lists, config.debug)
for _, list in ipairs(lists) do
local parent, children = unpack(list)
if not parent then
Expand All @@ -21,13 +20,11 @@ function M.two_nodes_from_list(config)
local sr, sc, er, ec = parent:range()

-- a and b are the nodes to swap
local a, b
local a_idx, b_idx

-- enable autoswapping with two children
-- and default to prompting for user input
if config.autoswap and #children == 2 then
a, b = unpack(children)
a_idx, b_idx = 1, 2
else
local user_input, user_keys = ui.prompt(bufnr, config, children, { { sr, sc }, { er, ec } }, 2)
Expand All @@ -37,10 +34,9 @@ function M.two_nodes_from_list(config)
return
end
a_idx, b_idx = unpack(user_input)
a, b = children[a_idx], children[b_idx]
end

if a ~= nil and b ~= nil then return children, a_idx, b_idx end
if children[a_idx] ~= nil and children[b_idx] ~= nil then return children, a_idx, b_idx end
err('some of the nodes were nil', config.debug)
::continue::
end
Expand All @@ -54,51 +50,43 @@ function M.one_other_node_from_list(direction, config)
if lists == nil then return end
for _, list in ipairs(lists) do
local parent, children, cur_node_idx = unpack(list)

if not parent or not children or not cur_node_idx then
err('did not find a satisfiable parent node', config.debug)
goto continue
end

local cur_node = table.remove(children, cur_node_idx)

local sr, sc, er, ec = parent:range()

-- a is the node to move the cur_node into the place of
local a, a_idx
local a_idx

-- enable autoswapping with one other child
-- and default to prompting for user input
if config.autoswap and #children == 1 then
a = children[1]
a_idx = 1
a_idx = 3 - cur_node_idx -- 2<->1
else
if direction == 'left' then
a = children[cur_node_idx - 1]
a_idx = cur_node_idx - 1
elseif direction == 'right' then
-- already shifted over, no need for +1
a = children[cur_node_idx]
a_idx = cur_node_idx
a_idx = cur_node_idx + 1
else
local cur_node = table.remove(children, cur_node_idx)

local user_input, user_keys = ui.prompt(bufnr, config, children, { { sr, sc }, { er, ec } }, 1)
if not (type(user_input) == 'table' and #user_input == 1) then
if user_keys[1] == config.expand_key then goto continue end
err('did not get a valid user input', config.debug)
return
end
a_idx = user_input[1]
a = children[a_idx]

-- restore cur_node into the correct position in children (and adjust indices)
table.insert(children, cur_node_idx, cur_node)
if cur_node_idx <= a_idx then a_idx = a_idx + 1 end
end
end

if a ~= nil then
-- restore cur_node into the correct position in children (and adjust indices)
table.insert(children, cur_node_idx, cur_node)
if cur_node_idx <= a_idx then a_idx = a_idx + 1 end

return children, cur_node_idx, a_idx
end
if children[a_idx] ~= nil then return children, cur_node_idx, a_idx end
err('the node was nil', config.debug)

::continue::
Expand Down Expand Up @@ -151,15 +139,13 @@ function M.two_nodes_from_any(config)
if children == nil or picked_node == nil or picked_parent == nil then return end
local sr, sc, er, ec = picked_parent:range()

local swap_node, swap_node_idx, picked_node_idx
local swap_node_idx, picked_node_idx

if config.autoswap and #children == 2 then -- auto swap picked_node with other sibling
if children[1] == picked_node then
swap_node = children[2]
swap_node_idx = 2
picked_node_idx = 1
else
swap_node = children[1]
swap_node_idx = 1
picked_node_idx = 2
end
Expand All @@ -170,16 +156,21 @@ function M.two_nodes_from_any(config)
break
end
end
table.remove(children, picked_node_idx)

local user_input = ui.prompt(bufnr, config, children, { { sr, sc }, { er, ec } }, 1)
if not (type(user_input) == 'table' and #user_input == 1) then
err('did not get two valid user inputs', config.debug)
return
end
swap_node_idx = user_input[1]
swap_node = children[swap_node_idx]

-- restore picked_node
table.insert(children, picked_node_idx, picked_node)
if picked_node_idx <= swap_node_idx then swap_node_idx = swap_node_idx + 1 end
end

if swap_node == nil then
if children[swap_node_idx] == nil then
err('picked nil swap node', config.debug)
return
end
Expand Down Expand Up @@ -247,6 +238,8 @@ function M.one_other_node_from_any(direction, config)
swap_node_idx = swap_node_idx - 1
end
else
table.remove(children, ancestor_idx)

local user_input, user_keys = ui.prompt(bufnr, config, children, { { sr, sc }, { er, ec } }, 1)
if not (type(user_input) == 'table' and #user_input == 1) then
if user_keys[1] == config.expand_key then goto continue end
Expand All @@ -255,6 +248,9 @@ function M.one_other_node_from_any(direction, config)
end
swap_node_idx = user_input[1]
swap_node = children[swap_node_idx]

table.insert(children, ancestor_idx, ancestor)
if ancestor_idx <= swap_node_idx then swap_node_idx = swap_node_idx + 1 end
end
end

Expand Down
4 changes: 3 additions & 1 deletion lua/iswap/defaults.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local M = {}

M.keys = 'asdfghjklqwertyuiopzxcvbnm'
M.keys = 'asdfghjklqwertyuiopxcvbnm'
M.hl_grey = 'Comment'
M.hl_snipe = 'Search'
M.hl_selection = 'Visual'
Expand All @@ -9,7 +9,9 @@ M.hl_flash = 'IncSearch'
M.hl_grey_priority = 1000
M.grey = 'enable'
M.expand_key = 'z'
M.autoswap = false
M.only_current_line = true
M.visual_select_list = true


return M
2 changes: 1 addition & 1 deletion lua/iswap/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function M.get_ancestors_at_cursor(cur_node, only_current_line, config)
return ancestors, last_row
end

-- returns list_nodes
-- returns list_nodes
function M.get_list_nodes_at_cursor(winid, config, needs_cursor_node)
local cursor_range = util.get_cursor_range(winid)
local visual_sel = #cursor_range > 2
Expand Down

0 comments on commit 2837ab7

Please sign in to comment.