|
27 | 27 | ;;; Code: |
28 | 28 |
|
29 | 29 | (require 'cl-lib) |
| 30 | +(require 'seq) |
30 | 31 | (require 'shr) |
| 32 | +(require 'subr-x) |
31 | 33 |
|
32 | 34 | (defsubst cider--render-pre* (dom) |
33 | 35 | "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 |
139 | 141 | second-attempt |
140 | 142 | first-attempt))) |
141 | 143 |
|
142 | | -(defun cider-docstring--dumb-trim (s &optional n) |
143 | | - "Returns up to the first N lines of string S, |
144 | | -adding \"...\" if trimming was necessary. |
145 | | -
|
146 | | -N defaults to `cider-docstring-max-lines'. |
147 | | -
|
148 | | -Also performs some bare-bones formatting, cleaning up some common whitespace issues." |
149 | | - (when s |
150 | | - (let* ((s (replace-regexp-in-string "\\. " ".\n\n" s)) ;; improve the formatting of e.g. clojure.core/reduce |
151 | | - (n (or n cider-docstring-max-lines)) |
152 | | - (lines (split-string s "\n")) |
153 | | - (lines-length (length lines)) |
154 | | - (selected-lines (cl-subseq lines 0 (min n lines-length))) |
155 | | - (result (mapconcat (lambda (f) |
156 | | - ;; Remove spaces at the beginning of each line, as it is common in many clojure.core defns: |
157 | | - (replace-regexp-in-string "\\`[ ]+" "" f)) |
158 | | - selected-lines |
159 | | - "\n"))) |
160 | | - (if (> lines-length n) |
161 | | - (concat result "...") |
162 | | - result)))) |
| 144 | +(cl-defun cider-docstring--trim (string &optional (max-lines cider-docstring-max-lines)) |
| 145 | + "Return MAX-LINES of STRING, adding \"...\" if trimming was necessary." |
| 146 | + (let* ((lines (split-string string "\n")) |
| 147 | + (string (string-join (seq-take lines max-lines) "\n"))) |
| 148 | + (concat string (when (> (length lines) max-lines) "...")))) |
| 149 | + |
| 150 | +(defun cider-docstring--format (string) |
| 151 | + "Return a nicely formatted STRING to be displayed to the user." |
| 152 | + (let* ((string (replace-regexp-in-string "\\. " ".\n\n" string)) ;; improve the formatting of e.g. clojure.core/reduce |
| 153 | + (string (mapconcat (lambda (line) |
| 154 | + ;; Remove spaces at the beginning of each line, as it is common in many clojure.core defns: |
| 155 | + (replace-regexp-in-string "\\`[ ]+" "" line)) |
| 156 | + (split-string string "\n") |
| 157 | + "\n"))) |
| 158 | + string)) |
163 | 159 |
|
164 | 160 | (provide 'cider-docstring) |
165 | 161 | ;;; cider-docstring.el ends here |
0 commit comments