Skip to content

Commit b1cca0f

Browse files
committed
Merge pull request #72 from birkenfeld/builtin-face
Change font-lock face for module paths
2 parents ae49380 + 304ae4b commit b1cca0f

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

rust-mode-tests.el

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,16 +1422,34 @@ this_is_not_a_string();)"
14221422
"\"/*! doc */\""
14231423
'("\"/*! doc */\"" font-lock-string-face)))
14241424

1425-
(ert-deftest font-lock-module ()
1425+
(ert-deftest font-lock-module-def ()
1426+
(rust-test-font-lock
1427+
"mod foo;"
1428+
'("mod" font-lock-keyword-face
1429+
"foo" font-lock-constant-face)))
1430+
1431+
(ert-deftest font-lock-module-use ()
1432+
(rust-test-font-lock
1433+
"use foo;"
1434+
'("use" font-lock-keyword-face
1435+
"foo" font-lock-constant-face)))
1436+
1437+
(ert-deftest font-lock-module-path ()
14261438
(rust-test-font-lock
14271439
"foo::bar"
1428-
'("foo" font-lock-type-face)))
1440+
'("foo" font-lock-constant-face)))
14291441

1430-
(ert-deftest font-lock-submodule ()
1442+
(ert-deftest font-lock-submodule-path ()
14311443
(rust-test-font-lock
14321444
"foo::bar::baz"
1433-
'("foo" font-lock-type-face
1434-
"bar" font-lock-type-face)))
1445+
'("foo" font-lock-constant-face
1446+
"bar" font-lock-constant-face)))
1447+
1448+
(ert-deftest font-lock-type ()
1449+
(rust-test-font-lock
1450+
"foo::Bar::baz"
1451+
'("foo" font-lock-constant-face
1452+
"Bar" font-lock-type-face)))
14351453

14361454
(ert-deftest font-lock-type-annotation ()
14371455
"Ensure type annotations are not confused with modules."

rust-mode.el

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
(list 'set (list 'make-local-variable (list 'quote var)) val))))
2929

3030
(defconst rust-re-ident "[[:word:][:multibyte:]_][[:word:][:multibyte:]_[:digit:]]*")
31+
(defconst rust-re-lc-ident "[[:lower:][:multibyte:]_][[:word:][:multibyte:]_[:digit:]]*")
32+
(defconst rust-re-uc-ident "[[:upper:]][[:word:][:multibyte:]_[:digit:]]*")
3133

3234
(defconst rust-re-non-standard-string
3335
(rx
@@ -536,19 +538,21 @@ function or trait. When nil, where will be aligned with fn or trait."
536538
(defconst rust-re-special-types (regexp-opt-symbols rust-special-types))
537539

538540

539-
(defun rust-module-font-lock-matcher (limit)
540-
"Matches module names \"foo::\" but does not match type annotations \"foo::<\"."
541-
(block nil
542-
(while t
543-
(let* ((symbol-then-colons (rx-to-string `(seq (group (regexp ,rust-re-ident)) "::")))
544-
(match (re-search-forward symbol-then-colons limit t)))
545-
(cond
546-
;; If we didn't find a match, there are no more occurrences
547-
;; of foo::, so return.
548-
((null match) (return nil))
549-
;; If this isn't a type annotation foo::<, we've found a
550-
;; match, so a return it!
551-
((not (looking-at (rx (0+ space) "<"))) (return match)))))))
541+
(defun rust-path-font-lock-matcher (re-ident)
542+
"Matches names like \"foo::\" or \"Foo::\" (depending on RE-IDENT, which should match
543+
the desired identifiers), but does not match type annotations \"foo::<\"."
544+
`(lambda (limit)
545+
(block nil
546+
(while t
547+
(let* ((symbol-then-colons (rx-to-string '(seq (group (regexp ,re-ident)) "::")))
548+
(match (re-search-forward symbol-then-colons limit t)))
549+
(cond
550+
;; If we didn't find a match, there are no more occurrences
551+
;; of foo::, so return.
552+
((null match) (return nil))
553+
;; If this isn't a type annotation foo::<, we've found a
554+
;; match, so a return it!
555+
((not (looking-at (rx (0+ space) "<"))) (return match))))))))
552556

553557
(defvar rust-mode-font-lock-keywords
554558
(append
@@ -573,8 +577,11 @@ function or trait. When nil, where will be aligned with fn or trait."
573577
;; Field names like `foo:`, highlight excluding the :
574578
(,(concat (rust-re-grab rust-re-ident) ":[^:]") 1 font-lock-variable-name-face)
575579

580+
;; Type names like `Foo::`, highlight excluding the ::
581+
(,(rust-path-font-lock-matcher rust-re-uc-ident) 1 font-lock-type-face)
582+
576583
;; Module names like `foo::`, highlight excluding the ::
577-
(rust-module-font-lock-matcher 1 font-lock-type-face)
584+
(,(rust-path-font-lock-matcher rust-re-lc-ident) 1 font-lock-constant-face)
578585

579586
;; Lifetimes like `'foo`
580587
(,(concat "'" (rust-re-grab rust-re-ident) "[^']") 1 font-lock-variable-name-face)
@@ -590,10 +597,9 @@ function or trait. When nil, where will be aligned with fn or trait."
590597
'(("enum" . font-lock-type-face)
591598
("struct" . font-lock-type-face)
592599
("type" . font-lock-type-face)
593-
("mod" . font-lock-type-face)
594-
("use" . font-lock-type-face)
595-
("fn" . font-lock-function-name-face)
596-
("static" . font-lock-constant-face)))))
600+
("mod" . font-lock-constant-face)
601+
("use" . font-lock-constant-face)
602+
("fn" . font-lock-function-name-face)))))
597603

598604
(defvar font-lock-beg)
599605
(defvar font-lock-end)

0 commit comments

Comments
 (0)