|
164 | 164 | (when rust-indent-method-chain
|
165 | 165 | (rust-align-to-method-chain))
|
166 | 166 | (save-excursion
|
| 167 | + (rust-rewind-irrelevant) |
167 | 168 | (backward-up-list)
|
168 | 169 | (rust-rewind-to-beginning-of-current-level-expr)
|
169 | 170 | (+ (current-column) rust-indent-offset))))))
|
170 | 171 | (cond
|
| 172 | + ;; Indent inside a non-raw string only if the the previous line |
| 173 | + ;; ends with a backslash that is is inside the same string |
| 174 | + ((nth 3 (syntax-ppss)) |
| 175 | + (let* |
| 176 | + ((string-begin-pos (nth 8 (syntax-ppss))) |
| 177 | + (end-of-prev-line-pos (when (> (line-number-at-pos) 1) |
| 178 | + (save-excursion |
| 179 | + (previous-line) |
| 180 | + (end-of-line) |
| 181 | + (point))))) |
| 182 | + (when |
| 183 | + (and |
| 184 | + ;; If the string begins with an "r" it's a raw string and |
| 185 | + ;; we should not change the indentation |
| 186 | + (/= ?r (char-after string-begin-pos)) |
| 187 | + |
| 188 | + ;; If we're on the first line this will be nil and the |
| 189 | + ;; rest does not apply |
| 190 | + end-of-prev-line-pos |
| 191 | + |
| 192 | + ;; The end of the previous line needs to be inside the |
| 193 | + ;; current string... |
| 194 | + (> end-of-prev-line-pos string-begin-pos) |
| 195 | + |
| 196 | + ;; ...and end with a backslash |
| 197 | + (= ?\\ (char-before end-of-prev-line-pos))) |
| 198 | + |
| 199 | + ;; Indent to the same level as the previous line, or the |
| 200 | + ;; start of the string if the previous line starts the string |
| 201 | + (if (= (line-number-at-pos end-of-prev-line-pos) (line-number-at-pos string-begin-pos)) |
| 202 | + ;; The previous line is the start of the string. |
| 203 | + ;; If the backslash is the only character after the |
| 204 | + ;; string beginning, indent to the next indent |
| 205 | + ;; level. Otherwise align with the start of the string. |
| 206 | + (if (> (- end-of-prev-line-pos string-begin-pos) 2) |
| 207 | + (save-excursion |
| 208 | + (goto-char (+ 1 string-begin-pos)) |
| 209 | + (current-column)) |
| 210 | + baseline) |
| 211 | + |
| 212 | + ;; The previous line is not the start of the string, so |
| 213 | + ;; match its indentation. |
| 214 | + (save-excursion |
| 215 | + (goto-char end-of-prev-line-pos) |
| 216 | + (back-to-indentation) |
| 217 | + (current-column)))))) |
| 218 | + |
171 | 219 | ;; A function return type is indented to the corresponding function arguments
|
172 | 220 | ((looking-at "->")
|
173 | 221 | (save-excursion
|
|
223 | 271 | ;; so add one additional indent level
|
224 | 272 | (+ baseline rust-indent-offset))))))))))
|
225 | 273 |
|
226 |
| - ;; If we're at the beginning of the line (before or at the current |
227 |
| - ;; indentation), jump with the indentation change. Otherwise, save the |
228 |
| - ;; excursion so that adding the indentations will leave us at the |
229 |
| - ;; equivalent position within the line to where we were before. |
230 |
| - (if (<= (current-column) (current-indentation)) |
231 |
| - (indent-line-to indent) |
232 |
| - (save-excursion (indent-line-to indent))))) |
| 274 | + (when indent |
| 275 | + ;; If we're at the beginning of the line (before or at the current |
| 276 | + ;; indentation), jump with the indentation change. Otherwise, save the |
| 277 | + ;; excursion so that adding the indentations will leave us at the |
| 278 | + ;; equivalent position within the line to where we were before. |
| 279 | + (if (<= (current-column) (current-indentation)) |
| 280 | + (indent-line-to indent) |
| 281 | + (save-excursion (indent-line-to indent)))))) |
233 | 282 |
|
234 | 283 |
|
235 | 284 | ;; Font-locking definitions and helpers
|
|
0 commit comments