aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-06-24 11:01:17 +0100
committerAleksey Kladov <[email protected]>2020-06-24 11:01:17 +0100
commit5e7a1a12038f822df346c65de613cc01882ce656 (patch)
tree06b5b90816e7b4c53be24d70174a9be3a2fe382d
parent04fe512f0d3567727cfb9ccab166c5344716711a (diff)
Simplify
-rw-r--r--crates/ra_ide/src/diagnostics.rs6
-rw-r--r--crates/ra_ide/src/mock_analysis.rs55
-rw-r--r--crates/ra_ide/src/parent_module.rs12
-rw-r--r--crates/ra_ide/src/syntax_tree.rs12
4 files changed, 38 insertions, 47 deletions
diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs
index 8a18bc18c..05fb799d6 100644
--- a/crates/ra_ide/src/diagnostics.rs
+++ b/crates/ra_ide/src/diagnostics.rs
@@ -573,10 +573,9 @@ mod tests {
573 573
574 impl Expr { 574 impl Expr {
575 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr { 575 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
576 Expr::Bin { <|> } 576 Expr::Bin { }
577 } 577 }
578 } 578 }
579
580 "; 579 ";
581 let after = r" 580 let after = r"
582 enum Expr { 581 enum Expr {
@@ -585,10 +584,9 @@ mod tests {
585 584
586 impl Expr { 585 impl Expr {
587 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr { 586 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
588 Expr::Bin { lhs: (), rhs: () <|> } 587 Expr::Bin { lhs: (), rhs: () }
589 } 588 }
590 } 589 }
591
592 "; 590 ";
593 check_apply_diagnostic_fix(before, after); 591 check_apply_diagnostic_fix(before, after);
594 } 592 }
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}
diff --git a/crates/ra_ide/src/parent_module.rs b/crates/ra_ide/src/parent_module.rs
index bc7f65470..e3e0c7639 100644
--- a/crates/ra_ide/src/parent_module.rs
+++ b/crates/ra_ide/src/parent_module.rs
@@ -125,12 +125,12 @@ mod tests {
125 #[test] 125 #[test]
126 fn test_resolve_crate_root() { 126 fn test_resolve_crate_root() {
127 let mock = MockAnalysis::with_files( 127 let mock = MockAnalysis::with_files(
128 " 128 r#"
129 //- /bar.rs 129//- /bar.rs
130 mod foo; 130mod foo;
131 //- /foo.rs 131//- /foo.rs
132 // empty <|> 132// empty
133 ", 133"#,
134 ); 134 );
135 let root_file = mock.id_of("/bar.rs"); 135 let root_file = mock.id_of("/bar.rs");
136 let mod_file = mock.id_of("/foo.rs"); 136 let mod_file = mock.id_of("/foo.rs");
diff --git a/crates/ra_ide/src/syntax_tree.rs b/crates/ra_ide/src/syntax_tree.rs
index a341684fd..33b1a0e16 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, single_file_with_range}; 107 use crate::mock_analysis::{single_file, analysis_and_range};
108 108
109 #[test] 109 #[test]
110 fn test_syntax_tree_without_range() { 110 fn test_syntax_tree_without_range() {
@@ -184,7 +184,7 @@ [email protected]
184 184
185 #[test] 185 #[test]
186 fn test_syntax_tree_with_range() { 186 fn test_syntax_tree_with_range() {
187 let (analysis, range) = single_file_with_range(r#"<|>fn foo() {}<|>"#.trim()); 187 let (analysis, range) = analysis_and_range(r#"<|>fn foo() {}<|>"#.trim());
188 let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap(); 188 let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap();
189 189
190 assert_eq_text!( 190 assert_eq_text!(
@@ -206,7 +206,7 @@ [email protected]
206 .trim() 206 .trim()
207 ); 207 );
208 208
209 let (analysis, range) = single_file_with_range( 209 let (analysis, range) = analysis_and_range(
210 r#"fn test() { 210 r#"fn test() {
211 <|>assert!(" 211 <|>assert!("
212 fn foo() { 212 fn foo() {
@@ -242,7 +242,7 @@ [email protected]
242 242
243 #[test] 243 #[test]
244 fn test_syntax_tree_inside_string() { 244 fn test_syntax_tree_inside_string() {
245 let (analysis, range) = single_file_with_range( 245 let (analysis, range) = analysis_and_range(
246 r#"fn test() { 246 r#"fn test() {
247 assert!(" 247 assert!("
248<|>fn foo() { 248<|>fn foo() {
@@ -276,7 +276,7 @@ [email protected]
276 ); 276 );
277 277
278 // With a raw string 278 // With a raw string
279 let (analysis, range) = single_file_with_range( 279 let (analysis, range) = analysis_and_range(
280 r###"fn test() { 280 r###"fn test() {
281 assert!(r#" 281 assert!(r#"
282<|>fn foo() { 282<|>fn foo() {
@@ -310,7 +310,7 @@ [email protected]
310 ); 310 );
311 311
312 // With a raw string 312 // With a raw string
313 let (analysis, range) = single_file_with_range( 313 let (analysis, range) = analysis_and_range(
314 r###"fn test() { 314 r###"fn test() {
315 assert!(r<|>#" 315 assert!(r<|>#"
316fn foo() { 316fn foo() {