Skip to content

Commit ad8b437

Browse files
committed
Make 'foo use font-lock-builtin-face, like module names, and make capitalized identifiers optionally use font-lock-type-face
1 parent 1ef8c48 commit ad8b437

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/etc/emacs/rust-mode.el

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
(require 'cm-mode)
99
(require 'cc-mode)
10-
(eval-when-compile (require 'cl))
1110

1211
(defun rust-electric-brace (arg)
1312
(interactive "*P")
@@ -17,6 +16,12 @@
1716
'(font-lock-comment-face font-lock-string-face))))
1817
(cm-indent)))
1918

19+
(defcustom rust-capitalized-idents-are-types t
20+
"If non-nil, capitalized identifiers will be treated as types for the purposes of font-lock mode"
21+
:type 'boolean
22+
:require 'rust-mode
23+
:group 'rust-mode)
24+
2025
(defvar rust-indent-unit 4)
2126
(defvar rust-syntax-table (let ((table (make-syntax-table)))
2227
(c-populate-syntax-table table)
@@ -115,12 +120,7 @@
115120
((rust-eat-re "[a-z_]+") (setf rust-tcat 'macro)))
116121
'font-lock-preprocessor-face)
117122
(def ((?a . ?z) (?A . ?Z) ?_)
118-
(rust-eat-re "[a-zA-Z_][a-zA-Z0-9_]*")
119-
(setf rust-tcat 'ident)
120-
(if (and (eq (char-after) ?:) (eq (char-after (+ (point) 1)) ?:)
121-
(not (eq (char-after (+ (point) 2)) ?:)))
122-
(progn (forward-char 2) 'font-lock-builtin-face)
123-
(match-string 0)))
123+
(rust-token-identifier))
124124
(def ((?0 . ?9))
125125
(rust-eat-re "0x[0-9a-fA-F_]+\\|0b[01_]+\\|[0-9_]+\\(\\.[0-9_]+\\)?\\(e[+\\-]?[0-9_]+\\)?")
126126
(setf rust-tcat 'atom)
@@ -143,15 +143,23 @@
143143
(setf rust-tcat 'op) nil)
144144
table)))
145145

146+
(defun rust-token-identifier ()
147+
(rust-eat-re "[a-zA-Z_][a-zA-Z0-9_]*")
148+
(setf rust-tcat 'ident)
149+
(if (and (eq (char-after) ?:) (eq (char-after (+ (point) 1)) ?:)
150+
(not (eq (char-after (+ (point) 2)) ?:)))
151+
(progn (forward-char 2) 'font-lock-builtin-face)
152+
(match-string 0)))
153+
146154
(defun rust-single-quote ()
147155
(forward-char)
148156
(setf rust-tcat 'atom)
149157
; Is this a lifetime?
150158
(if (or (looking-at "[a-zA-Z_]$")
151159
(looking-at "[a-zA-Z_][^']"))
152-
; If what we see is 'abc, use font-lock-type-face:
160+
; If what we see is 'abc, use font-lock-builtin-face:
153161
(progn (rust-eat-re "[a-zA-Z_][a-zA-Z_0-9]*")
154-
'font-lock-type-face)
162+
'font-lock-builtin-face)
155163
; Otherwise, handle as a character constant:
156164
(let ((is-escape (eq (char-after) ?\\))
157165
(start (point)))
@@ -200,6 +208,10 @@
200208
(dolist (cx (rust-state-context st))
201209
(when (eq (rust-context-type cx) ?\}) (return (rust-context-info cx)))))
202210

211+
(defun rust-is-capitalized (string)
212+
(let ((case-fold-search nil))
213+
(string-match-p "[A-Z]" string)))
214+
203215
(defun rust-token (st)
204216
(let ((cx (car (rust-state-context st))))
205217
(when (bolp)
@@ -216,6 +228,8 @@
216228
(setf tok (cond ((eq tok-id 'atom) 'font-lock-constant-face)
217229
(tok-id 'font-lock-keyword-face)
218230
((equal (rust-state-last-token st) 'def) 'font-lock-function-name-face)
231+
((and rust-capitalized-idents-are-types
232+
(rust-is-capitalized tok)) 'font-lock-type-face)
219233
(t nil))))
220234
(when rust-tcat
221235
(when (eq (rust-context-align cx) 'unset)

0 commit comments

Comments
 (0)