[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/go-mode 7c38d6a 477/495: Improve commenting partial lines.
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/go-mode 7c38d6a 477/495: Improve commenting partial lines. |
Date: |
Sat, 7 Aug 2021 09:06:12 -0400 (EDT) |
branch: elpa/go-mode
commit 7c38d6a7fc638b313fd8057afac1561b33e86023
Author: Muir Manders <muir@mnd.rs>
Commit: Peter Sanford <psanford@sanford.io>
Improve commenting partial lines.
For example, say you want to comment out "&& bar" in:
if foo && bar {
}
Previously comment-dwim would cause:
if foo // && bar
{
}
which is a syntax error.
Now we notice if you are commenting within a single line and switch to
the /* */ block style comments, yielding:
if foo /* && bar */ {
}
I also think I figured out that region based tests require transient
mark mode, so change go--should-fill back to only call
fill-paragraph (which itself delegates to fill-region).
Closes: #328 [via git-merge-pr]
---
go-mode.el | 20 +++++++++++++++-
test/go-comment-test.el | 52 ++++++++++++++++++++++++++++++++++++++++++
test/go-fill-paragraph-test.el | 10 ++++----
test/go-font-lock-test.el | 2 +-
4 files changed, 77 insertions(+), 7 deletions(-)
diff --git a/go-mode.el b/go-mode.el
index f2deb61..600b6c4 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -1633,6 +1633,23 @@ This is intended to be called from
`before-change-functions'."
(eq (char-before (- (point) 3)) ?a)
(eq (char-before (- (point) 4)) ?c)))))
+(defun go--comment-region (beg end &optional arg)
+ "Switch to block comment when commenting a partial line."
+ (save-excursion
+ (goto-char beg)
+ (let ((beg-bol (line-beginning-position)))
+ (goto-char end)
+ (if (and
+ ;; beg and end are on the same line
+ (eq (line-beginning-position) beg-bol)
+ ;; end is not at end of line
+ (not (eq end (line-end-position))))
+ (let ((comment-start "/* ")
+ (comment-end " */")
+ (comment-padding ""))
+ (comment-region-default beg end arg))
+ (comment-region-default beg end arg)))))
+
;;;###autoload
(define-derived-mode go-mode prog-mode "Go"
"Major mode for editing Go source text.
@@ -1692,7 +1709,7 @@ If you're looking for even more integration with Go,
namely
on-the-fly syntax checking, auto-completion and snippets, it is
recommended that you look at flycheck
\(see URL `https://github.com/flycheck/flycheck') or flymake in combination
-with goflymake \(see URL `https://github.com/dougm/goflymake'), gocode
+with goflymake (see URL `https://github.com/dougm/goflymake'), gocode
\(see URL `https://github.com/nsf/gocode'), go-eldoc
\(see URL `github.com/syohex/emacs-go-eldoc') and yasnippet-go
\(see URL `https://github.com/dominikh/yasnippet-go')"
@@ -1709,6 +1726,7 @@ with goflymake \(see URL
`https://github.com/dougm/goflymake'), gocode
(set (make-local-variable 'comment-end) "")
(set (make-local-variable 'comment-use-syntax) t)
(set (make-local-variable 'comment-start-skip) "\\(//+\\|/\\*+\\)\\s *")
+ (set (make-local-variable 'comment-region-function) 'go--comment-region)
(set (make-local-variable 'beginning-of-defun-function)
#'go-beginning-of-defun)
(set (make-local-variable 'end-of-defun-function) #'go-end-of-defun)
diff --git a/test/go-comment-test.el b/test/go-comment-test.el
new file mode 100644
index 0000000..819fda7
--- /dev/null
+++ b/test/go-comment-test.el
@@ -0,0 +1,52 @@
+;;; go-comment-test.el
+
+;; Copyright 2020 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)
+(require 'cl-lib)
+
+(ert-deftest go--comment-region ()
+ (go--should-comment
+ "
+<var foo int
+>"
+ "
+// var foo int
+")
+
+ (go--should-comment
+ "
+<// var foo int
+>"
+ "
+var foo int
+")
+
+ (go--should-comment
+ "var <foo> int"
+ "var /* foo */ int")
+
+ (go--should-comment
+ "var </* foo */> int"
+ "var foo int"))
+
+(defun go--should-comment (got expected)
+ "Run `comment-dwim' against GOT and make sure it matches EXPECTED.
+
+<> in GOT represents point. If they aren't next to each other, then it
+represents point and mark to test the region based comment-region."
+ (with-temp-buffer
+ (go-mode)
+ (transient-mark-mode)
+ (insert got)
+ (goto-char (point-min))
+ (let ((beg (progn (search-forward "<") (delete-char -1) (point)))
+ (end (progn (search-forward ">") (delete-char -1) (point))))
+ (when (/= beg end)
+ (set-mark beg))
+ (goto-char end)
+ (call-interactively 'comment-dwim)
+ (should (string= (buffer-string) expected)))))
diff --git a/test/go-fill-paragraph-test.el b/test/go-fill-paragraph-test.el
index c72bee9..0b5412a 100644
--- a/test/go-fill-paragraph-test.el
+++ b/test/go-fill-paragraph-test.el
@@ -14,15 +14,15 @@
represents point and mark to test the region based fill-paragraph."
(with-temp-buffer
(go-mode)
+ (transient-mark-mode)
(insert got)
(goto-char (point-min))
(let ((beg (progn (search-forward "<") (delete-char -1) (point)))
(end (progn (search-forward ">") (delete-char -1) (point))))
- (if (/= beg end)
- (progn
- (set-mark beg)
- (call-interactively 'fill-region))
- (call-interactively 'fill-paragraph))
+ (when (/= beg end)
+ (set-mark beg))
+ (goto-char end)
+ (call-interactively 'fill-paragraph)
(should (string= (buffer-string) expected)))))
(ert-deftest go--fill-paragraph-no-comment ()
diff --git a/test/go-font-lock-test.el b/test/go-font-lock-test.el
index 5de75f8..6c5ba8a 100644
--- a/test/go-font-lock-test.el
+++ b/test/go-font-lock-test.el
@@ -1,4 +1,4 @@
-;;; go-indentation-test.el
+;;; go-font-lock-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
- [nongnu] elpa/go-mode 409cc64 344/495: cmd/oracle: usability improvements to "describe", "referrers", (continued)
- [nongnu] elpa/go-mode 409cc64 344/495: cmd/oracle: usability improvements to "describe", "referrers", ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode d9d47fa 332/495: go.tools/oracle: change -ptalog default to ""., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode bd236e5 367/495: cmd/guru: emacs: update default-directory, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 2db2ff6 333/495: go.tools/oracle: change -pos flag syntax from "file pos-pos" to file:pos-pos., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 17a7d8a 370/495: cmd/guru: emacs: add function for expandiong region, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode dbc9124 378/495: Move go-guru.el to top level, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode b949a9b 363/495: cmd/guru: emacs: add go-guru-debug variable, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 04a2946 380/495: Merge branch 'import-guru', ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 259110b 396/495: Use a named function for before-change-functions., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode d964767 399/495: Use forward-word-strictly when available, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 7c38d6a 477/495: Improve commenting partial lines.,
ELPA Syncer <=
- [nongnu] elpa/go-mode 24990df 400/495: Some simple style fixes for issues found by checkdoc, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 10d6ab4 486/495: Allow leading spaces for compilation error message, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode e79970d 224/495: fix beginning-of-defun for abs(count) > 1, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode cec546c 306/495: Clarify some points in NEWS, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 5c1c546 307/495: Don't fontify parenthesis of multi-line type declaration, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 99b06da 408/495: Describe gofmt-args in the readme and the gofmt docstring (also gofmt-command), ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 7c50c80 410/495: Always move back to same column after applying rcs patch, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode e20a54a 426/495: Speed up go-previous-line-has-dangling-op-p, ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 1650ae5 434/495: Fix multiline case statement indent., ELPA Syncer, 2021/08/07
- [nongnu] elpa/go-mode 1fcf76d 435/495: Fix indentation regexes to be case insensitive, ELPA Syncer, 2021/08/07