Skip to content

Commit 5b68439

Browse files
Merge pull request #20583 from btj/comments-enable
Add `rust-analyzer.semanticHighlighting.comments.enable`
2 parents 78bb458 + d106d41 commit 5b68439

File tree

9 files changed

+156
-18
lines changed

9 files changed

+156
-18
lines changed

crates/ide/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,20 @@ impl Analysis {
703703
})
704704
}
705705

706+
/// Computes syntax highlighting for the given file.
707+
pub fn highlight_as_html_with_config(
708+
&self,
709+
config: HighlightConfig,
710+
file_id: FileId,
711+
rainbow: bool,
712+
) -> Cancellable<String> {
713+
// highlighting may construct a new database for "speculative" execution, so we can't currently attach the database
714+
// highlighting instead sets up the attach hook where neceesary for the trait solver
715+
Cancelled::catch(|| {
716+
syntax_highlighting::highlight_as_html_with_config(&self.db, config, file_id, rainbow)
717+
})
718+
}
719+
706720
/// Computes syntax highlighting for the given file.
707721
pub fn highlight_as_html(&self, file_id: FileId, rainbow: bool) -> Cancellable<String> {
708722
// highlighting may construct a new database for "speculative" execution, so we can't currently attach the database

crates/ide/src/syntax_highlighting.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::{
3535
};
3636

3737
pub(crate) use html::highlight_as_html;
38+
pub(crate) use html::highlight_as_html_with_config;
3839

3940
#[derive(Debug, Clone, Copy)]
4041
pub struct HlRange {
@@ -47,6 +48,8 @@ pub struct HlRange {
4748
pub struct HighlightConfig {
4849
/// Whether to highlight strings
4950
pub strings: bool,
51+
/// Whether to highlight comments
52+
pub comments: bool,
5053
/// Whether to highlight punctuation
5154
pub punctuation: bool,
5255
/// Whether to specialize punctuation highlights
@@ -588,6 +591,7 @@ fn descend_token(
588591
fn filter_by_config(highlight: &mut Highlight, config: HighlightConfig) -> bool {
589592
match &mut highlight.tag {
590593
HlTag::StringLiteral if !config.strings => return false,
594+
HlTag::Comment if !config.comments => return false,
591595
// If punctuation is disabled, make the macro bang part of the macro call again.
592596
tag @ HlTag::Punctuation(HlPunct::MacroBang) => {
593597
if !config.macro_bang {

crates/ide/src/syntax_highlighting/html.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ use crate::{
1010
syntax_highlighting::{HighlightConfig, highlight},
1111
};
1212

13-
pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String {
13+
pub(crate) fn highlight_as_html_with_config(
14+
db: &RootDatabase,
15+
config: HighlightConfig,
16+
file_id: FileId,
17+
rainbow: bool,
18+
) -> String {
1419
let sema = Semantics::new(db);
1520
let file_id = sema
1621
.attach_first_edition(file_id)
@@ -27,21 +32,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
2732
)
2833
}
2934

30-
let hl_ranges = highlight(
31-
db,
32-
HighlightConfig {
33-
strings: true,
34-
punctuation: true,
35-
specialize_punctuation: true,
36-
specialize_operator: true,
37-
operator: true,
38-
inject_doc_comment: true,
39-
macro_bang: true,
40-
syntactic_name_ref_highlighting: false,
41-
},
42-
file_id.file_id(db),
43-
None,
44-
);
35+
let hl_ranges = highlight(db, config, file_id.file_id(db), None);
4536
let text = file.to_string();
4637
let mut buf = String::new();
4738
buf.push_str(STYLE);
@@ -66,6 +57,25 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
6657
buf
6758
}
6859

60+
pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String {
61+
highlight_as_html_with_config(
62+
db,
63+
HighlightConfig {
64+
strings: true,
65+
comments: true,
66+
punctuation: true,
67+
specialize_punctuation: true,
68+
specialize_operator: true,
69+
operator: true,
70+
inject_doc_comment: true,
71+
macro_bang: true,
72+
syntactic_name_ref_highlighting: false,
73+
},
74+
file_id,
75+
rainbow,
76+
)
77+
}
78+
6979
//FIXME: like, real html escaping
7080
fn html_escape(text: &str) -> String {
7181
text.replace('<', "&lt;").replace('>', "&gt;")

crates/ide/src/syntax_highlighting/inject.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub(super) fn ra_fixture(
8080
.highlight(
8181
HighlightConfig {
8282
syntactic_name_ref_highlighting: false,
83+
comments: true,
8384
punctuation: true,
8485
operator: true,
8586
strings: true,
@@ -250,6 +251,7 @@ pub(super) fn doc_comment(
250251
db,
251252
HighlightConfig {
252253
syntactic_name_ref_highlighting: true,
254+
comments: true,
253255
punctuation: true,
254256
operator: true,
255257
strings: true,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
<style>
3+
body { margin: 0; }
4+
pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
5+
6+
.lifetime { color: #DFAF8F; font-style: italic; }
7+
.label { color: #DFAF8F; font-style: italic; }
8+
.comment { color: #7F9F7F; }
9+
.documentation { color: #629755; }
10+
.intra_doc_link { font-style: italic; }
11+
.injected { opacity: 0.65 ; }
12+
.struct, .enum { color: #7CB8BB; }
13+
.enum_variant { color: #BDE0F3; }
14+
.string_literal { color: #CC9393; }
15+
.field { color: #94BFF3; }
16+
.function { color: #93E0E3; }
17+
.parameter { color: #94BFF3; }
18+
.text { color: #DCDCCC; }
19+
.type { color: #7CB8BB; }
20+
.builtin_type { color: #8CD0D3; }
21+
.type_param { color: #DFAF8F; }
22+
.attribute { color: #94BFF3; }
23+
.numeric_literal { color: #BFEBBF; }
24+
.bool_literal { color: #BFE6EB; }
25+
.macro { color: #94BFF3; }
26+
.proc_macro { color: #94BFF3; text-decoration: underline; }
27+
.derive { color: #94BFF3; font-style: italic; }
28+
.module { color: #AFD8AF; }
29+
.value_param { color: #DCDCCC; }
30+
.variable { color: #DCDCCC; }
31+
.format_specifier { color: #CC696B; }
32+
.mutable { text-decoration: underline; }
33+
.escape_sequence { color: #94BFF3; }
34+
.keyword { color: #F0DFAF; font-weight: bold; }
35+
.control { font-style: italic; }
36+
.reference { font-style: italic; font-weight: bold; }
37+
.const { font-weight: bolder; }
38+
.unsafe { color: #BC8383; }
39+
40+
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
41+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
42+
</style>
43+
<pre><code>// This is a regular comment
44+
/// This is a doc comment
45+
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
46+
// Another comment
47+
<span class="unresolved_reference">println</span><span class="macro_bang">!</span><span class="parenthesis">(</span><span class="string_literal">"Hello, world!"</span><span class="parenthesis">)</span><span class="semicolon">;</span>
48+
<span class="brace">}</span></code></pre>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{FileRange, HighlightConfig, HlTag, TextRange, fixture};
99

1010
const HL_CONFIG: HighlightConfig = HighlightConfig {
1111
strings: true,
12+
comments: true,
1213
punctuation: true,
1314
specialize_punctuation: true,
1415
specialize_operator: true,
@@ -1220,16 +1221,25 @@ fn foo(x: &fn(&dyn Trait)) {}
12201221
/// Highlights the code given by the `ra_fixture` argument, renders the
12211222
/// result as HTML, and compares it with the HTML file given as `snapshot`.
12221223
/// Note that the `snapshot` file is overwritten by the rendered HTML.
1223-
fn check_highlighting(
1224+
fn check_highlighting_with_config(
12241225
#[rust_analyzer::rust_fixture] ra_fixture: &str,
1226+
config: HighlightConfig,
12251227
expect: ExpectFile,
12261228
rainbow: bool,
12271229
) {
12281230
let (analysis, file_id) = fixture::file(ra_fixture.trim());
1229-
let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap();
1231+
let actual_html = &analysis.highlight_as_html_with_config(config, file_id, rainbow).unwrap();
12301232
expect.assert_eq(actual_html)
12311233
}
12321234

1235+
fn check_highlighting(
1236+
#[rust_analyzer::rust_fixture] ra_fixture: &str,
1237+
expect: ExpectFile,
1238+
rainbow: bool,
1239+
) {
1240+
check_highlighting_with_config(ra_fixture, HL_CONFIG, expect, rainbow)
1241+
}
1242+
12331243
#[test]
12341244
fn benchmark_syntax_highlighting_long_struct() {
12351245
if skip_slow_tests() {
@@ -1435,3 +1445,24 @@ fn main() {
14351445
false,
14361446
);
14371447
}
1448+
1449+
#[test]
1450+
fn test_comment_highlighting_disabled() {
1451+
// Test that comments are not highlighted when disabled
1452+
check_highlighting_with_config(
1453+
r#"
1454+
// This is a regular comment
1455+
/// This is a doc comment
1456+
fn main() {
1457+
// Another comment
1458+
println!("Hello, world!");
1459+
}
1460+
"#,
1461+
HighlightConfig {
1462+
comments: false, // Disable comment highlighting
1463+
..HL_CONFIG
1464+
},
1465+
expect_file!["./test_data/highlight_comments_disabled.html"],
1466+
false,
1467+
);
1468+
}

crates/rust-analyzer/src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,13 @@ config_data! {
382382
/// Exclude tests from find-all-references and call-hierarchy.
383383
references_excludeTests: bool = false,
384384

385+
/// Use semantic tokens for comments.
386+
///
387+
/// In some editors (e.g. vscode) semantic tokens override other highlighting grammars.
388+
/// By disabling semantic tokens for comments, other grammars can be used to highlight
389+
/// their contents.
390+
semanticHighlighting_comments_enable: bool = true,
391+
385392
/// Inject additional highlighting into doc comments.
386393
///
387394
/// When enabled, rust-analyzer will highlight rust source in doc comments as well as intra
@@ -1968,6 +1975,7 @@ impl Config {
19681975
pub fn highlighting_config(&self) -> HighlightConfig {
19691976
HighlightConfig {
19701977
strings: self.semanticHighlighting_strings_enable().to_owned(),
1978+
comments: self.semanticHighlighting_comments_enable().to_owned(),
19711979
punctuation: self.semanticHighlighting_punctuation_enable().to_owned(),
19721980
specialize_punctuation: self
19731981
.semanticHighlighting_punctuation_specialization_enable()

docs/book/src/configuration_generated.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,17 @@ Enables the use of rustfmt's unstable range formatting command for the
13781378
available on a nightly build.
13791379

13801380

1381+
## rust-analyzer.semanticHighlighting.comments.enable {#semanticHighlighting.comments.enable}
1382+
1383+
Default: `true`
1384+
1385+
Use semantic tokens for comments.
1386+
1387+
In some editors (e.g. vscode) semantic tokens override other highlighting grammars.
1388+
By disabling semantic tokens for comments, other grammars can be used to highlight
1389+
their contents.
1390+
1391+
13811392
## rust-analyzer.semanticHighlighting.doc.comment.inject.enable {#semanticHighlighting.doc.comment.inject.enable}
13821393

13831394
Default: `true`

editors/code/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,6 +2850,16 @@
28502850
}
28512851
}
28522852
},
2853+
{
2854+
"title": "Semantic Highlighting",
2855+
"properties": {
2856+
"rust-analyzer.semanticHighlighting.comments.enable": {
2857+
"markdownDescription": "Use semantic tokens for comments.\n\nIn some editors (e.g. vscode) semantic tokens override other highlighting grammars.\nBy disabling semantic tokens for comments, other grammars can be used to highlight\ntheir contents.",
2858+
"default": true,
2859+
"type": "boolean"
2860+
}
2861+
}
2862+
},
28532863
{
28542864
"title": "Semantic Highlighting",
28552865
"properties": {

0 commit comments

Comments
 (0)