]>
Commit | Line | Data |
---|---|---|
064997fb FG |
1 | //! Syntax highlighting for format macro strings. |
2 | use ide_db::{ | |
3 | syntax_helpers::format_string::{is_format_string, lex_format_specifiers, FormatSpecifier}, | |
4 | SymbolKind, | |
5 | }; | |
6 | use syntax::{ast, TextRange}; | |
7 | ||
8 | use crate::{syntax_highlighting::highlights::Highlights, HlRange, HlTag}; | |
9 | ||
10 | pub(super) fn highlight_format_string( | |
11 | stack: &mut Highlights, | |
12 | string: &ast::String, | |
13 | expanded_string: &ast::String, | |
14 | range: TextRange, | |
15 | ) { | |
16 | if !is_format_string(expanded_string) { | |
17 | return; | |
18 | } | |
19 | ||
781aab86 | 20 | // FIXME: Replace this with the HIR info we have now. |
064997fb FG |
21 | lex_format_specifiers(string, &mut |piece_range, kind| { |
22 | if let Some(highlight) = highlight_format_specifier(kind) { | |
23 | stack.add(HlRange { | |
24 | range: piece_range + range.start(), | |
25 | highlight: highlight.into(), | |
26 | binding_hash: None, | |
27 | }); | |
28 | } | |
29 | }); | |
30 | } | |
31 | ||
32 | fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HlTag> { | |
33 | Some(match kind { | |
34 | FormatSpecifier::Open | |
35 | | FormatSpecifier::Close | |
36 | | FormatSpecifier::Colon | |
37 | | FormatSpecifier::Fill | |
38 | | FormatSpecifier::Align | |
39 | | FormatSpecifier::Sign | |
40 | | FormatSpecifier::NumberSign | |
41 | | FormatSpecifier::DollarSign | |
42 | | FormatSpecifier::Dot | |
43 | | FormatSpecifier::Asterisk | |
44 | | FormatSpecifier::QuestionMark => HlTag::FormatSpecifier, | |
45 | ||
46 | FormatSpecifier::Integer | FormatSpecifier::Zero => HlTag::NumericLiteral, | |
47 | ||
48 | FormatSpecifier::Identifier => HlTag::Symbol(SymbolKind::Local), | |
49 | FormatSpecifier::Escape => HlTag::EscapeSequence, | |
50 | }) | |
51 | } |