Skip to content

Commit

Permalink
Support client overrides for symbol extraction fixing #482 in a more …
Browse files Browse the repository at this point in the history
…generic and extensible way
  • Loading branch information
Olical committed Apr 17, 2023
1 parent a47d132 commit e0be542
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
10 changes: 10 additions & 0 deletions doc/conjure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,16 @@ examples: https://github.com/Olical/conjure/wiki/Client-features
want to reject any form that doesn't start with an opening
parenthesis.

`symbol-node?`
Function(node)
When tree-sitter is in use this will be called with each node
considered when looking for "symbol" nodes under the cursor with
the "eval word" pneumonic mapping.
returning `true` or `false`. For example, the Clojure client is
looking for nodes with "sym" in their type (part of the built in
shared behaviour between clients) and additionally "kwd" (short
for keyword and specialised for Clojure).

`get-form-modifier`
Function(node)
The second iteration of `form-node?`, allows the client to return
Expand Down
3 changes: 3 additions & 0 deletions fnl/conjure/client/clojure/nrepl/init.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
(or (ts.node-surrounded-by-form-pair-chars? node reader-macro-pairs)
(ts.node-prefixed-by-chars? node reader-macros)))

(defn symbol-node? [node]
(string.find (node:type) :kwd))

(def comment-node? ts.lisp-comment-node?)

(config.merge
Expand Down
5 changes: 3 additions & 2 deletions fnl/conjure/tree-sitter.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,18 @@

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

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

(let [node (or node (ts.get_node_at_cursor))]
(when (leaf? node)
(when (or (leaf? node) (sym? node))
(var node node)
(while (sym? (parent node))
(set node (parent node)))
Expand Down
4 changes: 4 additions & 0 deletions lua/conjure/client/clojure/nrepl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ local function form_node_3f(node)
return (ts["node-surrounded-by-form-pair-chars?"](node, reader_macro_pairs) or ts["node-prefixed-by-chars?"](node, reader_macros))
end
_2amodule_2a["form-node?"] = form_node_3f
local function symbol_node_3f(node)
return string.find(node:type(), "kwd")
end
_2amodule_2a["symbol-node?"] = symbol_node_3f
local comment_node_3f = ts["lisp-comment-node?"]
_2amodule_2a["comment-node?"] = comment_node_3f
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}}}}})
Expand Down
4 changes: 2 additions & 2 deletions lua/conjure/tree-sitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ end
_2amodule_2a["leaf?"] = leaf_3f
local function sym_3f(node)
if node then
return (string.find(node:type(), "sym") or string.find(node:type(), "kwd"))
return (string.find(node:type(), "sym") or client["optional-call"]("symbol-node?", node))
else
return nil
end
Expand All @@ -134,7 +134,7 @@ _2amodule_2a["sym?"] = sym_3f
local function get_leaf(node)
parse_21()
local node0 = (node or ts.get_node_at_cursor())
if leaf_3f(node0) then
if (leaf_3f(node0) or sym_3f(node0)) then
local node1 = node0
while sym_3f(parent(node1)) do
node1 = parent(node1)
Expand Down

0 comments on commit e0be542

Please sign in to comment.