ide/syntax_highlighting/
format.rs

1//! Syntax highlighting for format macro strings.
2use ide_db::{
3    SymbolKind,
4    defs::Definition,
5    syntax_helpers::format_string::{FormatSpecifier, is_format_string, lex_format_specifiers},
6};
7use span::Edition;
8use syntax::{AstToken, ast};
9
10use crate::{
11    HlRange, HlTag,
12    syntax_highlighting::{highlight::highlight_def, highlights::Highlights},
13};
14
15pub(super) fn highlight_format_string(
16    stack: &mut Highlights,
17    sema: &hir::Semantics<'_, ide_db::RootDatabase>,
18    krate: Option<hir::Crate>,
19    string: &ast::String,
20    expanded_string: &ast::String,
21    edition: Edition,
22) {
23    if is_format_string(expanded_string) {
24        let start = string.syntax().text_range().start();
25        // FIXME: Replace this with the HIR info we have now.
26        lex_format_specifiers(string, &mut |piece_range, kind| {
27            if let Some(highlight) = highlight_format_specifier(kind) {
28                stack.add(HlRange {
29                    range: piece_range + start,
30                    highlight: highlight.into(),
31                    binding_hash: None,
32                });
33            }
34        });
35
36        return;
37    }
38
39    if let Some(parts) = sema.as_format_args_parts(string) {
40        parts.into_iter().for_each(|(range, res)| {
41            if let Some(res) = res {
42                stack.add(HlRange {
43                    range,
44                    highlight: highlight_def(sema, krate, Definition::from(res), edition, true),
45                    binding_hash: None,
46                })
47            }
48        })
49    }
50}
51
52fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HlTag> {
53    Some(match kind {
54        FormatSpecifier::Open
55        | FormatSpecifier::Close
56        | FormatSpecifier::Colon
57        | FormatSpecifier::Fill
58        | FormatSpecifier::Align
59        | FormatSpecifier::Sign
60        | FormatSpecifier::NumberSign
61        | FormatSpecifier::DollarSign
62        | FormatSpecifier::Dot
63        | FormatSpecifier::Asterisk
64        | FormatSpecifier::QuestionMark => HlTag::FormatSpecifier,
65
66        FormatSpecifier::Integer | FormatSpecifier::Zero => HlTag::NumericLiteral,
67
68        FormatSpecifier::Identifier => HlTag::Symbol(SymbolKind::Local),
69        FormatSpecifier::Escape => HlTag::EscapeSequence,
70    })
71}