|
54 | 54 | :type 'integer
|
55 | 55 | :group 'rust-mode)
|
56 | 56 |
|
| 57 | +(defcustom rust-indent-method-chain nil |
| 58 | + "Indent Rust method chains, aligned by the '.' operators" |
| 59 | + :type 'boolean |
| 60 | + :group 'rust-mode) |
| 61 | + |
57 | 62 | (defun rust-paren-level () (nth 0 (syntax-ppss)))
|
58 | 63 | (defun rust-in-str-or-cmnt () (nth 8 (syntax-ppss)))
|
59 | 64 | (defun rust-rewind-past-str-cmnt () (goto-char (nth 8 (syntax-ppss))))
|
|
73 | 78 | ;; open bracket ends the line
|
74 | 79 | (when (not (looking-at "[[:blank:]]*\\(?://.*\\)?$"))
|
75 | 80 | (when (looking-at "[[:space:]]")
|
76 |
| - (forward-word 1) |
77 |
| - (backward-word 1)) |
| 81 | + (forward-word 1) |
| 82 | + (backward-word 1)) |
78 | 83 | (current-column))))
|
79 | 84 |
|
| 85 | +(defun rust-align-to-method-chain () |
| 86 | + (save-excursion |
| 87 | + (previous-line) |
| 88 | + (end-of-line) |
| 89 | + (backward-word 1) |
| 90 | + (backward-char) |
| 91 | + (when (looking-at "\\..+\(.*\)\n") |
| 92 | + (- (current-column) rust-indent-offset)))) |
| 93 | + |
80 | 94 | (defun rust-rewind-to-beginning-of-current-level-expr ()
|
81 | 95 | (let ((current-level (rust-paren-level)))
|
82 | 96 | (back-to-indentation)
|
|
99 | 113 | ;; the inside of it correctly relative to the outside.
|
100 | 114 | (if (= 0 level)
|
101 | 115 | 0
|
| 116 | + (or |
| 117 | + (when rust-indent-method-chain |
| 118 | + (rust-align-to-method-chain)) |
102 | 119 | (save-excursion
|
103 | 120 | (backward-up-list)
|
104 | 121 | (rust-rewind-to-beginning-of-current-level-expr)
|
105 |
| - (+ (current-column) rust-indent-offset))))) |
| 122 | + (+ (current-column) rust-indent-offset)))))) |
106 | 123 | (cond
|
107 | 124 | ;; A function return type is indented to the corresponding function arguments
|
108 | 125 | ((looking-at "->")
|
|
114 | 131 | ;; A closing brace is 1 level unindended
|
115 | 132 | ((looking-at "}") (- baseline rust-indent-offset))
|
116 | 133 |
|
| 134 | + ;;Line up method chains by their .'s |
| 135 | + ((when (and rust-indent-method-chain |
| 136 | + (looking-at "\..+\(.*\);?\n")) |
| 137 | + (or |
| 138 | + (let ((method-indent (rust-align-to-method-chain))) |
| 139 | + (when method-indent |
| 140 | + (+ method-indent rust-indent-offset))) |
| 141 | + (+ baseline rust-indent-offset)))) |
| 142 | + |
| 143 | + |
117 | 144 | ;; Doc comments in /** style with leading * indent to line up the *s
|
118 | 145 | ((and (nth 4 (syntax-ppss)) (looking-at "*"))
|
119 | 146 | (+ 1 baseline))
|
|
0 commit comments