Skip to content

Commit

Permalink
Find root form
Browse files Browse the repository at this point in the history
  • Loading branch information
grierson committed Aug 16, 2023
1 parent ec13f76 commit fda5b86
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Paredit in fennel
./scripts/nfnl # To download nfnl
```

[ ] - inner select
[ ] - root select
[ ] - slurp/barf
[ ] - inner select - Get all named value children on form
[ ] - root select - Is form and on column 0
[ ] - slurp/barf - ?

## Unlicensed

Expand Down
2 changes: 2 additions & 0 deletions docs/sample.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
[1 2 3]
#{1 2 3 (+ 2 (+ 1 2))}

(+ 1 (+ 2 3 4))

(defn foo [x] x)
(defn bar [x] x)
9 changes: 6 additions & 3 deletions fnl/formedit/find.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
(when parent (find-current-form parent)))))

(fn find-root-form [node]
(if (node:parent)
(find-root-form (node:parent))
node))
(let [[_ col] [(node:start)]
type (node:type)]
(if (and (. forms type) (= col 0))
node
(when (node:parent)
(find-root-form (node:parent))))))

;; Find node
(fn form []
Expand Down
11 changes: 8 additions & 3 deletions fnl/spec/delete_around_spec.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@
(h.setup {:content "(+ 1 (+ 2 3))" :cursor [1 7]})
(h.feedkeys :daF)
(h.expect {:content "" :cursor [1 0]})))
(it "cursor in sub form deletes root"
(it "only deletes root form that cursor is in"
(fn []
(h.setup {:content "(+ 1 (+ 2 3))" :cursor [1 7]})
(h.setup {:content ["(+ 1 2)" "(+ 3 4)"] :cursor [1 0]})
(h.feedkeys :daF)
(h.expect {:content ["" "(+ 3 4)"] :cursor [1 0]})))
(it "only deletes root form that cursor is in second form"
(fn []
(h.setup {:content ["(+ 1 2)" "(+ 3 4)"] :cursor [2 0]})
(h.feedkeys :daF)
(h.expect {:content "" :cursor [1 0]})))))
(h.expect {:content ["(+ 1 2)" ""] :cursor [2 0]})))))
7 changes: 5 additions & 2 deletions fnl/spec/helper.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

(fn expect [{: cursor : content}]
(when content
(assert.are.same content
(core.first (vim.api.nvim_buf_get_lines 0 0 10 false))))
(assert.are.same content (let [text (vim.api.nvim_buf_get_lines 0 0 10
false)]
(if (= (core.count text) 1)
(core.first text)
text))))
(when cursor
(assert.are.same cursor (vim.api.nvim_win_get_cursor 0))))

Expand Down
14 changes: 11 additions & 3 deletions lua/formedit/find.lua

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

12 changes: 9 additions & 3 deletions lua/spec/delete_around_spec.lua

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

10 changes: 9 additions & 1 deletion lua/spec/helper.lua

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

0 comments on commit fda5b86

Please sign in to comment.