[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/rust-mode 31ceb60 135/486: Indent inside strings after end
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/rust-mode 31ceb60 135/486: Indent inside strings after ending backslash |
Date: |
Sat, 7 Aug 2021 09:25:05 -0400 (EDT) |
branch: elpa/rust-mode
commit 31ceb60a5b669f740be47f5ba3d3536531090a14
Author: Micah Chalmer <micah@micahchalmer.net>
Commit: Micah Chalmer <micah@micahchalmer.net>
Indent inside strings after ending backslash
---
rust-mode-tests.el | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
rust-mode.el | 63 ++++++++++++++++++++++++++++++++++-----
2 files changed, 141 insertions(+), 9 deletions(-)
diff --git a/rust-mode-tests.el b/rust-mode-tests.el
index 4b32e94..310eeaf 100644
--- a/rust-mode-tests.el
+++ b/rust-mode-tests.el
@@ -284,8 +284,8 @@ very very very long string
*/"
))
-(defun test-indent (indented)
- (let ((deindented (replace-regexp-in-string "^[[:blank:]]*" " "
indented)))
+(defun test-indent (indented &optional deindented)
+ (let ((deindented (or deindented (replace-regexp-in-string "^[[:blank:]]*" "
" indented))))
(rust-test-manip-code
deindented
1
@@ -1207,3 +1207,86 @@ impl Foo for Bar {
}
"
))
+
+(ert-deftest test-indent-string-with-eol-backslash ()
+ (test-indent
+ "
+pub fn foo() {
+ format!(\"abc \\
+ def\")
+}
+"
+ ))
+
+(ert-deftest test-indent-string-with-eol-backslash-at-start ()
+ (test-indent
+ "
+pub fn foo() {
+ format!(\"\\
+ abc \\
+ def\")
+}
+"
+ ))
+
+(ert-deftest test-indent-string-without-eol-backslash-indent-is-not-touched ()
+ (test-indent
+ "
+pub fn foo() {
+ format!(\"
+abc
+def\");
+}
+
+pub fn foo() {
+ format!(\"la la la
+la
+la la\");
+}
+"
+ ;; Should still indent the code parts but leave the string internals alone:
+ "
+ pub fn foo() {
+ format!(\"
+abc
+def\");
+}
+
+pub fn foo() {
+ format!(\"la la la
+la
+la la\");
+ }
+"
+ ))
+
+(ert-deftest test-indent-string-eol-backslash-mixed-with-literal-eol ()
+ (test-indent
+ "
+fn foo() {
+ println!(\"
+Here is the beginning of the string
+ and here is a line that is arbitrarily indented \\
+ and a continuation of that indented line
+ and another arbitrary indentation
+ still another
+ yet another \\
+ with a line continuing it
+And another line not indented
+\")
+}
+"
+ "
+fn foo() {
+ println!(\"
+Here is the beginning of the string
+ and here is a line that is arbitrarily indented \\
+ and a continuation of that indented line
+ and another arbitrary indentation
+ still another
+ yet another \\
+with a line continuing it
+And another line not indented
+\")
+}
+"))
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
- [nongnu] elpa/rust-mode 186583d 096/486: Mark _ as a word constituent to avoid highlighting parts of identifiers as keywords, (continued)
- [nongnu] elpa/rust-mode 186583d 096/486: Mark _ as a word constituent to avoid highlighting parts of identifiers as keywords, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 04e4b49 103/486: Add .travis.yml, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode ec81a95 114/486: Merge pull request #22 from nikomatsakis/align-method-chain, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 351cc91 112/486: Merge pull request #16 from MicahChalmer/add-travis, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode b3f87b9 122/486: Make the test driver script a little more informative., ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 19bc0e9 121/486: Merge pull request #30 from MicahChalmer/emacs23-fixup, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode bddc933 124/486: Fix typo in comment, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 97f6445 132/486: Merge pull request #38 from MicahChalmer/fix-issue-36, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 42730b1 144/486: Merge pull request #49 from huonw/playpen, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 249e3ce 134/486: Merge pull request #40 from MicahChalmer/fix-comment-typos, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 31ceb60 135/486: Indent inside strings after ending backslash,
ELPA Syncer <=
- [nongnu] elpa/rust-mode 22c8cfa 138/486: Merge pull request #43 from MicahChalmer/indent-backslash-strings, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode ce976b9 145/486: Fix "-> Type" on a single line indentation, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode f2f4d67 158/486: Merge pull request #59 from lunaryorn/fix-byte-compiler-warnings, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode ddf45ed 146/486: Use (goto-char (point-max)) instead of (end-of-buffer) to silence warning., ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 0077bbf 160/486: Fix package.el URL, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 86db9bd 166/486: Merge pull request #65 from tromey/blink-matching-paren, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 53c558c 175/486: Factor out rust-conditional-re-search-forward, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 5231709 177/486: Speed up calls to "looking-back", ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode c801dda 197/486: Merge pull request #77 from tromey/fix-byte-compiler-add-lexbind, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode cdaf3fc 201/486: Update MELPA URL, ELPA Syncer, 2021/08/07