]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/ide/src/fixture.rs
Add 'src/tools/rust-analyzer/' from commit '977e12a0bdc3e329af179ef3a9d466af9eb613bb'
[rust.git] / src / tools / rust-analyzer / crates / ide / src / fixture.rs
1 //! Utilities for creating `Analysis` instances for tests.
2 use hir::db::DefDatabase;
3 use ide_db::base_db::fixture::ChangeFixture;
4 use test_utils::{extract_annotations, RangeOrOffset};
5
6 use crate::{Analysis, AnalysisHost, FileId, FilePosition, FileRange};
7
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.set_enable_proc_attr_macros(true);
13     host.db.apply_change(change_fixture.change);
14     (host.analysis(), change_fixture.files[0])
15 }
16
17 /// Creates analysis from a multi-file fixture, returns positions marked with $0.
18 pub(crate) fn position(ra_fixture: &str) -> (Analysis, FilePosition) {
19     let mut host = AnalysisHost::default();
20     let change_fixture = ChangeFixture::parse(ra_fixture);
21     host.db.set_enable_proc_attr_macros(true);
22     host.db.apply_change(change_fixture.change);
23     let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
24     let offset = range_or_offset.expect_offset();
25     (host.analysis(), FilePosition { file_id, offset })
26 }
27
28 /// Creates analysis for a single file, returns range marked with a pair of $0.
29 pub(crate) fn range(ra_fixture: &str) -> (Analysis, FileRange) {
30     let mut host = AnalysisHost::default();
31     let change_fixture = ChangeFixture::parse(ra_fixture);
32     host.db.set_enable_proc_attr_macros(true);
33     host.db.apply_change(change_fixture.change);
34     let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
35     let range = range_or_offset.expect_range();
36     (host.analysis(), FileRange { file_id, range })
37 }
38
39 /// Creates analysis for a single file, returns range marked with a pair of $0 or a position marked with $0.
40 pub(crate) fn range_or_position(ra_fixture: &str) -> (Analysis, FileId, RangeOrOffset) {
41     let mut host = AnalysisHost::default();
42     let change_fixture = ChangeFixture::parse(ra_fixture);
43     host.db.set_enable_proc_attr_macros(true);
44     host.db.apply_change(change_fixture.change);
45     let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
46     (host.analysis(), file_id, range_or_offset)
47 }
48
49 /// Creates analysis from a multi-file fixture, returns positions marked with $0.
50 pub(crate) fn annotations(ra_fixture: &str) -> (Analysis, FilePosition, Vec<(FileRange, String)>) {
51     let mut host = AnalysisHost::default();
52     let change_fixture = ChangeFixture::parse(ra_fixture);
53     host.db.set_enable_proc_attr_macros(true);
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 = range_or_offset.expect_offset();
57
58     let annotations = change_fixture
59         .files
60         .iter()
61         .flat_map(|&file_id| {
62             let file_text = host.analysis().file_text(file_id).unwrap();
63             let annotations = extract_annotations(&file_text);
64             annotations.into_iter().map(move |(range, data)| (FileRange { file_id, range }, data))
65         })
66         .collect();
67     (host.analysis(), FilePosition { file_id, offset }, annotations)
68 }
69
70 /// Creates analysis from a multi-file fixture with annonations without $0
71 pub(crate) fn annotations_without_marker(ra_fixture: &str) -> (Analysis, Vec<(FileRange, String)>) {
72     let mut host = AnalysisHost::default();
73     let change_fixture = ChangeFixture::parse(ra_fixture);
74     host.db.set_enable_proc_attr_macros(true);
75     host.db.apply_change(change_fixture.change);
76
77     let annotations = change_fixture
78         .files
79         .iter()
80         .flat_map(|&file_id| {
81             let file_text = host.analysis().file_text(file_id).unwrap();
82             let annotations = extract_annotations(&file_text);
83             annotations.into_iter().map(move |(range, data)| (FileRange { file_id, range }, data))
84         })
85         .collect();
86     (host.analysis(), annotations)
87 }