Skip to content

Commit bc71aa6

Browse files
authored
Merge pull request #430 from Chris00/master
Improve and add tests for my previous 2 contributions
2 parents 8aebccb + 2d4d2d2 commit bc71aa6

File tree

2 files changed

+96
-7
lines changed

2 files changed

+96
-7
lines changed

rust-mode-tests.el

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,92 @@ list of substrings of `STR' each followed by its face."
13541354
"mut" font-lock-keyword-face
13551355
"bar" font-lock-variable-name-face)))
13561356

1357+
(ert-deftest font-lock-ampersand ()
1358+
(rust-test-font-lock
1359+
"f(&a)"
1360+
'("&" rust-ampersand-face))
1361+
(rust-test-font-lock
1362+
"a && b &&& c"
1363+
nil)
1364+
(rust-test-font-lock
1365+
"&'a v"
1366+
'("&" rust-ampersand-face
1367+
"a" font-lock-variable-name-face))
1368+
(rust-test-font-lock
1369+
"&'static v"
1370+
'("&" rust-ampersand-face
1371+
"static" font-lock-keyword-face))
1372+
(rust-test-font-lock
1373+
"&mut v"
1374+
'("&" rust-ampersand-face
1375+
"mut" font-lock-keyword-face))
1376+
(rust-test-font-lock
1377+
"&f(&x)"
1378+
'("&" rust-ampersand-face
1379+
"&" rust-ampersand-face))
1380+
(rust-test-font-lock
1381+
"fn f(x: &X)"
1382+
'("fn" font-lock-keyword-face
1383+
"f" font-lock-function-name-face
1384+
"x" font-lock-variable-name-face
1385+
"&" rust-ampersand-face
1386+
"X" font-lock-type-face))
1387+
(rust-test-font-lock
1388+
"f(&X{x})"
1389+
'("&" rust-ampersand-face
1390+
"X" font-lock-type-face))
1391+
(rust-test-font-lock
1392+
"let x: &'_ f64 = &1.;"
1393+
'("let" font-lock-keyword-face
1394+
"x" font-lock-variable-name-face
1395+
"&" rust-ampersand-face
1396+
"_" font-lock-variable-name-face
1397+
"f64" font-lock-type-face
1398+
"&" rust-ampersand-face))
1399+
(rust-test-font-lock
1400+
"let x = &&1;"
1401+
'("let" font-lock-keyword-face
1402+
"x" font-lock-variable-name-face
1403+
"&&" rust-ampersand-face))
1404+
(rust-test-font-lock
1405+
"let x = &*y;"
1406+
'("let" font-lock-keyword-face
1407+
"x" font-lock-variable-name-face
1408+
"&" rust-ampersand-face))
1409+
(rust-test-font-lock
1410+
"let x = &::std::f64::consts::PI;"
1411+
'("let" font-lock-keyword-face
1412+
"x" font-lock-variable-name-face
1413+
"&" rust-ampersand-face
1414+
"std" font-lock-constant-face
1415+
"f64" font-lock-type-face
1416+
"consts" font-lock-constant-face
1417+
"PI" font-lock-type-face))
1418+
(rust-test-font-lock
1419+
"let x = &(1, 2);"
1420+
'("let" font-lock-keyword-face
1421+
"x" font-lock-variable-name-face
1422+
"&" rust-ampersand-face))
1423+
(rust-test-font-lock
1424+
"let x = &{1};"
1425+
'("let" font-lock-keyword-face
1426+
"x" font-lock-variable-name-face
1427+
"&" rust-ampersand-face))
1428+
(rust-test-font-lock
1429+
"let f = &|x| {x + 1};"
1430+
'("let" font-lock-keyword-face
1431+
"f" font-lock-variable-name-face
1432+
"&" rust-ampersand-face))
1433+
(rust-test-font-lock
1434+
"let x: &_ = &1;"
1435+
'("let" font-lock-keyword-face
1436+
"x" font-lock-variable-name-face
1437+
"&" rust-ampersand-face
1438+
"&" rust-ampersand-face))
1439+
(rust-test-font-lock
1440+
"&[1,2]"
1441+
'("&" rust-ampersand-face)))
1442+
13571443
(ert-deftest font-lock-if-let-binding ()
13581444
(rust-test-font-lock
13591445
"if let Some(var) = some_var { /* no-op */ }"

rust-mode.el

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,14 @@ Use idomenu (imenu with `ido-mode') for best mileage.")
222222
See `prettify-symbols-compose-predicate'."
223223
(and (fboundp 'prettify-symbols-default-compose-p)
224224
(prettify-symbols-default-compose-p start end match)
225-
;; Make sure there is a space before || as it is also used for
226-
;; functions with 0 arguments.
227-
(not (and (string= match "||")
228-
(save-excursion
229-
(goto-char start)
230-
(looking-back "\\(?:\\<move\\|=\\) *"))))))
225+
;; Make sure || is not a closure with 0 arguments and && is not
226+
;; a double reference.
227+
(pcase match
228+
("||" (not (save-excursion
229+
(goto-char start)
230+
(looking-back "\\(?:\\<move\\|[[({:=,;]\\) *"))))
231+
("&&" (char-equal (char-after end) ?\s))
232+
(_ t))))
231233

232234
;;; Mode
233235

@@ -465,7 +467,8 @@ Does not match type annotations of the form \"foo::<\"."
465467

466468
;; Question mark operator
467469
("\\?" . 'rust-question-mark)
468-
("\\(&\\)'?\\<" 1 'rust-ampersand-face)
470+
("\\(&+\\)\\(?:'\\(?:\\<\\|_\\)\\|\\<\\|[[({:*_|]\\)"
471+
1 'rust-ampersand-face)
469472
)
470473

471474
;; Ensure we highlight `Foo` in `struct Foo` as a type.

0 commit comments

Comments
 (0)