Skip to content

Commit

Permalink
fixes and tests for form api
Browse files Browse the repository at this point in the history
  • Loading branch information
armed committed Aug 20, 2023
1 parent 25df651 commit 0a600af
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lua/nvim-paredit/api/cursor.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local M = {}

function M.insert_mode()
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("i", true, true, true), "n", false)
vim.api.nvim_feedkeys("i", "n", true)
end

function M.place_cursor(form, opts)
Expand Down
14 changes: 7 additions & 7 deletions lua/nvim-paredit/api/form.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local function reparse(buf)
parser:parse()
end

local function find_element_under_cursor(lang)
function M.find_element_under_cursor(lang)
local node = ts.get_node_at_cursor()
if lang.element_lit then
return lang.element_lit(node)
Expand Down Expand Up @@ -47,7 +47,7 @@ end
function M.wrap_element_under_cursor(prefix, suffix)
local buf = vim.api.nvim_get_current_buf()
local lang = langs.get_language_api()
local current_element = find_element_under_cursor(lang)
local current_element = M.find_element_under_cursor(lang)

if not current_element then
return
Expand All @@ -70,16 +70,16 @@ end
function M.wrap_enclosing_form_under_cursor(prefix, suffix)
local buf = vim.api.nvim_get_current_buf()
local lang = langs.get_language_api()
local current_element = find_element_under_cursor(lang)
local current_element = M.find_element_under_cursor(lang)

if not current_element then
return
end

local use_parent = langs.is_whitespace_under_cursor()
local use_direct_parent = langs.is_whitespace_under_cursor() or lang.node_is_comment(ts.get_node_at_cursor())

local form
if use_parent then
if use_direct_parent then
form = M.find_form(current_element, lang)
else
form = M.find_parend_form(current_element, lang)
Expand All @@ -89,8 +89,8 @@ function M.wrap_enclosing_form_under_cursor(prefix, suffix)

reparse(buf)

current_element = find_element_under_cursor(lang)
if use_parent then
current_element = M.find_element_under_cursor(lang)
if use_direct_parent then
form = current_element
else
form = M.find_parend_form(current_element, lang)
Expand Down
9 changes: 6 additions & 3 deletions lua/nvim-paredit/lang/clojure.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ function M.get_node_root(node)
end

function M.element_lit(node)
if node:type():match("name$") then
return M.element_lit(node:parent())
local lit = node
if lit then
if not lit:type():match("lit$") then
lit = M.element_lit(lit:parent())
end
end
return node
return lit
end

function M.unwrap_form(node)
Expand Down
81 changes: 81 additions & 0 deletions tests/nvim-paredit/form_and_element_wrap_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
local paredit = require("nvim-paredit")
local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer
local expect = require("tests.nvim-paredit.utils").expect

describe("element and form wrap", function()
vim.api.nvim_buf_set_option(0, "filetype", "clojure")

it("should not wrap if cursor is whitespace", function()
prepare_buffer({
content = { "(+ 2 :foo/bar)" },
cursor = { 1, 4 },
})

paredit.form.wrap_element_under_cursor("(", ")")
expect({
content = { "(+ 2 :foo/bar)" },
})
end)

it("should wrap namespaced keyword", function()
prepare_buffer({
content = { "(+ 2 :foo/bar)" },
cursor = { 1, 7 },
})

paredit.form.wrap_element_under_cursor("(", ")")
expect({
content = { "(+ 2 (:foo/bar))" },
})
end)

it("should wrap namespaced keyword", function()
prepare_buffer({
content = { '(+ 2 "lol")' },
cursor = { 1, 7 },
})

paredit.form.wrap_element_under_cursor("(", ")")
expect({
content = { '(+ 2 ("lol"))' },
})
end)

it("should wrap enclosing form", function()
prepare_buffer({
content = {
"(+ 2",
" :foo/bar)",
},
cursor = { 2, 4 },
})

paredit.form.wrap_enclosing_form_under_cursor("(", ")")
expect({
content = {
"((+ 2",
" :foo/bar))",
},
})
end)

it("should wrap enclosing form if cursor is whitespace/comment", function()
prepare_buffer({
content = {
"(+ 2",
";; foo",
" :foo/bar)",
},
cursor = { 2, 4 },
})

paredit.form.wrap_enclosing_form_under_cursor("(", ")")
expect({
content = {
"((+ 2",
";; foo",
" :foo/bar))",
},
})
end)
end)

0 comments on commit 0a600af

Please sign in to comment.