Skip to content

Commit 304ae4b

Browse files
committed
Change font-lock face for module names.
Use font-lock-constant-face instead of font-lock-type-face. Especially in paths, this tones down the importance of the path prefix, and makes the suffix more visible.
1 parent fa5b38f commit 304ae4b

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
@@ -527,19 +529,21 @@ function or trait. When nil, where will be aligned with fn or trait."
527529
(defconst rust-re-special-types (regexp-opt-symbols rust-special-types))
528530

529531

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

544548
(defvar rust-mode-font-lock-keywords
545549
(append
@@ -564,8 +568,11 @@ function or trait. When nil, where will be aligned with fn or trait."
564568
;; Field names like `foo:`, highlight excluding the :
565569
(,(concat (rust-re-grab rust-re-ident) ":[^:]") 1 font-lock-variable-name-face)
566570

571+
;; Type names like `Foo::`, highlight excluding the ::
572+
(,(rust-path-font-lock-matcher rust-re-uc-ident) 1 font-lock-type-face)
573+
567574
;; Module names like `foo::`, highlight excluding the ::
568-
(rust-module-font-lock-matcher 1 font-lock-type-face)
575+
(,(rust-path-font-lock-matcher rust-re-lc-ident) 1 font-lock-constant-face)
569576

570577
;; Lifetimes like `'foo`
571578
(,(concat "'" (rust-re-grab rust-re-ident) "[^']") 1 font-lock-variable-name-face)
@@ -581,10 +588,9 @@ function or trait. When nil, where will be aligned with fn or trait."
581588
'(("enum" . font-lock-type-face)
582589
("struct" . font-lock-type-face)
583590
("type" . font-lock-type-face)
584-
("mod" . font-lock-type-face)
585-
("use" . font-lock-type-face)
586-
("fn" . font-lock-function-name-face)
587-
("static" . font-lock-constant-face)))))
591+
("mod" . font-lock-constant-face)
592+
("use" . font-lock-constant-face)
593+
("fn" . font-lock-function-name-face)))))
588594

589595
(defvar font-lock-beg)
590596
(defvar font-lock-end)

0 commit comments

Comments
 (0)