Closed mikpom1 closed 5 months ago
The readme says:
M-x prettier-prettify-region
to prettify region (but note that region should align with a complete block of code).
(Emphasis added . Maybe I didn't word this well, what I meant to say is a block of code that starts at indentation zero.)
The reason for this limitation is that at the most basic level, we're sending the block of code to Prettier without context, so Prettier couldn't know that it should be indented differently.
As far as I gather at this very moment (where I haven't spent much time thinking about this particular problem in years) the only way this could work is by sending everything from (beginning-of-buffer)
to (region-end)
but then only applying changes to region. This wouldn't be easy because currently changes are sent as a diff, and removing parts of the diff outside of region perhaps isn't impossible but also not entirely trivial.
Myself, I don't really see a use for this function. I always indent the whole buffer and happily so. So right now, I wouldn't be inclined to spend time on implementing such a feature. I might be missing something? What's the use case compared to prettifying the whole buffer, is it an optimization or something else?
Ok the problem is I really don't understand what should align with a complete block of code means. Even now when you point me to it I doubt whether it is related to zero indentation. By zero indentation I mean that the region is properly indented within, but globally has no indent after prettifying. This what I tried to highlight in the example.
If this is intended and I recognized it from the README, then I wouldn't even create that issue. So sorry for bothering if it is the case.
As for the use cases: I am just an ascetic of coding aids and unless the code is to be shared with others, I prefer keeping my ugly formatting except maybe some regions I occasionally want to fix.
Also thanks for the hints on potential implementation: If I am not satisfied with prettifying the whole buffer I at least know where to look.
@mikpom1 definitely no bother, the readme doesn't explain it very well in part because I didn't choose the words carefully and in part because I never spent much time on thinking how this feature ought to work. I guess what I was trying to say is "top-level block of code", i.e. where the first line has no indentation.
As for your use case, I think you could work around it pretty easily with a function of your own. Here's one that remembers the first line's indentation and prefixes region with it after prettification. There might be a few edge cases to handle (where region doesn't start/end on beginning of line perhaps) but should be a good start:
(defun my-prettify-region ()
"Prettify region, then restore original indentation."
(interactive)
(atomic-change-group
(let ((prefix
(save-excursion
(goto-char (region-beginning))
(beginning-of-line)
(buffer-substring
(point)
(re-search-forward "\\=[[:space:]]*" nil t))))
(deactivate-mark nil))
(prettier-prettify-region)
(unless (zerop (length prefix))
(save-excursion
(goto-char (region-end))
(let ((end (point-marker)))
(goto-char (region-beginning))
(beginning-of-line)
(while (< (point) end)
(save-excursion (insert prefix))
(forward-line))))))))
There seems to be something wonky with prettier-prettify-region
and undo though, it doesn't work properly while region is active. I'll try and take a look at fixing that soon.
Thanks, @jscheid . This workaround will do. Will let you know if not )
When running prettier-prettify-region global indent of a chunk being fixed is always set to zero Consider example
Let's say I want to prettify only a line containing inner function. If I select the line and prettify I get
What I expect is
I.e. the block with
inner
function is indent as original definition.Is it possible? I am new to prettier. This is something I enjoy in my Python+Black-mocchiato setup and I can't figure out for JS.