-
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
Changes from 1 commit
82d7ac7
a1ef56f
47378ff
34f0ef7
524923e
6ee624c
1628fcb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,27 +197,36 @@ and it's `cdr' is a list of arguments." | |
(message "Workspace formatted with cargo-fmt."))))) | ||
|
||
;;;###autoload | ||
(defun rustic-format-region () | ||
(defun rustic-format-region (begin end) | ||
"Format the current active region using rustfmt. | ||
|
||
This operation requires a nightly version of rustfmt. | ||
" | ||
(interactive) | ||
(interactive "r") | ||
(unless (or (eq major-mode 'rustic-mode) | ||
(eq major-mode 'rustic-macro-expansion-mode)) | ||
(error "Not a rustic-mode buffer.")) | ||
(let ((file (buffer-file-name (current-buffer))) | ||
(start (+ 1 (count-lines 1 (region-beginning)))) | ||
(len (- (count-lines (region-beginning) (region-end)) 1))) | ||
(rustic-compilation-process-live t) | ||
(rustic-format-start-process | ||
'rustic-format-file-sentinel | ||
:buffer (current-buffer) | ||
:command | ||
(append (list rustic-cargo-bin "+nightly" "fmt" "--") | ||
(rustic-compute-rustfmt-file-lines-args file | ||
start | ||
(+ start len)))))) | ||
(let* ((buf (current-buffer)) | ||
(file (buffer-file-name buf)) | ||
(r-begin) | ||
(r-end)) | ||
(cond ((region-active-p) | ||
(setq r-begin begin) | ||
(setq r-end end)) | ||
(t | ||
(setq r-begin (region-beginning)) | ||
(setq r-end (region-end)))) | ||
(let ((start (+ 1 (count-lines 1 r-begin))) | ||
(len (- (count-lines r-begin r-end) 1))) | ||
(rustic-compilation-process-live t) | ||
(rustic-format-start-process | ||
'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 commentThe 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 commentThe 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-compute-rustfmt-file-lines-args file | ||
start | ||
(+ start len))))))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't aware There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I didn't notice. Then maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But maybe we could only use Just do what you think makes sense ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
;;;###autoload | ||
(defun rustic-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.
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