diff --git a/README.md b/README.md index 3fd88eb..dc0ea4f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ Paredit in fennel ``` [ ] - inner select - Get all named value children on form -[ ] - root select - Is form and on column 0 [ ] - slurp/barf - ? ## Unlicensed diff --git a/fnl/formedit/slurp.fnl b/fnl/formedit/slurp.fnl new file mode 100644 index 0000000..73eaea4 --- /dev/null +++ b/fnl/formedit/slurp.fnl @@ -0,0 +1,28 @@ +(local find (require :formedit.find)) +(local ts (require :vim.treesitter)) + +(fn forward [] + (let [form (find.form) + [row col] [(form:end_)] + col (- col 1) + sibling (form:next_sibling) + sibling-text (ts.get_node_text sibling 0) + sibling-text (.. " " sibling-text) + [sibling-row-start sibling-col-start sibling-row-end sibling-col-end] [(sibling:range)]] + (vim.api.nvim_buf_set_text 0 sibling-row-start (- sibling-col-start 1) + sibling-row-end sibling-col-end [""]) + (vim.api.nvim_buf_set_text 0 row col row col [sibling-text]))) + +(fn backward [] + (let [form (find.form) + [row col] [(form:start)] + sibling (form:prev_sibling) + col (+ col 1) + sibling-text (ts.get_node_text sibling 0) + sibling-text (.. sibling-text " ") + [sibling-row-start sibling-col-start sibling-row-end sibling-col-end] [(sibling:range)]] + (vim.api.nvim_buf_set_text 0 row col row col [sibling-text]) + (vim.api.nvim_buf_set_text 0 sibling-row-start sibling-col-start + sibling-row-end (+ sibling-col-end 1) [""]))) + +{: forward : backward} diff --git a/fnl/spec/slurp_spec.fnl b/fnl/spec/slurp_spec.fnl new file mode 100644 index 0000000..e800664 --- /dev/null +++ b/fnl/spec/slurp_spec.fnl @@ -0,0 +1,37 @@ +(local {: describe : it : before_each} (require :plenary.busted)) +(local h (require :spec.helper)) +(local slurp (require :formedit.slurp)) + +(local forward-keymap ">)") +(local backward-keymap "<(") + +(describe "slurp forward" + (fn [] + (before_each (fn [] + (vim.keymap.set :n forward-keymap slurp.forward))) + (it :element + (fn [] + (h.setup {:content "((1) 2)" :cursor [1 1]}) + (h.feedkeys forward-keymap) + (h.expect {:content "((1 2))" :cursor [1 1]}))) + (it :form + (fn [] + (h.setup {:content "((1) (+ 2 3))" :cursor [1 1]}) + (h.feedkeys forward-keymap) + (h.expect {:content "((1 (+ 2 3)))" :cursor [1 1]}))))) + + +(describe "slurp backwards" + (fn [] + (before_each (fn [] + (vim.keymap.set :n backward-keymap slurp.backward))) + (it :element + (fn [] + (h.setup {:content "(1 (2))" :cursor [1 3]}) + (h.feedkeys backward-keymap) + (h.expect {:content "((1 2))" :cursor [1 3]}))) + (it :form + (fn [] + (h.setup {:content "((1) (+ 2 3))" :cursor [1 5]}) + (h.feedkeys backward-keymap) + (h.expect {:content "(((1) + 2 3))" :cursor [1 5]}))))) diff --git a/lua/formedit/slurp.lua b/lua/formedit/slurp.lua new file mode 100644 index 0000000..944268c --- /dev/null +++ b/lua/formedit/slurp.lua @@ -0,0 +1,38 @@ +-- [nfnl] Compiled from fnl/formedit/slurp.fnl by https://github.com/Olical/nfnl, do not edit. +local find = require("formedit.find") +local ts = require("vim.treesitter") +local function forward() + local form = find.form() + local _let_1_ = {form:end_()} + local row = _let_1_[1] + local col = _let_1_[2] + local col0 = (col - 1) + local sibling = form:next_sibling() + local sibling_text = ts.get_node_text(sibling, 0) + local sibling_text0 = (" " .. sibling_text) + local _let_2_ = {sibling:range()} + local sibling_row_start = _let_2_[1] + local sibling_col_start = _let_2_[2] + local sibling_row_end = _let_2_[3] + local sibling_col_end = _let_2_[4] + vim.api.nvim_buf_set_text(0, sibling_row_start, (sibling_col_start - 1), sibling_row_end, sibling_col_end, {""}) + return vim.api.nvim_buf_set_text(0, row, col0, row, col0, {sibling_text0}) +end +local function backward() + local form = find.form() + local _let_3_ = {form:start()} + local row = _let_3_[1] + local col = _let_3_[2] + local sibling = form:prev_sibling() + local col0 = (col + 1) + local sibling_text = ts.get_node_text(sibling, 0) + local sibling_text0 = (sibling_text .. " ") + local _let_4_ = {sibling:range()} + local sibling_row_start = _let_4_[1] + local sibling_col_start = _let_4_[2] + local sibling_row_end = _let_4_[3] + local sibling_col_end = _let_4_[4] + vim.api.nvim_buf_set_text(0, row, col0, row, col0, {sibling_text0}) + return vim.api.nvim_buf_set_text(0, sibling_row_start, sibling_col_start, sibling_row_end, (sibling_col_end + 1), {""}) +end +return {forward = forward, backward = backward} diff --git a/lua/spec/slurp_spec.lua b/lua/spec/slurp_spec.lua new file mode 100644 index 0000000..a7e946a --- /dev/null +++ b/lua/spec/slurp_spec.lua @@ -0,0 +1,47 @@ +-- [nfnl] Compiled from fnl/spec/slurp_spec.fnl by https://github.com/Olical/nfnl, do not edit. +local _local_1_ = require("plenary.busted") +local describe = _local_1_["describe"] +local it = _local_1_["it"] +local before_each = _local_1_["before_each"] +local h = require("spec.helper") +local slurp = require("formedit.slurp") +local forward_keymap = ">)" +local backward_keymap = "<(" +local function _2_() + local function _3_() + return vim.keymap.set("n", forward_keymap, slurp.forward) + end + before_each(_3_) + local function _4_() + h.setup({content = "((1) 2)", cursor = {1, 1}}) + h.feedkeys(forward_keymap) + return h.expect({content = "((1 2))", cursor = {1, 1}}) + end + it("element", _4_) + local function _5_() + h.setup({content = "((1) (+ 2 3))", cursor = {1, 1}}) + h.feedkeys(forward_keymap) + return h.expect({content = "((1 (+ 2 3)))", cursor = {1, 1}}) + end + return it("form", _5_) +end +describe("slurp forward", _2_) +local function _6_() + local function _7_() + return vim.keymap.set("n", backward_keymap, slurp.backward) + end + before_each(_7_) + local function _8_() + h.setup({content = "(1 (2))", cursor = {1, 3}}) + h.feedkeys(backward_keymap) + return h.expect({content = "((1 2))", cursor = {1, 3}}) + end + it("element", _8_) + local function _9_() + h.setup({content = "((1) (+ 2 3))", cursor = {1, 5}}) + h.feedkeys(backward_keymap) + return h.expect({content = "(((1) + 2 3))", cursor = {1, 5}}) + end + return it("form", _9_) +end +return describe("slurp backwards", _6_)