1 //! Syntax highlighting for format macro strings.
2 use ide_db::SymbolKind;
4 ast::{self, FormatSpecifier, HasFormatSpecifier},
5 AstNode, AstToken, TextRange,
8 use crate::{syntax_highlighting::highlights::Highlights, HlRange, HlTag};
10 pub(super) fn highlight_format_string(
11 stack: &mut Highlights,
15 if is_format_string(string).is_none() {
19 string.lex_format_specifier(|piece_range, kind| {
20 if let Some(highlight) = highlight_format_specifier(kind) {
22 range: piece_range + range.start(),
23 highlight: highlight.into(),
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).
37 // This setup lets us correctly highlight the components of `concat!("{}", "bla")` format
38 // strings. It still fails for `concat!("{", "}")`, but that is rare.
40 let macro_call = string.syntax().ancestors().find_map(ast::MacroCall::cast)?;
41 let name = macro_call.path()?.segment()?.name_ref()?;
45 "format_args" | "format_args_nl" | "const_format_args" | "panic_2015" | "panic_2021"
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.
56 fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HlTag> {
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,
70 FormatSpecifier::Integer | FormatSpecifier::Zero => HlTag::NumericLiteral,
72 FormatSpecifier::Identifier => HlTag::Symbol(SymbolKind::Local),