[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/rust-mode b2c9e05 069/486: Emacs: indent relative to enclo
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/rust-mode b2c9e05 069/486: Emacs: indent relative to enclosing block |
Date: |
Sat, 7 Aug 2021 09:24:50 -0400 (EDT) |
branch: elpa/rust-mode
commit b2c9e05bc9275bc180b0372bd7225b47c525993c
Author: Micah Chalmer <micah@micahchalmer.net>
Commit: Micah Chalmer <micah@micahchalmer.net>
Emacs: indent relative to enclosing block
This changes the indent to calculate positions relative to the enclosing
block (or braced/parenthesized expression), rather than by an absolute
nesting level within the whole file. This allows things like this to
work:
let x =
match expr {
Pattern => ...
}
With the old method, only one level of nesting would be added within the
match braces, so "Pattern" would have ended up aligned with the match.
The other change is that multiple parens/braces on the same line only
increase the indent once. This is a very common case for passing
closures/procs. The absolute nesting method would do this:
spawn(proc() {
// Indented out two indent levels...
})
whereas the code in this commit does this:
spawn(proc() {
// Indented out only one level...
})
---
rust-mode-tests.el | 64 ++++++++++++++++++++++++++++++++++
rust-mode.el | 100 +++++++++++++++++++++++++++++++++--------------------
2 files changed, 127 insertions(+), 37 deletions(-)
diff --git a/rust-mode-tests.el b/rust-mode-tests.el
index c0543b6..bdd7d63 100644
--- a/rust-mode-tests.el
+++ b/rust-mode-tests.el
@@ -452,6 +452,70 @@ fn foo() {
"
))
+(ert-deftest indent-indented-match ()
+ (test-indent
+ "
+fn foo() {
+ let x =
+ match blah {
+ Pattern |
+ Pattern2 => {
+ hello()
+ },
+ _ => whatever
+ };
+ y();
+}
+"
+ ))
+
+(ert-deftest indent-curly-braces-within-parens ()
+ (test-indent
+ "
+fn foo() {
+ let x =
+ foo(bar(|x| {
+ only_one_indent_here();
+ }));
+ y();
+}
+"
+ ))
+
+(ert-deftest indent-weirdly-indented-block ()
+ (rust-test-manip-code
+ "
+fn foo() {
+ {
+this_block_is_over_to_the_left_for_some_reason();
+ }
+
+}
+"
+ 16
+ #'indent-for-tab-command
+ "
+fn foo() {
+ {
+ this_block_is_over_to_the_left_for_some_reason();
+ }
+
+}
+"
+ ))
+
+(ert-deftest indent-multi-line-attrib ()
+ (test-indent
+ "
+#[attrib(
+ this,
+ that,
+ theotherthing)]
+mod function_with_multiline_attribute() {}
+"
+ ))
+
+
;; Make sure that in effort to cover match patterns we don't mistreat || or
expressions
(ert-deftest indent-nonmatch-or-expression ()
(test-indent
diff --git a/rust-mode.el b/rust-mode.el
index a2e1fc0..b304df8 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -59,61 +59,87 @@
(backward-word 1))
(current-column))))
+(defun rust-rewind-to-beginning-of-current-level-expr ()
+ (let ((current-level (rust-paren-level)))
+ (back-to-indentation)
+ (while (> (rust-paren-level) current-level)
+ (backward-up-list)
+ (back-to-indentation))))
+
(defun rust-mode-indent-line ()
(interactive)
(let ((indent
(save-excursion
(back-to-indentation)
- (let ((level (rust-paren-level)))
+ ;; Point is now at beginning of current line
+ (let* ((level (rust-paren-level))
+ (baseline
+ ;; Our "baseline" is one level out from the indentation of
the expression
+ ;; containing the innermost enclosing opening bracket. That
+ ;; way if we are within a block that has a different
+ ;; indentation than this mode would give it, we still indent
+ ;; the inside of it correctly relative to the outside.
+ (if (= 0 level)
+ 0
+ (save-excursion
+ (backward-up-list)
+ (rust-rewind-to-beginning-of-current-level-expr)
+ (+ (current-column) rust-indent-offset)))))
(cond
;; A function return type is indented to the corresponding
function arguments
((looking-at "->")
(save-excursion
(backward-list)
(or (rust-align-to-expr-after-brace)
- (* rust-indent-offset (+ 1 level)))))
+ (+ baseline rust-indent-offset))))
;; A closing brace is 1 level unindended
- ((looking-at "}") (* rust-indent-offset (- level 1)))
+ ((looking-at "}") (- baseline rust-indent-offset))
;; Doc comments in /** style with leading * indent to line up
the *s
((and (nth 4 (syntax-ppss)) (looking-at "*"))
- (+ 1 (* rust-indent-offset level)))
+ (+ 1 baseline))
;; If we're in any other token-tree / sexp, then:
- ;; - [ or ( means line up with the opening token
- ;; - { means indent to either nesting-level *
rust-indent-offset,
- ;; or one further indent from that if either current line
- ;; begins with 'else', or previous line didn't end in
- ;; semi, comma, brace or single pipe (other than whitespace
and line
- ;; comments) , and wasn't an attribute. But if we have
- ;; something after the open brace and ending with a comma,
- ;; treat it as fields and align them. PHEW.
- ((> level 0)
- (let ((pt (point)))
- (rust-rewind-irrelevant)
- (backward-up-list)
- (or (and (looking-at "[[({]")
- (rust-align-to-expr-after-brace))
- (progn
- (goto-char pt)
- (back-to-indentation)
- (if (looking-at "\\<else\\>")
- (* rust-indent-offset (+ 1 level))
- (progn
- (goto-char pt)
- (beginning-of-line)
- (rust-rewind-irrelevant)
- (end-of-line)
- (if (looking-back
"\\(?:[(,:;?[{}]\\|[^|]|\\)[[:space:]]*\\(?://.*\\)?")
- (* rust-indent-offset level)
- (back-to-indentation)
- (if (looking-at "#")
- (* rust-indent-offset level)
- (* rust-indent-offset (+ 1 level))))))))))
-
- ;; Otherwise we're in a column-zero definition
- (t 0))))))
+ (t
+ (or
+ ;; If we are inside a pair of braces, with something after the
+ ;; open brace on the same line and ending with a comma, treat
+ ;; it as fields and align them.
+ (when (> level 0)
+ (save-excursion
+ (rust-rewind-irrelevant)
+ (backward-up-list)
+ ;; Point is now at the beginning of the containing set of
braces
+ (rust-align-to-expr-after-brace)))
+
+ (progn
+ (back-to-indentation)
+ ;; Point is now at the beginning of the current line
+ (if (or
+ ;; If this line begins with "else" or "{", stay on the
+ ;; baseline as well (we are continuing an expression,
+ ;; but the "else" or "{" should align with the beginning
+ ;; of the expression it's in.)
+ (looking-at "\\<else\\>\\|{")
+
+ (save-excursion
+ (rust-rewind-irrelevant)
+ ;; Point is now at the end of the previous ine
+ (or
+ ;; If we are at the first line, no indentation is
needed, so stay at baseline...
+ (= 1 (line-number-at-pos (point)))
+ ;; ..or if the previous line ends with any of these:
+ ;; { ? : ( , ; [ }
+ ;; then we are at the beginning of an expression, so
stay on the baseline...
+ (looking-back "[(,:;?[{}]\\|[^|]|")
+ ;; or if the previous line is the end of an
attribute, stay at the baseline...
+ (progn
(rust-rewind-to-beginning-of-current-level-expr) (looking-at "#")))))
+ baseline
+
+ ;; Otherwise, we are continuing the same expression from
the previous line,
+ ;; so add one additional indent level
+ (+ baseline rust-indent-offset))))))))))
(when (not (eq (current-indentation) indent))
;; If we're at the beginning of the line (before or at the current
;; indentation), jump with the indentation change. Otherwise, save the
- [nongnu] elpa/rust-mode d82b5a7 062/486: Add "proc" as keyword to emacs mode, (continued)
- [nongnu] elpa/rust-mode d82b5a7 062/486: Add "proc" as keyword to emacs mode, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 891716a 059/486: Fix single-line-style paragraph fills with code immediately before or after, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 6b323f3 064/486: Add crate to emacs and kate modefiles, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 4f40156 071/486: Emacs: Remove README warning of old whitespace bug, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode ef048b3 082/486: emacs: highlight `#[foo = "bar"]` attributes, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode d4264a9 065/486: extern mod => extern crate, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode f3c784f 083/486: Update repo location, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 0617f1c 078/486: Recommend autoload instead of require, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode a8fad0f 085/486: emacs: Don't overwrite font lock for attributes, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 9c94ec4 079/486: auto merge of #13922 : Rufflewind/rust/patch-1, r=pnkfelix, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode b2c9e05 069/486: Emacs: indent relative to enclosing block,
ELPA Syncer <=
- [nongnu] elpa/rust-mode 336c052 087/486: emacs: Remove outdated references to ~ in tests, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 8622310 091/486: Add 'move' keyword to emacs, kate, and vim editor modes., ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 4ccac8e 095/486: Update editor syntax files for isize/usize., ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 2a81393 094/486: added optional method chain indentations for emacs major mode, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 079f0c2 093/486: Update emacs and vi modes., ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 0b9a7e0 092/486: rust-mode.el: Tweak syntax table, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 67f4832 099/486: Fix tests broken by trailing whitespace removal, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode f292554 097/486: Changed rust-mode code for tabs -> spaces, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 4659aee 098/486: rollup merge of #19913: KOMON/rust-mode-emacs-indentation, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode e6e16cc 102/486: Add test for closing braces in character literals, ELPA Syncer, 2021/08/07