2020-10-13 22:56:41 +00:00
|
|
|
//! Syntax highlighting for format macro strings.
|
2022-01-15 12:14:59 +00:00
|
|
|
use ide_db::{
|
2022-03-06 18:01:30 +00:00
|
|
|
syntax_helpers::format_string::{is_format_string, lex_format_specifiers, FormatSpecifier},
|
2022-01-15 12:14:59 +00:00
|
|
|
SymbolKind,
|
2020-10-13 22:56:41 +00:00
|
|
|
};
|
2022-01-15 12:14:59 +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-07 22:39:02 +00:00
|
|
|
|
2021-01-10 08:57:17 +00:00
|
|
|
pub(super) fn highlight_format_string(
|
|
|
|
stack: &mut Highlights,
|
|
|
|
string: &ast::String,
|
2021-11-23 21:05:52 +00:00
|
|
|
expanded_string: &ast::String,
|
2021-01-10 08:57:17 +00:00
|
|
|
range: TextRange,
|
|
|
|
) {
|
2022-01-15 11:12:02 +00:00
|
|
|
if !is_format_string(expanded_string) {
|
2021-01-10 08:57:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-13 22:56:41 +00:00
|
|
|
|
2022-01-15 12:14:59 +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
|
|
|
})
|
|
|
|
}
|