rust-analyzer/crates/ide/src/fixture.rs

87 lines
3.5 KiB
Rust
Raw Normal View History

2020-10-02 15:34:31 +00:00
//! Utilities for creating `Analysis` instances for tests.
2020-10-24 08:39:57 +00:00
use ide_db::base_db::fixture::ChangeFixture;
2021-02-09 15:13:11 +00:00
use syntax::{TextRange, TextSize};
2020-10-02 14:13:48 +00:00
use test_utils::{extract_annotations, RangeOrOffset};
2019-01-08 19:33:36 +00:00
2020-10-02 14:13:48 +00:00
use crate::{Analysis, AnalysisHost, FileId, FilePosition, FileRange};
2019-01-08 19:33:36 +00:00
2020-10-02 15:34:31 +00:00
/// Creates analysis for a single file.
pub(crate) fn file(ra_fixture: &str) -> (Analysis, FileId) {
2020-10-02 14:13:48 +00:00
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.apply_change(change_fixture.change);
2020-10-02 15:34:31 +00:00
(host.analysis(), change_fixture.files[0])
2019-01-08 19:33:36 +00:00
}
2020-10-02 15:34:31 +00:00
/// Creates analysis for many files.
pub(crate) fn files(ra_fixture: &str) -> (Analysis, Vec<FileId>) {
2020-10-02 14:13:48 +00:00
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.apply_change(change_fixture.change);
2020-10-02 15:34:31 +00:00
(host.analysis(), change_fixture.files)
2020-10-02 14:13:48 +00:00
}
2021-01-06 20:15:48 +00:00
/// Creates analysis from a multi-file fixture, returns positions marked with $0.
2020-10-02 15:34:31 +00:00
pub(crate) fn position(ra_fixture: &str) -> (Analysis, FilePosition) {
2020-10-02 14:13:48 +00:00
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.apply_change(change_fixture.change);
2021-01-06 20:15:48 +00:00
let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
2020-10-02 15:34:31 +00:00
let offset = match range_or_offset {
RangeOrOffset::Range(_) => panic!(),
RangeOrOffset::Offset(it) => it,
};
(host.analysis(), FilePosition { file_id, offset })
2019-01-08 19:33:36 +00:00
}
2021-01-06 20:15:48 +00:00
/// Creates analysis for a single file, returns range marked with a pair of $0.
2020-10-02 15:34:31 +00:00
pub(crate) fn range(ra_fixture: &str) -> (Analysis, FileRange) {
2020-10-02 14:13:48 +00:00
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.apply_change(change_fixture.change);
2021-01-06 20:15:48 +00:00
let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
2020-06-24 10:01:17 +00:00
let range = match range_or_offset {
RangeOrOffset::Range(it) => it,
RangeOrOffset::Offset(_) => panic!(),
};
2020-10-02 14:13:48 +00:00
(host.analysis(), FileRange { file_id, range })
}
2021-01-06 20:15:48 +00:00
/// Creates analysis from a multi-file fixture, returns positions marked with $0.
2020-10-02 15:34:31 +00:00
pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(FileRange, String)>) {
2020-10-02 14:13:48 +00:00
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.apply_change(change_fixture.change);
2021-01-06 20:15:48 +00:00
let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
2020-10-02 14:13:48 +00:00
let offset = match range_or_offset {
RangeOrOffset::Range(_) => panic!(),
RangeOrOffset::Offset(it) => it,
};
let annotations = change_fixture
.files
.iter()
.flat_map(|&file_id| {
let file_text = host.analysis().file_text(file_id).unwrap();
let annotations = extract_annotations(&file_text);
annotations.into_iter().map(move |(range, data)| (FileRange { file_id, range }, data))
})
.collect();
(host.analysis(), FilePosition { file_id, offset }, annotations)
2019-01-08 19:33:36 +00:00
}
2021-02-09 15:13:11 +00:00
pub(crate) fn nav_target_annotation(ra_fixture: &str) -> (Analysis, FilePosition, FileRange) {
let (analysis, position, mut annotations) = annotations(ra_fixture);
let (mut expected, data) = annotations.pop().unwrap();
assert!(annotations.is_empty());
match data.as_str() {
"" => (),
"file" => {
expected.range =
TextRange::up_to(TextSize::of(&*analysis.file_text(expected.file_id).unwrap()))
}
data => panic!("bad data: {}", data),
}
(analysis, position, expected)
}