diff --git a/docs/sample.clj b/docs/sample.clj index 872ad80..b9dff49 100644 --- a/docs/sample.clj +++ b/docs/sample.clj @@ -1,2 +1,5 @@ -(defn foo [x y] - (+ x (+ y 1))) +(+ 1 2 3) +[1 2 3] +'(1 2 3) +#{1 2 3} +{:a 1 :b 2} diff --git a/fnl/formedit/tree.fnl b/fnl/formedit/select.fnl similarity index 76% rename from fnl/formedit/tree.fnl rename to fnl/formedit/select.fnl index 198fd73..80f1dff 100644 --- a/fnl/formedit/tree.fnl +++ b/fnl/formedit/select.fnl @@ -1,5 +1,8 @@ (local ts (require :nvim-treesitter.ts_utils)) +(local query + "[(list_lit) (quoting_lit) (vec_lit) (set_lit) (anon_fn_lit) (map_lit)] @form") + (local forms {:list_lit true :vec_lit true :map_lit true @@ -18,7 +21,8 @@ form)) (fn select-form [] - (let [form (get-current-form)] + (let [form (get-current-form) + _ (print (form:type))] (ts.update_selection 0 form))) {: get-current-form : select-form} diff --git a/fnl/spec/delete_around_spec.fnl b/fnl/spec/delete_around_spec.fnl index 1abe644..a2bea63 100644 --- a/fnl/spec/delete_around_spec.fnl +++ b/fnl/spec/delete_around_spec.fnl @@ -1,11 +1,11 @@ (local {: describe : it : before_each} (require :plenary.busted)) (local h (require :spec.helper)) -(local tree (require :formedit.tree)) +(local select (require :formedit.select)) (describe "delete around" (fn [] (before_each (fn [] - (vim.keymap.set :o :af tree.select-form))) + (vim.keymap.set :o :af select.select-form))) (it :list (fn [] (h.setup {:content "(1)" :cursor [1 1]}) diff --git a/fnl/spec/delete_inside1_spec.fnl b/fnl/spec/delete_inside1_spec.fnl index 85cd3b8..1c31d68 100644 --- a/fnl/spec/delete_inside1_spec.fnl +++ b/fnl/spec/delete_inside1_spec.fnl @@ -1,11 +1,11 @@ (local {: describe : it : before_each} (require :plenary.busted)) (local h (require :spec.helper)) -(local tree (require :formedit.tree)) +(local select (require :formedit.select)) (describe "delete inside" (fn [] (before_each (fn [] - (vim.keymap.set :o :if tree.select-form))) + (vim.keymap.set :o :if select.select-form))) (it :list (fn [] (h.setup {:content "(1)" :cursor [1 0]}) diff --git a/fnl/spec/delete_inside_spec.fnl b/fnl/spec/delete_inside_spec.fnl index d95de06..b7c3c44 100644 --- a/fnl/spec/delete_inside_spec.fnl +++ b/fnl/spec/delete_inside_spec.fnl @@ -1,11 +1,11 @@ (local {: describe : it : before_each} (require :plenary.busted)) (local h (require :spec.helper)) -(local tree (require :formedit.tree)) +(local select (require :formedit.select)) (describe "delete inside" (fn [] (before_each (fn [] - (vim.keymap.set :o :if tree.select-form))) + (vim.keymap.set :o :if select.select-form))) (it :list (fn [] (h.setup {:content "(1)" :cursor [1 0]}) diff --git a/fnl/spec/tree_spec.fnl b/fnl/spec/tree_spec.fnl index 9079d75..8e979dd 100644 --- a/fnl/spec/tree_spec.fnl +++ b/fnl/spec/tree_spec.fnl @@ -1,6 +1,6 @@ (local {: describe : it} (require :plenary.busted)) (local h (require :spec.helper)) -(local tree (require :formedit.tree)) +(local select (require :formedit.select)) (local assert (require :luassert.assert)) (describe "No form return nil" @@ -8,7 +8,7 @@ (it "cursor start of form" (fn [] (h.setup {:content " " :cursor [1 1]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.falsy actual)))))) (describe :List @@ -17,17 +17,17 @@ (it "cursor start of form" (fn [] (h.setup {:content "(a)" :cursor [1 1]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type))))) (it "cursor within form" (fn [] (h.setup {:content "(a)" :cursor [1 2]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type))))) (it "cursor end of form" (fn [] (h.setup {:content "(a)" :cursor [1 3]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type)))))))) (describe :Vector @@ -36,17 +36,17 @@ (it "cursor start of form" (fn [] (h.setup {:content "[a]" :cursor [1 1]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type))))) (it "cursor within form" (fn [] (h.setup {:content "[a]" :cursor [1 2]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type))))) (it "cursor end of form" (fn [] (h.setup {:content "[a]" :cursor [1 3]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type)))))))) (describe :Map @@ -55,17 +55,17 @@ (it "cursor start of form" (fn [] (h.setup {:content "{:a 1}" :cursor [1 1]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type))))) (it "cursor within form" (fn [] (h.setup {:content "{:a 1}" :cursor [1 2]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type))))) (it "cursor end of form" (fn [] (h.setup {:content "{:a 1}" :cursor [1 6]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type)))))))) (describe :Set @@ -74,22 +74,22 @@ (it "cursor on #" (fn [] (h.setup {:content "#{1}" :cursor [1 1]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type))))) (it "cursor start of form" (fn [] (h.setup {:content "#{1}" :cursor [1 2]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type))))) (it "cursor within form" (fn [] (h.setup {:content "#{1}" :cursor [1 3]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type))))) (it "cursor end of form" (fn [] (h.setup {:content "#{1}" :cursor [1 4]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type)))))))) (describe "Anonymous function" @@ -98,20 +98,20 @@ (it "cursor on #" (fn [] (h.setup {:content "#(1)" :cursor [1 1]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type))))) (it "cursor start of form" (fn [] (h.setup {:content "#(1)" :cursor [1 2]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type))))) (it "cursor within form" (fn [] (h.setup {:content "#(1)" :cursor [1 3]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type))))) (it "cursor end of form" (fn [] (h.setup {:content "#(1)" :cursor [1 4]}) - (let [actual (tree.get-current-form)] + (let [actual (select.get-current-form)] (assert.equals form-type (actual:type)))))))) diff --git a/lua/formedit/select.lua b/lua/formedit/select.lua new file mode 100644 index 0000000..2c9c252 --- /dev/null +++ b/lua/formedit/select.lua @@ -0,0 +1,27 @@ +-- [nfnl] Compiled from fnl/formedit/select.fnl by https://github.com/Olical/nfnl, do not edit. +local ts = require("nvim-treesitter.ts_utils") +local query = "[(list_lit) (quoting_lit) (vec_lit) (set_lit) (anon_fn_lit) (map_lit)] @form" +local forms = {list_lit = true, vec_lit = true, map_lit = true, set_lit = true, anon_fn_lit = true} +local function get_form(node) + if forms[node:type()] then + return node + else + local parent = node:parent() + if parent then + return get_form(parent) + else + return nil + end + end +end +local function get_current_form() + local node = ts.get_node_at_cursor() + local form = get_form(node) + return form +end +local function select_form() + local form = get_current_form() + local _ = print(form:type()) + return ts.update_selection(0, form) +end +return {["get-current-form"] = get_current_form, ["select-form"] = select_form} diff --git a/lua/spec/delete_around_spec.lua b/lua/spec/delete_around_spec.lua index 4dc4e59..41bd483 100644 --- a/lua/spec/delete_around_spec.lua +++ b/lua/spec/delete_around_spec.lua @@ -4,10 +4,10 @@ local describe = _local_1_["describe"] local it = _local_1_["it"] local before_each = _local_1_["before_each"] local h = require("spec.helper") -local tree = require("formedit.tree") +local select = require("formedit.select") local function _2_() local function _3_() - return vim.keymap.set("o", "af", tree["select-form"]) + return vim.keymap.set("o", "af", select["select-form"]) end before_each(_3_) local function _4_() diff --git a/lua/spec/delete_inside1_spec.lua b/lua/spec/delete_inside1_spec.lua index 315bb1b..01ca832 100644 --- a/lua/spec/delete_inside1_spec.lua +++ b/lua/spec/delete_inside1_spec.lua @@ -4,10 +4,10 @@ local describe = _local_1_["describe"] local it = _local_1_["it"] local before_each = _local_1_["before_each"] local h = require("spec.helper") -local tree = require("formedit.tree") +local select = require("formedit.select") local function _2_() local function _3_() - return vim.keymap.set("o", "if", tree["select-form"]) + return vim.keymap.set("o", "if", select["select-form"]) end before_each(_3_) local function _4_() diff --git a/lua/spec/delete_inside_spec.lua b/lua/spec/delete_inside_spec.lua index 6a67b3a..5296699 100644 --- a/lua/spec/delete_inside_spec.lua +++ b/lua/spec/delete_inside_spec.lua @@ -4,10 +4,10 @@ local describe = _local_1_["describe"] local it = _local_1_["it"] local before_each = _local_1_["before_each"] local h = require("spec.helper") -local tree = require("formedit.tree") +local select = require("formedit.select") local function _2_() local function _3_() - return vim.keymap.set("o", "if", tree["select-form"]) + return vim.keymap.set("o", "if", select["select-form"]) end before_each(_3_) local function _4_() diff --git a/lua/spec/tree_spec.lua b/lua/spec/tree_spec.lua index f1a9d4b..bfc8045 100644 --- a/lua/spec/tree_spec.lua +++ b/lua/spec/tree_spec.lua @@ -3,12 +3,12 @@ local _local_1_ = require("plenary.busted") local describe = _local_1_["describe"] local it = _local_1_["it"] local h = require("spec.helper") -local tree = require("formedit.tree") +local select = require("formedit.select") local assert = require("luassert.assert") local function _2_() local function _3_() h.setup({content = " ", cursor = {1, 1}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.falsy(actual) end return it("cursor start of form", _3_) @@ -19,19 +19,19 @@ local function _4_(...) local function _5_() local function _6_() h.setup({content = "(a)", cursor = {1, 1}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end it("cursor start of form", _6_) local function _7_() h.setup({content = "(a)", cursor = {1, 2}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end it("cursor within form", _7_) local function _8_() h.setup({content = "(a)", cursor = {1, 3}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end return it("cursor end of form", _8_) @@ -44,19 +44,19 @@ local function _9_(...) local function _10_() local function _11_() h.setup({content = "[a]", cursor = {1, 1}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end it("cursor start of form", _11_) local function _12_() h.setup({content = "[a]", cursor = {1, 2}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end it("cursor within form", _12_) local function _13_() h.setup({content = "[a]", cursor = {1, 3}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end return it("cursor end of form", _13_) @@ -69,19 +69,19 @@ local function _14_(...) local function _15_() local function _16_() h.setup({content = "{:a 1}", cursor = {1, 1}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end it("cursor start of form", _16_) local function _17_() h.setup({content = "{:a 1}", cursor = {1, 2}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end it("cursor within form", _17_) local function _18_() h.setup({content = "{:a 1}", cursor = {1, 6}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end return it("cursor end of form", _18_) @@ -94,25 +94,25 @@ local function _19_(...) local function _20_() local function _21_() h.setup({content = "#{1}", cursor = {1, 1}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end it("cursor on #", _21_) local function _22_() h.setup({content = "#{1}", cursor = {1, 2}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end it("cursor start of form", _22_) local function _23_() h.setup({content = "#{1}", cursor = {1, 3}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end it("cursor within form", _23_) local function _24_() h.setup({content = "#{1}", cursor = {1, 4}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end return it("cursor end of form", _24_) @@ -125,25 +125,25 @@ local function _25_(...) local function _26_() local function _27_() h.setup({content = "#(1)", cursor = {1, 1}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end it("cursor on #", _27_) local function _28_() h.setup({content = "#(1)", cursor = {1, 2}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end it("cursor start of form", _28_) local function _29_() h.setup({content = "#(1)", cursor = {1, 3}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end it("cursor within form", _29_) local function _30_() h.setup({content = "#(1)", cursor = {1, 4}}) - local actual = tree["get-current-form"]() + local actual = select["get-current-form"]() return assert.equals(form_type, actual:type()) end return it("cursor end of form", _30_) diff --git a/queries/clojure/textobjects.scm b/queries/clojure/textobjects.scm new file mode 100644 index 0000000..74652f2 --- /dev/null +++ b/queries/clojure/textobjects.scm @@ -0,0 +1,9 @@ +([(list_lit) + (quoting_lit) + (vec_lit) + (set_lit) + (anon_fn_lit) + (map_lit)] @form.outer) + + +(list_lit (_) @form.inner)