-
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 #45 from julienvincent/splice_api
feat: Splice api (fixes #43)
- Loading branch information
Showing
5 changed files
with
109 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
local traversal = require("nvim-paredit.utils.traversal") | ||
local langs = require("nvim-paredit.lang") | ||
local ts = require("nvim-treesitter.ts_utils") | ||
|
||
local M = {} | ||
|
||
function M.unwrap_form(buf, form) | ||
local lang = langs.get_language_api() | ||
local edges = lang.get_form_edges(form) | ||
local left = edges.left | ||
local right = edges.right | ||
vim.api.nvim_buf_set_text( | ||
buf, | ||
right.range[1], | ||
right.range[2], | ||
right.range[3], | ||
right.range[4], | ||
{ "" } | ||
) | ||
vim.api.nvim_buf_set_text( | ||
buf, | ||
left.range[1], | ||
left.range[2], | ||
left.range[3], | ||
left.range[4], | ||
{ "" } | ||
) | ||
end | ||
|
||
function M.unwrap_form_under_cursor() | ||
local buf = vim.api.nvim_get_current_buf() | ||
local lang = langs.get_language_api() | ||
local node = ts.get_node_at_cursor() | ||
local current_element = lang.get_node_root(node) | ||
local form = traversal.find_nearest_form( | ||
current_element, | ||
{ lang = lang, use_source = false } | ||
) | ||
if not form then | ||
return false | ||
end | ||
|
||
M.unwrap_form(buf, form) | ||
return true | ||
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,56 @@ | ||
local paredit = require("nvim-paredit") | ||
local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer | ||
local expect = require("tests.nvim-paredit.utils").expect | ||
|
||
describe("form uwrap (e.g. splice)", function() | ||
vim.api.nvim_buf_set_option(0, "filetype", "clojure") | ||
local unwrap = paredit.unwrap | ||
|
||
it("should uwrap list under cursor", function() | ||
prepare_buffer({ | ||
content = { "(+ 2 :foo/bar)" }, | ||
cursor = { 1, 0 }, | ||
}) | ||
|
||
unwrap.unwrap_form_under_cursor() | ||
expect({ | ||
content = { "+ 2 :foo/bar" }, | ||
}) | ||
end) | ||
|
||
it("should uwrap list under cursor", function() | ||
prepare_buffer({ | ||
content = { "(+ 2 :foo/bar)" }, | ||
cursor = { 1, 13 }, | ||
}) | ||
|
||
unwrap.unwrap_form_under_cursor() | ||
expect({ | ||
content = { "+ 2 :foo/bar" }, | ||
}) | ||
end) | ||
|
||
it("should uwrap set under cursor", function() | ||
prepare_buffer({ | ||
content = { "#{1 2 3}" }, | ||
cursor = { 1, 4 }, | ||
}) | ||
|
||
unwrap.unwrap_form_under_cursor() | ||
expect({ | ||
content = { "1 2 3" }, | ||
}) | ||
end) | ||
|
||
it("should uwrap fn under cursor", function() | ||
prepare_buffer({ | ||
content = { "#(+ % 2 3)" }, | ||
cursor = { 1, 4 }, | ||
}) | ||
|
||
unwrap.unwrap_form_under_cursor() | ||
expect({ | ||
content = { "+ % 2 3" }, | ||
}) | ||
end) | ||
end) |