-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from julienvincent/jv/selections
Add text object selections for forms and elements
- Loading branch information
Showing
8 changed files
with
260 additions
and
31 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
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,105 @@ | ||
local traversal = require("nvim-paredit.utils.traversal") | ||
local ts = require("nvim-treesitter.ts_utils") | ||
local langs = require("nvim-paredit.lang") | ||
|
||
local M = {} | ||
|
||
function M.ensure_visual_mode() | ||
if vim.api.nvim_get_mode().mode ~= "v" then | ||
vim.api.nvim_command("normal! v") | ||
end | ||
end | ||
|
||
function M.get_range_around_form() | ||
local lang = langs.get_language_api() | ||
local current_form = traversal.find_nearest_form(ts.get_node_at_cursor(), { | ||
lang = lang, | ||
use_source = false, | ||
}) | ||
if not current_form then | ||
return | ||
end | ||
|
||
local root = lang.get_node_root(current_form) | ||
local range = { root:range() } | ||
|
||
-- stylua: ignore | ||
return { | ||
range[1], range[2], | ||
range[3], range[4], | ||
} | ||
end | ||
|
||
function M.select_around_form() | ||
local range = M.get_range_around_form() | ||
if not range then | ||
return | ||
end | ||
|
||
M.ensure_visual_mode() | ||
vim.api.nvim_win_set_cursor(0, { range[1] + 1, range[2] }) | ||
vim.api.nvim_command("normal! o") | ||
vim.api.nvim_win_set_cursor(0, { range[3] + 1, range[4] - 1 }) | ||
end | ||
|
||
function M.get_range_in_form() | ||
local lang = langs.get_language_api() | ||
local current_form = traversal.find_nearest_form(ts.get_node_at_cursor(), { | ||
lang = lang, | ||
use_source = false, | ||
}) | ||
if not current_form then | ||
return | ||
end | ||
|
||
local edges = lang.get_form_edges(current_form) | ||
|
||
-- stylua: ignore | ||
return { | ||
edges.left.range[3], edges.left.range[4], | ||
edges.right.range[1], edges.right.range[2], | ||
} | ||
end | ||
|
||
function M.select_in_form() | ||
local range = M.get_range_in_form() | ||
if not range then | ||
return | ||
end | ||
|
||
M.ensure_visual_mode() | ||
vim.api.nvim_win_set_cursor(0, { range[1] + 1, range[2] }) | ||
vim.api.nvim_command("normal! o") | ||
vim.api.nvim_win_set_cursor(0, { range[3] + 1, range[4] - 1 }) | ||
end | ||
|
||
function M.get_element_range() | ||
local lang = langs.get_language_api() | ||
local node = ts.get_node_at_cursor() | ||
if not node then | ||
return | ||
end | ||
|
||
local root = lang.get_node_root(node) | ||
local range = { root:range() } | ||
|
||
-- stylua: ignore | ||
return { | ||
range[1], range[2], | ||
range[3], range[4] | ||
} | ||
end | ||
|
||
function M.select_element() | ||
local range = M.get_element_range() | ||
if not range then | ||
return | ||
end | ||
|
||
M.ensure_visual_mode() | ||
vim.api.nvim_win_set_cursor(0, { range[1] + 1, range[2] }) | ||
vim.api.nvim_command("normal! o") | ||
vim.api.nvim_win_set_cursor(0, { range[3] + 1, range[4] }) | ||
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,102 @@ | ||
local paredit = require("nvim-paredit.api") | ||
|
||
local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer | ||
local feedkeys = require("tests.nvim-paredit.utils").feedkeys | ||
local expect = require("tests.nvim-paredit.utils").expect | ||
local utils = require("tests.nvim-paredit.utils") | ||
|
||
describe("form deletions", function() | ||
vim.api.nvim_buf_set_option(0, "filetype", "clojure") | ||
|
||
before_each(function() | ||
vim.keymap.set("o", "af", paredit.select_around_form, { buffer = true, remap = false }) | ||
vim.keymap.set("o", "if", paredit.select_in_form, { buffer = true, remap = false }) | ||
end) | ||
|
||
it("should delete the form", function() | ||
prepare_buffer({ | ||
content = "(a a)", | ||
cursor = { 1, 1 }, | ||
}) | ||
feedkeys("daf") | ||
expect({ | ||
content = "", | ||
cursor = { 1, 0 }, | ||
}) | ||
end) | ||
|
||
it("should delete a multi line form", function() | ||
prepare_buffer({ | ||
content = { "(a", "b", "c)" }, | ||
cursor = { 1, 1 }, | ||
}) | ||
feedkeys("daf") | ||
expect({ | ||
content = "", | ||
cursor = { 1, 0 }, | ||
}) | ||
end) | ||
|
||
it("should delete a nested form", function() | ||
prepare_buffer({ | ||
content = "(a (b c))", | ||
cursor = { 1, 5 }, | ||
}) | ||
feedkeys("daf") | ||
expect({ | ||
content = "(a )", | ||
cursor = { 1, 3 }, | ||
}) | ||
end) | ||
|
||
it("should delete everything in the form", function() | ||
prepare_buffer({ | ||
content = "(a b)", | ||
cursor = { 1, 2 }, | ||
}) | ||
feedkeys("dif") | ||
expect({ | ||
content = "()", | ||
cursor = { 1, 1 }, | ||
}) | ||
end) | ||
|
||
it("should delete everything within a multi line form", function() | ||
prepare_buffer({ | ||
content = { "(a", "b", "c)" }, | ||
cursor = { 2, 0 }, | ||
}) | ||
feedkeys("dif") | ||
expect({ | ||
content = "()", | ||
cursor = { 1, 1 }, | ||
}) | ||
end) | ||
end) | ||
|
||
describe("form selections", function() | ||
vim.api.nvim_buf_set_option(0, "filetype", "clojure") | ||
|
||
before_each(function() | ||
vim.keymap.set("v", "af", paredit.select_around_form, { buffer = true, remap = false }) | ||
vim.keymap.set("v", "if", paredit.select_in_form, { buffer = true, remap = false }) | ||
end) | ||
|
||
it("should select the form", function() | ||
prepare_buffer({ | ||
content = "(a a)", | ||
cursor = { 1, 1 }, | ||
}) | ||
feedkeys("vaf") | ||
assert.are.same("(a a)", utils.get_selected_text()) | ||
end) | ||
|
||
it("should select within the form", function() | ||
prepare_buffer({ | ||
content = "(a a)", | ||
cursor = { 1, 1 }, | ||
}) | ||
feedkeys("vif") | ||
assert.are.same("a a", utils.get_selected_text()) | ||
end) | ||
end) |
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