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

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

[nongnu] elpa/evil-nerd-commenter c9fa23ee7e 206/235: evil operator try


From: ELPA Syncer
Subject: [nongnu] elpa/evil-nerd-commenter c9fa23ee7e 206/235: evil operator try to comment on whole lines if possible
Date: Thu, 6 Jan 2022 02:59:48 -0500 (EST)

branch: elpa/evil-nerd-commenter
commit c9fa23ee7eba7e73fec1c35cd958eef5fa23585a
Author: Chen Bin <chenbin.sh@gmail.com>
Commit: Chen Bin <chenbin.sh@gmail.com>

    evil operator try to comment on whole lines if possible
---
 evil-nerd-commenter-operator.el | 54 ++++++++++++++++++++++-------------------
 evil-nerd-commenter-sdk.el      | 24 ++++++++++++++++++
 evil-nerd-commenter-tests.el    | 23 ++++++++++++++++++
 evil-nerd-commenter.el          | 29 ++++++----------------
 pkg.sh                          |  2 +-
 5 files changed, 85 insertions(+), 47 deletions(-)

diff --git a/evil-nerd-commenter-operator.el b/evil-nerd-commenter-operator.el
index eaa0e9e22f..c3c9f145ef 100644
--- a/evil-nerd-commenter-operator.el
+++ b/evil-nerd-commenter-operator.el
@@ -76,36 +76,40 @@
     (setq last-command tmp-command)
     (setq evilnc-temporary-goal-column 0)))
 
-(defun evilnc--extend-to-whole-comment (beg end)
-  "Extend the comment region defined by BEG and END so ALL comment is 
included."
-  (interactive)
-  (if (evilnc-is-pure-comment beg)
-      (save-excursion
-        (let* ((newbeg beg)
-               (newend end))
-
-          ;; extend the beginning
-          (goto-char newbeg)
-          (while (and (>= (1- newbeg) (line-beginning-position)) 
(evilnc-is-pure-comment (1- newbeg)))
-            (setq newbeg (1- newbeg)))
-
-          ;; extend the end
-          (goto-char newend)
-          (while (and (<= newend (line-end-position)) (evilnc-is-pure-comment 
newend))
-            (setq newend (1+ newend)))
-
-          (list newbeg newend)))
-    (list beg end)))
+(defun evilnc-extend-to-whole-comment-or-line (beg end)
+  "Extend the comment region defined by BEG and END so all comment is included.
+Or extend the region to contain whole lines if the region is not a comment."
+  (cond
+   ((evilnc-is-pure-comment beg)
+    (save-excursion
+      (let* ((newbeg beg)
+             (newend end))
+
+        ;; extend the beginning
+        (goto-char newbeg)
+        (while (and (>= (1- newbeg) (line-beginning-position)) 
(evilnc-is-pure-comment (1- newbeg)))
+          (setq newbeg (1- newbeg)))
+
+        ;; extend the end
+        (goto-char newend)
+        (while (and (<= newend (line-end-position)) (evilnc-is-pure-comment 
newend))
+          (setq newend (1+ newend)))
+
+        (cons newbeg newend))))
+   ((not (evilnc-sdk-inside-one-line-p beg end))
+    (evilnc-sdk-extend-to-contain-whole-lines beg end))
+   (t
+    (cons beg end))))
 
 (evil-define-operator evilnc-comment-operator (beg end type)
   "Comments text from BEG to END with TYPE."
   (interactive "<R>")
   (cond
    ((eq type 'block)
-    (let* ((newpos (evilnc--extend-to-whole-comment beg end) ))
+    (let* ((newpos (evilnc-extend-to-whole-comment-or-line beg end) ))
       (evil-apply-on-block #'evilnc--comment-or-uncomment-region
-                           (nth 0 newpos)
-                           (nth 1 newpos)
+                           (car newpos)
+                           (cdr newpos)
                            nil)))
    ((and (eq type 'line)
          (= end (point-max))
@@ -124,8 +128,8 @@
 
    (t
     (when (and beg end)
-      (let* ((newpos (evilnc--extend-to-whole-comment beg end)))
-        (evilnc--comment-or-uncomment-region (nth 0 newpos) (nth 1 newpos))))))
+      (let* ((newpos (evilnc-extend-to-whole-comment-or-line beg end)))
+        (evilnc--comment-or-uncomment-region (car newpos) (cdr newpos))))))
 
   ;; place cursor on beginning of line
   (if (and (called-interactively-p 'any) (eq type 'line))
diff --git a/evil-nerd-commenter-sdk.el b/evil-nerd-commenter-sdk.el
index 09d8550caa..3c245448fb 100644
--- a/evil-nerd-commenter-sdk.el
+++ b/evil-nerd-commenter-sdk.el
@@ -89,5 +89,29 @@ or else we can't select multiple lines comment."
          (evilnc--check-fonts fontfaces
                               '(font-lock-comment-delimiter-face)))))
 
+(defun evilnc-sdk-inside-one-line-p (beg end)
+  "Test BEG and END is inside one line."
+  (and (<= (line-beginning-position) beg)
+       (<= end (line-end-position))))
+
+(defun evilnc-sdk-extend-to-contain-whole-lines (beg end)
+  "Extend region between BEG and END so the region contain whole lines.
+Return new range like '(region_begin . region_end)."
+  (save-excursion
+    ;; Another work around for evil-visual-line bug:
+    ;; In `evil-mode', if we use hotkey V or `evil-visual-line' to select line,
+    ;; the (line-beginning-position) of the line which is after the last 
selected
+    ;; line is always (region-end)! Don't know why.
+    (when (and (> end beg)
+               (save-excursion (goto-char end) (= end 
(line-beginning-position)))
+               (boundp 'evil-state) (eq evil-state 'visual))
+      (setq end (1- end)))
+
+    (goto-char beg)
+    (setq beg (line-beginning-position))
+    (goto-char end)
+    (setq end (line-end-position)))
+  (cons beg end))
+
 (provide 'evil-nerd-commenter-sdk)
 ;;; evil-nerd-commenter-sdk.el ends here
diff --git a/evil-nerd-commenter-tests.el b/evil-nerd-commenter-tests.el
index b442a94913..c911804f11 100644
--- a/evil-nerd-commenter-tests.el
+++ b/evil-nerd-commenter-tests.el
@@ -216,4 +216,27 @@
       (should (eq 49 (nth 1 info)))
       (should (string= "python" (nth 2 info))))))
 
+(ert-deftest evilnc-test-extend-whole ()
+  (let* (info)
+    (with-temp-buffer
+      (insert "console.log('hello world');\n"
+              "console.log('hello world');\n"
+              "console.log('hello world');")
+
+      (goto-char (point-min))
+      ;; move foucs to the middle the line
+      (search-forward "hello world")
+      (should (evilnc-sdk-inside-one-line-p (point) (1- (line-end-position))))
+      (should (not (evilnc-sdk-inside-one-line-p (point) (1- (point-max)))))
+      (let* ((b (point))
+             e
+             range)
+        (search-forward "hello world")
+        (search-forward "hello world") ; middle of third line
+        (setq e (point))
+        (should (not (evilnc-sdk-inside-one-line-p b e)))
+        (setq range (evilnc-sdk-extend-to-contain-whole-lines b e))
+        (should (eq (car range) (save-excursion (goto-char b) 
(line-beginning-position))))
+        (should (eq (cdr range) (save-excursion (goto-char e) 
(line-end-position))))))))
+
 (ert-run-tests-batch-and-exit)
diff --git a/evil-nerd-commenter.el b/evil-nerd-commenter.el
index bda5bacdc5..fbd992d6cf 100644
--- a/evil-nerd-commenter.el
+++ b/evil-nerd-commenter.el
@@ -3,7 +3,7 @@
 ;; Author: Chen Bin <chenbin DOT sh AT gmail.com>
 
 ;; URL: http://github.com/redguardtoo/evil-nerd-commenter
-;; Version: 3.4.0
+;; Version: 3.5.0
 ;; Package-Requires: ((emacs "24.4"))
 ;; Keywords: convenience evil
 ;;
@@ -279,9 +279,7 @@ See 
http://lists.gnu.org/archive/html/bug-gnu-emacs/2013-03/msg00891.html.";
         (setq e (line-end-position)))
       (funcall fn b e)))
 
-   ;; Select region inside ONE line
-   ((and (<= (line-beginning-position) (region-beginning))
-          (<= (region-end) (line-end-position)))
+   ((evilnc-sdk-inside-one-line-p (region-beginning) (region-end))
     (cond
      ;; current comment syntax is NOT fit to comment out a region.
      ;; So we also need hack the `comment-start' and `comment-end'.
@@ -311,22 +309,11 @@ See 
http://lists.gnu.org/archive/html/bug-gnu-emacs/2013-03/msg00891.html.";
    ;; Select more than one line
    (t
     ;; selected region spans MORE than one line
-    (save-excursion
-      (let* ((b (region-beginning))
-             (e (region-end)))
-        ;; Another work around for evil-visual-line bug:
-        ;; In `evil-mode', if we use hotkey V or `evil-visual-line' to select 
line,
-        ;; the (line-beginning-position) of the line which is after the last 
selected
-        ;; line is always (region-end)! Don't know why.
-        (if (and (> e b)
-                 (save-excursion (goto-char e) (= e (line-beginning-position)))
-                 (boundp 'evil-state) (eq evil-state 'visual))
-            (setq e (1- e)))
-
-        (goto-char b)
-        (setq b (line-beginning-position))
-        (goto-char e)
-        (setq e (line-end-position))
+    (let* ((range (evilnc-sdk-extend-to-contain-whole-lines (region-beginning)
+                                                            (region-end)))
+           (b (car range))
+           (e (cdr range)))
+      (save-excursion
         (funcall fn b e))))))
 
 (defun evilnc--get-one-paragraph-region ()
@@ -751,7 +738,7 @@ Then we operate the expanded region.  NUM is ignored."
 (defun evilnc-version ()
   "The version number."
   (interactive)
-  (message "3.4.0"))
+  (message "3.5.0"))
 
 (defvar evil-normal-state-map)
 (defvar evil-visual-state-map)
diff --git a/pkg.sh b/pkg.sh
index 4479c5c519..e3c14ade5c 100755
--- a/pkg.sh
+++ b/pkg.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 name=evil-nerd-commenter
-version=3.4.0
+version=3.5.0
 pkg=$name-$version
 mkdir $pkg
 cp *.el $pkg



reply via email to

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