branch: master
commit 3e778f607e72cc5dee7a4f1aa445d6780b8cc1dd
Merge: 45b5ad0 ce29abd
Author: Jackson Ray Hamilton <[email protected]>
Commit: GitHub <[email protected]>
Merge pull request #6 from jacksonrayhamilton/narrow-to-region
Don't color before or beyond the buffer.
---
context-coloring-javascript.el | 23 +++++++++++++++--------
context-coloring-test.el | 9 +++++++++
fixtures/test/narrow-to-region.js | 3 +++
3 files changed, 27 insertions(+), 8 deletions(-)
diff --git a/context-coloring-javascript.el b/context-coloring-javascript.el
index d145184..29bafb8 100644
--- a/context-coloring-javascript.el
+++ b/context-coloring-javascript.el
@@ -73,26 +73,33 @@ this for ES6 code; disable it elsewhere."
;; `js2-prop-get-node', so this always works.
(eq node (js2-prop-get-node-right parent))))))))
+(defvar-local context-coloring-point-min nil
+ "Cached value of `point-min'.")
+
(defvar-local context-coloring-point-max nil
"Cached value of `point-max'.")
+(defsubst context-coloring-js2-bounded-point (point)
+ "Make POINT safe to set text properties.
+POINT may be unsafe if a JS2 node extends beyond the end of the
+buffer (in the case of an unterminated multiline comment). The
+region could also be narrowed and the node thus obscured."
+ (min (max point context-coloring-point-min) context-coloring-point-max))
+
(defsubst context-coloring-js2-colorize-node (node level)
"Color NODE with the color for LEVEL."
- (let ((start (js2-node-abs-pos node)))
+ (let* ((start (js2-node-abs-pos node))
+ (end (+ start (js2-node-len node))))
(context-coloring-colorize-region
- start
- (min
- ;; End
- (+ start (js2-node-len node))
- ;; Somes nodes (like the ast when there is an unterminated multiline
- ;; comment) will stretch to the value of `point-max'.
- context-coloring-point-max)
+ (context-coloring-js2-bounded-point start)
+ (context-coloring-js2-bounded-point end)
level)))
(defun context-coloring-js2-colorize-ast ()
"Color the buffer using the `js2-mode' abstract syntax tree."
;; Reset the hash table; the old one could be obsolete.
(setq context-coloring-js2-scope-level-hash-table (make-hash-table :test
#'eq))
+ (setq context-coloring-point-min (point-min))
(setq context-coloring-point-max (point-max))
(with-silent-modifications
(js2-visit-ast
diff --git a/context-coloring-test.el b/context-coloring-test.el
index 0d1f539..32080bb 100644
--- a/context-coloring-test.el
+++ b/context-coloring-test.el
@@ -663,6 +663,15 @@ ssssssssssss0"))
(context-coloring-test-assert-javascript-global-level))))
:fixture "initial-level.js")
+(context-coloring-test-deftest-javascript narrow-to-region
+ (lambda ()
+ (context-coloring-test-assert-coloring "
+1111111 0 11 11
+11111111 0 11 11
+11111111 0 11 1"))
+ :before (lambda ()
+ (narrow-to-region (+ (point-min) 1) (- (point-max) 2))))
+
(context-coloring-test-deftest-emacs-lisp defun
(lambda ()
(context-coloring-test-assert-coloring "
diff --git a/fixtures/test/narrow-to-region.js
b/fixtures/test/narrow-to-region.js
new file mode 100644
index 0000000..29f9aef
--- /dev/null
+++ b/fixtures/test/narrow-to-region.js
@@ -0,0 +1,3 @@
+function a () {}
+function b () {}
+function c () {}