rust/crates/ra_ide_api/src/syntax_highlighting.rs

69 lines
2.0 KiB
Rust
Raw Normal View History

2019-01-08 13:33:36 -06:00
use ra_syntax::{ast, AstNode,};
use ra_db::FilesDatabase;
2019-01-08 13:33:36 -06:00
use crate::{
2019-01-15 12:17:10 -06:00
FileId, HighlightedRange,
2019-01-08 13:33:36 -06:00
db::RootDatabase,
};
2019-01-15 12:17:10 -06:00
pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> {
2019-01-08 13:33:36 -06:00
let source_file = db.source_file(file_id);
let mut res = ra_ide_api_light::highlight(source_file.syntax());
for macro_call in source_file
.syntax()
.descendants()
.filter_map(ast::MacroCall::cast)
{
if let Some((off, exp)) = hir::MacroDef::ast_expand(macro_call) {
let mapped_ranges = ra_ide_api_light::highlight(&exp.syntax())
.into_iter()
.filter_map(|r| {
let mapped_range = exp.map_range_back(r.range)?;
let res = HighlightedRange {
range: mapped_range + off,
tag: r.tag,
};
Some(res)
});
res.extend(mapped_ranges);
}
}
2019-01-15 12:17:10 -06:00
res
2019-01-08 13:33:36 -06:00
}
#[cfg(test)]
mod tests {
use crate::mock_analysis::single_file;
2019-01-14 07:27:08 -06:00
use insta::assert_debug_snapshot_matches;
2019-01-08 13:33:36 -06:00
#[test]
fn highlights_code_inside_macros() {
let (analysis, file_id) = single_file(
"
fn main() {
ctry!({ let x = 92; x});
vec![{ let x = 92; x}];
}
",
);
let highlights = analysis.highlight(file_id).unwrap();
2019-01-14 07:27:08 -06:00
assert_debug_snapshot_matches!("highlights_code_inside_macros", &highlights);
2019-01-08 13:33:36 -06:00
}
// FIXME: this test is not really necessary: artifact of the inital hacky
// macros implementation.
#[test]
fn highlight_query_group_macro() {
let (analysis, file_id) = single_file(
"
salsa::query_group! {
pub trait HirDatabase: SyntaxDatabase {}
}
",
);
let highlights = analysis.highlight(file_id).unwrap();
2019-01-14 07:27:08 -06:00
assert_debug_snapshot_matches!("highlight_query_group_macro", &highlights);
2019-01-08 13:33:36 -06:00
}
}