Skip to content

Commit e48a650

Browse files
authored
Merge pull request #225 from Aankhen/add-var-colours
Use `font-lock-variable-name-face' for `let' bindings
2 parents b10ad41 + 90f70ac commit e48a650

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

rust-mode-tests.el

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,12 +1284,46 @@ list of substrings of `STR' each followed by its face."
12841284
'("fn" font-lock-keyword-face
12851285
"foo_Bar" font-lock-function-name-face)))
12861286

1287+
(ert-deftest font-lock-let-bindings ()
1288+
(rust-test-font-lock
1289+
"let foo;"
1290+
'("let" font-lock-keyword-face
1291+
"foo" font-lock-variable-name-face))
1292+
(rust-test-font-lock
1293+
"let mut foo;"
1294+
'("let" font-lock-keyword-face
1295+
"mut" font-lock-keyword-face
1296+
"foo" font-lock-variable-name-face))
1297+
(rust-test-font-lock
1298+
"let foo = 1;"
1299+
'("let" font-lock-keyword-face
1300+
"foo" font-lock-variable-name-face))
1301+
(rust-test-font-lock
1302+
"let mut foo = 1;"
1303+
'("let" font-lock-keyword-face
1304+
"mut" font-lock-keyword-face
1305+
"foo" font-lock-variable-name-face))
1306+
(rust-test-font-lock
1307+
"fn foo() { let bar = 1; }"
1308+
'("fn" font-lock-keyword-face
1309+
"foo" font-lock-function-name-face
1310+
"let" font-lock-keyword-face
1311+
"bar" font-lock-variable-name-face))
1312+
(rust-test-font-lock
1313+
"fn foo() { let mut bar = 1; }"
1314+
'("fn" font-lock-keyword-face
1315+
"foo" font-lock-function-name-face
1316+
"let" font-lock-keyword-face
1317+
"mut" font-lock-keyword-face
1318+
"bar" font-lock-variable-name-face)))
1319+
12871320
(ert-deftest font-lock-single-quote-character-literal ()
12881321
(rust-test-font-lock
12891322
"fn main() { let ch = '\\''; }"
12901323
'("fn" font-lock-keyword-face
12911324
"main" font-lock-function-name-face
12921325
"let" font-lock-keyword-face
1326+
"ch" font-lock-variable-name-face
12931327
"'\\''" font-lock-string-face)))
12941328

12951329
(ert-deftest font-lock-escaped-double-quote-character-literal ()
@@ -1298,6 +1332,7 @@ list of substrings of `STR' each followed by its face."
12981332
'("fn" font-lock-keyword-face
12991333
"main" font-lock-function-name-face
13001334
"let" font-lock-keyword-face
1335+
"ch" font-lock-variable-name-face
13011336
"'\\\"'" font-lock-string-face)))
13021337

13031338
(ert-deftest font-lock-escaped-backslash-character-literal ()
@@ -1306,18 +1341,21 @@ list of substrings of `STR' each followed by its face."
13061341
'("fn" font-lock-keyword-face
13071342
"main" font-lock-function-name-face
13081343
"let" font-lock-keyword-face
1344+
"ch" font-lock-variable-name-face
13091345
"'\\\\'" font-lock-string-face)))
13101346

13111347
(ert-deftest font-lock-hex-escape-character-literal ()
13121348
(rust-test-font-lock
13131349
"let ch = '\\x1f';"
13141350
'("let" font-lock-keyword-face
1351+
"ch" font-lock-variable-name-face
13151352
"'\\x1f'" font-lock-string-face)))
13161353

13171354
(ert-deftest font-lock-unicode-escape-character-literal ()
13181355
(rust-test-font-lock
13191356
"let ch = '\\u{1ffff}';"
13201357
'("let" font-lock-keyword-face
1358+
"ch" font-lock-variable-name-face
13211359
"'\\u{1ffff}'" font-lock-string-face)))
13221360

13231361
(ert-deftest font-lock-raw-strings-no-hashes ()
@@ -1583,6 +1621,7 @@ this_is_not_a_string();)"
15831621
(rust-test-font-lock
15841622
"let default = 7; impl foo { default fn f() { } }"
15851623
'("let" font-lock-keyword-face
1624+
"default" font-lock-variable-name-face
15861625
"impl" font-lock-keyword-face
15871626
"default" font-lock-keyword-face
15881627
"fn" font-lock-keyword-face
@@ -1592,7 +1631,8 @@ this_is_not_a_string();)"
15921631
(rust-test-font-lock
15931632
"let union = 7; union foo { x: &'union bar }"
15941633
'("let" font-lock-keyword-face
1595-
;; Ignore the first union, it's an unhighlighted variable.
1634+
;; The first union is a variable name.
1635+
"union" font-lock-variable-name-face
15961636
;; The second union is a contextual keyword.
15971637
"union" font-lock-keyword-face
15981638
"foo" font-lock-type-face

rust-mode.el

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,9 @@ match data if found. Returns nil if not within a Rust string."
681681
;; Field names like `foo:`, highlight excluding the :
682682
(,(concat (rust-re-grab rust-re-ident) ":[^:]") 1 font-lock-variable-name-face)
683683

684+
;; Type-inferred binding
685+
(,(concat "\\_<\\(?:let\\|ref\\)\\s-+\\(?:mut\\s-+\\)?" (rust-re-grab rust-re-ident) "\\_>") 1 font-lock-variable-name-face)
686+
684687
;; Type names like `Foo::`, highlight excluding the ::
685688
(,(rust-path-font-lock-matcher rust-re-uc-ident) 1 font-lock-type-face)
686689

0 commit comments

Comments
 (0)