Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to cc-mode-get-declarations #97

Merged
merged 4 commits into from
Jan 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 16 additions & 36 deletions color-identifiers-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -191,52 +191,46 @@ 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.
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
(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))))))
(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 . (""
"\\_<\\([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
`(js-mode . (,color-identifiers:re-not-inside-class-access
"\\_<\\([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
Expand All @@ -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
Expand Down Expand Up @@ -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 . (""
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -787,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))
Expand Down