aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/mock_analysis.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/mock_analysis.rs')
-rw-r--r--crates/ra_ide/src/mock_analysis.rs55
1 files changed, 24 insertions, 31 deletions
diff --git a/crates/ra_ide/src/mock_analysis.rs b/crates/ra_ide/src/mock_analysis.rs
index b7325bfc3..e3990cf18 100644
--- a/crates/ra_ide/src/mock_analysis.rs
+++ b/crates/ra_ide/src/mock_analysis.rs
@@ -3,7 +3,7 @@ use std::{str::FromStr, sync::Arc};
3 3
4use ra_cfg::CfgOptions; 4use ra_cfg::CfgOptions;
5use ra_db::{CrateName, Env, FileSet, SourceRoot, VfsPath}; 5use ra_db::{CrateName, Env, FileSet, SourceRoot, VfsPath};
6use test_utils::{extract_offset, extract_range, Fixture, CURSOR_MARKER}; 6use test_utils::{extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER};
7 7
8use crate::{ 8use crate::{
9 Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange, 9 Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange,
@@ -11,27 +11,18 @@ use crate::{
11 11
12#[derive(Debug)] 12#[derive(Debug)]
13enum MockFileData { 13enum MockFileData {
14 Plain { path: String, content: String },
15 Fixture(Fixture), 14 Fixture(Fixture),
16} 15}
17 16
18impl MockFileData { 17impl MockFileData {
19 fn new(path: String, content: String) -> Self {
20 // `Self::Plain` causes a false warning: 'variant is never constructed: `Plain` '
21 // see https://github.com/rust-lang/rust/issues/69018
22 MockFileData::Plain { path, content }
23 }
24
25 fn path(&self) -> &str { 18 fn path(&self) -> &str {
26 match self { 19 match self {
27 MockFileData::Plain { path, .. } => path.as_str(),
28 MockFileData::Fixture(f) => f.path.as_str(), 20 MockFileData::Fixture(f) => f.path.as_str(),
29 } 21 }
30 } 22 }
31 23
32 fn content(&self) -> &str { 24 fn content(&self) -> &str {
33 match self { 25 match self {
34 MockFileData::Plain { content, .. } => content,
35 MockFileData::Fixture(f) => f.text.as_str(), 26 MockFileData::Fixture(f) => f.text.as_str(),
36 } 27 }
37 } 28 }
@@ -44,7 +35,6 @@ impl MockFileData {
44 f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into())); 35 f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
45 cfg 36 cfg
46 } 37 }
47 _ => CfgOptions::default(),
48 } 38 }
49 } 39 }
50 40
@@ -53,14 +43,12 @@ impl MockFileData {
53 MockFileData::Fixture(f) => { 43 MockFileData::Fixture(f) => {
54 f.edition.as_ref().map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()) 44 f.edition.as_ref().map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap())
55 } 45 }
56 _ => Edition::Edition2018,
57 } 46 }
58 } 47 }
59 48
60 fn env(&self) -> Env { 49 fn env(&self) -> Env {
61 match self { 50 match self {
62 MockFileData::Fixture(f) => Env::from(f.env.iter()), 51 MockFileData::Fixture(f) => Env::from(f.env.iter()),
63 _ => Env::default(),
64 } 52 }
65 } 53 }
66} 54}
@@ -89,31 +77,38 @@ impl MockAnalysis {
89 /// //- /foo.rs 77 /// //- /foo.rs
90 /// struct Baz; 78 /// struct Baz;
91 /// ``` 79 /// ```
92 pub fn with_files(fixture: &str) -> MockAnalysis { 80 pub fn with_files(ra_fixture: &str) -> MockAnalysis {
93 let mut res = MockAnalysis::default(); 81 let (res, pos) = MockAnalysis::with_fixture(ra_fixture);
94 for entry in Fixture::parse(fixture) { 82 assert!(pos.is_none());
95 res.add_file_fixture(entry);
96 }
97 res 83 res
98 } 84 }
99 85
100 /// Same as `with_files`, but requires that a single file contains a `<|>` marker, 86 /// Same as `with_files`, but requires that a single file contains a `<|>` marker,
101 /// whose position is also returned. 87 /// whose position is also returned.
102 pub fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) { 88 pub fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) {
89 let (res, position) = MockAnalysis::with_fixture(fixture);
90 let (file_id, range_or_offset) = position.expect("expected a marker (<|>)");
91 let offset = match range_or_offset {
92 RangeOrOffset::Range(_) => panic!(),
93 RangeOrOffset::Offset(it) => it,
94 };
95 (res, FilePosition { file_id, offset })
96 }
97
98 fn with_fixture(fixture: &str) -> (MockAnalysis, Option<(FileId, RangeOrOffset)>) {
103 let mut position = None; 99 let mut position = None;
104 let mut res = MockAnalysis::default(); 100 let mut res = MockAnalysis::default();
105 for mut entry in Fixture::parse(fixture) { 101 for mut entry in Fixture::parse(fixture) {
106 if entry.text.contains(CURSOR_MARKER) { 102 if entry.text.contains(CURSOR_MARKER) {
107 assert!(position.is_none(), "only one marker (<|>) per fixture is allowed"); 103 assert!(position.is_none(), "only one marker (<|>) per fixture is allowed");
108 let (offset, text) = extract_offset(&entry.text); 104 let (range_or_offset, text) = extract_range_or_offset(&entry.text);
109 entry.text = text; 105 entry.text = text;
110 let file_id = res.add_file_fixture(entry); 106 let file_id = res.add_file_fixture(entry);
111 position = Some(FilePosition { file_id, offset }); 107 position = Some((file_id, range_or_offset));
112 } else { 108 } else {
113 res.add_file_fixture(entry); 109 res.add_file_fixture(entry);
114 } 110 }
115 } 111 }
116 let position = position.expect("expected a marker (<|>)");
117 (res, position) 112 (res, position)
118 } 113 }
119 114
@@ -123,12 +118,6 @@ impl MockAnalysis {
123 file_id 118 file_id
124 } 119 }
125 120
126 fn add_file_with_range(&mut self, path: &str, text: &str) -> FileRange {
127 let (range, text) = extract_range(text);
128 let file_id = self.next_id();
129 self.files.push(MockFileData::new(path.to_string(), text));
130 FileRange { file_id, range }
131 }
132 pub fn id_of(&self, path: &str) -> FileId { 121 pub fn id_of(&self, path: &str) -> FileId {
133 let (idx, _) = self 122 let (idx, _) = self
134 .files 123 .files
@@ -209,8 +198,12 @@ pub fn single_file(ra_fixture: &str) -> (Analysis, FileId) {
209} 198}
210 199
211/// Creates analysis for a single file, returns range marked with a pair of <|>. 200/// Creates analysis for a single file, returns range marked with a pair of <|>.
212pub fn single_file_with_range(ra_fixture: &str) -> (Analysis, FileRange) { 201pub fn analysis_and_range(ra_fixture: &str) -> (Analysis, FileRange) {
213 let mut mock = MockAnalysis::default(); 202 let (res, position) = MockAnalysis::with_fixture(ra_fixture);
214 let pos = mock.add_file_with_range("/main.rs", ra_fixture); 203 let (file_id, range_or_offset) = position.expect("expected a marker (<|>)");
215 (mock.analysis(), pos) 204 let range = match range_or_offset {
205 RangeOrOffset::Range(it) => it,
206 RangeOrOffset::Offset(_) => panic!(),
207 };
208 (res.analysis(), FileRange { file_id, range })
216} 209}