Skip to content

Commit d88a3c8

Browse files
committed
Support evaluating Clojure sets, reader conditionals, inline functions and quotes (to some extent) with tree sitter
1 parent 2c42367 commit d88a3c8

File tree

7 files changed

+53
-12
lines changed

7 files changed

+53
-12
lines changed

dev/clojure/src/dev/sandbox.cljc

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
[a b]
1515
(+ a b))
1616

17+
#{:a :b :c}
18+
#?(:clj :hi-clojure :cljs :hi-cljs)
19+
#(+ 1 %)
20+
'(+ 10 20)
21+
`(+ 10 20)
22+
1723
(println "\033[0;31mHello, World!\033[0m")
1824

1925
{:xyz

fnl/conjure/client/clojure/nrepl/init.fnl

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
bridge conjure.bridge
66
eval conjure.eval
77
str conjure.aniseed.string
8+
text conjure.text
89
config conjure.config
910
action conjure.client.clojure.nrepl.action
1011
server conjure.client.clojure.nrepl.server
@@ -15,7 +16,20 @@
1516
(def buf-suffix ".cljc")
1617
(def comment-prefix "; ")
1718
(def- cfg (config.get-in-fn [:client :clojure :nrepl]))
18-
(def form-node? ts.node-surrounded-by-form-pair-chars?)
19+
20+
(def- reader-macro-pairs
21+
[["#{" "}"]
22+
["#(" ")"]
23+
["#?(" ")"]
24+
["'(" ")"]
25+
["'[" "]"]
26+
["'{" "}"]
27+
["`(" ")"]
28+
["`[" "]"]
29+
["`{" "}"]])
30+
31+
(defn form-node? [node]
32+
(ts.node-surrounded-by-form-pair-chars? node reader-macro-pairs))
1933

2034
(config.merge
2135
{:client

fnl/conjure/client/fennel/aniseed.fnl

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
extract conjure.extract
1313
ts conjure.tree-sitter}})
1414

15+
(defn form-node? [node]
16+
(ts.node-surrounded-by-form-pair-chars? node [["#(" ")"]]))
17+
1518
(def buf-suffix ".fnl")
1619
(def context-pattern "%(%s*module%s+(.-)[%s){]")
1720
(def comment-prefix "; ")
18-
(def form-node? ts.node-surrounded-by-form-pair-chars?)
1921

2022
(config.merge
2123
{:client

fnl/conjure/tree-sitter.fnl

+8-3
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,18 @@
7373
(when (leaf? node)
7474
node)))
7575

76-
(defn node-surrounded-by-form-pair-chars? [node]
77-
(let [first-and-last-chars (text.first-and-last-chars
78-
(node->str node))]
76+
(defn node-surrounded-by-form-pair-chars? [node extra-pairs]
77+
(let [node-str (node->str node)
78+
first-and-last-chars (text.first-and-last-chars node-str)]
7979
(or (a.some
8080
(fn [[start end]]
8181
(= first-and-last-chars (.. start end)))
8282
(config.get-in [:extract :form_pairs]))
83+
(a.some
84+
(fn [[start end]]
85+
(and (text.starts-with node-str start)
86+
(text.ends-with node-str end)))
87+
extra-pairs)
8388
false)))
8489

8590
(defn get-form [node]

lua/conjure/client/clojure/nrepl/init.lua

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ do
1111
_2amodule_locals_2a = (_2amodule_2a)["aniseed/locals"]
1212
end
1313
local autoload = (require("conjure.aniseed.autoload")).autoload
14-
local a, action, bridge, client, config, eval, mapping, nvim, parse, server, str, ts = autoload("conjure.aniseed.core"), autoload("conjure.client.clojure.nrepl.action"), autoload("conjure.bridge"), autoload("conjure.client"), autoload("conjure.config"), autoload("conjure.eval"), autoload("conjure.mapping"), autoload("conjure.aniseed.nvim"), autoload("conjure.client.clojure.nrepl.parse"), autoload("conjure.client.clojure.nrepl.server"), autoload("conjure.aniseed.string"), autoload("conjure.tree-sitter")
14+
local a, action, bridge, client, config, eval, mapping, nvim, parse, server, str, text, ts = autoload("conjure.aniseed.core"), autoload("conjure.client.clojure.nrepl.action"), autoload("conjure.bridge"), autoload("conjure.client"), autoload("conjure.config"), autoload("conjure.eval"), autoload("conjure.mapping"), autoload("conjure.aniseed.nvim"), autoload("conjure.client.clojure.nrepl.parse"), autoload("conjure.client.clojure.nrepl.server"), autoload("conjure.aniseed.string"), autoload("conjure.text"), autoload("conjure.tree-sitter")
1515
do end (_2amodule_locals_2a)["a"] = a
1616
_2amodule_locals_2a["action"] = action
1717
_2amodule_locals_2a["bridge"] = bridge
@@ -23,14 +23,19 @@ _2amodule_locals_2a["nvim"] = nvim
2323
_2amodule_locals_2a["parse"] = parse
2424
_2amodule_locals_2a["server"] = server
2525
_2amodule_locals_2a["str"] = str
26+
_2amodule_locals_2a["text"] = text
2627
_2amodule_locals_2a["ts"] = ts
2728
local buf_suffix = ".cljc"
2829
_2amodule_2a["buf-suffix"] = buf_suffix
2930
local comment_prefix = "; "
3031
_2amodule_2a["comment-prefix"] = comment_prefix
3132
local cfg = config["get-in-fn"]({"client", "clojure", "nrepl"})
3233
do end (_2amodule_locals_2a)["cfg"] = cfg
33-
local form_node_3f = ts["node-surrounded-by-form-pair-chars?"]
34+
local reader_macro_pairs = {{"#{", "}"}, {"#(", ")"}, {"#?(", ")"}, {"'(", ")"}, {"'[", "]"}, {"'{", "}"}, {"`(", ")"}, {"`[", "]"}, {"`{", "}"}}
35+
_2amodule_locals_2a["reader-macro-pairs"] = reader_macro_pairs
36+
local function form_node_3f(node)
37+
return ts["node-surrounded-by-form-pair-chars?"](node, reader_macro_pairs)
38+
end
3439
_2amodule_2a["form-node?"] = form_node_3f
3540
config.merge({client = {clojure = {nrepl = {connection = {default_host = "localhost", port_files = {".nrepl-port", ".shadow-cljs/nrepl.port"}, auto_repl = {enabled = true, hidden = false, cmd = "bb nrepl-server localhost:8794", port_file = ".nrepl-port", port = "8794"}}, eval = {pretty_print = true, raw_out = false, auto_require = true, print_quota = nil, print_function = "conjure.internal/pprint", print_options = {length = 500, level = 50}}, interrupt = {sample_limit = 0.3}, refresh = {after = nil, before = nil, dirs = nil}, test = {current_form_names = {"deftest"}, raw_out = false, runner = "clojure", call_suffix = nil}, mapping = {disconnect = "cd", connect_port_file = "cf", interrupt = "ei", last_exception = "ve", result_1 = "v1", result_2 = "v2", result_3 = "v3", view_source = "vs", session_clone = "sc", session_fresh = "sf", session_close = "sq", session_close_all = "sQ", session_list = "sl", session_next = "sn", session_prev = "sp", session_select = "ss", run_all_tests = "ta", run_current_ns_tests = "tn", run_alternate_ns_tests = "tN", run_current_test = "tc", refresh_changed = "rr", refresh_all = "ra", refresh_clear = "rc"}, completion = {cljs = {use_suitable = true}, with_context = false}}}}})
3641
local function context(header)

lua/conjure/client/fennel/aniseed.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ _2amodule_locals_2a["str"] = str
2424
_2amodule_locals_2a["text"] = text
2525
_2amodule_locals_2a["ts"] = ts
2626
_2amodule_locals_2a["view"] = view
27+
local function form_node_3f(node)
28+
return ts["node-surrounded-by-form-pair-chars?"](node, {{"#(", ")"}})
29+
end
30+
_2amodule_2a["form-node?"] = form_node_3f
2731
local buf_suffix = ".fnl"
2832
_2amodule_2a["buf-suffix"] = buf_suffix
2933
local context_pattern = "%(%s*module%s+(.-)[%s){]"
3034
_2amodule_2a["context-pattern"] = context_pattern
3135
local comment_prefix = "; "
3236
_2amodule_2a["comment-prefix"] = comment_prefix
33-
local form_node_3f = ts["node-surrounded-by-form-pair-chars?"]
34-
_2amodule_2a["form-node?"] = form_node_3f
3537
config.merge({client = {fennel = {aniseed = {mapping = {run_buf_tests = "tt", run_all_tests = "ta", reset_repl = "rr", reset_all_repls = "ra"}, aniseed_module_prefix = "conjure.aniseed.", use_metadata = true}}}})
3638
local cfg = config["get-in-fn"]({"client", "fennel", "aniseed"})
3739
do end (_2amodule_locals_2a)["cfg"] = cfg

lua/conjure/tree-sitter.lua

+10-3
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,22 @@ local function get_leaf(node)
106106
end
107107
end
108108
_2amodule_2a["get-leaf"] = get_leaf
109-
local function node_surrounded_by_form_pair_chars_3f(node)
110-
local first_and_last_chars = text["first-and-last-chars"](node__3estr(node))
109+
local function node_surrounded_by_form_pair_chars_3f(node, extra_pairs)
110+
local node_str = node__3estr(node)
111+
local first_and_last_chars = text["first-and-last-chars"](node_str)
111112
local function _14_(_12_)
112113
local _arg_13_ = _12_
113114
local start = _arg_13_[1]
114115
local _end = _arg_13_[2]
115116
return (first_and_last_chars == (start .. _end))
116117
end
117-
return (a.some(_14_, config["get-in"]({"extract", "form_pairs"})) or false)
118+
local function _17_(_15_)
119+
local _arg_16_ = _15_
120+
local start = _arg_16_[1]
121+
local _end = _arg_16_[2]
122+
return (text["starts-with"](node_str, start) and text["ends-with"](node_str, _end))
123+
end
124+
return (a.some(_14_, config["get-in"]({"extract", "form_pairs"})) or a.some(_17_, extra_pairs) or false)
118125
end
119126
_2amodule_2a["node-surrounded-by-form-pair-chars?"] = node_surrounded_by_form_pair_chars_3f
120127
local function get_form(node)

0 commit comments

Comments
 (0)