rust-analyzer/crates/ra_ide_api/src/syntax_highlighting.rs

45 lines
1.3 KiB
Rust
Raw Normal View History

2019-01-08 19:33:36 +00:00
use ra_syntax::{ast, AstNode,};
2019-01-26 08:20:30 +00:00
use ra_db::SourceDatabase;
2019-01-08 19:33:36 +00:00
use crate::{
2019-01-15 18:17:10 +00:00
FileId, HighlightedRange,
2019-01-08 19:33:36 +00:00
db::RootDatabase,
};
2019-01-15 18:17:10 +00:00
pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> {
2019-01-26 08:51:36 +00:00
let source_file = db.parse(file_id);
2019-01-08 19:33:36 +00:00
let mut res = ra_ide_api_light::highlight(source_file.syntax());
2019-02-08 11:49:43 +00:00
for macro_call in source_file.syntax().descendants().filter_map(ast::MacroCall::cast) {
2019-01-08 19:33:36 +00:00
if let Some((off, exp)) = hir::MacroDef::ast_expand(macro_call) {
2019-02-08 11:49:43 +00:00
let mapped_ranges =
ra_ide_api_light::highlight(&exp.syntax()).into_iter().filter_map(|r| {
2019-01-08 19:33:36 +00:00
let mapped_range = exp.map_range_back(r.range)?;
2019-02-08 11:49:43 +00:00
let res = HighlightedRange { range: mapped_range + off, tag: r.tag };
2019-01-08 19:33:36 +00:00
Some(res)
});
res.extend(mapped_ranges);
}
}
2019-01-15 18:17:10 +00:00
res
2019-01-08 19:33:36 +00:00
}
#[cfg(test)]
mod tests {
use crate::mock_analysis::single_file;
2019-01-14 13:27:08 +00:00
use insta::assert_debug_snapshot_matches;
2019-01-08 19:33:36 +00:00
#[test]
fn highlights_code_inside_macros() {
let (analysis, file_id) = single_file(
"
fn main() {
vec![{ let x = 92; x}];
}
",
);
let highlights = analysis.highlight(file_id).unwrap();
2019-01-14 13:27:08 +00:00
assert_debug_snapshot_matches!("highlights_code_inside_macros", &highlights);
2019-01-08 19:33:36 +00:00
}
}