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

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

[nongnu] elpa/evil-nerd-commenter 8b0d9654ec 230/235: fix&doc of text ob


From: ELPA Syncer
Subject: [nongnu] elpa/evil-nerd-commenter 8b0d9654ec 230/235: fix&doc of text object; unit test for evil
Date: Thu, 6 Jan 2022 02:59:50 -0500 (EST)

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

    fix&doc of text object; unit test for evil
---
 .github/workflows/test.yml                         |   2 +-
 Makefile                                           |   3 +-
 README.org                                         |  15 ++-
 evil-nerd-commenter-operator.el                    | 121 +++++++++++----------
 evil-nerd-commenter.el                             |  24 +++-
 pkg.sh                                             |   2 +-
 .../evil-nerd-commenter-tests.el                   |  71 +++++++++++-
 tests/hello.js                                     |   9 ++
 8 files changed, 176 insertions(+), 71 deletions(-)

diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 50d7a1ae2f..e1f1a74f68 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -12,7 +12,7 @@ jobs:
           - 25.3
           - 26.3
           - 27.1
-          - snapshot
+          # - snapshot # evil 1.14 has legacy api call of defalias
     steps:
     - uses: purcell/setup-emacs@master
       with:
diff --git a/Makefile b/Makefile
index 411f57e308..52325eba2a 100644
--- a/Makefile
+++ b/Makefile
@@ -13,8 +13,9 @@ clean:
 
 deps:
        @mkdir -p deps;
+       @if [ ! -f deps/evil-1.14.0/evil.el ]; then curl -L 
https://stable.melpa.org/packages/evil-1.14.0.tar | tar x -C deps/; fi;
        @if [ ! -f deps/web-mode.el ]; then curl -L 
https://raw.githubusercontent.com/fxbois/web-mode/master/web-mode.el > 
deps/web-mode.el; fi;
 
 # Run tests.
 test: deps
-       $(EMACS) -batch -Q -L deps/ -l deps/web-mode.el -l 
evil-nerd-commenter-sdk.el -l evil-nerd-commenter.el -l 
evil-nerd-commenter-tests.el
+       $(EMACS) -batch -Q -L . -L deps/ -L deps/evil-1.14.0 -l 
deps/web-mode.el -l evil-nerd-commenter-sdk.el -l evil-nerd-commenter.el -l 
tests/evil-nerd-commenter-tests.el
diff --git a/README.org b/README.org
index 7678a0e6c2..ab29900c06 100644
--- a/README.org
+++ b/README.org
@@ -234,7 +234,20 @@ Press =vac= to select outer object (comment with limiters).
 
 Press =vic= to select inner object (comment without limiter).
 
-You can assign other key instead of "c" to the text object by customising 
=evilnc-comment-text-object=.
+The comment text object is created automatically in =evilnc-default-hotkeys=.
+
+You can assign other key instead of "c" to the text object by changing 
=evilnc-comment-text-object=.
+#+begin_src elisp
+(setq evilnc-comment-text-object "c")
+(evilnc-default-hotkeys)
+#+end_src
+
+You can also manually created the comment text object using below code,
+#+begin_src elisp
+(setq evilnc-comment-text-object "a")
+(define-key evil-inner-text-objects-map evilnc-comment-text-object 
'evilnc-inner-commenter)
+(define-key evil-outer-text-objects-map evilnc-comment-text-object 
'evilnc-outer-commenter)
+#+end_src
 ** evilnc-comment-operator
 =evilnc-comment-operator= acts much like the delete/change operator. Takes a 
motion or text object and comments it out, yanking its content in the process.
 
diff --git a/evil-nerd-commenter-operator.el b/evil-nerd-commenter-operator.el
index 63a32d1349..452f0ce14d 100644
--- a/evil-nerd-commenter-operator.el
+++ b/evil-nerd-commenter-operator.el
@@ -61,7 +61,7 @@
 (defadvice evil-visual-highlight-block (around 
evil-visual-highlight-block-hack activate)
   "Show overlay over inner comment text object."
   ad-do-it
-  (when (eq this-command 'evilnc-inner-comment)
+  (when (eq this-command 'evilnc-inner-commenter)
     (dolist (overlay evil-visual-block-overlays)
       (let* ((b (overlay-start overlay))
              (e (save-excursion
@@ -84,39 +84,39 @@
     (setq last-command tmp-command)
     (setq evilnc-temporary-goal-column 0)))
 
-(defun evilnc-expand-to-whole-comment-or-line (beg end)
-  "Expand the comment region defined by BEG and END so all comment is included.
-Or expand the region to contain whole lines if it's not comment and certain 
conditions met."
+(defun evilnc-expand-to-whole-comment-or-line (start end)
+  "Expand the comment region defined by START and END so all comment is 
included.
+Or expand the region to contain whole lines."
 
   (cond
-   ((evilnc-pure-comment-p beg)
+   ((evilnc-pure-comment-p start)
     (save-excursion
-      (let* ((newbeg beg)
+      (let* ((newstart start)
              (newend end))
 
         ;; expand the beginning
-        (goto-char newbeg)
-        (while (and (>= (1- newbeg) (line-beginning-position)) 
(evilnc-pure-comment-p (1- newbeg)))
-          (setq newbeg (1- newbeg)))
+        (goto-char newstart)
+        (while (and (>= (1- newstart) (line-beginning-position)) 
(evilnc-pure-comment-p (1- newstart)))
+          (setq newstart (1- newstart)))
 
         ;; expand the end
         (goto-char newend)
         (while (and (<= newend (line-end-position)) (evilnc-pure-comment-p 
newend))
           (setq newend (1+ newend)))
 
-        (cons newbeg newend))))
+        (cons newstart newend))))
 
    ;; try to expand region to contain whole line if,
    ;; - currently more than one line text in the region,
    ;; - a specific text object is touched just before the operator
-   ((and (not (evilnc-sdk-inside-one-line-p beg end))
+   ((and (not (evilnc-sdk-inside-one-line-p start end))
          evilnc-current-text-object
          (member (car evilnc-current-text-object) 
evilnc-whole-line-text-objects)
          ;; 0.5 second
          (< (- (float-time (current-time)) (cdr evilnc-current-text-object)) 
0.5))
-    (evilnc-sdk-expand-to-contain-whole-lines beg end))
+    (evilnc-sdk-expand-to-contain-whole-lines start end))
    (t
-    (cons beg end))))
+    (cons start end))))
 
 ;; {{ know text object type to operate on
 (defun evilnc-set-current-text-object (text-object)
@@ -139,12 +139,12 @@ Or expand the region to contain whole lines if it's not 
comment and certain cond
   (evilnc-set-current-text-object 'evil-forward-paragraph))
 ;; }}
 
-(evil-define-operator evilnc-comment-operator (beg end type)
-  "Comments text from BEG to END with TYPE."
+(evil-define-operator evilnc-comment-operator (start end type)
+  "Comments text from START to END with TYPE."
   (interactive "<R>")
   (cond
    ((eq type 'block)
-    (let* ((newpos (evilnc-expand-to-whole-comment-or-line beg end) ))
+    (let* ((newpos (evilnc-expand-to-whole-comment-or-line start end) ))
       (evil-apply-on-block #'evilnc-comment-or-uncomment-region
                            (car newpos)
                            (cdr newpos)
@@ -152,33 +152,33 @@ Or expand the region to contain whole lines if it's not 
comment and certain cond
 
    ((and (eq type 'line)
          (= end (point-max))
-         (or (= beg end)
+         (or (= start end)
              (/= (char-before end) ?\n))
-         (/= beg (point-min))
-         (=  (char-before beg) ?\n))
-    (evilnc-comment-or-uncomment-region (1- beg) end))
+         (/= start (point-min))
+         (=  (char-before start) ?\n))
+    (evilnc-comment-or-uncomment-region (1- start) end))
 
    ((eq type 'line)
     ;; comment whole line, for now
-    (evilnc-comment-or-uncomment-region beg
+    (evilnc-comment-or-uncomment-region start
                                          (save-excursion
                                            (goto-char (1- end))
                                            (line-end-position))))
 
    (t
-    (when (and beg end)
-      (let* ((newpos (evilnc-expand-to-whole-comment-or-line beg end)))
+    (when (and start end)
+      (let* ((newpos (evilnc-expand-to-whole-comment-or-line start 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))
       (evil-first-non-blank)))
 
-(defun evilnc-comment-or-uncomment-region-then-action (begin end commenter 
&optional action)
-  "Comment/uncomment between BEGIN and END using COMMENTER, then take ACTION."
+(defun evilnc-comment-or-uncomment-region-then-action (start end commenter 
&optional action)
+  "Comment/uncomment between START and END using COMMENTER, then take ACTION."
   (evil-with-single-undo
     ;; yank original text
-    (evil-yank-lines begin end nil 'lines)
+    (evil-yank-lines start end nil 'lines)
 
     (when (evil-visual-state-p)
       ;; `evil-paste-before' does not work in visual state.
@@ -187,8 +187,8 @@ Or expand the region to contain whole lines if it's not 
comment and certain cond
     (cond
      (evilnc-original-above-comment-when-copy-and-comment
       (let* ((p (point)))
-        (funcall commenter begin end)
-        (goto-char begin)
+        (funcall commenter start end)
+        (goto-char start)
         (when action (funcall action))
         (goto-char p)))
 
@@ -196,41 +196,41 @@ Or expand the region to contain whole lines if it's not 
comment and certain cond
       (goto-char end)
       (when action (funcall action))
       ;; actual comment operation should happen at last
-      ;; or else begin end will be screwed up
-      (funcall commenter begin end)))))
+      ;; or else "(start end)" is screwed up
+      (funcall commenter start end)))))
 
-(evil-define-operator evilnc-yank-and-comment-operator (begin end)
-  "(Un)comment and yank the text from BEGIN to END."
+(evil-define-operator evilnc-yank-and-comment-operator (start end)
+  "(Un)comment and yank the text from START to END."
   :move-point (not evilnc-original-above-comment-when-copy-and-comment)
   (interactive "<r>")
-  (evilnc-comment-or-uncomment-region-then-action begin
+  (evilnc-comment-or-uncomment-region-then-action start
                                                   end
                                                   
evilnc-comment-or-uncomment-region-function))
 
-(evil-define-operator evilnc-copy-and-comment-operator (begin end)
-  "Inserts a commented copy of the text from BEGIN to END."
+(evil-define-operator evilnc-copy-and-comment-operator (start end)
+  "Inserts a commented copy of the text from START to END."
   :move-point (not evilnc-original-above-comment-when-copy-and-comment)
   (interactive "<r>")
-  (evilnc-comment-or-uncomment-region-then-action begin
+  (evilnc-comment-or-uncomment-region-then-action start
                                                   end
                                                   'comment-region
                                                   (lambda () 
(evil-paste-before 1))))
 
-(defun evilnc-one-line-comment-p (begin end)
-  "Test if text between BEGIN and END is one line comment."
+(defun evilnc-one-line-comment-p (start end)
+  "Test if text between START and END is one line comment."
   (save-excursion
-    (goto-char begin)
-    (and (<= (line-beginning-position) begin)
+    (goto-char start)
+    (and (<= (line-beginning-position) start)
          ;; end is the upper limit great than (line-end-position)
          (<= end (1+ (line-end-position))))))
 
 (defun evilnc-get-comment-bounds ()
-  "Return bounds like (cons beg end)."
+  "Return bounds like (cons start end)."
   (let* ((b (point))
          (e (point))
          (col 0)
          rlt)
-    ;; decrease begin
+    ;; decrease start position
     (while (evilnc-comment-p (- b 1))
       (setq b (- b 1)))
 
@@ -251,11 +251,11 @@ Or expand the region to contain whole lines if it's not 
comment and certain cond
      ((>= b e)
       (setq rlt nil))
      ((evilnc-one-line-comment-p b e)
-      ;; contract begin
+      ;; contract from start position
       (while (not (evilnc-pure-comment-p b))
         (setq b (+ b 1)))
 
-      ;; contract end
+      ;; contract from end position
       (while (not (evilnc-pure-comment-p e))
         (setq e (- e 1)))
 
@@ -297,28 +297,28 @@ Or expand the region to contain whole lines if it's not 
comment and certain cond
        (= (evilnc-get-char pos) ?/)
        (= (memq (evilnc-get-char (1+ pos)) '(?/ ?*)))))
 
-(defun evilnc-comment-column-bounds (beg end &optional c-style)
-  "From BEG to END find column bounds of rectangle selection.
+(defun evilnc-comment-column-bounds (start end &optional c-style)
+  "From START to END find column bounds of rectangle selection.
 Return (cons col-min col-max) or nil.  If C-STYLE is t,
 we are processing C like language."
   (let* ((col-min most-positive-fixnum)
          (col-max 0))
-    (while (< beg end)
-      (when (and (not (evilnc-whitespace-p beg))
-                 (evilnc-pure-comment-p beg)
-                 (not (or (evilnc-comment-delimiter-p beg)
+    (while (< start end)
+      (when (and (not (evilnc-whitespace-p start))
+                 (evilnc-pure-comment-p start)
+                 (not (or (evilnc-comment-delimiter-p start)
                           (and c-style
-                               (memq (evilnc-get-char beg) '(?/ ?*))))))
-        (let* ((col (evil-column beg)))
+                               (memq (evilnc-get-char start) '(?/ ?*))))))
+        (let* ((col (evil-column start)))
           (if (< col col-min)
               (setq col-min col))
           (if (> col col-max)
               (setq col-max col))))
-      (setq beg (1+ beg)))
+      (setq start (1+ start)))
     (if (< col-min col-max)
         (cons col-min col-max))))
 
-(evil-define-text-object evilnc-inner-comment (&optional count begin end type)
+(evil-define-text-object evilnc-inner-commenter (&optional count start end 
type)
   "An inner comment text object."
   (let* ((bounds (evilnc-get-comment-bounds))
          b
@@ -376,17 +376,22 @@ we are processing C like language."
      (t
       (error "Not inside a comment")))))
 
-(evil-define-text-object evilnc-outer-commenter (&optional count begin end 
type)
+(evil-define-text-object evilnc-outer-commenter (&optional count start end 
type)
   "An outer comment text object."
   (let* ((bounds (evilnc-get-comment-bounds)))
     (cond
      (bounds
-      (let* ((b (car bounds))
-             (e (cdr bounds)))
-        (evil-range b e 'exclusive :expanded t)))
+      (evil-range (car bounds) (cdr bounds) 'exclusive :expanded t))
      (t
       (error "Not inside a comment")))))
 
+(evil-define-text-object evilnc-inner-comment (&optional count start end type)
+  "An inner comment text object."
+  (evilnc-inner-commenter count start end type))
+
+(evil-define-text-object evilnc-outer-comment (&optional count start end type)
+  "An outer comment text object."
+  (evilnc-outer-commenter count start end type))
 
 (provide 'evil-nerd-commenter-operator)
 ;;; evil-nerd-commenter-operator.el ends here
diff --git a/evil-nerd-commenter.el b/evil-nerd-commenter.el
index 54477f7391..3108464c37 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.5.4
+;; Version: 3.5.5
 ;; Package-Requires: ((emacs "25.1"))
 ;; Keywords: convenience evil
 ;;
@@ -93,9 +93,15 @@
 ;; Comment text object "c" is defined.  It can have multi-lines.
 ;; Press "vac" to select outer object (comment with limiters).
 ;; Press "vic" to select inner object (comment without limiter).
-;;
 ;; You can assign other key instead of "c" to the text object by
-;; customizing `evilnc-comment-text-object'.
+;; customizing `evilnc-comment-text-object'.  Either,
+;;   (setq evilnc-comment-text-object "c")
+;;   (evilnc-default-hotkeys)
+;;
+;; Or,
+;;   (setq evilnc-comment-text-object "a")
+;;   (define-key evil-inner-text-objects-map evilnc-comment-text-object 
'evilnc-inner-commenter)
+;;   (define-key evil-outer-text-objects-map evilnc-comment-text-object 
'evilnc-outer-commenter)
 ;;
 ;; You can list of comments in current buffer through using imenu.
 ;; by setup `imenu-create-index-function' to 
`evilnc-imenu-create-index-function',
@@ -490,8 +496,10 @@ Code snippets embedded in Org-mode is identified and right 
`major-mode' is used.
    ((eq major-mode 'web-mode)
     ;; elixir is not supported in web-mode for now
     (unless (fboundp 'web-mode-comment-elixir-block)
-      (defalias 'web-mode-comment-elixir-block 'web-mode-comment-erb-block)
-      (defalias 'web-mode-uncomment-elixir-block 
'web-mode-uncomment-erb-block))
+      (defun web-mode-comment-elixir-block (pos)
+        (web-mode-comment-erb-block pos))
+      (defun web-mode-uncomment-elixir-block (pos)
+        (web-mode-uncomment-erb-block pos)))
     (evilnc--web-mode-comment-or-uncomment beg end))
    (t
     (evilnc--working-on-region beg end 'comment-or-uncomment-region))))
@@ -783,7 +791,7 @@ Then we operate the expanded region.  NUM is ignored."
 (defun evilnc-version ()
   "The version number."
   (interactive)
-  (message "3.5.4"))
+  (message "3.5.5"))
 
 (defvar evil-normal-state-map)
 (defvar evil-visual-state-map)
@@ -815,6 +823,10 @@ if NO-EMACS-KEYBINDINGS is t, we don't define keybindings 
in EMACS mode."
     (define-key evil-normal-state-map ",cr" 'comment-or-uncomment-region)
     (define-key evil-normal-state-map ",cv" 
'evilnc-toggle-invert-comment-line-by-line)
 
+    ;; comment itself is text object
+    (define-key evil-inner-text-objects-map evilnc-comment-text-object 
'evilnc-inner-commenter)
+    (define-key evil-outer-text-objects-map evilnc-comment-text-object 
'evilnc-outer-commenter)
+
     (when evilnc-use-comment-object-setup
       ;; Install operator for evil text objects
       (define-key evil-normal-state-map ",." 'evilnc-copy-and-comment-operator)
diff --git a/pkg.sh b/pkg.sh
index 9aa89b386a..59f0021386 100755
--- a/pkg.sh
+++ b/pkg.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 name=evil-nerd-commenter
-version=3.5.4
+version=3.5.5
 pkg=$name-$version
 mkdir $pkg
 cp *.el $pkg
diff --git a/evil-nerd-commenter-tests.el b/tests/evil-nerd-commenter-tests.el
similarity index 79%
rename from evil-nerd-commenter-tests.el
rename to tests/evil-nerd-commenter-tests.el
index 644e19a1a3..550f9c1945 100644
--- a/evil-nerd-commenter-tests.el
+++ b/tests/evil-nerd-commenter-tests.el
@@ -22,12 +22,31 @@
 
 ;;; Commentary:
 
+;;; Code:
+
 (require 'ert)
 (require 'evil-nerd-commenter)
 (require 'js)
-
-(defun evilnc-get-lines (start end)
-  (split-string (buffer-substring-no-properties start end) "\n"))
+(require 'evil)
+
+(defun evilnc-get-full-path (filename)
+  "Get full path of FILENAME in current directory."
+  (concat
+   (if load-file-name (file-name-directory load-file-name) default-directory)
+   filename))
+
+(defun evilnc-get-lines (begin end)
+  "Get line from BEGIN to END."
+  (split-string (buffer-substring-no-properties begin end) "\n"))
+
+(defun evilnc-next-lines-text (n)
+  "Text of current line and next N lines."
+  (let* ((start (line-beginning-position))
+         end)
+    (save-excursion
+      (forward-line n)
+      (setq end (line-end-position)))
+    (buffer-substring-no-properties start end)))
 
 (ert-deftest evilnc-test-forward-line ()
   (with-temp-buffer
@@ -257,4 +276,50 @@
       ;; @see https://github.com/redguardtoo/evil-nerd-commenter/issues/115
       )))
 
+(ert-deftest evilnc-test-evil-comment-text-object ()
+  (with-temp-buffer
+    (let* (rlt)
+      (insert-file-contents (evilnc-get-full-path "hello.js"))
+      (js-mode)
+      (evil-mode)
+      (goto-char (point-min))
+      (should (string= evilnc-comment-text-object "c"))
+      (search-forward "comment begin")
+      (font-lock-ensure (point-min) (point-max))
+
+      ;; User presses "vac"
+      (setq rlt (evilnc-outer-commenter))
+      (should (eq (nth 0 rlt) 1))
+      (should (eq (nth 1 rlt) 41))
+      (should (eq (nth 2 rlt) 'exclusive))
+
+      ;; User presses "vic"
+      (setq rlt (evilnc-inner-commenter))
+      (should (eq (nth 0 rlt) 4))
+      (should (eq (nth 1 rlt) 41))
+      (should (eq (nth 2 rlt) 'block)))))
+
+(ert-deftest evilnc-test-evil-comment-operator ()
+  (with-temp-buffer
+    (let* (rlt)
+      (insert-file-contents (evilnc-get-full-path "hello.js"))
+      (js-mode)
+      (evil-mode)
+      (goto-char (point-min))
+      (search-forward "comment begin")
+      (font-lock-ensure (point-min) (point-max))
+
+      ;; press "gcj" to uncomment current line and next line
+      (should (string= (evilnc-next-lines-text 2)
+                       "// comment begin\n// hello\n// comment end"))
+      (evilnc-comment-operator 1 27 'line)
+      (should (string= (evilnc-next-lines-text 2)
+                       "comment begin\nhello\n// comment end"))
+      ;; press "gcj" again to restore the comment
+      (evilnc-comment-operator 1 21 'line)
+      (should (string= (evilnc-next-lines-text 2)
+                       "// comment begin\n// hello\n// comment end"))
+      (should (eq major-mode 'js-mode)))))
+
 (ert-run-tests-batch-and-exit)
+;;; evil-nerd-commenter-tests.el ends here
diff --git a/tests/hello.js b/tests/hello.js
new file mode 100644
index 0000000000..34d7a5214d
--- /dev/null
+++ b/tests/hello.js
@@ -0,0 +1,9 @@
+// comment begin
+// hello
+// comment end
+
+console.log('hello');
+function bye() {
+  throw new Error('bye');
+}
+bye();



reply via email to

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