From 8feb392dd2674fe5842655e71d8f77aad04e0e1e Mon Sep 17 00:00:00 2001 From: Konstantin Kharlamov Date: Sun, 22 Jan 2023 17:49:57 +0300 Subject: [PATCH] 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 9a68a4c..0330c93 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 @@ -544,8 +536,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 . ("" @@ -736,17 +726,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."