Skip to content

Commit d1ed015

Browse files
committed
Distinguish face for doc-comments
Using the syntactic-face-function, we can assign the proper `font-lock-doc-face' to doc comments ("///", "//!", "/**", "/*!"). Test changes graciously copied from #64
1 parent 5ed4675 commit d1ed015

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

rust-mode-tests.el

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,70 @@ this_is_not_a_string();)"
10851085
(should (equal nil (get-text-property 28 'face))) ;; Semicolon--should not be part of the string
10861086
))
10871087

1088+
;;; Documentation comments
1089+
1090+
(ert-deftest font-lock-doc-line-comment-parent ()
1091+
(rust-test-font-lock
1092+
"//! doc"
1093+
'("//! doc" font-lock-doc-face)))
1094+
1095+
(ert-deftest font-lock-doc-line-comment-item ()
1096+
(rust-test-font-lock
1097+
"/// doc"
1098+
'("/// doc" font-lock-doc-face)))
1099+
1100+
(ert-deftest font-lock-nondoc-line ()
1101+
(rust-test-font-lock
1102+
"////// doc"
1103+
'("////// " font-lock-comment-delimiter-face
1104+
"doc" font-lock-comment-face)))
1105+
1106+
(ert-deftest font-lock-doc-line-in-string ()
1107+
(rust-test-font-lock
1108+
"\"/// doc\""
1109+
'("\"/// doc\"" font-lock-string-face))
1110+
1111+
(rust-test-font-lock
1112+
"\"//! doc\""
1113+
'("\"//! doc\"" font-lock-string-face)))
1114+
1115+
(ert-deftest font-lock-doc-line-in-nested-comment ()
1116+
(rust-test-font-lock
1117+
"/* /// doc */"
1118+
'("/* " font-lock-comment-delimiter-face
1119+
"/// doc */" font-lock-comment-face))
1120+
1121+
(rust-test-font-lock
1122+
"/* //! doc */"
1123+
'("/* " font-lock-comment-delimiter-face
1124+
"//! doc */" font-lock-comment-face)))
1125+
1126+
1127+
(ert-deftest font-lock-doc-block-comment-parent ()
1128+
(rust-test-font-lock
1129+
"/*! doc */"
1130+
'("/*! doc */" font-lock-doc-face)))
1131+
1132+
(ert-deftest font-lock-doc-block-comment-item ()
1133+
(rust-test-font-lock
1134+
"/** doc */"
1135+
'("/** doc */" font-lock-doc-face)))
1136+
1137+
(ert-deftest font-lock-nondoc-block-comment-item ()
1138+
(rust-test-font-lock
1139+
"/***** doc */"
1140+
'("/**" font-lock-comment-delimiter-face
1141+
"*** doc */" font-lock-comment-face)))
1142+
1143+
(ert-deftest font-lock-doc-block-in-string ()
1144+
(rust-test-font-lock
1145+
"\"/** doc */\""
1146+
'("\"/** doc */\"" font-lock-string-face))
1147+
(rust-test-font-lock
1148+
"\"/*! doc */\""
1149+
'("\"/*! doc */\"" font-lock-string-face)))
1150+
1151+
10881152
(ert-deftest indent-method-chains-no-align ()
10891153
(let ((rust-indent-method-chain nil)) (test-indent
10901154
"

rust-mode.el

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,16 @@
480480
;; Handle raw strings:
481481
`((rust-look-for-raw-string (1 "|") (4 "_" nil t) (5 "|" nil t) (6 "|" nil t)))))
482482

483+
(defun rust-mode-syntactic-face-function (state)
484+
"Syntactic face function to distinguish doc comments from other comments."
485+
(if (nth 3 state) 'font-lock-string-face
486+
(save-excursion
487+
(goto-char (nth 8 state))
488+
(if (looking-at "/\\([*][*!][^*!]\\|/[/!][^/!]\\)")
489+
'font-lock-doc-face
490+
'font-lock-comment-face
491+
))))
492+
483493
(defun rust-fill-prefix-for-comment-start (line-start)
484494
"Determine what to use for `fill-prefix' based on what is at the beginning of a line."
485495
(let ((result
@@ -740,7 +750,11 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
740750

741751
;; Fonts
742752
(add-to-list 'font-lock-extend-region-functions 'rust-extend-region-raw-string)
743-
(setq-local font-lock-defaults '(rust-mode-font-lock-keywords nil nil nil nil (font-lock-syntactic-keywords . rust-mode-font-lock-syntactic-keywords)))
753+
(setq-local font-lock-defaults '(rust-mode-font-lock-keywords
754+
nil nil nil nil
755+
(font-lock-syntactic-keywords . rust-mode-font-lock-syntactic-keywords)
756+
(font-lock-syntactic-face-function . rust-mode-syntactic-face-function)
757+
))
744758

745759
;; Misc
746760
(setq-local comment-start "// ")

0 commit comments

Comments
 (0)