[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/go-mode d387378 438/495: indent: handle comments after mul
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/go-mode d387378 438/495: indent: handle comments after multiline case lists |
Date: |
Sat, 7 Aug 2021 09:06:04 -0400 (EDT) |
branch: elpa/go-mode
commit d387378ca3c1241d7b2fa8e63804e273851830e4
Author: Muir Manders <muir@mnd.rs>
Commit: Peter Sanford <psanford@sanford.io>
indent: handle comments after multiline case lists
Add some utility methods for dealing with lines while ignoring
trailing comments and multiline strings. In particular, use
go--line-suffix-p to check for trailing commas and colons in
go--in-case-clause-list-p so we don't get tripped up by trailing
comments.
There is a similar existing function go--backward-irrelevant, but that
skips case statements, so I didn't want to mess with it for now.
Closes: #288 [via git-merge-pr]
---
go-mode.el | 61 ++++++++++++++++++++++++++++---
test/testdata/indentation_tests/switch.go | 4 +-
2 files changed, 58 insertions(+), 7 deletions(-)
diff --git a/go-mode.el b/go-mode.el
index bbc0de2..d669442 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -656,15 +656,16 @@ case keyword. It returns nil for the case line itself."
(when (not (looking-at go--case-or-default-regexp))
(let (saw-colon)
;; optionally skip line with the colon
- (when (looking-at ".*:[[:space:]]*$")
+ (when (go--line-suffix-p ":")
(setq saw-colon t)
(forward-line -1))
;; go backwards while at a comment or a line ending in comma
(while (and
- (or (go-in-comment-p)
- (looking-at
"[[:space:]]*\\(//\\|/\\*\\)\\|.*,[[:space:]]*$"))
- (zerop (forward-line -1))))
+ (or
+ (go--boring-line-p)
+ (go--line-suffix-p ","))
+ (go--forward-line -1)))
(and
(looking-at go--case-regexp)
@@ -673,7 +674,7 @@ case keyword. It returns nil for the case line itself."
(not (and saw-colon (looking-at ".*:[[:space:]]*$"))))))))
(defun go--in-struct-definition-p ()
- "Return non-nil if inside a struct definition."
+ "Return non-nil if point is inside a struct definition."
(save-excursion
(and
;; inside curlies
@@ -754,6 +755,56 @@ The return value is the position of the opening paren."
)
(t
(current-indentation))))))
+(defun go--end-of-line ()
+ "Move to the end of the code on the current line.
+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 " "))
+ (when (go-in-string-p)
+ (go-goto-beginning-of-string-or-comment)
+ ;; forward one so point is after the opening "`"
+ (forward-char)))
+
+(defun go--line-suffix-p (re)
+ "Return non-nil if RE matches the end of the line starting from `point'.
+
+Trailing whitespace, trailing comments and trailing multiline strings are
+ignored."
+ (let ((start (point))
+ (end (save-excursion (go--end-of-line) (point))))
+ (when (< start end)
+ (string-match-p
+ (concat "\\(?:" re "\\)$")
+ (buffer-substring-no-properties start end)))))
+
+(defun go--boring-line-p ()
+ "Return non-nil if the current line probably doesn't impact indentation.
+
+A boring line is one that starts with a comment, is empty, is part of a
+multiline comment, or starts and ends in a multiline string."
+ (or
+ (looking-at (concat go--comment-start-regexp "\\|[[:space:]]*$"))
+ (go-in-comment-p)
+ (and (go-in-string-p) (save-excursion (end-of-line) (go-in-string-p)))))
+
+(defun go--forward-line (&optional count)
+ "Like `forward-line' but skip comments and empty lines.
+
+Return non-nil if point changed lines."
+ (let (moved)
+ (while (and
+ (zerop (forward-line count))
+ (setq moved t)
+ (go--boring-line-p))
+ (setq count (if (and count (< count 0 )) -1 1)))
+ moved))
(defconst go--comment-start-regexp "[[:space:]]*\\(/\\*\\|//\\)")
diff --git a/test/testdata/indentation_tests/switch.go
b/test/testdata/indentation_tests/switch.go
index 07fd34a..d0b99e9 100644
--- a/test/testdata/indentation_tests/switch.go
+++ b/test/testdata/indentation_tests/switch.go
@@ -26,8 +26,8 @@ func main() {
case
/* hi
there */
- "hi",
- "there":
+ "hi", // hi
+ "there": // there
}
switch {
- [nongnu] elpa/go-mode 120fb91 419/495: Use "guru -tags=xxx yyy", not "guru -tags=xxx, yyy", (continued)
- [nongnu] elpa/go-mode 120fb91 419/495: Use "guru -tags=xxx yyy", not "guru -tags=xxx, yyy", ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode ed5c2cf 421/495: Fixed compiler warnings, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 77c6aae 422/495: Fix quotation marks in the gofmt doc string, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 3f60838 424/495: Move testdata under test/ directory., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 3ba1982 427/495: Handle indentation for nested dangling operators, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode f6b1398 429/495: Fix filename handling in godoc-gogetdoc, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 00e72cd 431/495: Update default for godoc-and-godef-command, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 6b67088 432/495: Support trailing commas as dangling operators., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 8ddf0b3 433/495: Speed up go-goto-opening-parenthesis, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode fdc1545 436/495: Fix indent for case statement comments, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode d387378 438/495: indent: handle comments after multiline case lists,
ELPA Syncer <=
- [nongnu] elpa/go-mode fbf2544 444/495: Improve fill-paragraph for comment blocks., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode e7e0157 447/495: indent: fix indent at top level of file, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 3bc7479 450/495: Fix infinite loop in fill-region, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 88ffc9e 452/495: Add github action yaml file to run ert tests, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 4f15abf 449/495: Fix fill-paragraph for certain block comments., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode a13b814 448/495: Add option to reuse a single buffer for godoc, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode a82369b 453/495: Readme: add basic information about gopls., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 839190b 455/495: Run all tests in GH CI, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 7248358 462/495: Fontify type names in type switch statements, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 4fabba6 464/495: Small fix for fontification of array types, ELPA Syncer, 2021/08/07