]> code.delx.au - gnu-emacs-elpa/commitdiff
Simplified property name matching
authorScottyB <scott.n.barnett@gmail.com>
Sat, 29 Aug 2015 14:53:35 +0000 (00:53 +1000)
committerScottyB <scott.n.barnett@gmail.com>
Mon, 7 Sep 2015 07:36:28 +0000 (17:36 +1000)
js2-mode.el
tests/navigation.el

index 5b4ddc5fd8fdf6e0fe9845b6ab29a8ed2fe787dc..ce95353ec1483d2e508016f1c463f223a96905a8 100644 (file)
@@ -12354,17 +12354,15 @@ Supports navigation to 'foo.bar = 3' and 'foo = {bar: 3}'."
        (let ((parent (js2-node-parent node))
              matching-node)
          (unless endp
-           (if (or (and (js2-prop-get-node-p node)
-                    (not (or (js2-elem-get-node-p parent) (js2-call-node-p parent)))
-                    (setq matching-node (js2-build-prop-name-list node list-names)))
-                 (and (js2-name-node-p node)
-                    (js2-object-prop-node-p parent)
-                    (string= (js2-name-node-name node)
-                             (first list-names))))
+           (if (and (js2-name-node-p node)(setq matching-node (or
+                (js2-build-prop-name-list node list-names)
+                  (and (js2-object-prop-node-p parent)
+                    (string= (js2-name-node-name node)
+                             (first list-names))
+                     node))))
                (throw 'prop-found matching-node))
            t))))))
 
-
 (defun js2-name-declaration (name)
   "Return the declaration node for node named NAME."
   (let* ((node (js2-root-or-node))
@@ -12392,22 +12390,21 @@ the function."
         node
       (js2-node-get-enclosing-scope node))))
 
-(defun js2-build-prop-name-list (prop-node list-names)
-  "Compare the names in PROP-NODE to the ones in LIST-NAMES.
+(defun js2-build-prop-name-list (name-node list-names)
+  "Compare the names in NAME-NODE to the ones in LIST-NAMES.
 Returns the matching node to jump to or nil."
-  (let* (temp-node
-         match-node)
-    (unless (js2-prop-get-node-p prop-node)
-      (error "Node is not a property prop-node"))
-    (catch 'not-a-match
-      (while (js2-prop-get-node-p prop-node)
-        (setq temp-node (js2-prop-get-node-right prop-node))
-        (unless (string= (car list-names) (js2-name-node-name temp-node))
-          (throw 'not-a-match match-node))
-        (unless match-node
-          (setq match-node temp-node))
-        (pop list-names)
-        (setq prop-node (js2-node-parent prop-node))))))
+  (let ((list-names (reverse list-names))
+        (next-prop (js2-node-parent name-node)))
+    ;; check right side properties
+    (when (string= (pop list-names)
+                   (js2-name-node-name name-node))
+      ;; check left side properties
+      (while (and list-names
+                (js2-prop-get-node-p next-prop)
+                (string= (pop list-names)
+                         (js2-name-node-name
+                          (setq next-prop (js2-prop-get-node-right next-prop)))))))
+    (unless list-names name-node)))
 
 (defun js2-get-function-node (name scope)
   "Return node of function named NAME in SCOPE."
index bbd486114bc81b9cde5a5686c782dc2e8f126a9f..78b61f271d5023b05b917b95966a76b51f361623 100644 (file)
 (require 'ert)
 (require 'js2-mode)
 
-(cl-defun js2-navigation-helper (buffer-content expected-point &optional (point-offset 1))
+(cl-defun js2-navigation-helper (buffer-content &optional expected-point (point-offset 1))
   (with-temp-buffer
     (insert buffer-content)
-    (js2-mode)
-    (goto-char (or (- (point) point-offset)))
-    (js2-jump-to-definition)
-    (should (= (point) expected-point))))
+    (let ((start-point (or (- (point) point-offset))))
+      (js2-mode)
+      (goto-char start-point)
+      (js2-jump-to-definition)
+      (print (format "%d %d" (point) start-point))
+      (should (= (point) (or expected-point start-point))))))
 
 (ert-deftest js2-jump-to-var ()
   (js2-navigation-helper "var soup = 2; soup" 5))
@@ -41,3 +43,6 @@
 
 (ert-deftest js2-jump-to-object-property ()
   (js2-navigation-helper "var aObject = {prop1: 3, prop2: \"hello\"}; aObject.prop1" 16))
+
+;; (ert-deftest js2-jump-to-object-property ()
+;;   (js2-navigation-helper "var aObject = {prop1: 3, prop2: \"hello\"}; anotherObject.dprop1"))