aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-06-24 11:06:53 +0100
committerAleksey Kladov <[email protected]>2020-06-24 11:30:54 +0100
commit0a2b6087ec060bbf61330f0eec90cf5ca00c4f90 (patch)
treeca18b43553f014dce6660c6d4700cc0f07b4e455 /crates/ra_ide
parent5e7a1a12038f822df346c65de613cc01882ce656 (diff)
Simplify
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/mock_analysis.rs83
-rw-r--r--crates/ra_ide/src/syntax_tree.rs2
2 files changed, 18 insertions, 67 deletions
diff --git a/crates/ra_ide/src/mock_analysis.rs b/crates/ra_ide/src/mock_analysis.rs
index e3990cf18..889b84c59 100644
--- a/crates/ra_ide/src/mock_analysis.rs
+++ b/crates/ra_ide/src/mock_analysis.rs
@@ -1,5 +1,5 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2use std::{str::FromStr, sync::Arc}; 2use std::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};
@@ -9,61 +9,11 @@ use crate::{
9 Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange, 9 Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange,
10}; 10};
11 11
12#[derive(Debug)]
13enum MockFileData {
14 Fixture(Fixture),
15}
16
17impl MockFileData {
18 fn path(&self) -> &str {
19 match self {
20 MockFileData::Fixture(f) => f.path.as_str(),
21 }
22 }
23
24 fn content(&self) -> &str {
25 match self {
26 MockFileData::Fixture(f) => f.text.as_str(),
27 }
28 }
29
30 fn cfg_options(&self) -> CfgOptions {
31 match self {
32 MockFileData::Fixture(f) => {
33 let mut cfg = CfgOptions::default();
34 f.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into()));
35 f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
36 cfg
37 }
38 }
39 }
40
41 fn edition(&self) -> Edition {
42 match self {
43 MockFileData::Fixture(f) => {
44 f.edition.as_ref().map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap())
45 }
46 }
47 }
48
49 fn env(&self) -> Env {
50 match self {
51 MockFileData::Fixture(f) => Env::from(f.env.iter()),
52 }
53 }
54}
55
56impl From<Fixture> for MockFileData {
57 fn from(fixture: Fixture) -> Self {
58 Self::Fixture(fixture)
59 }
60}
61
62/// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis 12/// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis
63/// from a set of in-memory files. 13/// from a set of in-memory files.
64#[derive(Debug, Default)] 14#[derive(Debug, Default)]
65pub struct MockAnalysis { 15pub struct MockAnalysis {
66 files: Vec<MockFileData>, 16 files: Vec<Fixture>,
67} 17}
68 18
69impl MockAnalysis { 19impl MockAnalysis {
@@ -113,8 +63,8 @@ impl MockAnalysis {
113 } 63 }
114 64
115 fn add_file_fixture(&mut self, fixture: Fixture) -> FileId { 65 fn add_file_fixture(&mut self, fixture: Fixture) -> FileId {
116 let file_id = self.next_id(); 66 let file_id = FileId((self.files.len() + 1) as u32);
117 self.files.push(MockFileData::from(fixture)); 67 self.files.push(fixture);
118 file_id 68 file_id
119 } 69 }
120 70
@@ -123,7 +73,7 @@ impl MockAnalysis {
123 .files 73 .files
124 .iter() 74 .iter()
125 .enumerate() 75 .enumerate()
126 .find(|(_, data)| path == data.path()) 76 .find(|(_, data)| path == data.path)
127 .expect("no file in this mock"); 77 .expect("no file in this mock");
128 FileId(idx as u32 + 1) 78 FileId(idx as u32 + 1)
129 } 79 }
@@ -134,18 +84,23 @@ impl MockAnalysis {
134 let mut crate_graph = CrateGraph::default(); 84 let mut crate_graph = CrateGraph::default();
135 let mut root_crate = None; 85 let mut root_crate = None;
136 for (i, data) in self.files.into_iter().enumerate() { 86 for (i, data) in self.files.into_iter().enumerate() {
137 let path = data.path(); 87 let path = data.path;
138 assert!(path.starts_with('/')); 88 assert!(path.starts_with('/'));
139 let cfg_options = data.cfg_options(); 89
90 let mut cfg = CfgOptions::default();
91 data.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into()));
92 data.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
93 let edition: Edition =
94 data.edition.and_then(|it| it.parse().ok()).unwrap_or(Edition::Edition2018);
95
140 let file_id = FileId(i as u32 + 1); 96 let file_id = FileId(i as u32 + 1);
141 let edition = data.edition(); 97 let env = Env::from(data.env.iter());
142 let env = data.env();
143 if path == "/lib.rs" || path == "/main.rs" { 98 if path == "/lib.rs" || path == "/main.rs" {
144 root_crate = Some(crate_graph.add_crate_root( 99 root_crate = Some(crate_graph.add_crate_root(
145 file_id, 100 file_id,
146 edition, 101 edition,
147 None, 102 None,
148 cfg_options, 103 cfg,
149 env, 104 env,
150 Default::default(), 105 Default::default(),
151 )); 106 ));
@@ -156,7 +111,7 @@ impl MockAnalysis {
156 file_id, 111 file_id,
157 edition, 112 edition,
158 Some(CrateName::new(crate_name).unwrap()), 113 Some(CrateName::new(crate_name).unwrap()),
159 cfg_options, 114 cfg,
160 env, 115 env,
161 Default::default(), 116 Default::default(),
162 ); 117 );
@@ -168,7 +123,7 @@ impl MockAnalysis {
168 } 123 }
169 let path = VfsPath::new_virtual_path(path.to_string()); 124 let path = VfsPath::new_virtual_path(path.to_string());
170 file_set.insert(file_id, path); 125 file_set.insert(file_id, path);
171 change.change_file(file_id, Some(Arc::new(data.content().to_owned()))); 126 change.change_file(file_id, Some(Arc::new(data.text).to_owned()));
172 } 127 }
173 change.set_crate_graph(crate_graph); 128 change.set_crate_graph(crate_graph);
174 change.set_roots(vec![SourceRoot::new_local(file_set)]); 129 change.set_roots(vec![SourceRoot::new_local(file_set)]);
@@ -178,10 +133,6 @@ impl MockAnalysis {
178 pub fn analysis(self) -> Analysis { 133 pub fn analysis(self) -> Analysis {
179 self.analysis_host().analysis() 134 self.analysis_host().analysis()
180 } 135 }
181
182 fn next_id(&self) -> FileId {
183 FileId((self.files.len() + 1) as u32)
184 }
185} 136}
186 137
187/// Creates analysis from a multi-file fixture, returns positions marked with <|>. 138/// Creates analysis from a multi-file fixture, returns positions marked with <|>.
diff --git a/crates/ra_ide/src/syntax_tree.rs b/crates/ra_ide/src/syntax_tree.rs
index 33b1a0e16..f716a3861 100644
--- a/crates/ra_ide/src/syntax_tree.rs
+++ b/crates/ra_ide/src/syntax_tree.rs
@@ -104,7 +104,7 @@ fn syntax_tree_for_token(node: &SyntaxToken, text_range: TextRange) -> Option<St
104mod tests { 104mod tests {
105 use test_utils::assert_eq_text; 105 use test_utils::assert_eq_text;
106 106
107 use crate::mock_analysis::{single_file, analysis_and_range}; 107 use crate::mock_analysis::{analysis_and_range, single_file};
108 108
109 #[test] 109 #[test]
110 fn test_syntax_tree_without_range() { 110 fn test_syntax_tree_without_range() {