-
-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix #2126] Preserve the point position in cider-format-buffer
That's pretty important for people who want to apply the formatting automatically on buffer save. Unfortunately we can't use save-excursion here, because we're deleting the location we want to preserve, so we have to get a bit more creative.
- Loading branch information
Showing
2 changed files
with
10 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c1a500d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the right thing to do? If the buffer has
n
blank lines before point,cider-format-buffer
would delete all but one, causing point to shift byn-1
lines.c1a500d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, I didn't think of this at all. My idea was to just remember the current position and restore it. Not sure what would be a good approach for the scenario you mentioned.
I'm not sure I want to go overboard with a
diff
-based solution like the ones linked from the issue.c1a500d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A slightly more precise measure would be to compute the proportion of the buffer before the erase and interpolate after the insertion.
c1a500d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a pretty good heuristic:
We search for a snippet of text up to
l
chars long (here I've used 32) in the formatted code, replacing sequences of newlines w/ a regexp that matches one or more newlines.l
need only be long enough so that it's highly unlikely to occur multiple times in the text. If point is very close to(point-max)
, such that the snippet may be very short, we search from the end instead.I'm sure this can be cleaned up a little bit (e.g. to avoid the last
looking-at-p "\n"
) but it's pretty good and much easier than a diff-based solution.c1a500d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Search it before and after same number of times?
c1a500d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what you mean.
c1a500d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I was thinking of something along the same line after you flagged this commit @xiongtx, so I'm fine with this approach.
On a related note - to make this functionality more useful we should add the ability to format also region, defun and sexp I guess, to be a real alternative of what
clojure-mode
does.c1a500d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant that you search for the same regexp before till it gets to the current position and count the N times you did so. Then after the formatting you re-search again N times. This way you are sure that you arrived to he same position, no need for long or unique regexp.