Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#37): head tail movement support + deprecate previous API's #39

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,31 @@ paredit.setup({
["<localleader>O"] = { paredit.api.raise_element, "Raise element" },

["E"] = {
paredit.api.move_to_next_element,
paredit.api.move_to_next_element_tail,
"Jump to next element tail",
-- by default all keybindings are dot repeatable
repeatable = false,
mode = { "n", "x", "o", "v" },
},
["W"] = {
paredit.api.move_to_next_element_head,
"Jump to next element head",
repeatable = false,
mode = { "n", "x", "o", "v" },
},

["B"] = {
paredit.api.move_to_prev_element,
paredit.api.move_to_prev_element_head,
"Jump to previous element head",
repeatable = false,
mode = { "n", "x", "o", "v" },
},
["gE"] = {
paredit.api.move_to_prev_element_tail,
"Jump to previous element tail",
repeatable = false,
mode = { "n", "x", "o", "v" },
},

-- These are text object selection keybindings which can used with standard `d, y, c`, `v`
["af"] = {
Expand Down
12 changes: 10 additions & 2 deletions lua/nvim-paredit/api/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local raising = require("nvim-paredit.api.raising")
local motions = require("nvim-paredit.api.motions")
local selections = require("nvim-paredit.api.selections")
local deletions = require("nvim-paredit.api.deletions")
local common = require("nvim-paredit.utils.common")

local M = {
slurp_forwards = slurping.slurp_forwards,
Expand All @@ -20,8 +21,15 @@ local M = {
raise_form = raising.raise_form,
raise_element = raising.raise_element,

move_to_next_element = motions.move_to_next_element,
move_to_prev_element = motions.move_to_prev_element,
-- TODO: remove deprecated code in next versions
move_to_next_element = common.deprecate(motions.move_to_next_element_tail, "use `api.move_to_next_element_tail`"),
move_to_next_element_tail = motions.move_to_next_element_tail,
move_to_next_element_head = motions.move_to_next_element_head,

-- TODO: remove deprecated code in next versions
move_to_prev_element = common.deprecate(motions.move_to_prev_element_head, "use `api.move_to_prev_element_head`"),
move_to_prev_element_head = motions.move_to_prev_element_head,
move_to_prev_element_tail = motions.move_to_prev_element_tail,

select_around_form = selections.select_around_form,
select_in_form = selections.select_in_form,
Expand Down
43 changes: 30 additions & 13 deletions lua/nvim-paredit/api/motions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ local function get_next_node_from_cursor(lang, reversed)
end
end

function M._move_to_element(count, reversed)
function M._move_to_element(count, reversed, is_head)
is_head = is_head or false
local lang = langs.get_language_api()

local current_node = get_next_node_from_cursor(lang, reversed)
Expand All @@ -67,16 +68,20 @@ function M._move_to_element(count, reversed)

local is_in_middle = false
if reversed then
node_edge = { current_node:start() }
traversal_fn = traversal.get_prev_sibling_ignoring_comments
if common.compare_positions(cursor_pos, node_edge) == 1 then
is_in_middle = true
if is_head then
node_edge = { current_node:start() }
if common.compare_positions(cursor_pos, node_edge) == 1 then
is_in_middle = true
end
end
else
node_edge = { current_node:end_() }
traversal_fn = traversal.get_next_sibling_ignoring_comments
if common.compare_positions({ node_edge[1], node_edge[2] - 1 }, cursor_pos) == 1 then
is_in_middle = true
if not is_head then
node_edge = { current_node:end_() }
if common.compare_positions({ node_edge[1], node_edge[2] - 1 }, cursor_pos) == 1 then
is_in_middle = true
end
end
end

Expand All @@ -95,7 +100,7 @@ function M._move_to_element(count, reversed)
count = count,
})
if sibling then
if reversed then
if is_head then
next_pos = { sibling:start() }
else
next_pos = { sibling:end_() }
Expand All @@ -107,7 +112,7 @@ function M._move_to_element(count, reversed)
return
end

if reversed then
if is_head then
cursor_pos = { next_pos[1] + 1, next_pos[2] }
else
cursor_pos = { next_pos[1] + 1, next_pos[2] - 1 }
Expand All @@ -125,16 +130,28 @@ local function ensure_visual_if_operator_pending()
end
end

function M.move_to_prev_element()
function M.move_to_prev_element_head()
local count = vim.v.count1
ensure_visual_if_operator_pending()
M._move_to_element(count, true, true)
end

function M.move_to_prev_element_tail()
local count = vim.v.count1
ensure_visual_if_operator_pending()
M._move_to_element(count, true, false)
end

function M.move_to_next_element_tail()
local count = vim.v.count1
ensure_visual_if_operator_pending()
M._move_to_element(count, true)
M._move_to_element(count, false, false)
end

function M.move_to_next_element()
function M.move_to_next_element_head()
local count = vim.v.count1
ensure_visual_if_operator_pending()
M._move_to_element(count, false)
M._move_to_element(count, false, true)
end

return M
16 changes: 14 additions & 2 deletions lua/nvim-paredit/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,29 @@ M.default_keys = {
["<localleader>O"] = { api.raise_element, "Raise element" },

["E"] = {
api.move_to_next_element,
api.move_to_next_element_tail,
"Next element tail",
repeatable = false,
mode = { "n", "x", "o", "v" },
},
["W"] = {
api.move_to_next_element_head,
"Next element head",
repeatable = false,
mode = { "n", "x", "o", "v" },
},
["B"] = {
api.move_to_prev_element,
api.move_to_prev_element_head,
"Previous element head",
repeatable = false,
mode = { "n", "x", "o", "v" },
},
["gE"] = {
api.move_to_prev_element_tail,
"Previous element tail",
repeatable = false,
mode = { "n", "x", "o", "v" },
},

["af"] = {
api.select_around_form,
Expand Down
7 changes: 7 additions & 0 deletions lua/nvim-paredit/utils/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,11 @@ function M.is_whitespace_under_cursor(lang)
or char_under_cursor[1] == ""
end

function M.deprecate(fn, message)
return function(...)
print("Warning: Deprecated function called. " .. message)
return fn(...)
end
end

return M
Loading
Loading