|
49 | 49 | :type 'integer
|
50 | 50 | :group 'rust-mode)
|
51 | 51 |
|
| 52 | +(defcustom rust-indent-method-chain nil |
| 53 | + "Indent Rust method chains, aligned by the '.' operators" |
| 54 | + :type 'boolean |
| 55 | + :group 'rust-mode) |
| 56 | + |
52 | 57 | (defun rust-paren-level () (nth 0 (syntax-ppss)))
|
53 | 58 | (defun rust-in-str-or-cmnt () (nth 8 (syntax-ppss)))
|
54 | 59 | (defun rust-rewind-past-str-cmnt () (goto-char (nth 8 (syntax-ppss))))
|
|
72 | 77 | (backward-word 1))
|
73 | 78 | (current-column))))
|
74 | 79 |
|
| 80 | +(defun rust-align-to-method-chain () |
| 81 | + (save-excursion |
| 82 | + (previous-line) |
| 83 | + (end-of-line) |
| 84 | + (backward-word 1) |
| 85 | + (backward-char) |
| 86 | + (when (looking-at "\\..+\(.*\)\n") |
| 87 | + (- (current-column) rust-indent-offset)))) |
| 88 | + |
75 | 89 | (defun rust-rewind-to-beginning-of-current-level-expr ()
|
76 | 90 | (let ((current-level (rust-paren-level)))
|
77 | 91 | (back-to-indentation)
|
|
94 | 108 | ;; the inside of it correctly relative to the outside.
|
95 | 109 | (if (= 0 level)
|
96 | 110 | 0
|
| 111 | + (or |
| 112 | + (when rust-indent-method-chain |
| 113 | + (rust-align-to-method-chain)) |
97 | 114 | (save-excursion
|
98 | 115 | (backward-up-list)
|
99 | 116 | (rust-rewind-to-beginning-of-current-level-expr)
|
100 |
| - (+ (current-column) rust-indent-offset))))) |
| 117 | + (+ (current-column) rust-indent-offset)))))) |
101 | 118 | (cond
|
102 | 119 | ;; A function return type is indented to the corresponding function arguments
|
103 | 120 | ((looking-at "->")
|
|
109 | 126 | ;; A closing brace is 1 level unindended
|
110 | 127 | ((looking-at "}") (- baseline rust-indent-offset))
|
111 | 128 |
|
| 129 | + ;;Line up method chains by their .'s |
| 130 | + ((when (and rust-indent-method-chain |
| 131 | + (looking-at "\..+\(.*\);?\n")) |
| 132 | + (or |
| 133 | + (let ((method-indent (rust-align-to-method-chain))) |
| 134 | + (when method-indent |
| 135 | + (+ method-indent rust-indent-offset))) |
| 136 | + (+ baseline rust-indent-offset)))) |
| 137 | + |
| 138 | + |
112 | 139 | ;; Doc comments in /** style with leading * indent to line up the *s
|
113 | 140 | ((and (nth 4 (syntax-ppss)) (looking-at "*"))
|
114 | 141 | (+ 1 baseline))
|
|
0 commit comments