diff options
| author | Micah Chalmer <micah@micahchalmer.net> | 2015-02-23 00:29:33 -0500 |
|---|---|---|
| committer | Micah Chalmer <micah@micahchalmer.net> | 2015-02-23 00:29:33 -0500 |
| commit | 31ceb60a5b669f740be47f5ba3d3536531090a14 (patch) | |
| tree | 8d725b04315140b746888938d8c0afec0b42d3df /rust-mode.el | |
| parent | 249e3cef5d3dacfec1c1947637d009dafa0fad8d (diff) | |
| download | rust-mode-31ceb60a5b669f740be47f5ba3d3536531090a14.tar.gz | |
Indent inside strings after ending backslash
Diffstat (limited to 'rust-mode.el')
| -rw-r--r-- | rust-mode.el | 63 |
1 files changed, 56 insertions, 7 deletions
diff --git a/rust-mode.el b/rust-mode.el index 53d2e5e..fdc65b5 100644 --- a/rust-mode.el +++ b/rust-mode.el @@ -164,10 +164,58 @@ (when rust-indent-method-chain (rust-align-to-method-chain)) (save-excursion + (rust-rewind-irrelevant) (backward-up-list) (rust-rewind-to-beginning-of-current-level-expr) (+ (current-column) rust-indent-offset)))))) (cond + ;; Indent inside a non-raw string only if the the previous line + ;; ends with a backslash that is is inside the same string + ((nth 3 (syntax-ppss)) + (let* + ((string-begin-pos (nth 8 (syntax-ppss))) + (end-of-prev-line-pos (when (> (line-number-at-pos) 1) + (save-excursion + (previous-line) + (end-of-line) + (point))))) + (when + (and + ;; If the string begins with an "r" it's a raw string and + ;; we should not change the indentation + (/= ?r (char-after string-begin-pos)) + + ;; If we're on the first line this will be nil and the + ;; rest does not apply + end-of-prev-line-pos + + ;; The end of the previous line needs to be inside the + ;; current string... + (> end-of-prev-line-pos string-begin-pos) + + ;; ...and end with a backslash + (= ?\\ (char-before end-of-prev-line-pos))) + + ;; Indent to the same level as the previous line, or the + ;; start of the string if the previous line starts the string + (if (= (line-number-at-pos end-of-prev-line-pos) (line-number-at-pos string-begin-pos)) + ;; The previous line is the start of the string. + ;; If the backslash is the only character after the + ;; string beginning, indent to the next indent + ;; level. Otherwise align with the start of the string. + (if (> (- end-of-prev-line-pos string-begin-pos) 2) + (save-excursion + (goto-char (+ 1 string-begin-pos)) + (current-column)) + baseline) + + ;; The previous line is not the start of the string, so + ;; match its indentation. + (save-excursion + (goto-char end-of-prev-line-pos) + (back-to-indentation) + (current-column)))))) + ;; A function return type is indented to the corresponding function arguments ((looking-at "->") (save-excursion @@ -223,13 +271,14 @@ ;; so add one additional indent level (+ baseline rust-indent-offset)))))))))) - ;; If we're at the beginning of the line (before or at the current - ;; indentation), jump with the indentation change. Otherwise, save the - ;; excursion so that adding the indentations will leave us at the - ;; equivalent position within the line to where we were before. - (if (<= (current-column) (current-indentation)) - (indent-line-to indent) - (save-excursion (indent-line-to indent))))) + (when indent + ;; If we're at the beginning of the line (before or at the current + ;; indentation), jump with the indentation change. Otherwise, save the + ;; excursion so that adding the indentations will leave us at the + ;; equivalent position within the line to where we were before. + (if (<= (current-column) (current-indentation)) + (indent-line-to indent) + (save-excursion (indent-line-to indent)))))) ;; Font-locking definitions and helpers |
