Skip to content

Commit 4ff04e5

Browse files
Merge pull request #21420 from A4-Tacks/disable-string-hl
Fix not disable string escape highlights
2 parents ff9a2e8 + fa3d8af commit 4ff04e5

File tree

5 files changed

+113
-18
lines changed

5 files changed

+113
-18
lines changed

crates/ide/src/syntax_highlighting.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,21 +513,21 @@ fn string_injections(
513513
);
514514

515515
if !string.is_raw() {
516-
highlight_escape_string(hl, &string);
516+
highlight_escape_string(hl, config, &string);
517517
}
518518
}
519519
} else if let Some(byte_string) = ast::ByteString::cast(token.clone()) {
520520
if !byte_string.is_raw() {
521-
highlight_escape_string(hl, &byte_string);
521+
highlight_escape_string(hl, config, &byte_string);
522522
}
523523
} else if let Some(c_string) = ast::CString::cast(token.clone()) {
524524
if !c_string.is_raw() {
525-
highlight_escape_string(hl, &c_string);
525+
highlight_escape_string(hl, config, &c_string);
526526
}
527527
} else if let Some(char) = ast::Char::cast(token.clone()) {
528-
highlight_escape_char(hl, &char)
528+
highlight_escape_char(hl, config, &char)
529529
} else if let Some(byte) = ast::Byte::cast(token) {
530-
highlight_escape_byte(hl, &byte)
530+
highlight_escape_byte(hl, config, &byte)
531531
}
532532
ControlFlow::Continue(())
533533
}
@@ -586,7 +586,11 @@ fn descend_token(
586586

587587
fn filter_by_config(highlight: &mut Highlight, config: &HighlightConfig<'_>) -> bool {
588588
match &mut highlight.tag {
589-
HlTag::StringLiteral if !config.strings => return false,
589+
HlTag::StringLiteral | HlTag::EscapeSequence | HlTag::InvalidEscapeSequence
590+
if !config.strings =>
591+
{
592+
return false;
593+
}
590594
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) => {

crates/ide/src/syntax_highlighting/escape.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
//! Syntax highlighting for escape sequences
22
use crate::syntax_highlighting::highlights::Highlights;
3-
use crate::{HlRange, HlTag};
3+
use crate::{HighlightConfig, HlRange, HlTag};
44
use syntax::ast::{Byte, Char, IsString};
55
use syntax::{AstToken, TextRange, TextSize};
66

7-
pub(super) fn highlight_escape_string<T: IsString>(stack: &mut Highlights, string: &T) {
7+
pub(super) fn highlight_escape_string<T: IsString>(
8+
stack: &mut Highlights,
9+
config: &HighlightConfig<'_>,
10+
string: &T,
11+
) {
812
let text = string.text();
913
let start = string.syntax().text_range().start();
1014
string.escaped_char_ranges(&mut |piece_range, char| {
@@ -13,16 +17,23 @@ pub(super) fn highlight_escape_string<T: IsString>(stack: &mut Highlights, strin
1317
Ok(_) => HlTag::EscapeSequence,
1418
Err(_) => HlTag::InvalidEscapeSequence,
1519
};
16-
stack.add(HlRange {
17-
range: piece_range + start,
18-
highlight: highlight.into(),
19-
binding_hash: None,
20-
});
20+
stack.add_with(
21+
config,
22+
HlRange {
23+
range: piece_range + start,
24+
highlight: highlight.into(),
25+
binding_hash: None,
26+
},
27+
);
2128
}
2229
});
2330
}
2431

25-
pub(super) fn highlight_escape_char(stack: &mut Highlights, char: &Char) {
32+
pub(super) fn highlight_escape_char(
33+
stack: &mut Highlights,
34+
config: &HighlightConfig<'_>,
35+
char: &Char,
36+
) {
2637
if char.value().is_err() {
2738
// We do not emit invalid escapes highlighting here. The lexer would likely be in a bad
2839
// state and this token contains junk, since `'` is not a reliable delimiter (consider
@@ -43,10 +54,17 @@ pub(super) fn highlight_escape_char(stack: &mut Highlights, char: &Char) {
4354
char.syntax().text_range().start() + TextSize::from(1),
4455
TextSize::from(text.len() as u32),
4556
);
46-
stack.add(HlRange { range, highlight: HlTag::EscapeSequence.into(), binding_hash: None })
57+
stack.add_with(
58+
config,
59+
HlRange { range, highlight: HlTag::EscapeSequence.into(), binding_hash: None },
60+
)
4761
}
4862

49-
pub(super) fn highlight_escape_byte(stack: &mut Highlights, byte: &Byte) {
63+
pub(super) fn highlight_escape_byte(
64+
stack: &mut Highlights,
65+
config: &HighlightConfig<'_>,
66+
byte: &Byte,
67+
) {
5068
if byte.value().is_err() {
5169
// See `highlight_escape_char` for why no error highlighting here.
5270
return;
@@ -65,5 +83,8 @@ pub(super) fn highlight_escape_byte(stack: &mut Highlights, byte: &Byte) {
6583
byte.syntax().text_range().start() + TextSize::from(2),
6684
TextSize::from(text.len() as u32),
6785
);
68-
stack.add(HlRange { range, highlight: HlTag::EscapeSequence.into(), binding_hash: None })
86+
stack.add_with(
87+
config,
88+
HlRange { range, highlight: HlTag::EscapeSequence.into(), binding_hash: None },
89+
)
6990
}

crates/ide/src/syntax_highlighting/highlights.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::iter;
44
use stdx::equal_range_by;
55
use syntax::TextRange;
66

7-
use crate::{HlRange, HlTag};
7+
use crate::{HighlightConfig, HlRange, HlTag};
88

99
pub(super) struct Highlights {
1010
root: Node,
@@ -26,6 +26,12 @@ impl Highlights {
2626
self.root.add(hl_range);
2727
}
2828

29+
pub(super) fn add_with(&mut self, config: &HighlightConfig<'_>, mut hl_range: HlRange) {
30+
if super::filter_by_config(&mut hl_range.highlight, config) {
31+
self.root.add(hl_range);
32+
}
33+
}
34+
2935
pub(super) fn to_vec(&self) -> Vec<HlRange> {
3036
let mut res = Vec::new();
3137
self.root.flatten(&mut res);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
.deprecated { text-decoration: line-through; }
40+
41+
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
42+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
43+
</style>
44+
<pre><code><span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
45+
<span class="macro default_library library">format_args</span><span class="macro_bang">!</span><span class="parenthesis">(</span>"foo\nbar"<span class="parenthesis">)</span><span class="semicolon">;</span>
46+
<span class="macro default_library library">format_args</span><span class="macro_bang">!</span><span class="parenthesis">(</span>"foo\invalid"<span class="parenthesis">)</span><span class="semicolon">;</span>
47+
<span class="brace">}</span></code></pre>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,23 @@ fn main() {
14981498
);
14991499
}
15001500

1501+
#[test]
1502+
fn test_strings_highlighting_disabled() {
1503+
// Test that comments are not highlighted when disabled
1504+
check_highlighting_with_config(
1505+
r#"
1506+
//- minicore: fmt
1507+
fn main() {
1508+
format_args!("foo\nbar");
1509+
format_args!("foo\invalid");
1510+
}
1511+
"#,
1512+
HighlightConfig { strings: false, ..HL_CONFIG },
1513+
expect_file!["./test_data/highlight_strings_disabled.html"],
1514+
false,
1515+
);
1516+
}
1517+
15011518
#[test]
15021519
fn regression_20952() {
15031520
check_highlighting(

0 commit comments

Comments
 (0)