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::{
|
2023-12-05 15:30:57 +00:00
|
|
|
defs::Definition,
|
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
|
|
|
|
2023-12-05 15:30:57 +00:00
|
|
|
use crate::{
|
|
|
|
syntax_highlighting::{highlight::highlight_def, 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,
|
2023-12-05 15:30:57 +00:00
|
|
|
sema: &hir::Semantics<'_, ide_db::RootDatabase>,
|
|
|
|
krate: hir::Crate,
|
2021-01-10 08:57:17 +00:00
|
|
|
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
|
|
|
|
2023-09-06 16:00:30 +00:00
|
|
|
// FIXME: Replace this with the HIR info we have now.
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2023-12-05 15:30:57 +00:00
|
|
|
|
|
|
|
if let Some(parts) = sema.as_format_args_parts(string) {
|
|
|
|
parts.into_iter().for_each(|(range, res)| {
|
|
|
|
if let Some(res) = res {
|
|
|
|
stack.add(HlRange {
|
|
|
|
range,
|
|
|
|
highlight: highlight_def(sema, krate, Definition::from(res)),
|
|
|
|
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),
|
2022-05-22 06:40:47 +00:00
|
|
|
FormatSpecifier::Escape => HlTag::EscapeSequence,
|
2020-10-13 22:56:41 +00:00
|
|
|
})
|
|
|
|
}
|