Skip to content
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

Merged
merged 7 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions rustic-rustfmt.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Owner

@brotzeit brotzeit Jul 9, 2021

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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good

(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" "--")
Copy link
Owner

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.

Copy link
Contributor Author

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-compute-rustfmt-file-lines-args file
start
(+ start len)))))))
Copy link
Owner

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 ?

Copy link
Contributor Author

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).

Copy link
Owner

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.

Copy link
Owner

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 ;)

Copy link
Contributor Author

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.


;;;###autoload
(defun rustic-format-buffer ()
Expand Down
26 changes: 14 additions & 12 deletions test/rustic-format-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,20 @@
(should (string= (buffer-string) formatted-string)))
(kill-buffer buffer1))))

(ert-deftest rustic-test-format-file ()
(let* ((string "fn main() \n{\nlet x = 1;\n}\n")
(formatted-string "fn main() \n{\n let x = 1;\n}\n")
(ert-deftest rustic-test-format-region ()
(let* ((buffer1 (get-buffer-create "b1"))
(string "fn main() {\n let x =\n 1;\n}")
(formatted-string "fn main() {\n let x = 1;\n}\n")
(dir (rustic-babel-generate-project t))
(main (expand-file-name "main.rs" (concat dir "/src")))
(buf (get-buffer-create "test")))
(with-current-buffer buf (write-file main))
(write-region string nil main nil 0)
(goto-char 13)
(push-mark (buffer-end-position))
(setq mark-active t)
(rustic-format-region)
(with-temp-buffer
(insert-file-contents main)
(default-directory dir))
(with-current-buffer buffer1
(insert string)
(write-file main)
(rustic-mode)
(goto-char 16)
(push-mark 34)
(setq mark-active t)
(rustic-format-region 16 34)
(sit-for 1)
(should (string= (buffer-string) formatted-string)))))