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 2 commits
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: 35 additions & 2 deletions rustic-rustfmt.el
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

(require 'rustic-cargo)

(declare-function project-root "project")
(if (version<= emacs-version "28.0")
(declare-function project-roots "project")
(declare-function project-root "project"))
Copy link
Owner

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

Copy link
Contributor Author

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?

Copy link
Owner

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.


;;; Options

Expand Down Expand Up @@ -103,6 +105,12 @@ and it's `cdr' is a list of arguments."
(push (format "%s=%s" key (if (booleanp val) (if val "true" "false") val)) args)
(push "--config" args)))))

(defun rustic-compute-rustfmt-file-lines-args (file start end)
"Compute the arguments to rustfmt to modify a particular region."
(list "--unstable-features"
"--file-lines"
(format "[{\"file\":\"%s\",\"range\":[%d,%d]}]" file start end)))

(defun rustic-format-sentinel (proc output)
"Sentinel for rustfmt processes when using stdin."
(ignore-errors
Expand Down Expand Up @@ -188,6 +196,29 @@ and it's `cdr' is a list of arguments."
(kill-buffer proc-buffer)
(message "Workspace formatted with cargo-fmt.")))))

;;;###autoload
(defun rustic-format-region ()
"Format the current active region using rustfmt.

This operation requires a nightly version of rustfmt.
"
(interactive)
(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)))
rbartlensky marked this conversation as resolved.
Show resolved Hide resolved
(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))))))

;;;###autoload
(defun rustic-format-buffer ()
"Format the current buffer using rustfmt.
Expand Down Expand Up @@ -226,7 +257,9 @@ This is basically a wrapper around `project--buffer-list'."
(if (fboundp 'project--buffer-list)
(project--buffer-list pr)
;; Like the above function but for releases before Emacs 28.
(let ((root (project-root pr))
(let ((root (if (version<= emacs-version "28.0")
(car (project-roots pr))
(project-root pr)))
bufs)
(dolist (buf (buffer-list))
(let ((filename (or (buffer-file-name buf)
Expand Down
16 changes: 16 additions & 0 deletions test/rustic-format-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,19 @@
(revert-buffer t t)
(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")
(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)
(should (string= (buffer-string) formatted-string)))))