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

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

[nongnu] elpa/go-mode fbf2544 444/495: Improve fill-paragraph for commen


From: ELPA Syncer
Subject: [nongnu] elpa/go-mode fbf2544 444/495: Improve fill-paragraph for comment blocks.
Date: Sat, 7 Aug 2021 09:06:05 -0400 (EDT)

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

    Improve fill-paragraph for comment blocks.
    
    Implement a few choice functions so that fill-paragraph and friends
    works better for Go comment blocks. The two keys are:
    
    - go--fill-prefix which extracts the fill prefix for the following
      comment block. This knows, for example, that "/*" is not a prefix
      that needs to be applied to every comment.
    - go--fill-forward-paragraph which essentially defines what a comment
      paragraph is. fill-paragraph works by jumping forward
      with (fill-forward-paragraph 1) and then jumping back
      with (fill-forward-paragraph -1). That defines the start and end of
      the region to be filled.
    
    Fixes #91, #119
    
    Closes: #294 [via git-merge-pr]
---
 go-mode.el                     | 106 +++++++++++++++++++++++-
 test/go-fill-paragraph-test.el | 178 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 283 insertions(+), 1 deletion(-)

diff --git a/go-mode.el b/go-mode.el
index 1c85c63..90897b5 100644
--- a/go-mode.el
+++ b/go-mode.el
@@ -632,6 +632,106 @@ case keyword. It returns nil for the case line itself."
        ;; line or the previous character is a comma or colon.
        (or (bolp) (looking-back "[,:]" (1- (point)))))))))
 
+(defun go--fill-prefix ()
+  "Return fill prefix for following comment paragraph."
+  (save-excursion
+    (beginning-of-line)
+
+    ;; Skip over empty lines and empty comment openers/closers.
+    (while (and
+            (or (go--empty-line-p) (go--boring-comment-p))
+            (zerop (forward-line 1))))
+
+    ;; If we are looking at the start of an interesting comment, our
+    ;; prefix is the comment opener and any space following.
+    (if (looking-at (concat go--comment-start-regexp "[[:space:]]*"))
+        ;; Replace "/*" opener with spaces so following lines don't
+        ;; get "/*" prefix.
+        (replace-regexp-in-string "/\\*" "  "
+                                  (match-string-no-properties 0)))))
+
+(defun go--fill-paragraph (&rest args)
+  "Wrap fill-paragraph to set custom fill-prefix."
+  (let ((fill-prefix (go--fill-prefix))
+        (fill-paragraph-function nil))
+    (apply 'fill-paragraph args)))
+
+(defun go--empty-line-p ()
+  (looking-at "[[:space:]]*$"))
+
+(defun go--boring-comment-p ()
+  "Return non-nil if we are looking at a boring comment.
+
+A boring comment is a comment with no content. For example:
+
+////////  ; boring
+// hello
+////////  ; boring
+
+/*        ; boring
+  hello
+*/        ; boring
+"
+  (or
+   (looking-at-p "[[:space:]]*//+[[:space:]]*$")
+   (looking-at-p "[[:space:]]*\\(?:/\\*+\\|\\*/+\\)[[:space:]]*$")))
+
+(defun go--interesting-comment-p ()
+  "Return non-nil if we are looking at an interesting comment.
+
+An interesting comment is one that contains content other than
+comment starter/closer characters."
+
+  (if (go-in-comment-p)
+      (and
+       (not (go--empty-line-p))
+       (not (looking-at-p "[[:space:]]*\\*/")))
+    (and
+     (looking-at go--comment-start-regexp)
+     (not (go--boring-comment-p)))))
+
+(defun go--fill-forward-paragraph (&optional arg)
+  "forward-paragraph like function used for fill-paragraph.
+
+This function is key for making fill-paragraph do the right
+thing for comments."
+  (beginning-of-line)
+  (let* ((arg (or arg 1))
+         (single (if (> arg 0) 1 -1))
+         (done nil))
+    (while (and (not done) (not (zerop arg)))
+      ;; If we are moving backwards and aren't currently looking at a
+      ;; comment, move back one line. This is to make sure
+      ;; (go--file-forward-paragraph -1) always works properly as the
+      ;; inverse of (go--file-forward-paragraph 1).
+      (when (and
+             (= single -1)
+             (not (go-in-comment-p))
+             (not (looking-at-p go--comment-start-regexp)))
+        (forward-line -1))
+
+      ;; Skip empty lines and comment lines with no content.
+      (while (and
+              (or (go--empty-line-p) (go--boring-comment-p))
+              (zerop (forward-line single))))
+
+      (let (saw-comment)
+        ;; Skip comment lines that have content.
+        (while (and
+                (go--interesting-comment-p)
+                (zerop (forward-line single)))
+          (setq saw-comment t))
+
+        (if (not saw-comment)
+            (setq done t)
+          ;; If we are going backwards, back up one more line so
+          ;; we are on the line before the comment.
+          (when (= single -1)
+            (forward-line 1))
+          (cl-decf arg single))))
+    arg))
+
+
 (defun go--open-paren-position ()
   "Return non-nil if point is between '(' and ')'.
 
@@ -924,7 +1024,7 @@ Return non-nil if point changed lines."
       (setq count (if (and count (< count 0 )) -1 1)))
     moved))
 
-(defconst go--comment-start-regexp "[[:space:]]*\\(/\\*\\|//\\)")
+(defconst go--comment-start-regexp "[[:space:]]*\\(?:/[/*]\\)")
 
 (defun go--case-comment-p (indent)
   "Return non-nil if looking at a comment attached to a case statement.
@@ -1387,6 +1487,10 @@ with goflymake \(see URL 
`https://github.com/dougm/goflymake'), gocode
   (set (make-local-variable 'go-dangling-cache) (make-hash-table :test 'eql))
   (add-hook 'before-change-functions #'go--reset-dangling-cache-before-change 
t t)
 
+  (set (make-local-variable 'adaptive-fill-function) #'go--fill-prefix)
+  (set (make-local-variable 'fill-paragraph-function) #'go--fill-paragraph)
+  (set (make-local-variable 'fill-forward-paragraph-function) 
#'go--fill-forward-paragraph)
+
   ;; ff-find-other-file
   (setq ff-other-file-alist 'go-other-file-alist)
 
diff --git a/test/go-fill-paragraph-test.el b/test/go-fill-paragraph-test.el
new file mode 100644
index 0000000..7a2c34e
--- /dev/null
+++ b/test/go-fill-paragraph-test.el
@@ -0,0 +1,178 @@
+;;; go-fill-paragraph-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.
+
+(defun go--should-fill (got expected)
+  "Run `fill-paragraph' 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 fill-paragraph."
+  (with-temp-buffer
+    (go-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))
+      (call-interactively 'fill-paragraph)
+      (should (string= (buffer-string) expected)))))
+
+(ert-deftest go--fill-paragraph-no-comment ()
+  (go--should-fill
+   "
+func main() {
+<>
+  reallyLongLineButThereAreNoCommentsHere() + 
reallyLongLineButThereAreNoCommentsHere()
+}"
+
+   "
+func main() {
+
+  reallyLongLineButThereAreNoCommentsHere() + 
reallyLongLineButThereAreNoCommentsHere()
+}"
+   ))
+
+(ert-deftest go--fill-paragraph-single ()
+  (go--should-fill
+   "
+func main() {
+<>  // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do 
eiusmod tempor incididunt ut labore et dolore magna aliqua.
+}"
+
+   "
+func main() {
+  // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
+  // eiusmod tempor incididunt ut labore et dolore magna aliqua.
+}"
+   ))
+
+(ert-deftest go--fill-paragraph-single-region ()
+  (go--should-fill
+   "
+func main() {
+<  // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
tempor incididunt ut labore et dolore magna aliqua.
+>}"
+
+   "
+func main() {
+  // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
+  // eiusmod tempor incididunt ut labore et dolore magna aliqua.
+}"
+   ))
+
+(ert-deftest go--fill-paragraph-block ()
+  (go--should-fill
+   "
+func main() {
+<>  /* Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do 
eiusmod tempor incididunt ut labore et dolore magna aliqua. */
+}"
+
+   "
+func main() {
+  /* Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
+     eiusmod tempor incididunt ut labore et dolore magna aliqua. */
+}"
+   ))
+
+(ert-deftest go--fill-paragraph-block-region ()
+  (go--should-fill
+   "
+func main() {
+<  /* Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
tempor incididunt ut labore et dolore magna aliqua. */
+>}"
+
+   "
+func main() {
+  /* Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
+     eiusmod tempor incididunt ut labore et dolore magna aliqua. */
+}"
+   ))
+
+(ert-deftest go--fill-paragraph-block-empty-first ()
+  (go--should-fill
+   "
+func main() {
+<>  /*
+       Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do 
eiusmod tempor incididunt ut labore et dolore magna aliqua.
+  */
+}"
+
+   "
+func main() {
+  /*
+       Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
+       do eiusmod tempor incididunt ut labore et dolore magna aliqua.
+  */
+}"
+   ))
+
+(ert-deftest go--fill-paragraph-block-empty-first-region ()
+  (go--should-fill
+   "
+func main() {
+<  /*
+       Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do 
eiusmod tempor incididunt ut labore et dolore magna aliqua.
+  */
+>}"
+
+   "
+func main() {
+  /*
+       Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
+       do eiusmod tempor incididunt ut labore et dolore magna aliqua.
+  */
+}"
+   ))
+
+(ert-deftest go--fill-paragraph-block-region ()
+  (go--should-fill
+   "
+func main() {
+<  /* Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
tempor incididunt ut labore et dolore magna aliqua. */
+>}"
+
+   "
+func main() {
+  /* Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
+     eiusmod tempor incididunt ut labore et dolore magna aliqua. */
+}"
+   ))
+
+(ert-deftest go--fill-paragraph-single-artful ()
+  (go--should-fill
+   "
+func main() {
+<>  /////////////////////
+  // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
tempor incididunt ut labore et dolore magna aliqua.
+  /////////////////////
+}"
+
+   "
+func main() {
+  /////////////////////
+  // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
+  // eiusmod tempor incididunt ut labore et dolore magna aliqua.
+  /////////////////////
+}"
+   ))
+
+(ert-deftest go--fill-paragraph-single-artful-region ()
+  (go--should-fill
+   "
+func main() {
+<  /////////////////////
+  // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
tempor incididunt ut labore et dolore magna aliqua.
+  /////////////////////
+>}"
+
+   "
+func main() {
+  /////////////////////
+  // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
+  // eiusmod tempor incididunt ut labore et dolore magna aliqua.
+  /////////////////////
+}"
+   ))



reply via email to

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