clojure-emacs / cider

The Clojure Interactive Development Environment that Rocks for Emacs
https://cider.mx
GNU General Public License v3.0
3.52k stars 643 forks source link

Refactor `cider-docstring.el` #3707

Closed katomuso closed 4 weeks ago

katomuso commented 1 month ago

While I was trying to clean up cider-doc.el, I came across cider-docstring.el and noticed the cider-docstring--dumb-trim function. I don't know why it's named that way, but it looks pretty smart to me. Maybe too smart, as it does too many things, one of which, along with trimming, is formatting. So I think it is a good idea to separate it into two functions: cider-docstring--trim and cider-docstring--format.

The main reason for this is that these two functionalities are needed in different contexts. For example, we need to format the docstring when displaying it in the Doc or ClojureDocs buffer (notice that we don't need trimming here because it's a buffer and there's plenty of space to display the docstring), and we need trimming when displaying the docstring in the minibuffer or in a popup along with completions.

Also, the current formatting of the docstring in cider-docstring--dumb-trim is too opinionated. For example, it moves every single paragraph (signified by ". ") to a new line and adds an empty line after it, which totally messes up the original formatting (there are still newlines just in the middle of paragraphs, so we need to either remove all newlines and reformat the docstring from scratch, or intervene as little as possible). I guess nobody had noticed this strange formatting because not many people look for docstrings in the minibuffer or in completions, and even fewer care how it is formatted (but I do). Also, it removes a variable number of spaces from the beginning of every line. As we discussed in #3701, it will be safer to just remove 2 spaces from the beginning while performing formatting. Of course, later we can change this, and it will be much easier to do as we will need to change it in just one place (cider-docstring--format).

Additionally, there are a few small tweaks following this change:

katomuso commented 4 weeks ago

@vemv Thanks for the suggestions! I think you are right; there is too much going on, so I will create smaller PRs to be more gradual.