branch: master commit ba015cea148300fb10f19acfcbebf046d52e9258 Author: Jackson Ray Hamilton <jack...@jacksonrayhamilton.com> Commit: Jackson Ray Hamilton <jack...@jacksonrayhamilton.com>
Add option to only colorize comments specially. --- context-coloring.el | 24 +++++++++++++++++++++--- test/context-coloring-test.el | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/context-coloring.el b/context-coloring.el index 849d392..4755ca0 100644 --- a/context-coloring.el +++ b/context-coloring.el @@ -136,12 +136,27 @@ the END point (exclusive) with the face corresponding to LEVEL." "If non-nil, also color comments and strings using `font-lock'." :group 'context-coloring) +(defcustom context-coloring-syntactic-comments nil + "If non-nil, also color comments using `font-lock'." + :group 'context-coloring) + +(defun context-coloring-font-lock-syntactic-comment-function (state) + "Tell `font-lock' to color a comment but not a string." + (if (nth 3 state) nil font-lock-comment-face)) + (defsubst context-coloring-maybe-colorize-comments-and-strings () "Color the current buffer's comments and strings if `context-coloring-comments-and-strings' is non-nil." - (when context-coloring-comments-and-strings - (save-excursion - (font-lock-fontify-syntactically-region (point-min) (point-max))))) + (when (or context-coloring-comments-and-strings + context-coloring-syntactic-comments) + (let ((old-function font-lock-syntactic-face-function)) + (when context-coloring-syntactic-comments + (setq font-lock-syntactic-face-function + 'context-coloring-font-lock-syntactic-comment-function)) + (save-excursion + (font-lock-fontify-syntactically-region (point-min) (point-max))) + (when context-coloring-syntactic-comments + (setq font-lock-syntactic-face-function old-function))))) ;;; js2-mode colorization @@ -835,6 +850,9 @@ Supported modes: `js-mode', `js3-mode'" (font-lock-mode 0) (jit-lock-mode nil) + ;; Safely change the valye of this function as necessary. + (make-local-variable 'font-lock-syntactic-face-function) + (let ((dispatch (gethash major-mode context-coloring-mode-hash-table))) (when dispatch (let ((command (plist-get dispatch :command)) diff --git a/test/context-coloring-test.el b/test/context-coloring-test.el index 8e26991..e8d6474 100644 --- a/test/context-coloring-test.el +++ b/test/context-coloring-test.el @@ -52,6 +52,7 @@ (defun context-coloring-test-cleanup () "Cleanup after all tests." (setq context-coloring-comments-and-strings t) + (setq context-coloring-syntactic-comments nil) (setq context-coloring-js-block-scopes nil)) (defmacro context-coloring-test-with-fixture (fixture &rest body) @@ -717,6 +718,32 @@ see that function." (context-coloring-colorize) (context-coloring-test-js-comments-and-strings))) +(defun context-coloring-test-js-syntactic-comments () + "Test fixtures/comments-and-strings.js." + (context-coloring-test-assert-region-comment-delimiter 1 4) + (context-coloring-test-assert-region-comment 4 8) + (context-coloring-test-assert-region-comment-delimiter 9 12) + (context-coloring-test-assert-region-comment 12 19) + (context-coloring-test-assert-region-level 20 33 0)) + +(ert-deftest-async context-coloring-test-js-mode-syntactic-comments (done) + (context-coloring-test-js-mode + "./fixtures/comments-and-strings.js" + (lambda (teardown) + (unwind-protect + (context-coloring-test-js-syntactic-comments) + (funcall teardown)) + (funcall done)) + (lambda () + (setq context-coloring-syntactic-comments t)))) + +(ert-deftest context-coloring-test-js2-mode-syntactic-comments () + (context-coloring-test-js2-mode + "./fixtures/comments-and-strings.js" + (setq context-coloring-syntactic-comments t) + (context-coloring-colorize) + (context-coloring-test-js-syntactic-comments))) + (provide 'context-coloring-test) ;;; context-coloring-test.el ends here