Skip to content

Commit

Permalink
[Fix clojure-emacs#1577] Show first line of docstring in ns browser
Browse files Browse the repository at this point in the history
  • Loading branch information
mallt committed May 29, 2016
1 parent afd5549 commit aa9f848
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 27 additions & 3 deletions cider-browse-ns.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand Down
25 changes: 23 additions & 2 deletions test/cider-browse-ns-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,33 @@
: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)))
(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))))
(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...")))

0 comments on commit aa9f848

Please sign in to comment.