diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | cider-browse-ns.el | 30 | ||||
-rw-r--r-- | test/cider-browse-ns-tests.el | 25 |
3 files changed, 51 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 575ecd0c..c701ea69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ * Rebind `cider-eval-last-sexp-and-replace` to `C-c C-v w`. * Rebind `cider-eval-region` to `C-c C-v r`. * Rebind `cider-eval-ns-form` to `C-c C-v n`. +* [#1577](https://github.com/clojure-emacs/cider/issues/1577): Show first line of docstring in ns browser. ### Bugs fixed diff --git a/cider-browse-ns.el b/cider-browse-ns.el index 14c81c64..d4963878 100644 --- a/cider-browse-ns.el +++ b/cider-browse-ns.el @@ -114,6 +114,31 @@ contents of the buffer are not reset before inserting TITLE and ITEMS." 'cider-browse-ns-current-ns ns))) (goto-char (point-min))))) +(defun cider-browse-ns--first-doc-line (doc) + "Return the first line of the given DOC string. +If the first line of the DOC string contains multiple sentences, only +the first sentence is returned. If the DOC string is nil, a Not documented +string is returned." + (if doc + (let* ((split-newline (split-string (read doc) "\n")) + (first-line (car split-newline))) + (cond + ((string-match "\\. " first-line) (substring first-line 0 (match-end 0))) + ((= 1 (length split-newline)) first-line) + (t (concat first-line "...")))) + "Not documented.")) + +(defun cider-browse-ns--items (namespace) + "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)) + (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"))) + (first-doc-line (cider-browse-ns--first-doc-line doc))) + (concat ns-var " " (propertize first-doc-line 'font-lock-face 'font-lock-doc-face)))) + propertized-ns-vars))) + ;; Interactive Functions ;;;###autoload @@ -123,8 +148,7 @@ contents of the buffer are not reset before inserting TITLE and ITEMS." (with-current-buffer (cider-popup-buffer cider-browse-ns-buffer t) (cider-browse-ns--list (current-buffer) namespace - (nrepl-dict-map #'cider-browse-ns--properties - (cider-sync-request:ns-vars-with-meta namespace))) + (cider-browse-ns--items namespace)) (setq-local cider-browse-ns-current-ns namespace))) ;;;###autoload @@ -143,7 +167,7 @@ contents of the buffer are not reset before inserting TITLE and ITEMS." (defun cider-browse-ns--thing-at-point () "Get the thing at point. Return a list of the type ('ns or 'var) and the value." - (let ((line (cider-string-trim (thing-at-point 'line)))) + (let ((line (car (split-string (cider-string-trim (thing-at-point 'line)) " ")))) (if (string-match "\\." line) (list 'ns line) (list 'var (format "%s/%s" diff --git a/test/cider-browse-ns-tests.el b/test/cider-browse-ns-tests.el index b6cd538d..317bcce2 100644 --- a/test/cider-browse-ns-tests.el +++ b/test/cider-browse-ns-tests.el @@ -47,7 +47,9 @@ :var (cider-browse-ns-buffer) (it "lists out all forms of a namespace with correct font-locks" (spy-on 'cider-sync-request:ns-vars-with-meta :and-return-value - '(dict "blank?" (dict "arglists" "fn arg list"))) + '(dict "blank?" + (dict "arglists" "fn arg list" + "doc" "\"True if s is nil, empty, or contains only whitespace.\""))) (with-temp-buffer (setq cider-browse-ns-buffer (buffer-name (current-buffer))) @@ -55,4 +57,23 @@ (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)))) + (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)))) + +(describe "cider-browse-ns--first-doc-line" + (it "returns Not documented if the doc string is missing" + (expect (cider-browse-ns--first-doc-line nil) + :to-equal "Not documented.")) + + (it "returns the first line of the doc string" + (expect (cider-browse-ns--first-doc-line "\"True if s is nil, empty, or contains only whitespace.\"") + :to-equal "True if s is nil, empty, or contains only whitespace.")) + + (it "returns the first sentence of the doc string if the first line contains multiple sentences" + (expect (cider-browse-ns--first-doc-line "\"First sentence. Second sentence.\"") + :to-equal "First sentence. ")) + + (it "returns the first line of the doc string if the first sentence spans multiple lines" + (expect (cider-browse-ns--first-doc-line "\"True if s is nil, empty, or\n contains only whitespace.\"") + :to-equal "True if s is nil, empty, or..."))) |