Skip to content

Commit e0be542

Browse files
committed
Support client overrides for symbol extraction fixing #482 in a more generic and extensible way
1 parent a47d132 commit e0be542

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

doc/conjure.txt

+10
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,16 @@ examples: https://github.com/Olical/conjure/wiki/Client-features
10821082
want to reject any form that doesn't start with an opening
10831083
parenthesis.
10841084

1085+
`symbol-node?`
1086+
Function(node)
1087+
When tree-sitter is in use this will be called with each node
1088+
considered when looking for "symbol" nodes under the cursor with
1089+
the "eval word" pneumonic mapping.
1090+
returning `true` or `false`. For example, the Clojure client is
1091+
looking for nodes with "sym" in their type (part of the built in
1092+
shared behaviour between clients) and additionally "kwd" (short
1093+
for keyword and specialised for Clojure).
1094+
10851095
`get-form-modifier`
10861096
Function(node)
10871097
The second iteration of `form-node?`, allows the client to return

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

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
(or (ts.node-surrounded-by-form-pair-chars? node reader-macro-pairs)
4040
(ts.node-prefixed-by-chars? node reader-macros)))
4141

42+
(defn symbol-node? [node]
43+
(string.find (node:type) :kwd))
44+
4245
(def comment-node? ts.lisp-comment-node?)
4346

4447
(config.merge

fnl/conjure/tree-sitter.fnl

+3-2
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,18 @@
9393

9494
;; Some node types I've seen: sym_lit, symbol, multi_symbol...
9595
;; So I'm not sure if each language just picks a flavour, but this should cover all of our bases.
96+
;; Clients can also opt in and hint with their own symbol-node? functions now too.
9697
(defn sym? [node]
9798
(when node
9899
(or (string.find (node:type) :sym)
99-
(string.find (node:type) :kwd))))
100+
(client.optional-call :symbol-node? node))))
100101

101102
(defn get-leaf [node]
102103
"Return the leaf node under the cursor or nothing at all."
103104
(parse!)
104105

105106
(let [node (or node (ts.get_node_at_cursor))]
106-
(when (leaf? node)
107+
(when (or (leaf? node) (sym? node))
107108
(var node node)
108109
(while (sym? (parent node))
109110
(set node (parent node)))

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

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ local function form_node_3f(node)
4141
return (ts["node-surrounded-by-form-pair-chars?"](node, reader_macro_pairs) or ts["node-prefixed-by-chars?"](node, reader_macros))
4242
end
4343
_2amodule_2a["form-node?"] = form_node_3f
44+
local function symbol_node_3f(node)
45+
return string.find(node:type(), "kwd")
46+
end
47+
_2amodule_2a["symbol-node?"] = symbol_node_3f
4448
local comment_node_3f = ts["lisp-comment-node?"]
4549
_2amodule_2a["comment-node?"] = comment_node_3f
4650
config.merge({client = {clojure = {nrepl = {connection = {default_host = "localhost", port_files = {".nrepl-port", ".shadow-cljs/nrepl.port"}, auto_repl = {enabled = true, cmd = "bb nrepl-server localhost:$port", port_file = ".nrepl-port", hidden = false}}, eval = {pretty_print = true, auto_require = true, print_quota = nil, print_function = "conjure.internal/pprint", print_options = {length = 500, level = 50}, raw_out = false}, interrupt = {sample_limit = 0.3}, refresh = {after = nil, before = nil, dirs = nil}, test = {current_form_names = {"deftest"}, runner = "clojure", call_suffix = nil, raw_out = false}, 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}}}}})

lua/conjure/tree-sitter.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ end
125125
_2amodule_2a["leaf?"] = leaf_3f
126126
local function sym_3f(node)
127127
if node then
128-
return (string.find(node:type(), "sym") or string.find(node:type(), "kwd"))
128+
return (string.find(node:type(), "sym") or client["optional-call"]("symbol-node?", node))
129129
else
130130
return nil
131131
end
@@ -134,7 +134,7 @@ _2amodule_2a["sym?"] = sym_3f
134134
local function get_leaf(node)
135135
parse_21()
136136
local node0 = (node or ts.get_node_at_cursor())
137-
if leaf_3f(node0) then
137+
if (leaf_3f(node0) or sym_3f(node0)) then
138138
local node1 = node0
139139
while sym_3f(parent(node1)) do
140140
node1 = parent(node1)

0 commit comments

Comments
 (0)