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

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

[nongnu] elpa/go-mode e7e0157 447/495: indent: fix indent at top level o


From: ELPA Syncer
Subject: [nongnu] elpa/go-mode e7e0157 447/495: indent: fix indent at top level of file
Date: Sat, 7 Aug 2021 09:06:06 -0400 (EDT)

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

    indent: fix indent at top level of file
    
    We were always adding the (current-indentation) to the computed
    indent, but that is not correct for statements at the top
    level (i.e. outside any function). It was causing those statements to
    maintain their current indent instead of indenting to the beginning of
    the line.
    
    I added a new type of indent test so we can differentiate between the
    "maintains current indent" and "indent gets corrected" cases.
    
    Closes: #296 [via git-merge-pr]
---
 go-mode.el                  |  9 +++++++--
 test/go-indentation-test.el | 25 +++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/go-mode.el b/go-mode.el
index 91f4245..853de70 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -895,8 +895,13 @@ is done."
              (not (go--continuation-line-indents-p)))
         (cl-decf indent tab-width))
 
-      ;; Apply our computed indent relative to the indent of the ending line.
-      (+ indent (current-indentation)))))
+      ;; Apply our computed indent relative to the indent of the
+      ;; ending line, or 0 if we are at the top level.
+      (if (and
+           (= 0 (go-paren-level))
+           (not (go-previous-line-has-dangling-op-p)))
+          indent
+        (+ indent (current-indentation))))))
 
 (defconst go--operator-chars "*/%<>&\\^+\\-|=!,"
   "Individual characters that appear in operators.
diff --git a/test/go-indentation-test.el b/test/go-indentation-test.el
index a1be805..03b0f9c 100644
--- a/test/go-indentation-test.el
+++ b/test/go-indentation-test.el
@@ -15,3 +15,28 @@
       (let ((contents-before-indent (buffer-string)))
         (indent-region (point-min) (point-max) nil)
         (should (string= contents-before-indent (buffer-string)))))))
+
+(defun go--should-indent (input expected)
+  "Run `indent-region' against INPUT and make sure it matches EXPECTED."
+  (with-temp-buffer
+    (go-mode)
+    (insert input)
+    (indent-region (point-min) (point-max))
+    (should (string= (buffer-string) expected))))
+
+(ert-deftest go--indent-top-level ()
+  (go--should-indent
+   "
+package foo
+  var foo = 123 +
+    456 +
+    789
+"
+
+   "
+package foo
+var foo = 123 +
+       456 +
+       789
+"
+   ))



reply via email to

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