Skip to content

Commit

Permalink
[Fix clojure-emacs#3200] Add private var listing below public vars
Browse files Browse the repository at this point in the history
Adds a second "ns-vars-with-meta" nrepl call to fetch the private
variables.  Adds a new param to `cider-browse-ns--list` to be able to
provide a list of the private items and have this function format
accordingly.
  • Loading branch information
zkry committed Jun 22, 2022
1 parent 14246b8 commit 803b360
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
## Changes

* Upgrade injected `cider-nrepl` to [0.28.5](https://github.com/clojure-emacs/cider-nrepl/releases/tag/v0.28.5).
* [#3200](https://github.com/clojure-emacs/cider/issues/3200): cider-browse-ns additionally displays the list of private vars

## 1.4.1 (2022-05-25)

Expand Down
22 changes: 16 additions & 6 deletions cider-browse-ns.el
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ VAR-META is used to decide a font-lock face."
'mouse-face 'highlight
'keymap cider-browse-ns-mouse-map)))

(defun cider-browse-ns--list (buffer title items &optional ns noerase)
(defun cider-browse-ns--list (buffer title items &optional private-items ns noerase)
"Reset contents of BUFFER.
Display TITLE at the top and ITEMS are indented underneath.
Display TITLE at the top and ITEMS are indented underneath. PRIVATE-ITEMS
is displayed as a separate section under the public items.
If NS is non-nil, it is added to each item as the
`cider-browse-ns-current-ns' text property. If NOERASE is non-nil, the
contents of the buffer are not reset before inserting TITLE and ITEMS."
Expand All @@ -121,6 +122,11 @@ contents of the buffer are not reset before inserting TITLE and ITEMS."
(dolist (item items)
(insert (propertize (concat " " item "\n")
'cider-browse-ns-current-ns ns)))
(when private-items
(insert "\n Private:\n")
(dolist (item private-items)
(insert (propertize (concat " " item "\n")
'cider-browse-ns-current-ns ns))))
(goto-char (point-min)))))

(defun cider-browse-ns--first-doc-line (doc)
Expand All @@ -137,10 +143,13 @@ string is returned."
(t (concat first-line "..."))))
"Not documented."))

(defun cider-browse-ns--items (namespace)
(defun cider-browse-ns--items (namespace &optional private)
"Return the items to show in the namespace browser of the given NAMESPACE.
Each item consists of a ns-var and the first line of its docstring."
(let* ((ns-vars-with-meta (cider-sync-request:ns-vars-with-meta namespace))
Each item consists of a ns-var and the first line of its docstring. If
PRIVATE is non-nil, return only the items with :private metadata."
(let* ((ns-vars-with-meta (if private
(cider-sync-request:private-ns-vars-with-meta namespace)
(cider-sync-request:ns-vars-with-meta namespace)))
(propertized-ns-vars (nrepl-dict-map #'cider-browse-ns--properties ns-vars-with-meta)))
(mapcar (lambda (ns-var)
(let* ((doc (nrepl-dict-get-in ns-vars-with-meta (list ns-var "doc")))
Expand All @@ -160,7 +169,8 @@ Each item consists of a ns-var and the first line of its docstring."
(with-current-buffer (cider-popup-buffer cider-browse-ns-buffer 'select nil 'ancillary)
(cider-browse-ns--list (current-buffer)
namespace
(cider-browse-ns--items namespace))
(cider-browse-ns--items namespace)
(cider-browse-ns--items namespace t))
(setq-local cider-browse-ns-current-ns namespace)))

;;;###autoload
Expand Down
9 changes: 9 additions & 0 deletions cider-client.el
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,15 @@ returned."
(cider-nrepl-send-sync-request)
(nrepl-dict-get "ns-vars-with-meta")))

(defun cider-sync-request:private-ns-vars-with-meta (ns)
"Get a map of the vars in NS to its metadata information."
(thread-first `("op" "ns-vars-with-meta"
"ns" ,ns
"var-query" ,(nrepl-dict "private?" "t"
"include-meta-key" '("private")))
(cider-nrepl-send-sync-request)
(nrepl-dict-get "ns-vars-with-meta")))

(defun cider-sync-request:ns-load-all ()
"Load all project namespaces."
(thread-first '("op" "ns-load-all")
Expand Down
3 changes: 1 addition & 2 deletions cider-debug.el
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ configure `cider-debug-prompt' instead."
(seq-mapn #'cider-browse-ns--properties
(cdr list)
instrumented-meta)

ns 'noerase)
nil ns 'noerase)
(goto-char (point-max))
(insert "\n"))))
(goto-char (point-min)))
Expand Down
26 changes: 26 additions & 0 deletions test/cider-browse-ns-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'(dict "blank?"
(dict "arglists" "fn arg list"
"doc" "\"True if s is nil, empty, or contains only whitespace.\"")))
(spy-on 'cider-sync-request:private-ns-vars-with-meta :and-return-value '(dict))

(with-temp-buffer
(setq cider-browse-ns-buffer (buffer-name (current-buffer)))
Expand All @@ -59,6 +60,31 @@
(search-forward "blank")
(expect (get-text-property (point) 'font-lock-face) :to-equal 'font-lock-function-name-face)
(search-forward "True")
(expect (get-text-property (point) 'font-lock-face) :to-equal 'font-lock-doc-face)
(expect (not (search-forward "Private" nil t)))))
(it "correctly displays private vars below regular vars"
(spy-on 'cider-sync-request:ns-vars-with-meta :and-return-value
'(dict "blank?"
(dict "arglists" "fn arg list"
"doc" "\"True if s is nil, empty, or contains only whitespace.\"")))
(spy-on 'cider-sync-request:private-ns-vars-with-meta :and-return-value
'(dict "secret-blank?"
(dict "arglists" "fn arg list"
"doc" "\"False if s is nil, empty, or contains only whitespace.\"")))

(with-temp-buffer
(setq cider-browse-ns-buffer (buffer-name (current-buffer)))
(cider-browse-ns "clojure.string")
(search-forward "clojure")
(expect (get-text-property (point) 'face) :to-equal 'font-lock-type-face)
(search-forward "blank")
(expect (get-text-property (point) 'font-lock-face) :to-equal 'font-lock-function-name-face)
(search-forward "True")
(expect (get-text-property (point) 'font-lock-face) :to-equal 'font-lock-doc-face)
(search-forward "Private")
(search-forward "secret-blank")
(expect (get-text-property (point) 'font-lock-face) :to-equal 'font-lock-function-name-face)
(search-forward "False")
(expect (get-text-property (point) 'font-lock-face) :to-equal 'font-lock-doc-face))))

(describe "cider-browse-ns--first-doc-line"
Expand Down

0 comments on commit 803b360

Please sign in to comment.