Skip to content

Commit

Permalink
Support evaluating Clojure sets, reader conditionals, inline function…
Browse files Browse the repository at this point in the history
…s and quotes (to some extent) with tree sitter
  • Loading branch information
Olical committed May 28, 2022
1 parent 2c42367 commit d88a3c8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
6 changes: 6 additions & 0 deletions dev/clojure/src/dev/sandbox.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
[a b]
(+ a b))

#{:a :b :c}
#?(:clj :hi-clojure :cljs :hi-cljs)
#(+ 1 %)
'(+ 10 20)
`(+ 10 20)

(println "\033[0;31mHello, World!\033[0m")

{:xyz
Expand Down
16 changes: 15 additions & 1 deletion fnl/conjure/client/clojure/nrepl/init.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
bridge conjure.bridge
eval conjure.eval
str conjure.aniseed.string
text conjure.text
config conjure.config
action conjure.client.clojure.nrepl.action
server conjure.client.clojure.nrepl.server
Expand All @@ -15,7 +16,20 @@
(def buf-suffix ".cljc")
(def comment-prefix "; ")
(def- cfg (config.get-in-fn [:client :clojure :nrepl]))
(def form-node? ts.node-surrounded-by-form-pair-chars?)

(def- reader-macro-pairs
[["#{" "}"]
["#(" ")"]
["#?(" ")"]
["'(" ")"]
["'[" "]"]
["'{" "}"]
["`(" ")"]
["`[" "]"]
["`{" "}"]])

(defn form-node? [node]
(ts.node-surrounded-by-form-pair-chars? node reader-macro-pairs))

(config.merge
{:client
Expand Down
4 changes: 3 additions & 1 deletion fnl/conjure/client/fennel/aniseed.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
extract conjure.extract
ts conjure.tree-sitter}})

(defn form-node? [node]
(ts.node-surrounded-by-form-pair-chars? node [["#(" ")"]]))

(def buf-suffix ".fnl")
(def context-pattern "%(%s*module%s+(.-)[%s){]")
(def comment-prefix "; ")
(def form-node? ts.node-surrounded-by-form-pair-chars?)

(config.merge
{:client
Expand Down
11 changes: 8 additions & 3 deletions fnl/conjure/tree-sitter.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,18 @@
(when (leaf? node)
node)))

(defn node-surrounded-by-form-pair-chars? [node]
(let [first-and-last-chars (text.first-and-last-chars
(node->str node))]
(defn node-surrounded-by-form-pair-chars? [node extra-pairs]
(let [node-str (node->str node)
first-and-last-chars (text.first-and-last-chars node-str)]
(or (a.some
(fn [[start end]]
(= first-and-last-chars (.. start end)))
(config.get-in [:extract :form_pairs]))
(a.some
(fn [[start end]]
(and (text.starts-with node-str start)
(text.ends-with node-str end)))
extra-pairs)
false)))

(defn get-form [node]
Expand Down
9 changes: 7 additions & 2 deletions lua/conjure/client/clojure/nrepl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ do
_2amodule_locals_2a = (_2amodule_2a)["aniseed/locals"]
end
local autoload = (require("conjure.aniseed.autoload")).autoload
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")
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")
do end (_2amodule_locals_2a)["a"] = a
_2amodule_locals_2a["action"] = action
_2amodule_locals_2a["bridge"] = bridge
Expand All @@ -23,14 +23,19 @@ _2amodule_locals_2a["nvim"] = nvim
_2amodule_locals_2a["parse"] = parse
_2amodule_locals_2a["server"] = server
_2amodule_locals_2a["str"] = str
_2amodule_locals_2a["text"] = text
_2amodule_locals_2a["ts"] = ts
local buf_suffix = ".cljc"
_2amodule_2a["buf-suffix"] = buf_suffix
local comment_prefix = "; "
_2amodule_2a["comment-prefix"] = comment_prefix
local cfg = config["get-in-fn"]({"client", "clojure", "nrepl"})
do end (_2amodule_locals_2a)["cfg"] = cfg
local form_node_3f = ts["node-surrounded-by-form-pair-chars?"]
local reader_macro_pairs = {{"#{", "}"}, {"#(", ")"}, {"#?(", ")"}, {"'(", ")"}, {"'[", "]"}, {"'{", "}"}, {"`(", ")"}, {"`[", "]"}, {"`{", "}"}}
_2amodule_locals_2a["reader-macro-pairs"] = reader_macro_pairs
local function form_node_3f(node)
return ts["node-surrounded-by-form-pair-chars?"](node, reader_macro_pairs)
end
_2amodule_2a["form-node?"] = form_node_3f
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}}}}})
local function context(header)
Expand Down
6 changes: 4 additions & 2 deletions lua/conjure/client/fennel/aniseed.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ _2amodule_locals_2a["str"] = str
_2amodule_locals_2a["text"] = text
_2amodule_locals_2a["ts"] = ts
_2amodule_locals_2a["view"] = view
local function form_node_3f(node)
return ts["node-surrounded-by-form-pair-chars?"](node, {{"#(", ")"}})
end
_2amodule_2a["form-node?"] = form_node_3f
local buf_suffix = ".fnl"
_2amodule_2a["buf-suffix"] = buf_suffix
local context_pattern = "%(%s*module%s+(.-)[%s){]"
_2amodule_2a["context-pattern"] = context_pattern
local comment_prefix = "; "
_2amodule_2a["comment-prefix"] = comment_prefix
local form_node_3f = ts["node-surrounded-by-form-pair-chars?"]
_2amodule_2a["form-node?"] = form_node_3f
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}}}})
local cfg = config["get-in-fn"]({"client", "fennel", "aniseed"})
do end (_2amodule_locals_2a)["cfg"] = cfg
Expand Down
13 changes: 10 additions & 3 deletions lua/conjure/tree-sitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,22 @@ local function get_leaf(node)
end
end
_2amodule_2a["get-leaf"] = get_leaf
local function node_surrounded_by_form_pair_chars_3f(node)
local first_and_last_chars = text["first-and-last-chars"](node__3estr(node))
local function node_surrounded_by_form_pair_chars_3f(node, extra_pairs)
local node_str = node__3estr(node)
local first_and_last_chars = text["first-and-last-chars"](node_str)
local function _14_(_12_)
local _arg_13_ = _12_
local start = _arg_13_[1]
local _end = _arg_13_[2]
return (first_and_last_chars == (start .. _end))
end
return (a.some(_14_, config["get-in"]({"extract", "form_pairs"})) or false)
local function _17_(_15_)
local _arg_16_ = _15_
local start = _arg_16_[1]
local _end = _arg_16_[2]
return (text["starts-with"](node_str, start) and text["ends-with"](node_str, _end))
end
return (a.some(_14_, config["get-in"]({"extract", "form_pairs"})) or a.some(_17_, extra_pairs) or false)
end
_2amodule_2a["node-surrounded-by-form-pair-chars?"] = node_surrounded_by_form_pair_chars_3f
local function get_form(node)
Expand Down

0 comments on commit d88a3c8

Please sign in to comment.