1 //! Utilities for creating `Analysis` instances for tests.
2 use ide_db::base_db::fixture::ChangeFixture;
3 use syntax::{TextRange, TextSize};
4 use test_utils::{extract_annotations, RangeOrOffset};
6 use crate::{Analysis, AnalysisHost, FileId, FilePosition, FileRange};
8 /// Creates analysis for a single file.
9 pub(crate) fn file(ra_fixture: &str) -> (Analysis, FileId) {
10 let mut host = AnalysisHost::default();
11 let change_fixture = ChangeFixture::parse(ra_fixture);
12 host.db.apply_change(change_fixture.change);
13 (host.analysis(), change_fixture.files[0])
16 /// Creates analysis for many files.
17 pub(crate) fn files(ra_fixture: &str) -> (Analysis, Vec<FileId>) {
18 let mut host = AnalysisHost::default();
19 let change_fixture = ChangeFixture::parse(ra_fixture);
20 host.db.apply_change(change_fixture.change);
21 (host.analysis(), change_fixture.files)
24 /// Creates analysis from a multi-file fixture, returns positions marked with $0.
25 pub(crate) fn position(ra_fixture: &str) -> (Analysis, FilePosition) {
26 let mut host = AnalysisHost::default();
27 let change_fixture = ChangeFixture::parse(ra_fixture);
28 host.db.apply_change(change_fixture.change);
29 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
30 let offset = match range_or_offset {
31 RangeOrOffset::Range(_) => panic!(),
32 RangeOrOffset::Offset(it) => it,
34 (host.analysis(), FilePosition { file_id, offset })
37 /// Creates analysis for a single file, returns range marked with a pair of $0.
38 pub(crate) fn range(ra_fixture: &str) -> (Analysis, FileRange) {
39 let mut host = AnalysisHost::default();
40 let change_fixture = ChangeFixture::parse(ra_fixture);
41 host.db.apply_change(change_fixture.change);
42 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
43 let range = match range_or_offset {
44 RangeOrOffset::Range(it) => it,
45 RangeOrOffset::Offset(_) => panic!(),
47 (host.analysis(), FileRange { file_id, range })
50 /// Creates analysis from a multi-file fixture, returns positions marked with $0.
51 pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(FileRange, String)>) {
52 let mut host = AnalysisHost::default();
53 let change_fixture = ChangeFixture::parse(ra_fixture);
54 host.db.apply_change(change_fixture.change);
55 let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
56 let offset = match range_or_offset {
57 RangeOrOffset::Range(_) => panic!(),
58 RangeOrOffset::Offset(it) => it,
61 let annotations = change_fixture
64 .flat_map(|&file_id| {
65 let file_text = host.analysis().file_text(file_id).unwrap();
66 let annotations = extract_annotations(&file_text);
67 annotations.into_iter().map(move |(range, data)| (FileRange { file_id, range }, data))
70 (host.analysis(), FilePosition { file_id, offset }, annotations)
73 pub(crate) fn nav_target_annotation(ra_fixture: &str) -> (Analysis, FilePosition, FileRange) {
74 let (analysis, position, mut annotations) = annotations(ra_fixture);
75 let (mut expected, data) = annotations.pop().unwrap();
76 assert!(annotations.is_empty());
81 TextRange::up_to(TextSize::of(&*analysis.file_text(expected.file_id).unwrap()))
83 data => panic!("bad data: {}", data),
85 (analysis, position, expected)