summaryrefslogtreecommitdiff
path: root/rust-mode.el
diff options
context:
space:
mode:
authormrBliss <dewinant@gmail.com>2015-12-02 11:36:11 +0100
committermrBliss <dewinant@gmail.com>2015-12-02 13:53:58 +0100
commitb76e803768bd572424b3f111aa55269f5d8226fc (patch)
treec53c56a8cbd350d622201490da9af1dfbf91bcdb /rust-mode.el
parent0601540d43b901b0ad0ec62363a143cd3a054364 (diff)
downloadrust-mode-b76e803768bd572424b3f111aa55269f5d8226fc.tar.gz
Correctly indent where clauses
Revisit #82. Now rustfmt has a default style for `where` clauses, it makes sense to let rust-mode support it by default. How rust-mode currently indents `where` clauses is totally broken anyway. The line starting with `where` itself, the following lines that are part of the `where` clause, and the body were all indented incorrectly. This commit fixes this. **Note that this commit does not prescribe a certain indentation style for where clauses, but supports the most common ones, including rustfmt's default.** By choosing the location of (1) `where` and (2) the type parameter bounds, the user can follow three major indentation styles. There is no need for a configuration option to choose the style, as the locations of 1 and 2 dictate the style to follow. So all three styles can be used together in the same file. For each major style, the opening brace can be on the last line of the type parameter bounds or on a new line (a or b in the examples). All 6 styles are supported without having to change any configuration option. See the examples below and the new test cases. There is one more style that is unfortunately incompatible with the default `where` indentation style of rustfmt: when the user does not want `where` to be indented, but aligned with the `fn` or `trait` on the line before (see examples `foo4a` and `foo4b` below). To enable this style, the user has to set the new option `rust-indent-where-clause` to nil (it defaults to t). Putting `where` and the type parameter bounds on the same line as the function header is still supported of course (see examples `fooa` and `foob` below). As there is no indentation, this commit does not influence this. Note that rust-mode's indentation has a different goal than rustfmt. rustfmt reflows code, adds or removes line breaks, reindents, etc. rust-mode's indentation only indents lines. The goal is not to imitate rustfmt, but after running rustfmt on a file, reindenting it with rust-mode should be idempotent. This way, users do not have to deal with the eternal battle of differing indentation styles of rustfmt and rust-mode. The supported styles: ```rust fn foo1a(a: A, b: B) -> C where A: Clone + Default, B: Eq, C: PartialEq { let body; } fn foo1b(a: A, b: B) -> C where A: Clone + Default, B: Eq, C: PartialEq { let body; } ``` ```rust fn foo2a(a: A, b: B) -> C where A: Clone + Default, B: Eq, C: PartialEq { let body; } fn foo2b(a: A, b: B) -> C where A: Clone + Default, B: Eq, C: PartialEq { let body; } ``` ```rust fn foo3a(a: A, b: B) -> C where A: Clone + Default, B: Eq, C: PartialEq { let body; } fn foo3b(a: A, b: B) -> C where A: Clone + Default, B: Eq, C: PartialEq { let body; } ``` If the user wants `foo4` instead of `foo1`, `rust-indent-where-clause` must be set to nil. `foo2` and `foo3` are not influenced by this. ```rust fn foo4a(a: A, b: B) -> C where A: Clone + Default, B: Eq, C: PartialEq { let body; } fn foo4a(a: A, b: B) -> C where A: Clone + Default, B: Eq, C: PartialEq { let body; } ``` Unchanged: ```rust fn fooa(a: A, b: B) -> C where A: Clone + Default, ... { let body; } fn foob(a: A, b: B) -> C where A: Clone + Default, ... { let body; } ```
Diffstat (limited to 'rust-mode.el')
-rw-r--r--rust-mode.el73
1 files changed, 72 insertions, 1 deletions
diff --git a/rust-mode.el b/rust-mode.el
index 5b50d29..d857f25 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -157,6 +157,13 @@
:group 'rust-mode
:safe #'booleanp)
+(defcustom rust-indent-where-clause t
+ "Indent the line starting with the where keyword following a
+function or trait. When nil, where will be aligned with fn or trait."
+ :type 'boolean
+ :group 'rust-mode
+ :safe #'booleanp)
+
(defcustom rust-playpen-url-format "https://play.rust-lang.org/?code=%s"
"Format string to use when submitting code to the playpen"
:type 'string
@@ -213,7 +220,25 @@
(back-to-indentation))
(while (> (rust-paren-level) current-level)
(backward-up-list)
- (back-to-indentation))))
+ (back-to-indentation))
+ ;; When we're in the where clause, skip over it. First find out the start
+ ;; of the function and its paren level.
+ (let ((function-start nil) (function-level nil))
+ (save-excursion
+ (rust-beginning-of-defun)
+ (back-to-indentation)
+ ;; Avoid using multiple-value-bind
+ (setq function-start (point)
+ function-level (rust-paren-level)))
+ ;; On a where clause
+ (when (or (looking-at "\\bwhere\\b")
+ ;; or in one of the following lines, e.g.
+ ;; where A: Eq
+ ;; B: Hash <- on this line
+ (and (save-excursion
+ (re-search-backward "\\bwhere\\b" function-start t))
+ (= current-level function-level)))
+ (goto-char function-start)))))
(defun rust-align-to-method-chain ()
(save-excursion
@@ -348,6 +373,11 @@
((and (nth 4 (syntax-ppss)) (looking-at "*"))
(+ 1 baseline))
+ ;; When the user chose not to indent the start of the where
+ ;; clause, put it on the baseline.
+ ((and (not rust-indent-where-clause) (looking-at "\\bwhere\\b"))
+ baseline)
+
;; If we're in any other token-tree / sexp, then:
(t
(or
@@ -361,6 +391,47 @@
;; Point is now at the beginning of the containing set of braces
(rust-align-to-expr-after-brace)))
+ ;; When where-clauses are spread over multiple lines, clauses
+ ;; should be aligned on the type parameters. In this case we
+ ;; take care of the second and following clauses (the ones
+ ;; that don't start with "where ")
+ (save-excursion
+ ;; Find the start of the function, we'll use this to limit
+ ;; our search for "where ".
+ (let ((function-start nil) (function-level nil))
+ (save-excursion
+ (rust-beginning-of-defun)
+ (back-to-indentation)
+ ;; Avoid using multiple-value-bind
+ (setq function-start (point)
+ function-level (rust-paren-level)))
+ ;; When we're not on a line starting with "where ", but
+ ;; still on a where-clause line, go to "where "
+ (when (and
+ (not (looking-at "\\bwhere\\b"))
+ ;; We're looking at something like "F: ..."
+ (and (looking-at (concat rust-re-ident ":"))
+ ;; There is a "where " somewhere after the
+ ;; start of the function.
+ (re-search-backward "\\bwhere\\b"
+ function-start t)
+ ;; Make sure we're not inside the function
+ ;; already (e.g. initializing a struct) by
+ ;; checking we are the same level.
+ (= function-level level)))
+ ;; skip over "where"
+ (forward-char 5)
+ ;; Unless "where" is at the end of the line
+ (if (eolp)
+ ;; in this case the type parameters bounds are just
+ ;; indented once
+ (+ baseline rust-indent-offset)
+ ;; otherwise, skip over whitespace,
+ (skip-chars-forward "[:space:]")
+ ;; get the column of the type parameter and use that
+ ;; as indentation offset
+ (current-column)))))
+
(progn
(back-to-indentation)
;; Point is now at the beginning of the current line