Skip to content

Commit

Permalink
slurp
Browse files Browse the repository at this point in the history
  • Loading branch information
grierson committed Aug 16, 2023
1 parent fda5b86 commit b798583
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions fnl/formedit/slurp.fnl
Original file line number Diff line number Diff line change
@@ -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}
37 changes: 37 additions & 0 deletions fnl/spec/slurp_spec.fnl
Original file line number Diff line number Diff line change
@@ -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]})))))
38 changes: 38 additions & 0 deletions lua/formedit/slurp.lua

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

47 changes: 47 additions & 0 deletions lua/spec/slurp_spec.lua

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

0 comments on commit b798583

Please sign in to comment.