emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/go-mode 9ab06b3 443/495: indent: fix nested dangling lines


From: ELPA Syncer
Subject: [nongnu] elpa/go-mode 9ab06b3 443/495: indent: fix nested dangling lines that don't use parens
Date: Sat, 7 Aug 2021 09:06:05 -0400 (EDT)

branch: elpa/go-mode
commit 9ab06b3deb1cbf00802d7824bf7107c31865f9fb
Author: Muir Manders <muir@mnd.rs>
Commit: Peter Sanford <psanford@sanford.io>

    indent: fix nested dangling lines that don't use parens
    
    We weren't handling cases like:
    
    1. if foo ||
    2.   foo &&
    3.    foo {
    4.   code()
    5. }
    
    We were indenting line 4 too far because we weren't following the
    dangling lines all the way up to line 1. There can be arbitrary
    amounts of nested indents with no parentheses, so to find the indent
    inside the "if" block you must follow all dangling lines to the
    beginning of the statement.
    
    I also fixed a couple other minor indent issues:
     - fix an edge case with multi-line case statements where we weren't
       indenting the final line with the colon
     - fix go--end-of-line to skip multiple /* */ style comments at the
       end of the line
    
    Closes: #292 [via git-merge-pr]
---
 go-mode.el                                         | 64 ++++++++++++++++------
 test/testdata/indentation_tests/comments.go        |  7 +++
 .../indentation_tests/dangling_operator.go         | 14 +++++
 test/testdata/indentation_tests/multiline_if.go    | 14 +++++
 test/testdata/indentation_tests/switch.go          |  7 +++
 5 files changed, 90 insertions(+), 16 deletions(-)

diff --git a/go-mode.el b/go-mode.el
index 089b220..1c85c63 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -590,6 +590,7 @@ case keyword. It returns nil for the case line itself."
                 (or
                  (go--boring-line-p)
                  (go--line-suffix-p ","))
+                (not (looking-at go--case-regexp))
                 (go--forward-line -1)))
 
         (and
@@ -656,21 +657,33 @@ encounters a closing paren or brace it bounces to the 
corresponding
 opener. If it arrives at the beginning of the line you are indenting,
 it moves to the end of the previous line if the current line is a
 continuation line, else it moves to the containing opening paren or
-brace. If it arrives at the beginning of a line other than the
-starting line, it is done."
+brace. If it arrives at the beginning of a line other than the line
+you are indenting, it will continue to the previous dangling line if
+the line you are indenting was not a continuation line, otherwise it
+is done."
   (save-excursion
     (beginning-of-line)
 
-    (let ((start-line (point))
+    (let (
+          ;; Beginning of our starting line.
+          (start-line (point))
+
+          ;; Whether this is our first iteration of the outer while loop.
           (first t)
+
+          ;; Whether we start in a block (i.e. our first line is not a
+          ;; continuation line and is in an "if", "for", "func" etc. block).
+          (in-block)
+
+          ;; Our desired indent relative to our ending line's indent.
           (indent 0))
 
       ;; Skip leading whitespace.
       (skip-syntax-forward " ")
 
-      ;; Move after following char if it is a closer. This is so below code
-      ;; sees it and jumps to the corresponding opener.
-      (skip-syntax-forward ")" (1+ (point)))
+      ;; Decrement indent if the first character on the line is a closer.
+      (when (or (eq (char-after) ?\)) (eq (char-after) ?}))
+        (cl-decf indent tab-width))
 
       (while (or
               ;; Always run the first iteration so we process empty lines.
@@ -695,8 +708,8 @@ starting line, it is done."
              ;; relative to the opener's line, and that indent should not
              ;; be inherited by our starting line.
              (when (and
-                    ;; Opener wasn't on our starting line.
-                    (< bol start-line)
+                    ;; We care about dangling expressions, not child blocks.
+                    (not in-block)
 
                     ;; Opener and closer aren't on same line.
                     (< (point) bol)
@@ -736,7 +749,24 @@ starting line, it is done."
             ;; If we aren't a continuation line and we have an enclosing paren
             ;; or brace, jump to opener and increment our indent.
             (when (go-goto-opening-parenthesis)
-              (cl-incf indent tab-width)))))
+              ;; We started in a child block if our opener is a curly brace.
+              (setq in-block (and
+                              (eq (char-after) ?{)
+                              (looking-back "[^[:space:]][[:space:]]" (- 
(point) 2))))
+              (cl-incf indent tab-width))))
+
+        ;; If we stared in a child block we must follow dangling lines
+        ;; until they don't dangle anymore. This is to handle cases like:
+        ;;
+        ;; if foo ||
+        ;;      foo &&
+        ;;        foo {
+        ;;   X
+        ;;
+        ;; There can be an arbitrary number of indents, so we must go back to
+        ;; the "if" to determine the indent of "X".
+        (when (and in-block (bolp) (go-previous-line-has-dangling-op-p))
+          (goto-char (go-previous-line-has-dangling-op-p))))
 
       ;; If our ending line is a continuation line but doesn't open
       ;; an extra indent, reduce indent. We tentatively gave indents to all
@@ -846,13 +876,15 @@ foo ||
 Point will be left before any trailing comments. Point will be left
 after the opening backtick of multiline strings."
   (end-of-line)
-  (skip-syntax-backward " ")
-  (when (looking-back "\\*/" (- (point) 2))
-    ;; back up so we are in the /* comment */
-    (backward-char))
-  (when (go-in-comment-p)
-    (go-goto-beginning-of-string-or-comment)
-    (skip-syntax-backward " "))
+  (let ((keep-going t))
+    (while keep-going
+      (skip-syntax-backward " ")
+      (when (looking-back "\\*/" (- (point) 2))
+        ;; back up so we are in the /* comment */
+        (backward-char))
+      (if (go-in-comment-p)
+          (go-goto-beginning-of-string-or-comment)
+        (setq keep-going nil))))
   (when (go-in-string-p)
     (go-goto-beginning-of-string-or-comment)
     ;; forward one so point is after the opening "`"
diff --git a/test/testdata/indentation_tests/comments.go 
b/test/testdata/indentation_tests/comments.go
new file mode 100644
index 0000000..9322f68
--- /dev/null
+++ b/test/testdata/indentation_tests/comments.go
@@ -0,0 +1,7 @@
+package comments
+
+func _() {
+       if foo {
+               X /* why */ /* do this */
+       }
+}
diff --git a/test/testdata/indentation_tests/dangling_operator.go 
b/test/testdata/indentation_tests/dangling_operator.go
index 55fe125..f8f77e0 100644
--- a/test/testdata/indentation_tests/dangling_operator.go
+++ b/test/testdata/indentation_tests/dangling_operator.go
@@ -265,6 +265,20 @@ lol` +
                2 +
                3
 
+       foo ||
+               foo &&
+                       foo(
+                               123,
+                       )
+
+       foo ||
+               foo &&
+                       foo{
+                               {
+                                       foo: bar,
+                               },
+                       }
+
        return 123,
                456
 }
diff --git a/test/testdata/indentation_tests/multiline_if.go 
b/test/testdata/indentation_tests/multiline_if.go
index e3ae385..5d6d732 100644
--- a/test/testdata/indentation_tests/multiline_if.go
+++ b/test/testdata/indentation_tests/multiline_if.go
@@ -135,4 +135,18 @@ func _() {
                return
        }
 
+       if foo ||
+               foo &&
+                       foo ==
+                               foo+
+                                       foo*
+                                               foo {
+               foo
+       }
+
+       if foo() ||
+               foo() &&
+                       foo() {
+               foo
+       }
 }
diff --git a/test/testdata/indentation_tests/switch.go 
b/test/testdata/indentation_tests/switch.go
index 71a58ed..89a6991 100644
--- a/test/testdata/indentation_tests/switch.go
+++ b/test/testdata/indentation_tests/switch.go
@@ -66,4 +66,11 @@ func main() {
        // also works
        default:
        }
+
+       switch {
+       case 1:
+       case foo,
+               foo,
+               foo:
+       }
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]