rust-analyzer/crates/ide/src/syntax_highlighting/format.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.5 KiB
Rust
Raw Normal View History

2020-10-13 22:56:41 +00:00
//! Syntax highlighting for format macro strings.
use ide_db::{
2022-03-06 18:01:30 +00:00
syntax_helpers::format_string::{is_format_string, lex_format_specifiers, FormatSpecifier},
SymbolKind,
2020-10-13 22:56:41 +00:00
};
use syntax::{ast, TextRange};
2020-10-13 22:56:41 +00:00
2021-01-20 14:25:34 +00:00
use crate::{syntax_highlighting::highlights::Highlights, HlRange, HlTag};
2021-01-10 08:57:17 +00:00
pub(super) fn highlight_format_string(
stack: &mut Highlights,
string: &ast::String,
expanded_string: &ast::String,
2021-01-10 08:57:17 +00:00
range: TextRange,
) {
if !is_format_string(expanded_string) {
2021-01-10 08:57:17 +00:00
return;
}
2020-10-13 22:56:41 +00:00
lex_format_specifiers(string, &mut |piece_range, kind| {
2021-01-10 08:57:17 +00:00
if let Some(highlight) = highlight_format_specifier(kind) {
stack.add(HlRange {
range: piece_range + range.start(),
highlight: highlight.into(),
binding_hash: None,
});
}
});
2020-10-13 22:56:41 +00:00
}
2021-01-09 11:44:01 +00:00
fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HlTag> {
2020-10-13 22:56:41 +00:00
Some(match kind {
FormatSpecifier::Open
| FormatSpecifier::Close
| FormatSpecifier::Colon
| FormatSpecifier::Fill
| FormatSpecifier::Align
| FormatSpecifier::Sign
| FormatSpecifier::NumberSign
| FormatSpecifier::DollarSign
| FormatSpecifier::Dot
| FormatSpecifier::Asterisk
2021-01-09 11:44:01 +00:00
| FormatSpecifier::QuestionMark => HlTag::FormatSpecifier,
2021-01-10 08:57:17 +00:00
2021-01-09 11:44:01 +00:00
FormatSpecifier::Integer | FormatSpecifier::Zero => HlTag::NumericLiteral,
2021-01-10 08:57:17 +00:00
2021-01-09 11:44:01 +00:00
FormatSpecifier::Identifier => HlTag::Symbol(SymbolKind::Local),
2020-10-13 22:56:41 +00:00
})
}