From 5e7a1a12038f822df346c65de613cc01882ce656 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 24 Jun 2020 12:01:17 +0200 Subject: Simplify --- crates/ra_ide/src/diagnostics.rs | 6 ++--- crates/ra_ide/src/mock_analysis.rs | 55 +++++++++++++++++--------------------- crates/ra_ide/src/parent_module.rs | 12 ++++----- crates/ra_ide/src/syntax_tree.rs | 12 ++++----- 4 files changed, 38 insertions(+), 47 deletions(-) (limited to 'crates') 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 { impl Expr { fn new_bin(lhs: Box, rhs: Box) -> Expr { - Expr::Bin { <|> } + Expr::Bin { } } } - "; let after = r" enum Expr { @@ -585,10 +584,9 @@ mod tests { impl Expr { fn new_bin(lhs: Box, rhs: Box) -> Expr { - Expr::Bin { lhs: (), rhs: () <|> } + Expr::Bin { lhs: (), rhs: () } } } - "; check_apply_diagnostic_fix(before, after); } 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}; use ra_cfg::CfgOptions; use ra_db::{CrateName, Env, FileSet, SourceRoot, VfsPath}; -use test_utils::{extract_offset, extract_range, Fixture, CURSOR_MARKER}; +use test_utils::{extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER}; use crate::{ Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange, @@ -11,27 +11,18 @@ use crate::{ #[derive(Debug)] enum MockFileData { - Plain { path: String, content: String }, Fixture(Fixture), } impl MockFileData { - fn new(path: String, content: String) -> Self { - // `Self::Plain` causes a false warning: 'variant is never constructed: `Plain` ' - // see https://github.com/rust-lang/rust/issues/69018 - MockFileData::Plain { path, content } - } - fn path(&self) -> &str { match self { - MockFileData::Plain { path, .. } => path.as_str(), MockFileData::Fixture(f) => f.path.as_str(), } } fn content(&self) -> &str { match self { - MockFileData::Plain { content, .. } => content, MockFileData::Fixture(f) => f.text.as_str(), } } @@ -44,7 +35,6 @@ impl MockFileData { f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into())); cfg } - _ => CfgOptions::default(), } } @@ -53,14 +43,12 @@ impl MockFileData { MockFileData::Fixture(f) => { f.edition.as_ref().map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()) } - _ => Edition::Edition2018, } } fn env(&self) -> Env { match self { MockFileData::Fixture(f) => Env::from(f.env.iter()), - _ => Env::default(), } } } @@ -89,31 +77,38 @@ impl MockAnalysis { /// //- /foo.rs /// struct Baz; /// ``` - pub fn with_files(fixture: &str) -> MockAnalysis { - let mut res = MockAnalysis::default(); - for entry in Fixture::parse(fixture) { - res.add_file_fixture(entry); - } + pub fn with_files(ra_fixture: &str) -> MockAnalysis { + let (res, pos) = MockAnalysis::with_fixture(ra_fixture); + assert!(pos.is_none()); res } /// Same as `with_files`, but requires that a single file contains a `<|>` marker, /// whose position is also returned. pub fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) { + let (res, position) = MockAnalysis::with_fixture(fixture); + let (file_id, range_or_offset) = position.expect("expected a marker (<|>)"); + let offset = match range_or_offset { + RangeOrOffset::Range(_) => panic!(), + RangeOrOffset::Offset(it) => it, + }; + (res, FilePosition { file_id, offset }) + } + + fn with_fixture(fixture: &str) -> (MockAnalysis, Option<(FileId, RangeOrOffset)>) { let mut position = None; let mut res = MockAnalysis::default(); for mut entry in Fixture::parse(fixture) { if entry.text.contains(CURSOR_MARKER) { assert!(position.is_none(), "only one marker (<|>) per fixture is allowed"); - let (offset, text) = extract_offset(&entry.text); + let (range_or_offset, text) = extract_range_or_offset(&entry.text); entry.text = text; let file_id = res.add_file_fixture(entry); - position = Some(FilePosition { file_id, offset }); + position = Some((file_id, range_or_offset)); } else { res.add_file_fixture(entry); } } - let position = position.expect("expected a marker (<|>)"); (res, position) } @@ -123,12 +118,6 @@ impl MockAnalysis { file_id } - fn add_file_with_range(&mut self, path: &str, text: &str) -> FileRange { - let (range, text) = extract_range(text); - let file_id = self.next_id(); - self.files.push(MockFileData::new(path.to_string(), text)); - FileRange { file_id, range } - } pub fn id_of(&self, path: &str) -> FileId { let (idx, _) = self .files @@ -209,8 +198,12 @@ pub fn single_file(ra_fixture: &str) -> (Analysis, FileId) { } /// Creates analysis for a single file, returns range marked with a pair of <|>. -pub fn single_file_with_range(ra_fixture: &str) -> (Analysis, FileRange) { - let mut mock = MockAnalysis::default(); - let pos = mock.add_file_with_range("/main.rs", ra_fixture); - (mock.analysis(), pos) +pub fn analysis_and_range(ra_fixture: &str) -> (Analysis, FileRange) { + let (res, position) = MockAnalysis::with_fixture(ra_fixture); + let (file_id, range_or_offset) = position.expect("expected a marker (<|>)"); + let range = match range_or_offset { + RangeOrOffset::Range(it) => it, + RangeOrOffset::Offset(_) => panic!(), + }; + (res.analysis(), FileRange { file_id, range }) } 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 { #[test] fn test_resolve_crate_root() { let mock = MockAnalysis::with_files( - " - //- /bar.rs - mod foo; - //- /foo.rs - // empty <|> - ", + r#" +//- /bar.rs +mod foo; +//- /foo.rs +// empty +"#, ); let root_file = mock.id_of("/bar.rs"); 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) -> Optionfn foo() {}<|>"#.trim()); + let (analysis, range) = analysis_and_range(r#"<|>fn foo() {}<|>"#.trim()); let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap(); assert_eq_text!( @@ -206,7 +206,7 @@ FN_DEF@0..11 .trim() ); - let (analysis, range) = single_file_with_range( + let (analysis, range) = analysis_and_range( r#"fn test() { <|>assert!(" fn foo() { @@ -242,7 +242,7 @@ EXPR_STMT@16..58 #[test] fn test_syntax_tree_inside_string() { - let (analysis, range) = single_file_with_range( + let (analysis, range) = analysis_and_range( r#"fn test() { assert!(" <|>fn foo() { @@ -276,7 +276,7 @@ SOURCE_FILE@0..12 ); // With a raw string - let (analysis, range) = single_file_with_range( + let (analysis, range) = analysis_and_range( r###"fn test() { assert!(r#" <|>fn foo() { @@ -310,7 +310,7 @@ SOURCE_FILE@0..12 ); // With a raw string - let (analysis, range) = single_file_with_range( + let (analysis, range) = analysis_and_range( r###"fn test() { assert!(r<|>#" fn foo() { -- cgit v1.2.3