]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/syntax_highlighting/format.rs
Merge #10423
[rust.git] / crates / ide / src / syntax_highlighting / format.rs
1 //! Syntax highlighting for format macro strings.
2 use ide_db::SymbolKind;
3 use syntax::{
4     ast::{self, FormatSpecifier, HasFormatSpecifier},
5     AstNode, AstToken, TextRange,
6 };
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     range: TextRange,
14 ) {
15     if is_format_string(string).is_none() {
16         return;
17     }
18
19     string.lex_format_specifier(|piece_range, kind| {
20         if let Some(highlight) = highlight_format_specifier(kind) {
21             stack.add(HlRange {
22                 range: piece_range + range.start(),
23                 highlight: highlight.into(),
24                 binding_hash: None,
25             });
26         }
27     });
28 }
29
30 fn is_format_string(string: &ast::String) -> Option<()> {
31     // Check if `string` is a format string argument of a macro invocation.
32     // `string` is a string literal, mapped down into the innermost macro expansion.
33     // Since `format_args!` etc. remove the format string when expanding, but place all arguments
34     // in the expanded output, we know that the string token is (part of) the format string if it
35     // appears in `format_args!` (otherwise it would have been mapped down further).
36     //
37     // This setup lets us correctly highlight the components of `concat!("{}", "bla")` format
38     // strings. It still fails for `concat!("{", "}")`, but that is rare.
39
40     let macro_call = string.syntax().ancestors().find_map(ast::MacroCall::cast)?;
41     let name = macro_call.path()?.segment()?.name_ref()?;
42
43     if !matches!(
44         name.text().as_str(),
45         "format_args" | "format_args_nl" | "const_format_args" | "panic_2015" | "panic_2021"
46     ) {
47         return None;
48     }
49
50     // NB: we match against `panic_2015`/`panic_2021` here because they have a special-cased arm for
51     // `"{}"`, which otherwise wouldn't get highlighted.
52
53     Some(())
54 }
55
56 fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HlTag> {
57     Some(match kind {
58         FormatSpecifier::Open
59         | FormatSpecifier::Close
60         | FormatSpecifier::Colon
61         | FormatSpecifier::Fill
62         | FormatSpecifier::Align
63         | FormatSpecifier::Sign
64         | FormatSpecifier::NumberSign
65         | FormatSpecifier::DollarSign
66         | FormatSpecifier::Dot
67         | FormatSpecifier::Asterisk
68         | FormatSpecifier::QuestionMark => HlTag::FormatSpecifier,
69
70         FormatSpecifier::Integer | FormatSpecifier::Zero => HlTag::NumericLiteral,
71
72         FormatSpecifier::Identifier => HlTag::Symbol(SymbolKind::Local),
73     })
74 }