Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split cider-docstring--dumb-trim into two single-purpose functions #3708

Merged
merged 5 commits into from
Jun 8, 2024

Conversation

katomuso
Copy link
Contributor

@katomuso katomuso commented Jun 8, 2024

The function cider-docstring--dumb-trim performs both formatting and trimming. I propose splitting it into two separate functions: cider-docstring--trim and cider-docstring--format because these two functionalities are needed in different contexts. For example, we need to format the docstring when displaying it in the documentation buffer, and we need to trim the docstring when displaying it in the minibuffer or in a popup along with completions.

@katomuso
Copy link
Contributor Author

katomuso commented Jun 8, 2024

To easily verify that it doesn't change the output, you can use the following snippet:

(let* ((info (cider-var-info "clojure.core/reduce"))
       (doc (nrepl-dict-get info "doc")))
  (string= (cider-docstring--dumb-trim doc)
           (cider-docstring--trim (cider-docstring--format doc))))

Copy link
Member

@vemv vemv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

I appreciate starting over more cleanly.

(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")))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nil value for the second argument of seq-take appears to be undocumented, so I'd prefer to have an explicit default for it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean if the max-lines argument is nil, right? Well, I don't really think there should be a default value because it should always be a number, but if anything, I think it should just return the input string if max-lines is nil.

(if (> lines-length n)
(concat result "...")
result))))
(cl-defun cider-docstring--trim (string &optional (max-lines cider-docstring-max-lines))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cider-docstring-max-lines is unused

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value of cider-docstring-max-lines is the default for max-lines if max-lines is not specified explicitly, so there is no need to use it in the body. So, if we call cider-docstring--trim without a second argument, cider-docstring-max-lines is used, right? So I'm not sure what you mean by saying that cider-docstring-max-lines is unused.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok! I wasn't immediately familar with that cl-defun syntax.

@vemv vemv merged commit 05e7570 into clojure-emacs:master Jun 8, 2024
39 checks passed
@vemv
Copy link
Member

vemv commented Jun 8, 2024

Thanks!

@katomuso katomuso deleted the docstring-trim-format branch June 9, 2024 03:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants