branch: elpa/cider
commit 05e7570e33d1aa795b1a0f85a6aa2162b42b93cd
Merge: 09d3dd4041 fd84fad37d
Author: vemv <v...@users.noreply.github.com>
Commit: GitHub <nore...@github.com>

    Merge pull request #3708 from katomuso/docstring-trim-format
    
    Split `cider-docstring--dumb-trim` into two single-purpose functions
---
 cider-doc.el       |  3 ++-
 cider-docstring.el | 38 +++++++++++++++++---------------------
 cider-eldoc.el     |  4 +++-
 3 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/cider-doc.el b/cider-doc.el
index 88a4ac17ff..addfdeb829 100644
--- a/cider-doc.el
+++ b/cider-doc.el
@@ -429,7 +429,8 @@ in a COMPACT format is specified, FOR-TOOLTIP if specified."
          (fetched-doc (nrepl-dict-get info "doc"))
          (doc     (or rendered-fragments
                       (if compact
-                          (cider-docstring--dumb-trim fetched-doc)
+                          (cider-docstring--trim
+                           (cider-docstring--format fetched-doc))
                         fetched-doc)
                       (unless compact
                         "Not documented.")))
diff --git a/cider-docstring.el b/cider-docstring.el
index 0b3ceebaab..007bb0a8b7 100644
--- a/cider-docstring.el
+++ b/cider-docstring.el
@@ -27,7 +27,9 @@
 ;;; Code:
 
 (require 'cl-lib)
+(require 'seq)
 (require 'shr)
+(require 'subr-x)
 
 (defsubst cider--render-pre* (dom)
   "Render DOM nodes, formatting them them as Java if they are strings."
@@ -139,27 +141,21 @@ Prioritize rendering as much as possible while staying 
within `cider-docstring-m
         second-attempt
         first-attempt)))
 
-(defun cider-docstring--dumb-trim (s &optional n)
-  "Returns up to the first N lines of string S,
-adding \"...\" if trimming was necessary.
-
-N defaults to `cider-docstring-max-lines'.
-
-Also performs some bare-bones formatting, cleaning up some common whitespace 
issues."
-  (when s
-    (let* ((s (replace-regexp-in-string "\\.  " ".\n\n" s)) ;; improve the 
formatting of e.g. clojure.core/reduce
-           (n (or n cider-docstring-max-lines))
-           (lines (split-string s "\n"))
-           (lines-length (length lines))
-           (selected-lines (cl-subseq lines 0 (min n lines-length)))
-           (result (mapconcat (lambda (f)
-                                ;; Remove spaces at the beginning of each 
line, as it is common in many clojure.core defns:
-                                (replace-regexp-in-string "\\`[ ]+" "" f))
-                              selected-lines
-                              "\n")))
-      (if (> lines-length n)
-          (concat result "...")
-        result))))
+(cl-defun cider-docstring--trim (string &optional (max-lines 
cider-docstring-max-lines))
+  "Return MAX-LINES of STRING, adding \"...\" if trimming was necessary."
+  (let* ((lines (split-string string "\n"))
+         (string (string-join (seq-take lines max-lines) "\n")))
+    (concat string (when (> (length lines) max-lines) "..."))))
+
+(defun cider-docstring--format (string)
+  "Return a nicely formatted STRING to be displayed to the user."
+  (let* ((string (replace-regexp-in-string "\\.  " ".\n\n" string)) ;; improve 
the formatting of e.g. clojure.core/reduce
+         (string (mapconcat (lambda (line)
+                              ;; Remove spaces at the beginning of each line, 
as it is common in many clojure.core defns:
+                              (replace-regexp-in-string "\\`[ ]+" "" line))
+                            (split-string string "\n")
+                            "\n")))
+    string))
 
 (provide 'cider-docstring)
 ;;; cider-docstring.el ends here
diff --git a/cider-eldoc.el b/cider-eldoc.el
index f0ceeec8b7..4c094b06c0 100644
--- a/cider-eldoc.el
+++ b/cider-eldoc.el
@@ -218,7 +218,9 @@ information."
          (symbol (lax-plist-get eldoc-info "symbol"))
          (docstring (or (cider--render-docstring-first-sentence eldoc-info)
                         (cider--render-docstring eldoc-info)
-                        (cider-docstring--dumb-trim (lax-plist-get eldoc-info 
"docstring"))))
+                        (cider-docstring--trim
+                         (cider-docstring--format
+                          (lax-plist-get eldoc-info "docstring")))))
          ;; if it's a single class (and not multiple class candidates), that's 
it
          (maybe-class (car (lax-plist-get eldoc-info "class")))
          (formatted-var (or (when maybe-class

Reply via email to