diff --git a/fnl/formedit/find.fnl b/fnl/formedit/find.fnl index d727c08..eb29eca 100644 --- a/fnl/formedit/find.fnl +++ b/fnl/formedit/find.fnl @@ -6,17 +6,7 @@ :set_lit true :anon_fn_lit true}) -(local start-offset {:list_lit 0 - :vec_lit 0 - :map_lit 0 - :set_lit 1 - :anon_fn_lit 1}) - -(local insert-offset {:list_lit 1 - :vec_lit 1 - :map_lit 1 - :set_lit 2 - :anon_fn_lit 2}) +(local offset {:list_lit 1 :vec_lit 1 :map_lit 1 :set_lit 2 :anon_fn_lit 2}) ;; Tree search (fn find-current-form [node] @@ -49,4 +39,4 @@ (let [node (ts.get_node_at_cursor)] (find-root-form node))) -{: form : root : insert-offset : start-offset : first : last} +{: form : root : offset : first : last} diff --git a/fnl/formedit/insertion.fnl b/fnl/formedit/insertion.fnl index 97f9cc2..7ba18c8 100644 --- a/fnl/formedit/insertion.fnl +++ b/fnl/formedit/insertion.fnl @@ -2,9 +2,9 @@ (fn head [] (let [form (find.form) - insert-offset (. find.insert-offset (form:type)) + offset (. find.offset (form:type)) [start-row start-col] [(form:start)] - start-col (+ start-col insert-offset)] + start-col (+ start-col offset)] (vim.api.nvim_buf_set_text 0 start-row start-col start-row start-col [" "]) (vim.api.nvim_win_set_cursor 0 [(+ start-row 1) (+ start-col 1)]) (vim.api.nvim_feedkeys :i :n true))) diff --git a/fnl/formedit/select.fnl b/fnl/formedit/select.fnl index d57d274..672c9a8 100644 --- a/fnl/formedit/select.fnl +++ b/fnl/formedit/select.fnl @@ -9,4 +9,20 @@ (let [form (find.root)] (ts.update_selection 0 form))) -{: form : root} +(fn inner-select [form] + (let [[start-row start-col end-row end-col] [(form:range)] + type (form:type) + offset (. find.offset type)] + (vim.api.nvim_win_set_cursor 0 [(+ start-row 1) (+ start-col offset)]) + (vim.api.nvim_command "normal! v") + (vim.api.nvim_win_set_cursor 0 [(+ end-row 1) (- end-col 2)]))) + +(fn inner [] + (let [form (find.form)] + (inner-select form))) + +(fn inner-root [] + (let [form (find.root)] + (inner-select form))) + +{: form : root : inner : inner-root} diff --git a/fnl/formedit/slurp.fnl b/fnl/formedit/slurp.fnl index 2bc3160..e0eca1a 100644 --- a/fnl/formedit/slurp.fnl +++ b/fnl/formedit/slurp.fnl @@ -17,7 +17,7 @@ (let [form (find.form) [row col] [(form:start)] type (form:type) - offset (. find.insert-offset type) + offset (. find.offset type) col (+ col offset) sibling (form:prev_sibling) sibling-text (ts.get_node_text sibling 0) diff --git a/fnl/formedit/wrap.fnl b/fnl/formedit/wrap.fnl index dc283f6..4be38bf 100644 --- a/fnl/formedit/wrap.fnl +++ b/fnl/formedit/wrap.fnl @@ -2,13 +2,11 @@ (fn head [] (let [form (find.form) - insert-offset (. find.insert-offset (form:type)) + offset (. find.offset (form:type)) [start-row start-col end-row end-col] [(form:range)]] (vim.api.nvim_buf_set_text 0 end-row end-col end-row end-col [")"]) (vim.api.nvim_buf_set_text 0 start-row start-col start-row start-col ["( "]) - (vim.api.nvim_win_set_cursor 0 - [(+ start-row 1) - (+ start-col insert-offset 1)]) + (vim.api.nvim_win_set_cursor 0 [(+ start-row 1) (+ start-col offset 1)]) (vim.api.nvim_feedkeys :i :n true))) {: head} diff --git a/fnl/spec/delete_inner_spec.fnl b/fnl/spec/delete_inner_spec.fnl new file mode 100644 index 0000000..be62190 --- /dev/null +++ b/fnl/spec/delete_inner_spec.fnl @@ -0,0 +1,33 @@ +(local {: describe : it : before_each} (require :plenary.busted)) +(local h (require :spec.helper)) +(local select (require :formedit.select)) + +(describe "delete inner form" + (fn [] + (before_each (fn [] + (vim.keymap.set :o :if select.inner))) + (it :list + (fn [] + (h.setup {:content "(1)" :cursor [1 0]}) + (h.feedkeys :dif) + (h.expect {:content "()" :cursor [1 1]}))) + (it :set + (fn [] + (h.setup {:content "#{1}" :cursor [1 0]}) + (h.feedkeys :dif) + (h.expect {:content "#{}" :cursor [1 2]}))))) + +(describe "delete inner root form" + (fn [] + (before_each (fn [] + (vim.keymap.set :o :iF select.inner-root))) + (it "within first node" + (fn [] + (h.setup {:content "([1] 2)" :cursor [1 1]}) + (h.feedkeys :diF) + (h.expect {:content "()" :cursor [1 1]}))) + (it "within second node" + (fn [] + (h.setup {:content "(1 [2])" :cursor [1 3]}) + (h.feedkeys :diF) + (h.expect {:content "()" :cursor [1 1]}))))) diff --git a/lua/formedit/find.lua b/lua/formedit/find.lua index 369aee0..5b1b32d 100644 --- a/lua/formedit/find.lua +++ b/lua/formedit/find.lua @@ -1,8 +1,7 @@ -- [nfnl] Compiled from fnl/formedit/find.fnl by https://github.com/Olical/nfnl, do not edit. local ts = require("nvim-treesitter.ts_utils") local forms = {list_lit = true, vec_lit = true, map_lit = true, set_lit = true, anon_fn_lit = true} -local start_offset = {list_lit = 0, vec_lit = 0, map_lit = 0, set_lit = 1, anon_fn_lit = 1} -local insert_offset = {list_lit = 1, vec_lit = 1, map_lit = 1, set_lit = 2, anon_fn_lit = 2} +local offset = {list_lit = 1, vec_lit = 1, map_lit = 1, set_lit = 2, anon_fn_lit = 2} local function find_current_form(node) if forms[node:type()] then return node @@ -45,4 +44,4 @@ local function root() local node = ts.get_node_at_cursor() return find_root_form(node) end -return {form = form, root = root, ["insert-offset"] = insert_offset, ["start-offset"] = start_offset, first = first, last = last} +return {form = form, root = root, offset = offset, first = first, last = last} diff --git a/lua/formedit/insertion.lua b/lua/formedit/insertion.lua index 8061444..ce9c4c0 100644 --- a/lua/formedit/insertion.lua +++ b/lua/formedit/insertion.lua @@ -2,11 +2,11 @@ local find = require("formedit.find") local function head() local form = find.form() - local insert_offset = (find["insert-offset"])[form:type()] + local offset = find.offset[form:type()] local _let_1_ = {form:start()} local start_row = _let_1_[1] local start_col = _let_1_[2] - local start_col0 = (start_col + insert_offset) + local start_col0 = (start_col + offset) vim.api.nvim_buf_set_text(0, start_row, start_col0, start_row, start_col0, {" "}) vim.api.nvim_win_set_cursor(0, {(start_row + 1), (start_col0 + 1)}) return vim.api.nvim_feedkeys("i", "n", true) diff --git a/lua/formedit/select.lua b/lua/formedit/select.lua index f469f00..f0bbea5 100644 --- a/lua/formedit/select.lua +++ b/lua/formedit/select.lua @@ -9,4 +9,24 @@ local function root() local form0 = find.root() return ts.update_selection(0, form0) end -return {form = form, root = root} +local function inner_select(form0) + local _let_1_ = {form0:range()} + local start_row = _let_1_[1] + local start_col = _let_1_[2] + local end_row = _let_1_[3] + local end_col = _let_1_[4] + local type = form0:type() + local offset = find.offset[type] + vim.api.nvim_win_set_cursor(0, {(start_row + 1), (start_col + offset)}) + vim.api.nvim_command("normal! v") + return vim.api.nvim_win_set_cursor(0, {(end_row + 1), (end_col - 2)}) +end +local function inner() + local form0 = find.form() + return inner_select(form0) +end +local function inner_root() + local form0 = find.root() + return inner_select(form0) +end +return {form = form, root = root, inner = inner, ["inner-root"] = inner_root} diff --git a/lua/formedit/slurp.lua b/lua/formedit/slurp.lua index 6a5ea9d..dbe2f2e 100644 --- a/lua/formedit/slurp.lua +++ b/lua/formedit/slurp.lua @@ -24,7 +24,7 @@ local function backward() local row = _let_3_[1] local col = _let_3_[2] local type = form:type() - local offset = (find["insert-offset"])[type] + local offset = find.offset[type] local col0 = (col + offset) local sibling = form:prev_sibling() local sibling_text = ts.get_node_text(sibling, 0) diff --git a/lua/formedit/wrap.lua b/lua/formedit/wrap.lua index 0b61552..4956c97 100644 --- a/lua/formedit/wrap.lua +++ b/lua/formedit/wrap.lua @@ -2,7 +2,7 @@ local find = require("formedit.find") local function head() local form = find.form() - local insert_offset = (find["insert-offset"])[form:type()] + local offset = find.offset[form:type()] local _let_1_ = {form:range()} local start_row = _let_1_[1] local start_col = _let_1_[2] @@ -10,7 +10,7 @@ local function head() local end_col = _let_1_[4] vim.api.nvim_buf_set_text(0, end_row, end_col, end_row, end_col, {")"}) vim.api.nvim_buf_set_text(0, start_row, start_col, start_row, start_col, {"( "}) - vim.api.nvim_win_set_cursor(0, {(start_row + 1), (start_col + insert_offset + 1)}) + vim.api.nvim_win_set_cursor(0, {(start_row + 1), (start_col + offset + 1)}) return vim.api.nvim_feedkeys("i", "n", true) end return {head = head} diff --git a/lua/spec/delete_inner_spec.lua b/lua/spec/delete_inner_spec.lua new file mode 100644 index 0000000..af549c8 --- /dev/null +++ b/lua/spec/delete_inner_spec.lua @@ -0,0 +1,45 @@ +-- [nfnl] Compiled from fnl/spec/delete_inner_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 select = require("formedit.select") +local function _2_() + local function _3_() + return vim.keymap.set("o", "if", select.inner) + end + before_each(_3_) + local function _4_() + h.setup({content = "(1)", cursor = {1, 0}}) + h.feedkeys("dif") + return h.expect({content = "()", cursor = {1, 1}}) + end + it("list", _4_) + local function _5_() + h.setup({content = "#{1}", cursor = {1, 0}}) + h.feedkeys("dif") + return h.expect({content = "#{}", cursor = {1, 2}}) + end + return it("set", _5_) +end +describe("delete inner form", _2_) +local function _6_() + local function _7_() + return vim.keymap.set("o", "iF", select["inner-root"]) + end + before_each(_7_) + local function _8_() + h.setup({content = "([1] 2)", cursor = {1, 1}}) + h.feedkeys("diF") + return h.expect({content = "()", cursor = {1, 1}}) + end + it("within first node", _8_) + local function _9_() + h.setup({content = "(1 [2])", cursor = {1, 3}}) + h.feedkeys("diF") + return h.expect({content = "()", cursor = {1, 1}}) + end + return it("within second node", _9_) +end +return describe("delete inner root form", _6_)