-
Notifications
You must be signed in to change notification settings - Fork 102
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
Implement rustic-format-region
.
#258
Conversation
Based on brotzeit#257 (@iitalics's work).
This unfortunately only works with rustfmt nightly, but it's still an interesting function to have at your disposal. Fixes: brotzeit#229
1c2eeae
to
a1ef56f
Compare
Thanks for the pull request! What do you think about using
|
(declare-function project-root "project") | ||
(if (version<= emacs-version "28.0") | ||
(declare-function project-roots "project") | ||
(declare-function project-root "project")) |
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.
Do you think it makes sense to write a simple test that only checks if this is working. This change have been a little annoying #256
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 how I would test this. Do you have any ideas?
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.
No, never mind. Maybe we add something later.
@brotzeit I attempted to address all your comments, although my elisp isn't that great, so I might need another look over this. |
rustic-rustfmt.el
Outdated
(append (list rustic-cargo-bin "+nightly" "fmt" "--") | ||
(rustic-compute-rustfmt-file-lines-args file | ||
start | ||
(+ start len))))))) |
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.
Maybe I'm missing something, but isn't it possible to use the arguments begin and end here ?
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 wasn't aware (interactive "r")
gives you begin and end 😅 , but now I know. I removed the unnecessary code. However, in this particular case, rustfmt asks you for lines, while begin and end are in bytes (AFAICT).
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.
However, in this particular case, rustfmt asks you for lines, while begin and end are in bytes (AFAICT).
I didn't notice. Then maybe (interactive "r")
isn't the right solution. line-number-at-pos
could help here.
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.
But maybe we could only use end
...
Just do what you think makes sense ;)
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 think I found a good middle-ground solution. Check out the last commit.
'rustic-format-file-sentinel | ||
:buffer buf | ||
:command | ||
(append (list rustic-cargo-bin "+nightly" "fmt" "--") |
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.
So this feature is only available on nightly or why do you pin the toolchain ? I guess nowadays lots of people are using stable instead, so we should cover the case when there's no nightly available.
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.
Yep, it only works with rustfmt nightly. It looks like they are close to stabilizing (rust-lang/rustfmt#3397)! I'll try to handle this more gracefully
rustic-rustfmt.el
Outdated
(file (buffer-file-name buf)) | ||
(r-begin) | ||
(r-end)) | ||
(cond ((region-active-p) |
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.
Maybe we should just do something like if region is active -> format region, if not -> format buffer ?
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.
Sounds good
@brotzeit thanks again for all your input. I believe I have addressed all your comments :) (let me know if you want to squash my commits or not) |
cdf8ea7
to
524923e
Compare
This helps get the line number at a particular position in the buffer. Some context: https://emacs.stackexchange.com/questions/3821/a-faster-method-to-obtain-line-number-at-pos-in-large-buffers
@brotzeit I think this is ready |
Oh, I missed this pull request, sorry... Thanks again! |
This unfortunately only works with rustfmt nightly, but it's still
an interesting function to have at your disposal.