From 5fe20c380acb66565b2f05bc3dabd1a99c53e0cd Mon Sep 17 00:00:00 2001 From: Konstantin Kharlamov Date: Sun, 22 Jan 2023 18:04:01 +0300 Subject: [PATCH 1/4] Move "C/C++ mode support" code to the top of the paragraph This is a preparation before the next commit where that part will no longer be c-mode specific. --- color-identifiers-mode.el | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/color-identifiers-mode.el b/color-identifiers-mode.el index 592482f..46acdfa 100644 --- a/color-identifiers-mode.el +++ b/color-identifiers-mode.el @@ -191,13 +191,6 @@ SCAN-FN." ;;; MAJOR MODE SUPPORT ========================================================= -;; Scala -(add-to-list - 'color-identifiers:modes-alist - `(scala-mode . (,color-identifiers:re-not-inside-class-access - "\\_<\\([[:lower:]]\\([_]??[[:lower:][:upper:]\\$0-9]+\\)*\\(_+[#:<=>@!%&*+/?\\\\^|~-]+\\|_\\)?\\)" - (nil scala-font-lock:var-face font-lock-variable-name-face tree-sitter-hl-face:variable)))) - ;; C/C++ (defun color-identifiers:cc-mode-get-declarations () "Extract a list of identifiers declared in the current buffer. @@ -228,6 +221,13 @@ For cc-mode support within color-identifiers-mode." "\\_<\\([a-zA-Z_$]\\(?:\\s_\\|\\sw\\)*\\)" (nil font-lock-variable-name-face tree-sitter-hl-face:variable))))) +;; Scala +(add-to-list + 'color-identifiers:modes-alist + `(scala-mode . (,color-identifiers:re-not-inside-class-access + "\\_<\\([[:lower:]]\\([_]??[[:lower:][:upper:]\\$0-9]+\\)*\\(_+[#:<=>@!%&*+/?\\\\^|~-]+\\|_\\)?\\)" + (nil scala-font-lock:var-face font-lock-variable-name-face tree-sitter-hl-face:variable)))) + ;;; JavaScript (add-to-list 'color-identifiers:modes-alist From 9e4d1cc8cf502f10bfbdf972eb83d96c4f0b0bc7 Mon Sep 17 00:00:00 2001 From: Konstantin Kharlamov Date: Sun, 22 Jan 2023 17:49:57 +0300 Subject: [PATCH 2/4] Default to `cc-mode-get-declarations` It isn't actually specific to cc-mode, and works in a way similar to the previous default `scan-identifiers`, except that because it only goes through places with properties set, it is: 1. more performant due to less motion 2. less likely to introduce wrong coloring as in #40 or #62 We also rename the function to remove infix `cc-mode` as it isn't (and never really have been) specific to c-mode. Fixes: https://github.com/ankurdave/color-identifiers-mode/issues/94 --- color-identifiers-mode.el | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/color-identifiers-mode.el b/color-identifiers-mode.el index 46acdfa..9d5a4c1 100644 --- a/color-identifiers-mode.el +++ b/color-identifiers-mode.el @@ -191,13 +191,11 @@ SCAN-FN." ;;; MAJOR MODE SUPPORT ========================================================= -;; C/C++ -(defun color-identifiers:cc-mode-get-declarations () - "Extract a list of identifiers declared in the current buffer. -For cc-mode support within color-identifiers-mode." +(defun color-identifiers:get-declarations () + "Extract a list of identifiers declared in the current buffer." (let ((result (make-hash-table :test 'equal)) (identifier-faces (color-identifiers:curr-identifier-faces))) - ;; Entities that cc-mode highlighted as variables + ;; Entities that major mode highlighted as variables (save-excursion (let ((next-change (next-property-change (point-min)))) (while next-change @@ -213,8 +211,6 @@ For cc-mode support within color-identifiers-mode." (hash-table-keys result))) (dolist (maj-mode '(c-mode c++-mode java-mode rust-mode rustic-mode meson-mode typescript-mode cuda-mode tsx-ts-mode typescript-ts-mode)) - (color-identifiers:set-declaration-scan-fn - maj-mode 'color-identifiers:cc-mode-get-declarations) (add-to-list 'color-identifiers:modes-alist `(,maj-mode . ("" @@ -235,8 +231,6 @@ For cc-mode support within color-identifiers-mode." "\\_<\\([a-zA-Z_$]\\(?:\\s_\\|\\sw\\)*\\)" (nil font-lock-variable-name-face)))) -(color-identifiers:set-declaration-scan-fn - 'js2-mode 'color-identifiers:cc-mode-get-declarations) (add-to-list 'color-identifiers:modes-alist `(js2-mode . (,color-identifiers:re-not-inside-class-access @@ -255,8 +249,6 @@ For cc-mode support within color-identifiers-mode." "\\_<\\([a-zA-Z_$]\\(?:\\s_\\|\\sw\\)*\\)" (nil font-lock-variable-name-face js2-function-param)))) -(color-identifiers:set-declaration-scan-fn - 'js2-jsx-mode 'color-identifiers:cc-mode-get-declarations) (add-to-list 'color-identifiers:modes-alist `(js2-jsx-mode . (,color-identifiers:re-not-inside-class-access @@ -538,8 +530,6 @@ incompatible with Emacs Lisp syntax, such as reader macros (#)." (nil)))) (dolist (maj-mode '(tuareg-mode sml-mode)) - (color-identifiers:set-declaration-scan-fn - maj-mode 'color-identifiers:cc-mode-get-declarations) (add-to-list 'color-identifiers:modes-alist `(,maj-mode . ("" @@ -730,17 +720,8 @@ major mode, identifiers are saved to (if (color-identifiers:get-declaration-scan-fn major-mode) (funcall (color-identifiers:get-declaration-scan-fn major-mode)) ;; When no scan function is registered, fall back to - ;; `color-identifiers:scan-identifiers', which returns all identifiers - (save-excursion - (goto-char (point-min)) - (catch 'input-pending - (let ((result (make-hash-table :test 'equal))) - (color-identifiers:scan-identifiers - (lambda (start end) - (puthash (buffer-substring-no-properties start end) t result)) - (point-max) - (lambda () (if (input-pending-p) (throw 'input-pending nil) t))) - (hash-table-keys result)))))) + ;; `color-identifiers:get-declarations', which returns all identifiers + (color-identifiers:get-declarations))) (defun color-identifiers:refontify () "Refontify the buffer using font-lock." From 9393a1406276623a8a1cf54b1dc6d224e0f9b69e Mon Sep 17 00:00:00 2001 From: Konstantin Kharlamov Date: Sun, 22 Jan 2023 19:37:48 +0300 Subject: [PATCH 3/4] Skip regions we already colorized --- color-identifiers-mode.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/color-identifiers-mode.el b/color-identifiers-mode.el index 9d5a4c1..3cd6c9f 100644 --- a/color-identifiers-mode.el +++ b/color-identifiers-mode.el @@ -768,8 +768,7 @@ evaluates to true." (if continue-p (funcall continue-p) t)) (if (or (memq (get-text-property (point) 'face) identifier-faces) (let ((flface-prop (get-text-property (point) 'font-lock-face))) - (and flface-prop (memq flface-prop identifier-faces))) - (get-text-property (point) 'color-identifiers:fontified)) + (and flface-prop (memq flface-prop identifier-faces)))) (if (and (looking-back identifier-context-re (line-beginning-position)) (or (not identifier-exclusion-re) (not (looking-at identifier-exclusion-re))) (looking-at identifier-re)) From 3a9e24ab0fa11ebfa2892a941eab1e74d7ef350e Mon Sep 17 00:00:00 2001 From: Konstantin Kharlamov Date: Sun, 22 Jan 2023 21:17:00 +0300 Subject: [PATCH 4/4] Clarify a comment in `get-declarations` --- color-identifiers-mode.el | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/color-identifiers-mode.el b/color-identifiers-mode.el index 3cd6c9f..4f50398 100644 --- a/color-identifiers-mode.el +++ b/color-identifiers-mode.el @@ -202,9 +202,9 @@ SCAN-FN." (goto-char next-change) (let ((face-at-point (get-text-property (point) 'face))) (when (or (and face-at-point (memq face-at-point identifier-faces)) - ;; If we fontified it in the past, assume it should - ;; continue to be fontified. This avoids alternating - ;; between fontified and unfontified. + ;; If we fontified X in the past, keep X in the list for + ;; consistency. Otherwise `scan-identifiers' will stop + ;; colorizing new Xes while older ones remain colorized. (get-text-property (point) 'color-identifiers:fontified)) (puthash (substring-no-properties (symbol-name (symbol-at-point))) t result))) (setq next-change (next-property-change (point))))))