Skip to content

Commit

Permalink
Delete inner and around form
Browse files Browse the repository at this point in the history
  • Loading branch information
grierson committed Aug 12, 2023
1 parent b5c78ea commit 4777c68
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 136 deletions.
18 changes: 5 additions & 13 deletions fnl/formedit/selection.fnl
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
(local tree (require :formedit.tree))
(local ts (require :nvim-treesitter.ts_utils))

(fn delete-around-form []
(let [node (ts.get_node_at_cursor)
form (tree.get-form node)
(let [form (tree.get-current-form)
[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 {})))

(fn delete-inner-form [node]
(let [node (ts.get_node_at_cursor)
form (tree.get-form node)
(fn delete-inner-form []
(let [form (tree.get-current-form)
[start-row start-col end-row end-col] [(form:range)]
text (vim.api.nvim_buf_get_text 0 start-row start-col end-row end-col
{})
_ (print "Inner form text")
_ (print (vim.inspect text))
_ (print :range)
_ (print (vim.inspect [(form:range)]))
buffer 0
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
(vim.api.nvim_buf_set_text buffer start-row (+ start-col offset) end-row
(- end-col 1) {})))

{: delete-around-form : delete-inner-form}
9 changes: 8 additions & 1 deletion fnl/formedit/tree.fnl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
(local ts (require :nvim-treesitter.ts_utils))

(local forms {:list_lit true
:vec_lit true
:map_lit true
Expand All @@ -10,4 +12,9 @@
(let [parent (node:parent)]
(when parent (get-form parent)))))

{: get-form}
(fn get-current-form []
(let [node (ts.get_node_at_cursor)
form (get-form node)]
form))

{: get-current-form}
23 changes: 18 additions & 5 deletions fnl/spec/delete_inner_spec.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
(fn []
(it :list
(fn []
(h.setup {:content "(1)" :cursor [1 1]})
(h.setup {:content "(1)" :cursor [1 0]})
(selection.delete-inner-form)
(h.expect {:content "()" :cursor [1 1]})))
(h.expect {:content "()" :cursor [1 0]})))
(it :vector
(fn []
(h.setup {:content "[1]" :cursor [1 1]})
Expand All @@ -28,9 +28,22 @@
(fn []
(h.setup {:content "([1] [2])" :cursor [1 0]})
(selection.delete-inner-form)
(h.expect {:content "()" :cursor [1 0]})))))

(describe "delete inner across lines"
(fn []
(it :list
(fn []
(h.setup {:content ["(1" "2)"] :cursor [1 0]})
(selection.delete-inner-form)
(h.expect {:content "()" :cursor [1 0]})))
(it "delete across lines"
(it :vector
(fn []
(h.setup {:content ["[1" "2]"] :cursor [1 0]})
(selection.delete-inner-form)
(h.expect {:content "[]" :cursor [1 0]})))
(it :set
(fn []
(h.setup {:content ["([1]," "[2])"] :cursor [1 0]})
(h.setup {:content ["#{1" "2}"] :cursor [1 0]})
(selection.delete-inner-form)
(h.expect {:content "()" :cursor [1 0] :lines 2})))))
(h.expect {:content "#{}" :cursor [1 0]})))))
17 changes: 6 additions & 11 deletions fnl/spec/helper.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@
(vim.api.nvim_win_set_cursor 0 cursor)
(: (vim.treesitter.get_parser 0) :parse)))

(fn expect [{: cursor : content : lines}]
(let [lines (or lines 0)
_ (print lines)
foo (vim.api.nvim_buf_get_lines 0 lines -1 false)
_ (print "found text in expect")
_ (print (vim.inspect foo))]
(when content
(assert.are.same content
(core.first (vim.api.nvim_buf_get_lines 0 lines -1 false))))
(when cursor
(assert.are.same cursor (vim.api.nvim_win_get_cursor 0)))))
(fn expect [{: cursor : content}]
(when content
(assert.are.same content
(core.first (vim.api.nvim_buf_get_lines 0 0 10 false))))
(when cursor
(assert.are.same cursor (vim.api.nvim_win_get_cursor 0))))

{: setup : expect}
4 changes: 1 addition & 3 deletions fnl/spec/selection_spec.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
(local h (require :spec.helper))
(local tree (require :formedit.tree))
(local assert (require :luassert.assert))
(local ts (require :nvim-treesitter.ts_utils))

(describe :selection
(fn []
(it :list
(fn []
(h.setup {:content "(a)" :cursor [1 1]})
(let [node (ts.get_node_at_cursor)
form (tree.get-form node)]
(let [form (tree.get-current-form)]
(assert.are.same [0 0 0 3] [(form:range)]))))))
54 changes: 18 additions & 36 deletions fnl/spec/tree_spec.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
(it "cursor start of form"
(fn []
(h.setup {:content " " :cursor [1 1]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.falsy actual))))))

(describe :List
Expand All @@ -19,20 +18,17 @@
(it "cursor start of form"
(fn []
(h.setup {:content "(a)" :cursor [1 1]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type)))))
(it "cursor within form"
(fn []
(h.setup {:content "(a)" :cursor [1 2]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type)))))
(it "cursor end of form"
(fn []
(h.setup {:content "(a)" :cursor [1 3]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type))))))))

(describe :Vector
Expand All @@ -41,20 +37,17 @@
(it "cursor start of form"
(fn []
(h.setup {:content "[a]" :cursor [1 1]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type)))))
(it "cursor within form"
(fn []
(h.setup {:content "[a]" :cursor [1 2]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type)))))
(it "cursor end of form"
(fn []
(h.setup {:content "[a]" :cursor [1 3]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type))))))))

(describe :Map
Expand All @@ -63,20 +56,17 @@
(it "cursor start of form"
(fn []
(h.setup {:content "{:a 1}" :cursor [1 1]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type)))))
(it "cursor within form"
(fn []
(h.setup {:content "{:a 1}" :cursor [1 2]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type)))))
(it "cursor end of form"
(fn []
(h.setup {:content "{:a 1}" :cursor [1 6]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type))))))))

(describe :Set
Expand All @@ -85,26 +75,22 @@
(it "cursor on #"
(fn []
(h.setup {:content "#{1}" :cursor [1 1]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type)))))
(it "cursor start of form"
(fn []
(h.setup {:content "#{1}" :cursor [1 2]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [ actual (tree.get-current-form)]
(assert.equals form-type (actual:type)))))
(it "cursor within form"
(fn []
(h.setup {:content "#{1}" :cursor [1 3]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type)))))
(it "cursor end of form"
(fn []
(h.setup {:content "#{1}" :cursor [1 4]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type))))))))

(describe "Anonymous function"
Expand All @@ -113,24 +99,20 @@
(it "cursor on #"
(fn []
(h.setup {:content "#(1)" :cursor [1 1]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type)))))
(it "cursor start of form"
(fn []
(h.setup {:content "#(1)" :cursor [1 2]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type)))))
(it "cursor within form"
(fn []
(h.setup {:content "#(1)" :cursor [1 3]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type)))))
(it "cursor end of form"
(fn []
(h.setup {:content "#(1)" :cursor [1 4]})
(let [node (ts.get_node_at_cursor)
actual (tree.get-form node)]
(let [actual (tree.get-current-form)]
(assert.equals form-type (actual:type))))))))
17 changes: 5 additions & 12 deletions lua/formedit/selection.lua

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

8 changes: 7 additions & 1 deletion lua/formedit/tree.lua

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

31 changes: 23 additions & 8 deletions lua/spec/delete_inner_spec.lua

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

8 changes: 1 addition & 7 deletions lua/spec/helper.lua

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

Loading

0 comments on commit 4777c68

Please sign in to comment.