diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/handlers/add_from_impl_for_enum.rs | 2 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs | 2 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/fill_match_arms.rs | 2 | ||||
-rw-r--r-- | crates/ra_db/src/fixture.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/method_resolution.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide/src/mock_analysis.rs | 10 | ||||
-rw-r--r-- | crates/ra_ide/src/ssr.rs | 1 | ||||
-rw-r--r-- | crates/ra_ssr/src/lib.rs | 18 | ||||
-rw-r--r-- | crates/ra_ssr/src/matching.rs | 105 | ||||
-rw-r--r-- | crates/ra_ssr/src/replacing.rs | 10 | ||||
-rw-r--r-- | crates/ra_ssr/src/tests.rs | 53 | ||||
-rw-r--r-- | crates/rust-analyzer/tests/heavy_tests/main.rs | 54 | ||||
-rw-r--r-- | crates/rust-analyzer/tests/heavy_tests/support.rs | 2 | ||||
-rw-r--r-- | crates/test_utils/src/lib.rs | 10 |
14 files changed, 221 insertions, 60 deletions
diff --git a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs index 776bddf91..b0e56e1b5 100644 --- a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs +++ b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs | |||
@@ -128,7 +128,7 @@ impl From<foo::bar::baz::Boo> for A { | |||
128 | 128 | ||
129 | fn check_not_applicable(ra_fixture: &str) { | 129 | fn check_not_applicable(ra_fixture: &str) { |
130 | let fixture = | 130 | let fixture = |
131 | format!("//- main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); | 131 | format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); |
132 | check_assist_not_applicable(add_from_impl_for_enum, &fixture) | 132 | check_assist_not_applicable(add_from_impl_for_enum, &fixture) |
133 | } | 133 | } |
134 | 134 | ||
diff --git a/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs index 44db7917a..43b4584b4 100644 --- a/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs | |||
@@ -301,7 +301,7 @@ fn another_fn() { | |||
301 | 301 | ||
302 | fn check_not_applicable(ra_fixture: &str) { | 302 | fn check_not_applicable(ra_fixture: &str) { |
303 | let fixture = | 303 | let fixture = |
304 | format!("//- main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); | 304 | format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); |
305 | check_assist_not_applicable(extract_struct_from_enum_variant, &fixture) | 305 | check_assist_not_applicable(extract_struct_from_enum_variant, &fixture) |
306 | } | 306 | } |
307 | 307 | ||
diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs index 569efb768..3c12c1daa 100644 --- a/crates/ra_assists/src/handlers/fill_match_arms.rs +++ b/crates/ra_assists/src/handlers/fill_match_arms.rs | |||
@@ -765,7 +765,7 @@ fn foo(opt: Option<i32>) { | |||
765 | } | 765 | } |
766 | }"#; | 766 | }"#; |
767 | let before = | 767 | let before = |
768 | &format!("//- main.rs crate:main deps:core\n{}{}", before, FamousDefs::FIXTURE); | 768 | &format!("//- /main.rs crate:main deps:core\n{}{}", before, FamousDefs::FIXTURE); |
769 | 769 | ||
770 | check_assist( | 770 | check_assist( |
771 | fill_match_arms, | 771 | fill_match_arms, |
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 482a2f3e6..af8fe11ec 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs | |||
@@ -164,7 +164,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
164 | 164 | ||
165 | let mut source_root = SourceRoot::new_local(); | 165 | let mut source_root = SourceRoot::new_local(); |
166 | let mut source_root_id = WORKSPACE; | 166 | let mut source_root_id = WORKSPACE; |
167 | let mut source_root_prefix: RelativePathBuf = "/".into(); | 167 | let mut source_root_prefix = "/".to_string(); |
168 | let mut file_id = FileId(0); | 168 | let mut file_id = FileId(0); |
169 | 169 | ||
170 | let mut file_position = None; | 170 | let mut file_position = None; |
@@ -212,9 +212,9 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
212 | }; | 212 | }; |
213 | 213 | ||
214 | db.set_file_text(file_id, Arc::new(text)); | 214 | db.set_file_text(file_id, Arc::new(text)); |
215 | db.set_file_relative_path(file_id, meta.path.clone()); | 215 | db.set_file_relative_path(file_id, meta.path.clone().into()); |
216 | db.set_file_source_root(file_id, source_root_id); | 216 | db.set_file_source_root(file_id, source_root_id); |
217 | source_root.insert_file(meta.path, file_id); | 217 | source_root.insert_file(meta.path.into(), file_id); |
218 | 218 | ||
219 | file_id.0 += 1; | 219 | file_id.0 += 1; |
220 | } | 220 | } |
@@ -245,12 +245,12 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
245 | } | 245 | } |
246 | 246 | ||
247 | enum ParsedMeta { | 247 | enum ParsedMeta { |
248 | Root { path: RelativePathBuf }, | 248 | Root { path: String }, |
249 | File(FileMeta), | 249 | File(FileMeta), |
250 | } | 250 | } |
251 | 251 | ||
252 | struct FileMeta { | 252 | struct FileMeta { |
253 | path: RelativePathBuf, | 253 | path: String, |
254 | krate: Option<String>, | 254 | krate: Option<String>, |
255 | deps: Vec<String>, | 255 | deps: Vec<String>, |
256 | cfg: CfgOptions, | 256 | cfg: CfgOptions, |
diff --git a/crates/ra_hir_ty/src/tests/method_resolution.rs b/crates/ra_hir_ty/src/tests/method_resolution.rs index 804297315..a98efb1cc 100644 --- a/crates/ra_hir_ty/src/tests/method_resolution.rs +++ b/crates/ra_hir_ty/src/tests/method_resolution.rs | |||
@@ -933,7 +933,7 @@ fn method_resolution_overloaded_method() { | |||
933 | test_utils::mark::check!(impl_self_type_match_without_receiver); | 933 | test_utils::mark::check!(impl_self_type_match_without_receiver); |
934 | let t = type_at( | 934 | let t = type_at( |
935 | r#" | 935 | r#" |
936 | //- main.rs | 936 | //- /main.rs |
937 | struct Wrapper<T>(T); | 937 | struct Wrapper<T>(T); |
938 | struct Foo<T>(T); | 938 | struct Foo<T>(T); |
939 | struct Bar<T>(T); | 939 | struct Bar<T>(T); |
diff --git a/crates/ra_ide/src/mock_analysis.rs b/crates/ra_ide/src/mock_analysis.rs index ad78d2d93..76910d09b 100644 --- a/crates/ra_ide/src/mock_analysis.rs +++ b/crates/ra_ide/src/mock_analysis.rs | |||
@@ -4,7 +4,7 @@ use std::str::FromStr; | |||
4 | use std::sync::Arc; | 4 | use std::sync::Arc; |
5 | 5 | ||
6 | use ra_cfg::CfgOptions; | 6 | use ra_cfg::CfgOptions; |
7 | use ra_db::{CrateName, Env, RelativePathBuf}; | 7 | use ra_db::{CrateName, Env}; |
8 | use test_utils::{extract_offset, extract_range, parse_fixture, FixtureEntry, CURSOR_MARKER}; | 8 | use test_utils::{extract_offset, extract_range, parse_fixture, FixtureEntry, CURSOR_MARKER}; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
@@ -28,7 +28,7 @@ impl MockFileData { | |||
28 | fn path(&self) -> &str { | 28 | fn path(&self) -> &str { |
29 | match self { | 29 | match self { |
30 | MockFileData::Plain { path, .. } => path.as_str(), | 30 | MockFileData::Plain { path, .. } => path.as_str(), |
31 | MockFileData::Fixture(f) => f.meta.path().as_str(), | 31 | MockFileData::Fixture(f) => f.meta.path(), |
32 | } | 32 | } |
33 | } | 33 | } |
34 | 34 | ||
@@ -167,7 +167,6 @@ impl MockAnalysis { | |||
167 | for (i, data) in self.files.into_iter().enumerate() { | 167 | for (i, data) in self.files.into_iter().enumerate() { |
168 | let path = data.path(); | 168 | let path = data.path(); |
169 | assert!(path.starts_with('/')); | 169 | assert!(path.starts_with('/')); |
170 | let path = RelativePathBuf::from_path(&path[1..]).unwrap(); | ||
171 | let cfg_options = data.cfg_options(); | 170 | let cfg_options = data.cfg_options(); |
172 | let file_id = FileId(i as u32 + 1); | 171 | let file_id = FileId(i as u32 + 1); |
173 | let edition = data.edition(); | 172 | let edition = data.edition(); |
@@ -183,7 +182,8 @@ impl MockAnalysis { | |||
183 | Default::default(), | 182 | Default::default(), |
184 | )); | 183 | )); |
185 | } else if path.ends_with("/lib.rs") { | 184 | } else if path.ends_with("/lib.rs") { |
186 | let crate_name = path.parent().unwrap().file_name().unwrap(); | 185 | let base = &path[..path.len() - "/lib.rs".len()]; |
186 | let crate_name = &base[base.rfind('/').unwrap() + '/'.len_utf8()..]; | ||
187 | let other_crate = crate_graph.add_crate_root( | 187 | let other_crate = crate_graph.add_crate_root( |
188 | file_id, | 188 | file_id, |
189 | edition, | 189 | edition, |
@@ -199,7 +199,7 @@ impl MockAnalysis { | |||
199 | .unwrap(); | 199 | .unwrap(); |
200 | } | 200 | } |
201 | } | 201 | } |
202 | change.add_file(source_root, file_id, path, Arc::new(data.content().to_owned())); | 202 | change.add_file(source_root, file_id, path.into(), Arc::new(data.content().to_owned())); |
203 | } | 203 | } |
204 | change.set_crate_graph(crate_graph); | 204 | change.set_crate_graph(crate_graph); |
205 | host.apply_change(change); | 205 | host.apply_change(change); |
diff --git a/crates/ra_ide/src/ssr.rs b/crates/ra_ide/src/ssr.rs index 59c230f6c..03f18c617 100644 --- a/crates/ra_ide/src/ssr.rs +++ b/crates/ra_ide/src/ssr.rs | |||
@@ -9,6 +9,7 @@ use ra_ssr::{MatchFinder, SsrError, SsrRule}; | |||
9 | // Search and replace with named wildcards that will match any expression, type, path, pattern or item. | 9 | // Search and replace with named wildcards that will match any expression, type, path, pattern or item. |
10 | // The syntax for a structural search replace command is `<search_pattern> ==>> <replace_pattern>`. | 10 | // The syntax for a structural search replace command is `<search_pattern> ==>> <replace_pattern>`. |
11 | // A `$<name>` placeholder in the search pattern will match any AST node and `$<name>` will reference it in the replacement. | 11 | // A `$<name>` placeholder in the search pattern will match any AST node and `$<name>` will reference it in the replacement. |
12 | // Within a macro call, a placeholder will match up until whatever token follows the placeholder. | ||
12 | // Available via the command `rust-analyzer.ssr`. | 13 | // Available via the command `rust-analyzer.ssr`. |
13 | // | 14 | // |
14 | // ```rust | 15 | // ```rust |
diff --git a/crates/ra_ssr/src/lib.rs b/crates/ra_ssr/src/lib.rs index fc716ae82..da26ee669 100644 --- a/crates/ra_ssr/src/lib.rs +++ b/crates/ra_ssr/src/lib.rs | |||
@@ -91,14 +91,16 @@ impl<'db> MatchFinder<'db> { | |||
91 | if let Ok(mut m) = matching::get_match(false, rule, &code, restrict_range, &self.sema) { | 91 | if let Ok(mut m) = matching::get_match(false, rule, &code, restrict_range, &self.sema) { |
92 | // Continue searching in each of our placeholders. | 92 | // Continue searching in each of our placeholders. |
93 | for placeholder_value in m.placeholder_values.values_mut() { | 93 | for placeholder_value in m.placeholder_values.values_mut() { |
94 | // Don't search our placeholder if it's the entire matched node, otherwise we'd | 94 | if let Some(placeholder_node) = &placeholder_value.node { |
95 | // find the same match over and over until we got a stack overflow. | 95 | // Don't search our placeholder if it's the entire matched node, otherwise we'd |
96 | if placeholder_value.node != *code { | 96 | // find the same match over and over until we got a stack overflow. |
97 | self.find_matches( | 97 | if placeholder_node != code { |
98 | &placeholder_value.node, | 98 | self.find_matches( |
99 | restrict_range, | 99 | placeholder_node, |
100 | &mut placeholder_value.inner_matches, | 100 | restrict_range, |
101 | ); | 101 | &mut placeholder_value.inner_matches, |
102 | ); | ||
103 | } | ||
102 | } | 104 | } |
103 | } | 105 | } |
104 | matches_out.matches.push(m); | 106 | matches_out.matches.push(m); |
diff --git a/crates/ra_ssr/src/matching.rs b/crates/ra_ssr/src/matching.rs index 265b6d793..bdaba9f1b 100644 --- a/crates/ra_ssr/src/matching.rs +++ b/crates/ra_ssr/src/matching.rs | |||
@@ -61,8 +61,9 @@ pub(crate) struct Var(pub String); | |||
61 | /// Information about a placeholder bound in a match. | 61 | /// Information about a placeholder bound in a match. |
62 | #[derive(Debug)] | 62 | #[derive(Debug)] |
63 | pub(crate) struct PlaceholderMatch { | 63 | pub(crate) struct PlaceholderMatch { |
64 | /// The node that the placeholder matched to. | 64 | /// The node that the placeholder matched to. If set, then we'll search for further matches |
65 | pub(crate) node: SyntaxNode, | 65 | /// within this node. It isn't set when we match tokens within a macro call's token tree. |
66 | pub(crate) node: Option<SyntaxNode>, | ||
66 | pub(crate) range: FileRange, | 67 | pub(crate) range: FileRange, |
67 | /// More matches, found within `node`. | 68 | /// More matches, found within `node`. |
68 | pub(crate) inner_matches: SsrMatches, | 69 | pub(crate) inner_matches: SsrMatches, |
@@ -195,6 +196,7 @@ impl<'db, 'sema> MatchState<'db, 'sema> { | |||
195 | SyntaxKind::RECORD_FIELD_LIST => { | 196 | SyntaxKind::RECORD_FIELD_LIST => { |
196 | self.attempt_match_record_field_list(match_inputs, pattern, code) | 197 | self.attempt_match_record_field_list(match_inputs, pattern, code) |
197 | } | 198 | } |
199 | SyntaxKind::TOKEN_TREE => self.attempt_match_token_tree(match_inputs, pattern, code), | ||
198 | _ => self.attempt_match_node_children(match_inputs, pattern, code), | 200 | _ => self.attempt_match_node_children(match_inputs, pattern, code), |
199 | } | 201 | } |
200 | } | 202 | } |
@@ -340,6 +342,90 @@ impl<'db, 'sema> MatchState<'db, 'sema> { | |||
340 | Ok(()) | 342 | Ok(()) |
341 | } | 343 | } |
342 | 344 | ||
345 | /// Outside of token trees, a placeholder can only match a single AST node, whereas in a token | ||
346 | /// tree it can match a sequence of tokens. | ||
347 | fn attempt_match_token_tree( | ||
348 | &mut self, | ||
349 | match_inputs: &MatchInputs, | ||
350 | pattern: &SyntaxNode, | ||
351 | code: &ra_syntax::SyntaxNode, | ||
352 | ) -> Result<(), MatchFailed> { | ||
353 | let mut pattern = PatternIterator::new(pattern).peekable(); | ||
354 | let mut children = code.children_with_tokens(); | ||
355 | while let Some(child) = children.next() { | ||
356 | if let Some(placeholder) = pattern.peek().and_then(|p| match_inputs.get_placeholder(p)) | ||
357 | { | ||
358 | pattern.next(); | ||
359 | let next_pattern_token = pattern | ||
360 | .peek() | ||
361 | .and_then(|p| match p { | ||
362 | SyntaxElement::Token(t) => Some(t.clone()), | ||
363 | SyntaxElement::Node(n) => n.first_token(), | ||
364 | }) | ||
365 | .map(|p| p.text().to_string()); | ||
366 | let first_matched_token = child.clone(); | ||
367 | let mut last_matched_token = child; | ||
368 | // Read code tokens util we reach one equal to the next token from our pattern | ||
369 | // or we reach the end of the token tree. | ||
370 | while let Some(next) = children.next() { | ||
371 | match &next { | ||
372 | SyntaxElement::Token(t) => { | ||
373 | if Some(t.to_string()) == next_pattern_token { | ||
374 | pattern.next(); | ||
375 | break; | ||
376 | } | ||
377 | } | ||
378 | SyntaxElement::Node(n) => { | ||
379 | if let Some(first_token) = n.first_token() { | ||
380 | if Some(first_token.to_string()) == next_pattern_token { | ||
381 | if let Some(SyntaxElement::Node(p)) = pattern.next() { | ||
382 | // We have a subtree that starts with the next token in our pattern. | ||
383 | self.attempt_match_token_tree(match_inputs, &p, &n)?; | ||
384 | break; | ||
385 | } | ||
386 | } | ||
387 | } | ||
388 | } | ||
389 | }; | ||
390 | last_matched_token = next; | ||
391 | } | ||
392 | if let Some(match_out) = &mut self.match_out { | ||
393 | match_out.placeholder_values.insert( | ||
394 | Var(placeholder.ident.to_string()), | ||
395 | PlaceholderMatch::from_range(FileRange { | ||
396 | file_id: self.sema.original_range(code).file_id, | ||
397 | range: first_matched_token | ||
398 | .text_range() | ||
399 | .cover(last_matched_token.text_range()), | ||
400 | }), | ||
401 | ); | ||
402 | } | ||
403 | continue; | ||
404 | } | ||
405 | // Match literal (non-placeholder) tokens. | ||
406 | match child { | ||
407 | SyntaxElement::Token(token) => { | ||
408 | self.attempt_match_token(&mut pattern, &token)?; | ||
409 | } | ||
410 | SyntaxElement::Node(node) => match pattern.next() { | ||
411 | Some(SyntaxElement::Node(p)) => { | ||
412 | self.attempt_match_token_tree(match_inputs, &p, &node)?; | ||
413 | } | ||
414 | Some(SyntaxElement::Token(p)) => fail_match!( | ||
415 | "Pattern has token '{}', code has subtree '{}'", | ||
416 | p.text(), | ||
417 | node.text() | ||
418 | ), | ||
419 | None => fail_match!("Pattern has nothing, code has '{}'", node.text()), | ||
420 | }, | ||
421 | } | ||
422 | } | ||
423 | if let Some(p) = pattern.next() { | ||
424 | fail_match!("Reached end of token tree in code, but pattern still has {:?}", p); | ||
425 | } | ||
426 | Ok(()) | ||
427 | } | ||
428 | |||
343 | fn next_non_trivial(&mut self, code_it: &mut SyntaxElementChildren) -> Option<SyntaxElement> { | 429 | fn next_non_trivial(&mut self, code_it: &mut SyntaxElementChildren) -> Option<SyntaxElement> { |
344 | loop { | 430 | loop { |
345 | let c = code_it.next(); | 431 | let c = code_it.next(); |
@@ -399,7 +485,11 @@ fn recording_match_fail_reasons() -> bool { | |||
399 | 485 | ||
400 | impl PlaceholderMatch { | 486 | impl PlaceholderMatch { |
401 | fn new(node: &SyntaxNode, range: FileRange) -> Self { | 487 | fn new(node: &SyntaxNode, range: FileRange) -> Self { |
402 | Self { node: node.clone(), range, inner_matches: SsrMatches::default() } | 488 | Self { node: Some(node.clone()), range, inner_matches: SsrMatches::default() } |
489 | } | ||
490 | |||
491 | fn from_range(range: FileRange) -> Self { | ||
492 | Self { node: None, range, inner_matches: SsrMatches::default() } | ||
403 | } | 493 | } |
404 | } | 494 | } |
405 | 495 | ||
@@ -484,7 +574,14 @@ mod tests { | |||
484 | assert_eq!(matches.matches.len(), 1); | 574 | assert_eq!(matches.matches.len(), 1); |
485 | assert_eq!(matches.matches[0].matched_node.text(), "foo(1+2)"); | 575 | assert_eq!(matches.matches[0].matched_node.text(), "foo(1+2)"); |
486 | assert_eq!(matches.matches[0].placeholder_values.len(), 1); | 576 | assert_eq!(matches.matches[0].placeholder_values.len(), 1); |
487 | assert_eq!(matches.matches[0].placeholder_values[&Var("x".to_string())].node.text(), "1+2"); | 577 | assert_eq!( |
578 | matches.matches[0].placeholder_values[&Var("x".to_string())] | ||
579 | .node | ||
580 | .as_ref() | ||
581 | .unwrap() | ||
582 | .text(), | ||
583 | "1+2" | ||
584 | ); | ||
488 | 585 | ||
489 | let edit = crate::replacing::matches_to_edit(&matches); | 586 | let edit = crate::replacing::matches_to_edit(&matches); |
490 | let mut after = input.to_string(); | 587 | let mut after = input.to_string(); |
diff --git a/crates/ra_ssr/src/replacing.rs b/crates/ra_ssr/src/replacing.rs index 81a5e06a9..5dcde82a2 100644 --- a/crates/ra_ssr/src/replacing.rs +++ b/crates/ra_ssr/src/replacing.rs | |||
@@ -24,6 +24,7 @@ fn matches_to_edit_at_offset(matches: &SsrMatches, relative_start: TextSize) -> | |||
24 | 24 | ||
25 | fn render_replace(match_info: &Match) -> String { | 25 | fn render_replace(match_info: &Match) -> String { |
26 | let mut out = String::new(); | 26 | let mut out = String::new(); |
27 | let match_start = match_info.matched_node.text_range().start(); | ||
27 | for r in &match_info.template.tokens { | 28 | for r in &match_info.template.tokens { |
28 | match r { | 29 | match r { |
29 | PatternElement::Token(t) => out.push_str(t.text.as_str()), | 30 | PatternElement::Token(t) => out.push_str(t.text.as_str()), |
@@ -32,7 +33,14 @@ fn render_replace(match_info: &Match) -> String { | |||
32 | match_info.placeholder_values.get(&Var(p.ident.to_string())) | 33 | match_info.placeholder_values.get(&Var(p.ident.to_string())) |
33 | { | 34 | { |
34 | let range = &placeholder_value.range.range; | 35 | let range = &placeholder_value.range.range; |
35 | let mut matched_text = placeholder_value.node.text().to_string(); | 36 | let mut matched_text = if let Some(node) = &placeholder_value.node { |
37 | node.text().to_string() | ||
38 | } else { | ||
39 | let relative_range = range.checked_sub(match_start).unwrap(); | ||
40 | match_info.matched_node.text().to_string() | ||
41 | [usize::from(relative_range.start())..usize::from(relative_range.end())] | ||
42 | .to_string() | ||
43 | }; | ||
36 | let edit = | 44 | let edit = |
37 | matches_to_edit_at_offset(&placeholder_value.inner_matches, range.start()); | 45 | matches_to_edit_at_offset(&placeholder_value.inner_matches, range.start()); |
38 | edit.apply(&mut matched_text); | 46 | edit.apply(&mut matched_text); |
diff --git a/crates/ra_ssr/src/tests.rs b/crates/ra_ssr/src/tests.rs index 4b747fe18..3ee1e74e9 100644 --- a/crates/ra_ssr/src/tests.rs +++ b/crates/ra_ssr/src/tests.rs | |||
@@ -427,6 +427,45 @@ fn match_reordered_struct_instantiation() { | |||
427 | } | 427 | } |
428 | 428 | ||
429 | #[test] | 429 | #[test] |
430 | fn match_macro_invocation() { | ||
431 | assert_matches("foo!($a)", "fn() {foo(foo!(foo()))}", &["foo!(foo())"]); | ||
432 | assert_matches("foo!(41, $a, 43)", "fn() {foo!(41, 42, 43)}", &["foo!(41, 42, 43)"]); | ||
433 | assert_no_match("foo!(50, $a, 43)", "fn() {foo!(41, 42, 43}"); | ||
434 | assert_no_match("foo!(41, $a, 50)", "fn() {foo!(41, 42, 43}"); | ||
435 | assert_matches("foo!($a())", "fn() {foo!(bar())}", &["foo!(bar())"]); | ||
436 | } | ||
437 | |||
438 | // When matching within a macro expansion, we only allow matches of nodes that originated from | ||
439 | // the macro call, not from the macro definition. | ||
440 | #[test] | ||
441 | fn no_match_expression_from_macro() { | ||
442 | assert_no_match( | ||
443 | "$a.clone()", | ||
444 | r#" | ||
445 | macro_rules! m1 { | ||
446 | () => {42.clone()} | ||
447 | } | ||
448 | fn f1() {m1!()} | ||
449 | "#, | ||
450 | ); | ||
451 | } | ||
452 | |||
453 | // We definitely don't want to allow matching of an expression that part originates from the | ||
454 | // macro call `42` and part from the macro definition `.clone()`. | ||
455 | #[test] | ||
456 | fn no_match_split_expression() { | ||
457 | assert_no_match( | ||
458 | "$a.clone()", | ||
459 | r#" | ||
460 | macro_rules! m1 { | ||
461 | ($x:expr) => {$x.clone()} | ||
462 | } | ||
463 | fn f1() {m1!(42)} | ||
464 | "#, | ||
465 | ); | ||
466 | } | ||
467 | |||
468 | #[test] | ||
430 | fn replace_function_call() { | 469 | fn replace_function_call() { |
431 | assert_ssr_transform("foo() ==>> bar()", "fn f1() {foo(); foo();}", "fn f1() {bar(); bar();}"); | 470 | assert_ssr_transform("foo() ==>> bar()", "fn f1() {foo(); foo();}", "fn f1() {bar(); bar();}"); |
432 | } | 471 | } |
@@ -468,6 +507,20 @@ fn replace_struct_init() { | |||
468 | } | 507 | } |
469 | 508 | ||
470 | #[test] | 509 | #[test] |
510 | fn replace_macro_invocations() { | ||
511 | assert_ssr_transform( | ||
512 | "try!($a) ==>> $a?", | ||
513 | "fn f1() -> Result<(), E> {bar(try!(foo()));}", | ||
514 | "fn f1() -> Result<(), E> {bar(foo()?);}", | ||
515 | ); | ||
516 | assert_ssr_transform( | ||
517 | "foo!($a($b)) ==>> foo($b, $a)", | ||
518 | "fn f1() {foo!(abc(def() + 2));}", | ||
519 | "fn f1() {foo(def() + 2, abc);}", | ||
520 | ); | ||
521 | } | ||
522 | |||
523 | #[test] | ||
471 | fn replace_binary_op() { | 524 | fn replace_binary_op() { |
472 | assert_ssr_transform( | 525 | assert_ssr_transform( |
473 | "$a + $b ==>> $b + $a", | 526 | "$a + $b ==>> $b + $a", |
diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs index 0e2a83c6a..48ce831af 100644 --- a/crates/rust-analyzer/tests/heavy_tests/main.rs +++ b/crates/rust-analyzer/tests/heavy_tests/main.rs | |||
@@ -29,12 +29,12 @@ fn completes_items_from_standard_library() { | |||
29 | let project_start = Instant::now(); | 29 | let project_start = Instant::now(); |
30 | let server = Project::with_fixture( | 30 | let server = Project::with_fixture( |
31 | r#" | 31 | r#" |
32 | //- Cargo.toml | 32 | //- /Cargo.toml |
33 | [package] | 33 | [package] |
34 | name = "foo" | 34 | name = "foo" |
35 | version = "0.0.0" | 35 | version = "0.0.0" |
36 | 36 | ||
37 | //- src/lib.rs | 37 | //- /src/lib.rs |
38 | use std::collections::Spam; | 38 | use std::collections::Spam; |
39 | "#, | 39 | "#, |
40 | ) | 40 | ) |
@@ -63,24 +63,24 @@ fn test_runnables_project() { | |||
63 | } | 63 | } |
64 | 64 | ||
65 | let code = r#" | 65 | let code = r#" |
66 | //- foo/Cargo.toml | 66 | //- /foo/Cargo.toml |
67 | [package] | 67 | [package] |
68 | name = "foo" | 68 | name = "foo" |
69 | version = "0.0.0" | 69 | version = "0.0.0" |
70 | 70 | ||
71 | //- foo/src/lib.rs | 71 | //- /foo/src/lib.rs |
72 | pub fn foo() {} | 72 | pub fn foo() {} |
73 | 73 | ||
74 | //- foo/tests/spam.rs | 74 | //- /foo/tests/spam.rs |
75 | #[test] | 75 | #[test] |
76 | fn test_eggs() {} | 76 | fn test_eggs() {} |
77 | 77 | ||
78 | //- bar/Cargo.toml | 78 | //- /bar/Cargo.toml |
79 | [package] | 79 | [package] |
80 | name = "bar" | 80 | name = "bar" |
81 | version = "0.0.0" | 81 | version = "0.0.0" |
82 | 82 | ||
83 | //- bar/src/main.rs | 83 | //- /bar/src/main.rs |
84 | fn main() {} | 84 | fn main() {} |
85 | "#; | 85 | "#; |
86 | 86 | ||
@@ -140,12 +140,12 @@ fn test_format_document() { | |||
140 | 140 | ||
141 | let server = project( | 141 | let server = project( |
142 | r#" | 142 | r#" |
143 | //- Cargo.toml | 143 | //- /Cargo.toml |
144 | [package] | 144 | [package] |
145 | name = "foo" | 145 | name = "foo" |
146 | version = "0.0.0" | 146 | version = "0.0.0" |
147 | 147 | ||
148 | //- src/lib.rs | 148 | //- /src/lib.rs |
149 | mod bar; | 149 | mod bar; |
150 | 150 | ||
151 | fn main() { | 151 | fn main() { |
@@ -200,13 +200,13 @@ fn test_format_document_2018() { | |||
200 | 200 | ||
201 | let server = project( | 201 | let server = project( |
202 | r#" | 202 | r#" |
203 | //- Cargo.toml | 203 | //- /Cargo.toml |
204 | [package] | 204 | [package] |
205 | name = "foo" | 205 | name = "foo" |
206 | version = "0.0.0" | 206 | version = "0.0.0" |
207 | edition = "2018" | 207 | edition = "2018" |
208 | 208 | ||
209 | //- src/lib.rs | 209 | //- /src/lib.rs |
210 | mod bar; | 210 | mod bar; |
211 | 211 | ||
212 | async fn test() { | 212 | async fn test() { |
@@ -266,12 +266,12 @@ fn test_missing_module_code_action() { | |||
266 | 266 | ||
267 | let server = project( | 267 | let server = project( |
268 | r#" | 268 | r#" |
269 | //- Cargo.toml | 269 | //- /Cargo.toml |
270 | [package] | 270 | [package] |
271 | name = "foo" | 271 | name = "foo" |
272 | version = "0.0.0" | 272 | version = "0.0.0" |
273 | 273 | ||
274 | //- src/lib.rs | 274 | //- /src/lib.rs |
275 | mod bar; | 275 | mod bar; |
276 | 276 | ||
277 | fn main() {} | 277 | fn main() {} |
@@ -335,10 +335,10 @@ fn test_missing_module_code_action_in_json_project() { | |||
335 | 335 | ||
336 | let code = format!( | 336 | let code = format!( |
337 | r#" | 337 | r#" |
338 | //- rust-project.json | 338 | //- /rust-project.json |
339 | {PROJECT} | 339 | {PROJECT} |
340 | 340 | ||
341 | //- src/lib.rs | 341 | //- /src/lib.rs |
342 | mod bar; | 342 | mod bar; |
343 | 343 | ||
344 | fn main() {{}} | 344 | fn main() {{}} |
@@ -391,15 +391,15 @@ fn diagnostics_dont_block_typing() { | |||
391 | } | 391 | } |
392 | 392 | ||
393 | let librs: String = (0..10).map(|i| format!("mod m{};", i)).collect(); | 393 | let librs: String = (0..10).map(|i| format!("mod m{};", i)).collect(); |
394 | let libs: String = (0..10).map(|i| format!("//- src/m{}.rs\nfn foo() {{}}\n\n", i)).collect(); | 394 | let libs: String = (0..10).map(|i| format!("//- /src/m{}.rs\nfn foo() {{}}\n\n", i)).collect(); |
395 | let server = Project::with_fixture(&format!( | 395 | let server = Project::with_fixture(&format!( |
396 | r#" | 396 | r#" |
397 | //- Cargo.toml | 397 | //- /Cargo.toml |
398 | [package] | 398 | [package] |
399 | name = "foo" | 399 | name = "foo" |
400 | version = "0.0.0" | 400 | version = "0.0.0" |
401 | 401 | ||
402 | //- src/lib.rs | 402 | //- /src/lib.rs |
403 | {} | 403 | {} |
404 | 404 | ||
405 | {} | 405 | {} |
@@ -449,12 +449,12 @@ fn preserves_dos_line_endings() { | |||
449 | 449 | ||
450 | let server = Project::with_fixture( | 450 | let server = Project::with_fixture( |
451 | &" | 451 | &" |
452 | //- Cargo.toml | 452 | //- /Cargo.toml |
453 | [package] | 453 | [package] |
454 | name = \"foo\" | 454 | name = \"foo\" |
455 | version = \"0.0.0\" | 455 | version = \"0.0.0\" |
456 | 456 | ||
457 | //- src/main.rs | 457 | //- /src/main.rs |
458 | /// Some Docs\r\nfn main() {} | 458 | /// Some Docs\r\nfn main() {} |
459 | ", | 459 | ", |
460 | ) | 460 | ) |
@@ -484,12 +484,12 @@ fn out_dirs_check() { | |||
484 | 484 | ||
485 | let server = Project::with_fixture( | 485 | let server = Project::with_fixture( |
486 | r###" | 486 | r###" |
487 | //- Cargo.toml | 487 | //- /Cargo.toml |
488 | [package] | 488 | [package] |
489 | name = "foo" | 489 | name = "foo" |
490 | version = "0.0.0" | 490 | version = "0.0.0" |
491 | 491 | ||
492 | //- build.rs | 492 | //- /build.rs |
493 | use std::{env, fs, path::Path}; | 493 | use std::{env, fs, path::Path}; |
494 | 494 | ||
495 | fn main() { | 495 | fn main() { |
@@ -504,7 +504,7 @@ fn main() { | |||
504 | println!("cargo:rustc-cfg=featlike=\"set\""); | 504 | println!("cargo:rustc-cfg=featlike=\"set\""); |
505 | println!("cargo:rerun-if-changed=build.rs"); | 505 | println!("cargo:rerun-if-changed=build.rs"); |
506 | } | 506 | } |
507 | //- src/main.rs | 507 | //- /src/main.rs |
508 | #[rustc_builtin_macro] macro_rules! include {} | 508 | #[rustc_builtin_macro] macro_rules! include {} |
509 | #[rustc_builtin_macro] macro_rules! concat {} | 509 | #[rustc_builtin_macro] macro_rules! concat {} |
510 | #[rustc_builtin_macro] macro_rules! env {} | 510 | #[rustc_builtin_macro] macro_rules! env {} |
@@ -599,7 +599,7 @@ fn resolve_proc_macro() { | |||
599 | } | 599 | } |
600 | let server = Project::with_fixture( | 600 | let server = Project::with_fixture( |
601 | r###" | 601 | r###" |
602 | //- foo/Cargo.toml | 602 | //- /foo/Cargo.toml |
603 | [package] | 603 | [package] |
604 | name = "foo" | 604 | name = "foo" |
605 | version = "0.0.0" | 605 | version = "0.0.0" |
@@ -607,7 +607,7 @@ edition = "2018" | |||
607 | [dependencies] | 607 | [dependencies] |
608 | bar = {path = "../bar"} | 608 | bar = {path = "../bar"} |
609 | 609 | ||
610 | //- foo/src/main.rs | 610 | //- /foo/src/main.rs |
611 | use bar::Bar; | 611 | use bar::Bar; |
612 | trait Bar { | 612 | trait Bar { |
613 | fn bar(); | 613 | fn bar(); |
@@ -618,7 +618,7 @@ fn main() { | |||
618 | Foo::bar(); | 618 | Foo::bar(); |
619 | } | 619 | } |
620 | 620 | ||
621 | //- bar/Cargo.toml | 621 | //- /bar/Cargo.toml |
622 | [package] | 622 | [package] |
623 | name = "bar" | 623 | name = "bar" |
624 | version = "0.0.0" | 624 | version = "0.0.0" |
@@ -627,7 +627,7 @@ edition = "2018" | |||
627 | [lib] | 627 | [lib] |
628 | proc-macro = true | 628 | proc-macro = true |
629 | 629 | ||
630 | //- bar/src/lib.rs | 630 | //- /bar/src/lib.rs |
631 | extern crate proc_macro; | 631 | extern crate proc_macro; |
632 | use proc_macro::{Delimiter, Group, Ident, Span, TokenStream, TokenTree}; | 632 | use proc_macro::{Delimiter, Group, Ident, Span, TokenStream, TokenTree}; |
633 | macro_rules! t { | 633 | macro_rules! t { |
diff --git a/crates/rust-analyzer/tests/heavy_tests/support.rs b/crates/rust-analyzer/tests/heavy_tests/support.rs index 30d03b622..f58790ded 100644 --- a/crates/rust-analyzer/tests/heavy_tests/support.rs +++ b/crates/rust-analyzer/tests/heavy_tests/support.rs | |||
@@ -69,7 +69,7 @@ impl<'a> Project<'a> { | |||
69 | let mut paths = vec![]; | 69 | let mut paths = vec![]; |
70 | 70 | ||
71 | for entry in parse_fixture(self.fixture) { | 71 | for entry in parse_fixture(self.fixture) { |
72 | let path = tmp_dir.path().join(entry.meta.path().as_str()); | 72 | let path = tmp_dir.path().join(&entry.meta.path()['/'.len_utf8()..]); |
73 | fs::create_dir_all(path.parent().unwrap()).unwrap(); | 73 | fs::create_dir_all(path.parent().unwrap()).unwrap(); |
74 | fs::write(path.as_path(), entry.text.as_bytes()).unwrap(); | 74 | fs::write(path.as_path(), entry.text.as_bytes()).unwrap(); |
75 | paths.push((path, entry.text)); | 75 | paths.push((path, entry.text)); |
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 981565cd7..b1333cf15 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs | |||
@@ -168,13 +168,13 @@ pub struct FixtureEntry { | |||
168 | 168 | ||
169 | #[derive(Debug, Eq, PartialEq)] | 169 | #[derive(Debug, Eq, PartialEq)] |
170 | pub enum FixtureMeta { | 170 | pub enum FixtureMeta { |
171 | Root { path: RelativePathBuf }, | 171 | Root { path: String }, |
172 | File(FileMeta), | 172 | File(FileMeta), |
173 | } | 173 | } |
174 | 174 | ||
175 | #[derive(Debug, Eq, PartialEq)] | 175 | #[derive(Debug, Eq, PartialEq)] |
176 | pub struct FileMeta { | 176 | pub struct FileMeta { |
177 | pub path: RelativePathBuf, | 177 | pub path: String, |
178 | pub crate_name: Option<String>, | 178 | pub crate_name: Option<String>, |
179 | pub deps: Vec<String>, | 179 | pub deps: Vec<String>, |
180 | pub cfg: CfgOptions, | 180 | pub cfg: CfgOptions, |
@@ -183,7 +183,7 @@ pub struct FileMeta { | |||
183 | } | 183 | } |
184 | 184 | ||
185 | impl FixtureMeta { | 185 | impl FixtureMeta { |
186 | pub fn path(&self) -> &RelativePath { | 186 | pub fn path(&self) -> &str { |
187 | match self { | 187 | match self { |
188 | FixtureMeta::Root { path } => &path, | 188 | FixtureMeta::Root { path } => &path, |
189 | FixtureMeta::File(f) => &f.path, | 189 | FixtureMeta::File(f) => &f.path, |
@@ -292,12 +292,12 @@ fn parse_meta(meta: &str) -> FixtureMeta { | |||
292 | let components = meta.split_ascii_whitespace().collect::<Vec<_>>(); | 292 | let components = meta.split_ascii_whitespace().collect::<Vec<_>>(); |
293 | 293 | ||
294 | if components[0] == "root" { | 294 | if components[0] == "root" { |
295 | let path: RelativePathBuf = components[1].into(); | 295 | let path = components[1].to_string(); |
296 | assert!(path.starts_with("/") && path.ends_with("/")); | 296 | assert!(path.starts_with("/") && path.ends_with("/")); |
297 | return FixtureMeta::Root { path }; | 297 | return FixtureMeta::Root { path }; |
298 | } | 298 | } |
299 | 299 | ||
300 | let path: RelativePathBuf = components[0].into(); | 300 | let path = components[0].to_string(); |
301 | assert!(path.starts_with("/")); | 301 | assert!(path.starts_with("/")); |
302 | 302 | ||
303 | let mut krate = None; | 303 | let mut krate = None; |