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

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

[nongnu] elpa/go-mode b990645 425/495: Fix call expr indenting after dan


From: ELPA Syncer
Subject: [nongnu] elpa/go-mode b990645 425/495: Fix call expr indenting after dangling operator.
Date: Sat, 7 Aug 2021 09:06:01 -0400 (EDT)

branch: elpa/go-mode
commit b990645d59dd6c54ac7f54608510a218f605848e
Author: Muir Manders <muir@retailnext.net>
Commit: Peter Sanford <psanford@sanford.io>

    Fix call expr indenting after dangling operator.
    
    We were indenting like
    
        f :=
                print(1,
                2,
        )
    
    because the "(" follows a line with a dangling operator, so we weren't
    adding an extra indent. I think this logic was to avoid giving an
    extra indent when a block opening "{" follows a line with a dangling
    operator. I added some extra logic to always indent the line after an
    opening "(".
    
    Similarly we were indenting like
    
        f :=
                int64(4 *
                3 *
                1)
    
    because "3"'s previous line was already indented due to a dangling
    operator. I added more logic to always indent in this case when the
    previous line opens a "(".
    
    Note that there are still other broken cases involving "{" because we
    can't currently differentiate between a block opening curly and a
    composite literal curly:
    
        f :=
                map[int]bool{
                1: 3,
        }
    
    Fixes #13
---
 go-mode.el                                         | 26 ++++++++++++++++++----
 test/go-indentation-test.el                        | 17 ++++++++++++++
 .../indentation_tests/dangling_operator.go         | 10 +++++++++
 3 files changed, 49 insertions(+), 4 deletions(-)

diff --git a/go-mode.el b/go-mode.el
index d515174..361c862 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -586,6 +586,17 @@ current line will be returned."
           (current-indentation))
       (current-indentation))))
 
+(defun go--line-opens-paren-p ()
+  "Returns whether current line opens a paren that contains point."
+  (save-excursion
+    (let ((start-paren-level (go-paren-level))
+          (line-beginning (line-beginning-position)))
+      (go-goto-opening-parenthesis)
+      (and
+       (eq (char-after) ?\() ; opening paren-like character is actually a paren
+       (< (go-paren-level) start-paren-level) ; point is before the closing 
paren
+       (>= (point) line-beginning))))) ; still on starting line
+
 (defun go-indentation-at-point ()
   (save-excursion
     (let (start-nesting)
@@ -597,20 +608,27 @@ current line will be returned."
         (current-indentation))
        ((looking-at "[])}]")
         (go-goto-opening-parenthesis)
-        (if (go-previous-line-has-dangling-op-p)
+        (if (and
+             (not (eq (char-after) ?\()) ; opening parens always indent
+             (go-previous-line-has-dangling-op-p))
             (- (current-indentation) tab-width)
           (go--indentation-for-opening-parenthesis)))
        ((progn (go--backward-irrelevant t)
                (looking-back go-dangling-operators-regexp
                              (- (point) go--max-dangling-operator-length)))
         ;; only one nesting for all dangling operators in one operation
-        (if (go-previous-line-has-dangling-op-p)
-            (current-indentation)
+        (if (and
+             (not (go--line-opens-paren-p))
+             (go-previous-line-has-dangling-op-p))
+            (progn
+              (current-indentation))
           (+ (current-indentation) tab-width)))
        ((zerop (go-paren-level))
         0)
        ((progn (go-goto-opening-parenthesis) (< (go-paren-level) 
start-nesting))
-        (if (go-previous-line-has-dangling-op-p)
+        (if (and
+             (not (eq (char-after) ?\()) ; opening parens always indent
+             (go-previous-line-has-dangling-op-p))
             (current-indentation)
           (+ (go--indentation-for-opening-parenthesis) tab-width)))
        (t
diff --git a/test/go-indentation-test.el b/test/go-indentation-test.el
new file mode 100644
index 0000000..a1be805
--- /dev/null
+++ b/test/go-indentation-test.el
@@ -0,0 +1,17 @@
+;;; go-indentation-test.el
+
+;; Copyright 2019 The go-mode Authors.  All rights reserved.
+;; Use of this source code is governed by a BSD-style
+;; license that can be found in the LICENSE file.
+
+(require 'ert)
+(require 'go-mode)
+
+(ert-deftest go--indent-line ()
+  (dolist (file (directory-files (expand-file-name 
"testdata/indentation_tests/") t ".*\\.go$"))
+    (with-temp-buffer
+      (go-mode)
+      (insert-file-contents file)
+      (let ((contents-before-indent (buffer-string)))
+        (indent-region (point-min) (point-max) nil)
+        (should (string= contents-before-indent (buffer-string)))))))
diff --git a/test/testdata/indentation_tests/dangling_operator.go 
b/test/testdata/indentation_tests/dangling_operator.go
index 9753358..6eff406 100644
--- a/test/testdata/indentation_tests/dangling_operator.go
+++ b/test/testdata/indentation_tests/dangling_operator.go
@@ -25,5 +25,15 @@ func init() {
                456,
        )
 
+       f :=
+               print(1,
+                       2,
+               )
+
+       g :=
+               int64(4 *
+                       3 *
+                       1)
+
        return
 }



reply via email to

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