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: Splice api (fixes #43) #45

Merged
merged 5 commits into from
Oct 22, 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ paredit.setup({

-- list of default keybindings
keys = {
["<localleader>@"] = { paredit.unwrap.unwrap_form_under_cursor, "Splice sexp" },
[">)"] = { paredit.api.slurp_forwards, "Slurp forwards" },
[">("] = { paredit.api.barf_backwards, "Barf backwards" },

Expand Down Expand Up @@ -327,7 +328,7 @@ Cursor api `paredit.cursor`
- `placement` - enumeration `left_edge`,`inner_start`,`inner_end`,`right_edge`
- `mode` - currently only `insert` is supported, defaults to `normal`

## API usage recipes
## Additional API usage recipes

### `vim-sexp` wrap form (head/tail) replication

Expand Down
47 changes: 47 additions & 0 deletions lua/nvim-paredit/api/unwrap.lua
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
3 changes: 3 additions & 0 deletions lua/nvim-paredit/defaults.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
local api = require("nvim-paredit.api")
local unwrap = require("nvim-paredit.api.unwrap")

local M = {}

M.default_keys = {
["<localleader>@"] = { unwrap.unwrap_form_under_cursor, "Splice sexp", },

[">)"] = { api.slurp_forwards, "Slurp forwards" },
[">("] = { api.barf_backwards, "Barf backwards" },

Expand Down
1 change: 1 addition & 0 deletions lua/nvim-paredit/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ end
local M = {
api = require("nvim-paredit.api"),
wrap = require("nvim-paredit.api.wrap"),
unwrap = require("nvim-paredit.api.unwrap"),
cursor = require("nvim-paredit.api.cursor"),
extension = {
add_language_extension = add_language_extension,
Expand Down
56 changes: 56 additions & 0 deletions tests/nvim-paredit/form_unwrap_spec.lua
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)
Loading