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 let parent = string.syntax().parent()?;
33 let name = parent.parent().and_then(ast::MacroCall::cast)?.path()?.segment()?.name_ref()?;
36 "format_args" | "format_args_nl" | "const_format_args" | "panic_2015" | "panic_2021"
41 let first_literal = parent
42 .children_with_tokens()
43 .find_map(|it| it.as_token().cloned().and_then(ast::String::cast))?;
44 if &first_literal != string {
51 fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HlTag> {
54 | FormatSpecifier::Close
55 | FormatSpecifier::Colon
56 | FormatSpecifier::Fill
57 | FormatSpecifier::Align
58 | FormatSpecifier::Sign
59 | FormatSpecifier::NumberSign
60 | FormatSpecifier::DollarSign
61 | FormatSpecifier::Dot
62 | FormatSpecifier::Asterisk
63 | FormatSpecifier::QuestionMark => HlTag::FormatSpecifier,
65 FormatSpecifier::Integer | FormatSpecifier::Zero => HlTag::NumericLiteral,
67 FormatSpecifier::Identifier => HlTag::Symbol(SymbolKind::Local),