about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-01-21 09:13:36 -0800
committerAlex Crichton <alex@alexcrichton.com>2015-01-21 09:13:36 -0800
commit83af23ea49a907ddda8915775cfb5d8c4cff5682 (patch)
tree6584e1895c571731b62af312bbc27091b51d0b95
parent6869645e86c91544b8737b89809bdf10bef536d9 (diff)
parent8051bd06260e4587e183e6055a6794414e7c76f9 (diff)
downloadrust-83af23ea49a907ddda8915775cfb5d8c4cff5682.tar.gz
rust-83af23ea49a907ddda8915775cfb5d8c4cff5682.zip
rollup merge of #19913: KOMON/rust-mode-emacs-indentation
I added an option to auto-indent method chains to line up along their '.' operators. Like so:

```
let input = io::stdin().readline()
                       .ok()
                       .expect("Failed to read line");
```

The old default would indent like so:
```
let input = io::stdin().readme()
    .ok()
    .expect("Failed to read line");
```

The Rust guide explicitly condones the former, so I thought it would be nice for the emacs mode to support it. It's off by default, you have to set ```rust-indent-method-chain``` to ```t``` via your .emacs or the customize menu
-rw-r--r--src/etc/emacs/rust-mode.el33
1 files changed, 30 insertions, 3 deletions
diff --git a/src/etc/emacs/rust-mode.el b/src/etc/emacs/rust-mode.el
index f25a59015fc..dae685f3a54 100644
--- a/src/etc/emacs/rust-mode.el
+++ b/src/etc/emacs/rust-mode.el
@@ -54,6 +54,11 @@
   :type 'integer
   :group 'rust-mode)
 
+(defcustom rust-indent-method-chain nil
+  "Indent Rust method chains, aligned by the '.' operators"
+  :type 'boolean
+  :group 'rust-mode)
+
 (defun rust-paren-level () (nth 0 (syntax-ppss)))
 (defun rust-in-str-or-cmnt () (nth 8 (syntax-ppss)))
 (defun rust-rewind-past-str-cmnt () (goto-char (nth 8 (syntax-ppss))))
@@ -73,10 +78,19 @@
     ;; open bracket ends the line
     (when (not (looking-at "[[:blank:]]*\\(?://.*\\)?$"))
       (when (looking-at "[[:space:]]")
-	(forward-word 1)
-	(backward-word 1))
+    (forward-word 1)
+    (backward-word 1))
       (current-column))))
 
+(defun rust-align-to-method-chain ()
+  (save-excursion
+    (previous-line)
+    (end-of-line)
+    (backward-word 1)
+    (backward-char)
+    (when (looking-at "\\..+\(.*\)\n")
+      (- (current-column) rust-indent-offset))))
+
 (defun rust-rewind-to-beginning-of-current-level-expr ()
   (let ((current-level (rust-paren-level)))
     (back-to-indentation)
@@ -99,10 +113,13 @@
                    ;; the inside of it correctly relative to the outside.
                    (if (= 0 level)
                        0
+                     (or
+                      (when rust-indent-method-chain
+                        (rust-align-to-method-chain))
                      (save-excursion
                        (backward-up-list)
                        (rust-rewind-to-beginning-of-current-level-expr)
-                       (+ (current-column) rust-indent-offset)))))
+                       (+ (current-column) rust-indent-offset))))))
              (cond
               ;; A function return type is indented to the corresponding function arguments
               ((looking-at "->")
@@ -114,6 +131,16 @@
               ;; A closing brace is 1 level unindended
               ((looking-at "}") (- baseline rust-indent-offset))
 
+              ;;Line up method chains by their .'s
+              ((when (and rust-indent-method-chain
+                          (looking-at "\..+\(.*\);?\n"))
+                 (or
+                  (let ((method-indent (rust-align-to-method-chain)))
+                    (when method-indent
+                      (+ method-indent rust-indent-offset)))
+                  (+ baseline rust-indent-offset))))
+
+              
               ;; Doc comments in /** style with leading * indent to line up the *s
               ((and (nth 4 (syntax-ppss)) (looking-at "*"))
                (+ 1 baseline))