1
Fork 0

Make 'foo use font-lock-builtin-face, like module names, and make capitalized identifiers optionally use font-lock-type-face

This commit is contained in:
Niko Matsakis 2013-02-12 14:39:11 -08:00
parent 1ef8c48a20
commit ad8b437ada

View file

@ -7,7 +7,6 @@
(require 'cm-mode) (require 'cm-mode)
(require 'cc-mode) (require 'cc-mode)
(eval-when-compile (require 'cl))
(defun rust-electric-brace (arg) (defun rust-electric-brace (arg)
(interactive "*P") (interactive "*P")
@ -17,6 +16,12 @@
'(font-lock-comment-face font-lock-string-face)))) '(font-lock-comment-face font-lock-string-face))))
(cm-indent))) (cm-indent)))
(defcustom rust-capitalized-idents-are-types t
"If non-nil, capitalized identifiers will be treated as types for the purposes of font-lock mode"
:type 'boolean
:require 'rust-mode
:group 'rust-mode)
(defvar rust-indent-unit 4) (defvar rust-indent-unit 4)
(defvar rust-syntax-table (let ((table (make-syntax-table))) (defvar rust-syntax-table (let ((table (make-syntax-table)))
(c-populate-syntax-table table) (c-populate-syntax-table table)
@ -115,12 +120,7 @@
((rust-eat-re "[a-z_]+") (setf rust-tcat 'macro))) ((rust-eat-re "[a-z_]+") (setf rust-tcat 'macro)))
'font-lock-preprocessor-face) 'font-lock-preprocessor-face)
(def ((?a . ?z) (?A . ?Z) ?_) (def ((?a . ?z) (?A . ?Z) ?_)
(rust-eat-re "[a-zA-Z_][a-zA-Z0-9_]*") (rust-token-identifier))
(setf rust-tcat 'ident)
(if (and (eq (char-after) ?:) (eq (char-after (+ (point) 1)) ?:)
(not (eq (char-after (+ (point) 2)) ?:)))
(progn (forward-char 2) 'font-lock-builtin-face)
(match-string 0)))
(def ((?0 . ?9)) (def ((?0 . ?9))
(rust-eat-re "0x[0-9a-fA-F_]+\\|0b[01_]+\\|[0-9_]+\\(\\.[0-9_]+\\)?\\(e[+\\-]?[0-9_]+\\)?") (rust-eat-re "0x[0-9a-fA-F_]+\\|0b[01_]+\\|[0-9_]+\\(\\.[0-9_]+\\)?\\(e[+\\-]?[0-9_]+\\)?")
(setf rust-tcat 'atom) (setf rust-tcat 'atom)
@ -143,15 +143,23 @@
(setf rust-tcat 'op) nil) (setf rust-tcat 'op) nil)
table))) table)))
(defun rust-token-identifier ()
(rust-eat-re "[a-zA-Z_][a-zA-Z0-9_]*")
(setf rust-tcat 'ident)
(if (and (eq (char-after) ?:) (eq (char-after (+ (point) 1)) ?:)
(not (eq (char-after (+ (point) 2)) ?:)))
(progn (forward-char 2) 'font-lock-builtin-face)
(match-string 0)))
(defun rust-single-quote () (defun rust-single-quote ()
(forward-char) (forward-char)
(setf rust-tcat 'atom) (setf rust-tcat 'atom)
; Is this a lifetime? ; Is this a lifetime?
(if (or (looking-at "[a-zA-Z_]$") (if (or (looking-at "[a-zA-Z_]$")
(looking-at "[a-zA-Z_][^']")) (looking-at "[a-zA-Z_][^']"))
; If what we see is 'abc, use font-lock-type-face: ; If what we see is 'abc, use font-lock-builtin-face:
(progn (rust-eat-re "[a-zA-Z_][a-zA-Z_0-9]*") (progn (rust-eat-re "[a-zA-Z_][a-zA-Z_0-9]*")
'font-lock-type-face) 'font-lock-builtin-face)
; Otherwise, handle as a character constant: ; Otherwise, handle as a character constant:
(let ((is-escape (eq (char-after) ?\\)) (let ((is-escape (eq (char-after) ?\\))
(start (point))) (start (point)))
@ -200,6 +208,10 @@
(dolist (cx (rust-state-context st)) (dolist (cx (rust-state-context st))
(when (eq (rust-context-type cx) ?\}) (return (rust-context-info cx))))) (when (eq (rust-context-type cx) ?\}) (return (rust-context-info cx)))))
(defun rust-is-capitalized (string)
(let ((case-fold-search nil))
(string-match-p "[A-Z]" string)))
(defun rust-token (st) (defun rust-token (st)
(let ((cx (car (rust-state-context st)))) (let ((cx (car (rust-state-context st))))
(when (bolp) (when (bolp)
@ -216,6 +228,8 @@
(setf tok (cond ((eq tok-id 'atom) 'font-lock-constant-face) (setf tok (cond ((eq tok-id 'atom) 'font-lock-constant-face)
(tok-id 'font-lock-keyword-face) (tok-id 'font-lock-keyword-face)
((equal (rust-state-last-token st) 'def) 'font-lock-function-name-face) ((equal (rust-state-last-token st) 'def) 'font-lock-function-name-face)
((and rust-capitalized-idents-are-types
(rust-is-capitalized tok)) 'font-lock-type-face)
(t nil)))) (t nil))))
(when rust-tcat (when rust-tcat
(when (eq (rust-context-align cx) 'unset) (when (eq (rust-context-align cx) 'unset)