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

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

[elpa] master dad7d09 32/49: feat: add js2-comments-between func


From: Dmitry Gutov
Subject: [elpa] master dad7d09 32/49: feat: add js2-comments-between func
Date: Mon, 16 Jan 2017 15:35:49 +0000 (UTC)

branch: master
commit dad7d094b792bb08e8c80f0bae053b3d2d3db71b
Author: 1111hui <address@hidden>
Commit: 1111hui <address@hidden>

    feat: add js2-comments-between func
---
 Makefile         |    4 ++--
 js2-mode.el      |   15 +++++++++++++-
 tests/consume.el |   59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index b181ed8..bad22c9 100644
--- a/Makefile
+++ b/Makefile
@@ -18,6 +18,6 @@ clean:
        -rm -f $(OBJS)
 
 test:
-       ${EMACS} $(BATCHFLAGS) -L . -l js2-mode.el -l js2-old-indent.el -l 
tests/parser.el\
-         -l tests/indent.el -l tests/externs.el -l tests/json-path.el \
+       ${EMACS} $(BATCHFLAGS) -L . -l js2-mode.el -l js2-old-indent.el -l 
tests/parser.el \
+         -l tests/indent.el -l tests/externs.el -l tests/json-path.el -l 
tests/consume.el \
          -l tests/navigation.el -f ert-run-tests-batch-and-exit
diff --git a/js2-mode.el b/js2-mode.el
index 12a5c6a..c2b72f8 100644
--- a/js2-mode.el
+++ b/js2-mode.el
@@ -4962,7 +4962,20 @@ Function returns nil if POS was not in any comment node."
               end (+ beg (js2-node-len comment)))
         (if (and (>= x beg)
                  (<= x end))
-            (throw 'done comment))))))
+          (throw 'done comment))))))
+
+(defun js2-comments-between (start end comments-list)
+  "Return comment nodes between START and END, nil if not found.
+START and END are absolute positions in current buffer.
+COMMENTS-LIST is the comments list to check."
+  (let (comments c-start c-end)
+    (nreverse
+      (dolist (comment comments-list comments)
+        (setq c-start (js2-node-abs-pos comment)
+              c-end (1- (+ c-start (js2-node-len comment))))
+        (unless (or (< c-end start)
+                    (> c-start end))
+          (push comment comments))))))
 
 (defun js2-mode-find-parent-fn (node)
   "Find function enclosing NODE.
diff --git a/tests/consume.el b/tests/consume.el
new file mode 100644
index 0000000..6012699
--- /dev/null
+++ b/tests/consume.el
@@ -0,0 +1,59 @@
+;;; tests/consume.el --- Some tests for js2-mode.
+
+;; Copyright (C) 2009, 2011-2016  Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert)
+(require 'js2-mode)
+
+(defun js2-mode--and-parse ()
+  (js2-mode)
+  (js2-reparse))
+
+;;; Comments
+
+(ert-deftest js2-comments-between ()
+  (with-temp-buffer
+    (insert "0\n//\n[0,/* */1]")
+    (js2-mode--and-parse)
+    (let ((comments-list (js2-ast-root-comments js2-mode-ast))
+           comments)
+      (setq comments (js2-comments-between 1 2 comments-list))
+      (should (null comments))
+      ;; comment head between region
+      (setq comments (js2-comments-between 1 3 comments-list))
+      (should (= (length comments) 1))
+      ;; comment body between region
+      (setq comments (js2-comments-between 4 5 comments-list))
+      (should (= (length comments) 1))
+      ;; comment tail between region
+      (setq comments (js2-comments-between 5 6 comments-list))
+      (should (= (length comments) 1))
+      (setq comments (js2-comments-between 6 6 comments-list))
+      (should (null comments))
+      (setq comments (js2-comments-between 10 12 comments-list))
+      (should (= (length comments) 1))
+      ;; multiple comments between
+      (setq comments (js2-comments-between 5 15 comments-list))
+      (should (= (length comments) 2))
+      ;; pass comments-list when no AST available
+      (setq js2-mode-ast nil)
+      (setq comments (js2-comments-between 8 9 comments))
+      (should (= (length comments) 1))
+      )))



reply via email to

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