rust/crates/ide/src/fixture.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
3.8 KiB
Rust
Raw Normal View History

2020-10-02 10:34:31 -05:00
//! Utilities for creating `Analysis` instances for tests.
use test_fixture::ChangeFixture;
2021-08-11 06:39:36 -05:00
use test_utils::{extract_annotations, RangeOrOffset};
2019-01-08 13:33:36 -06:00
2020-10-02 09:13:48 -05:00
use crate::{Analysis, AnalysisHost, FileId, FilePosition, FileRange};
2019-01-08 13:33:36 -06:00
2020-10-02 10:34:31 -05:00
/// Creates analysis for a single file.
pub(crate) fn file(ra_fixture: &str) -> (Analysis, FileId) {
2020-10-02 09:13:48 -05:00
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.enable_proc_attr_macros();
2020-10-02 09:13:48 -05:00
host.db.apply_change(change_fixture.change);
2020-10-02 10:34:31 -05:00
(host.analysis(), change_fixture.files[0])
2019-01-08 13:33:36 -06:00
}
2021-01-06 14:15:48 -06:00
/// Creates analysis from a multi-file fixture, returns positions marked with $0.
2020-10-02 10:34:31 -05:00
pub(crate) fn position(ra_fixture: &str) -> (Analysis, FilePosition) {
2020-10-02 09:13:48 -05:00
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.enable_proc_attr_macros();
2020-10-02 09:13:48 -05:00
host.db.apply_change(change_fixture.change);
2021-01-06 14:15:48 -06:00
let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
2021-05-28 13:46:09 -05:00
let offset = range_or_offset.expect_offset();
2020-10-02 10:34:31 -05:00
(host.analysis(), FilePosition { file_id, offset })
2019-01-08 13:33:36 -06:00
}
2021-01-06 14:15:48 -06:00
/// Creates analysis for a single file, returns range marked with a pair of $0.
2020-10-02 10:34:31 -05:00
pub(crate) fn range(ra_fixture: &str) -> (Analysis, FileRange) {
2020-10-02 09:13:48 -05:00
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.enable_proc_attr_macros();
2020-10-02 09:13:48 -05:00
host.db.apply_change(change_fixture.change);
2021-01-06 14:15:48 -06:00
let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
2021-05-28 13:46:09 -05:00
let range = range_or_offset.expect_range();
2020-10-02 09:13:48 -05:00
(host.analysis(), FileRange { file_id, range })
}
2021-08-11 06:39:36 -05:00
/// Creates analysis for a single file, returns range marked with a pair of $0 or a position marked with $0.
pub(crate) fn range_or_position(ra_fixture: &str) -> (Analysis, FileId, RangeOrOffset) {
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.enable_proc_attr_macros();
2021-08-11 06:39:36 -05:00
host.db.apply_change(change_fixture.change);
let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
(host.analysis(), file_id, range_or_offset)
}
2021-01-06 14:15:48 -06:00
/// Creates analysis from a multi-file fixture, returns positions marked with $0.
2020-10-02 10:34:31 -05:00
pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(FileRange, String)>) {
2020-10-02 09:13:48 -05:00
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.enable_proc_attr_macros();
2020-10-02 09:13:48 -05:00
host.db.apply_change(change_fixture.change);
2021-01-06 14:15:48 -06:00
let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
2021-05-28 13:46:09 -05:00
let offset = range_or_offset.expect_offset();
2020-10-02 09:13:48 -05:00
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 13:33:36 -06:00
}
2021-09-26 04:17:57 -05:00
/// Creates analysis from a multi-file fixture with annotations without $0
2021-09-26 04:17:57 -05:00
pub(crate) fn annotations_without_marker(ra_fixture: &str) -> (Analysis, Vec<(FileRange, String)>) {
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.enable_proc_attr_macros();
2021-09-26 04:17:57 -05:00
host.db.apply_change(change_fixture.change);
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(), annotations)
}