Skip to content

Commit 3e80273

Browse files
committed
Merge pull request #799 from vitoshka/dict-api
Expand nREPL dict api and apply throughout
2 parents 7a3b345 + 34fe3bf commit 3e80273

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

cider-doc.el

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,9 @@ Tables are marked to be ignored by line wrap."
351351
(newline))
352352
(let ((beg (point-min))
353353
(end (point-max)))
354-
(cl-loop for x on info by #'cddr
355-
do (put-text-property beg end (car x) (cadr x)))))
354+
(nrepl-dict-map (lambda (k v)
355+
(put-text-property beg end k v))
356+
info)))
356357
(current-buffer))))
357358

358359
(defun cider-docview-render (buffer symbol info)

cider-eldoc.el

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@ POS is the index of current argument."
9696
"Return the arglist for THING."
9797
(when (nrepl-op-supported-p "info")
9898
(let* ((var-info (cider-var-info thing t))
99-
(candidates (cdr (nrepl-dict-get var-info "candidates"))))
100-
(if candidates
101-
(->> (cl-loop for x on candidates by #'cddr
102-
collect (nrepl-dict-get (cadr x) "arglists-str"))
103-
(-map 'read)
104-
-flatten
105-
-distinct)
106-
(let ((arglists (nrepl-dict-get var-info "arglists-str")))
107-
(when arglists
108-
(read arglists)))))))
99+
(candidates (nrepl-dict-get var-info "candidates")))
100+
(if (nrepl-dict-empty-p candidates)
101+
(let ((arglists (nrepl-dict-get var-info "arglists-str")))
102+
(when arglists
103+
(read arglists)))
104+
(->> candidates
105+
(nrepl-dict-map (lambda (_ v) (nrepl-dict-get v "arglists-str") ))
106+
(-map 'read)
107+
-flatten
108+
-distinct)))))
109109

110110
(defun cider-eldoc ()
111111
"Backend function for eldoc to show argument list in the echo area."

nrepl-client.el

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ To be used for tooling calls (i.e. completion, eldoc, etc)")
235235
(and (listp object)
236236
(eq (car object) 'dict)))
237237

238+
(defun nrepl-dict-empty-p (dict)
239+
"Return t if nREPL dict is empty."
240+
(null (cdr dict)))
241+
238242
(defun nrepl-dict-get (dict key)
239243
"Get from DICT value associated with KEY.
240244
If dict is nil, return nil."
@@ -254,12 +258,27 @@ Return new dict. Dict is modified by side effects."
254258
dict)))
255259

256260
(defun nrepl-dict-keys (dict)
257-
"Return all the keys in the nREPL dict."
261+
"Return all the keys in the nREPL DICT."
258262
(if (nrepl-dict-p dict)
259263
(cl-loop for l on (cdr dict) by #'cddr
260264
collect (car l))
261265
(error "Not a nREPL dict.")))
262266

267+
(defun nrepl-dict-vals (dict)
268+
"Return all the values in the nREPL DICT."
269+
(if (nrepl-dict-p dict)
270+
(cl-loop for l on (cdr dict) by #'cddr
271+
collect (cadr l))
272+
(error "Not a nREPL dict.")))
273+
274+
(defun nrepl-dict-map (fn dict)
275+
"Map FN on nREPL DICT.
276+
FN must accept two arguments key and value."
277+
(if (nrepl-dict-p dict)
278+
(cl-loop for l on (cdr dict) by #'cddr
279+
collect (funcall fn (car l) (cadr l)))
280+
(error "Not a nREPL dict.")))
281+
263282
(defun nrepl--cons (car list-or-dict)
264283
"Generic cons of CAR to LIST-OR-DICT."
265284
(if (eq (car list-or-dict) 'dict)
@@ -591,7 +610,7 @@ Falls back to `nrepl-port' if not found."
591610
"Destructure an nREPL RESPONSE dict.
592611
Bind the value of the provided KEYS and execute BODY."
593612
`(let ,(cl-loop for key in keys
594-
collect `(,key (lax-plist-get (cdr ,response) ,(format "%s" key))))
613+
collect `(,key (nrepl-dict-get ,response ,(format "%s" key))))
595614
,@body))
596615
(put 'nrepl-dbind-response 'lisp-indent-function 2)
597616

test/nrepl-bencode-tests.el

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ If object is incomplete, return a decoded path."
288288

289289
(ert-deftest test-nrepl-dict ()
290290
(should (equal '(dict (23 . 44) (2 . 3) (3 . 4) (4 . 5))
291-
(nrepl--cons '(23 . 44) '(dict (2 . 3) (3 . 4) (4 . 5 )))))
291+
(nrepl--cons '(23 . 44) '(dict (2 . 3) (3 . 4) (4 . 5)))))
292292
(should (equal '((34))
293293
(nrepl--push 34 '(()))))
294294
(should (equal '(((34)) (1 2 3))
@@ -300,7 +300,14 @@ If object is incomplete, return a decoded path."
300300
(should (equal '(((34) 1) (2))
301301
(nrepl--push '(34) '((1) (2)))))
302302
(should (equal '((dict 34 a b) (2))
303-
(nrepl--push 34 '((dict a b) (2))))))
303+
(nrepl--push 34 '((dict a b) (2)))))
304+
(should (equal '((3 . 4) nil)
305+
(nrepl-dict-vals '(dict (2 . 3) (3 . 4) (4 . 5)))))
306+
(should (equal '((2 . 3) (4 . 5))
307+
(nrepl-dict-keys '(dict (2 . 3) (3 . 4) (4 . 5)))))
308+
(should (equal '(1 5 9)
309+
(nrepl-dict-map (lambda (k v) (+ k v))
310+
'(dict 0 1 2 3 4 5)))))
304311

305312

306313
;; benchmarks

0 commit comments

Comments
 (0)