]> code.delx.au - gnu-emacs-elpa/commitdiff
Use a hash table as a scope level fast track.
authorJackson Ray Hamilton <jackson@jacksonrayhamilton.com>
Mon, 5 Jan 2015 07:19:36 +0000 (23:19 -0800)
committerJackson Ray Hamilton <jackson@jacksonrayhamilton.com>
Mon, 5 Jan 2015 07:19:36 +0000 (23:19 -0800)
context-coloring.el

index 7005dcfab29bdba2f8f8172ca8fa3610e9a234e0..ac941db97e9a5c95419c0203ee2d83cb162d3c5d 100644 (file)
@@ -227,21 +227,28 @@ END (exclusive) with the face corresponding to LEVEL."
 
 ;;; js2-mode colorization
 
+(defvar-local context-coloring-js2-scope-level-hash-table nil
+  "Associates `js2-scope' structures and with their scope
+  levels.")
+
 (defsubst context-coloring-js2-scope-level (scope)
   "Gets the level of SCOPE."
-  (let ((level 0)
-        enclosing-scope)
-    (while (and scope
-                (js2-node-parent scope)
-                (setq enclosing-scope (js2-node-get-enclosing-scope scope)))
-      (when (or context-coloring-js-block-scopes
-                (let ((type (js2-scope-type scope)))
-                  (or (= type js2-SCRIPT)
-                      (= type js2-FUNCTION)
-                      (= type js2-CATCH))))
-        (setq level (+ level 1)))
-      (setq scope enclosing-scope))
-    level))
+  (cond ((gethash scope context-coloring-js2-scope-level-hash-table))
+        (t
+         (let ((level 0)
+               (current-scope scope)
+               enclosing-scope)
+           (while (and current-scope
+                       (js2-node-parent current-scope)
+                       (setq enclosing-scope (js2-node-get-enclosing-scope current-scope)))
+             (when (or context-coloring-js-block-scopes
+                       (let ((type (js2-scope-type current-scope)))
+                         (or (= type js2-SCRIPT)
+                             (= type js2-FUNCTION)
+                             (= type js2-CATCH))))
+               (setq level (+ level 1)))
+             (setq current-scope enclosing-scope))
+           (puthash scope level context-coloring-js2-scope-level-hash-table)))))
 
 (defsubst context-coloring-js2-name-node-level (node)
   (cond ((js2-node-top-level-decl-p node)
@@ -274,6 +281,8 @@ variable."
 (defun context-coloring-js2-colorize ()
   "Colorizes the current buffer using the abstract syntax tree
 generated by js2-mode."
+  ;; Reset the hash table; the old one could be obsolete.
+  (setq context-coloring-js2-scope-level-hash-table (make-hash-table :test 'eq))
   (with-silent-modifications
     (js2-visit-ast
      js2-mode-ast