Skip to content

Commit

Permalink
Delete around
Browse files Browse the repository at this point in the history
  • Loading branch information
grierson committed Aug 12, 2023
1 parent 93b0880 commit a0a116d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 14 deletions.
14 changes: 12 additions & 2 deletions fnl/formedit/selection.fnl
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
(local tree (require :formedit.tree))
(local ts (require :nvim-treesitter.ts_utils))

(fn delete-form []
(fn delete-around-form []
(let [node (ts.get_node_at_cursor)
form (tree.get-form node)
[start-row start-col end-row end-col] [(form:range)]]
(vim.api.nvim_buf_set_text 0 start-row start-col end-row end-col {})))

{: delete-form}
(fn delete-inner-form [node]
(let [node (ts.get_node_at_cursor)
form (tree.get-form node)
[start-row start-col end-row end-col] [(form:range)]
offset (if (or (= :set_lit (form:type)) (= :anon_fn_lit (form:type)))
2
1)]
(vim.api.nvim_buf_set_text 0 start-row (+ start-col offset) end-row
(- end-col 1) {})))

{: delete-around-form : delete-inner-form}
41 changes: 37 additions & 4 deletions fnl/spec/selection_spec.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,53 @@
(it :list
(fn []
(h.setup {:content "(1)" :cursor [1 1]})
(selection.delete-form)
(selection.delete-around-form)
(h.expect {:content "" :cursor [1 0]})))
(it :vector
(fn []
(h.setup {:content "[1]" :cursor [1 1]})
(selection.delete-form)
(selection.delete-around-form)
(h.expect {:content "" :cursor [1 0]})))
(it :set
(fn []
(h.setup {:content "#{1}" :cursor [1 1]})
(selection.delete-form)
(selection.delete-around-form)
(h.expect {:content "" :cursor [1 0]})))
(it "anonymous function"
(fn []
(h.setup {:content "#(fn [x] x)" :cursor [1 1]})
(selection.delete-form)
(selection.delete-around-form)
(h.expect {:content "" :cursor [1 0]})))
(it "delete multiple children"
(fn []
(h.setup {:content "([1] [2])" :cursor [1 0]})
(selection.delete-around-form)
(h.expect {:content "" :cursor [1 0]})))))

(describe "delete inner"
(fn []
(it :list
(fn []
(h.setup {:content "(1)" :cursor [1 1]})
(selection.delete-inner-form)
(h.expect {:content "()" :cursor [1 1]})))
(it :vector
(fn []
(h.setup {:content "[1]" :cursor [1 1]})
(selection.delete-inner-form)
(h.expect {:content "[]" :cursor [1 1]})))
(it :set
(fn []
(h.setup {:content "#{1}" :cursor [1 1]})
(selection.delete-inner-form)
(h.expect {:content "#{}" :cursor [1 1]})))
(it "anonymous function"
(fn []
(h.setup {:content "#(fn [x] x)" :cursor [1 1]})
(selection.delete-inner-form)
(h.expect {:content "#()" :cursor [1 1]})))
(it "delete multiple children"
(fn []
(h.setup {:content "([1] [2])" :cursor [1 0]})
(selection.delete-inner-form)
(h.expect {:content "()" :cursor [1 0]})))))
20 changes: 18 additions & 2 deletions lua/formedit/selection.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 45 additions & 6 deletions lua/spec/selection_spec.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a0a116d

Please sign in to comment.