From d00a285fa757307bbe0f8dac9e49ac247cf9dab1 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Sat, 10 Aug 2019 16:40:48 +0100 Subject: Initial implementation of Ok-wrapping --- crates/ra_ide_api/src/diagnostics.rs | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'crates/ra_ide_api/src/diagnostics.rs') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index c2b959cb3..be5197767 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -75,6 +75,19 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec severity: Severity::Error, fix: Some(fix), }) + }) + .on::(|d| { + let node = d.ast(db); + let mut builder = TextEditBuilder::default(); + let replacement = format!("Ok({})", node.syntax().text()); + builder.replace(node.syntax().text_range(), replacement); + let fix = SourceChange::source_file_edit_from("wrap with ok", file_id, builder.finish()); + res.borrow_mut().push(Diagnostic { + range: d.highlight_range(), + message: d.message(), + severity: Severity::Error, + fix: Some(fix), + }) }); if let Some(m) = source_binder::module_from_file_id(db, file_id) { m.diagnostics(db, &mut sink); @@ -218,6 +231,43 @@ mod tests { assert_eq!(diagnostics.len(), 0); } + #[test] + fn test_wrap_return_type() { + let before = r#" + enum Result { Ok(T), Err(E) } + struct String { } + + fn div(x: i32, y: i32) -> Result { + if y == 0 { + return Err("div by zero".into()); + } + x / y + } + "#; + let after = r#" + enum Result { Ok(T), Err(E) } + struct String { } + + fn div(x: i32, y: i32) -> Result { + if y == 0 { + return Err("div by zero".into()); + } + Ok(x / y) + } + "#; + check_apply_diagnostic_fix(before, after); + } + + #[test] + fn test_wrap_return_type_not_applicable() { + let content = r#" + fn foo() -> Result { + 0 + } + "#; + check_no_diagnostic(content); + } + #[test] fn test_fill_struct_fields_empty() { let before = r" -- cgit v1.2.3 From d025016f92866a932729394c22603b615cb458df Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Sun, 11 Aug 2019 13:25:36 +0100 Subject: Mock std String and Result types in tests for ok-wrapping diagnostic --- crates/ra_ide_api/src/diagnostics.rs | 62 ++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 14 deletions(-) (limited to 'crates/ra_ide_api/src/diagnostics.rs') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index be5197767..84d2b7fb1 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -187,7 +187,7 @@ mod tests { use ra_syntax::SourceFile; use test_utils::assert_eq_text; - use crate::mock_analysis::single_file; + use crate::mock_analysis::{fixture_with_target_file, single_file}; use super::*; @@ -216,6 +216,15 @@ mod tests { assert_eq_text!(after, &actual); } + fn check_apply_diagnostic_fix_for_target_file(target_file: &str, fixture: &str, after: &str) { + let (analysis, file_id, target_file_contents) = fixture_with_target_file(fixture, target_file); + let diagnostic = analysis.diagnostics(file_id).unwrap().pop().unwrap(); + let mut fix = diagnostic.fix.unwrap(); + let edit = fix.source_file_edits.pop().unwrap().edit; + let actual = edit.apply(&target_file_contents); + assert_eq_text!(after, &actual); + } + fn check_apply_diagnostic_fix(before: &str, after: &str) { let (analysis, file_id) = single_file(before); let diagnostic = analysis.diagnostics(file_id).unwrap().pop().unwrap(); @@ -225,6 +234,12 @@ mod tests { assert_eq_text!(after, &actual); } + fn check_no_diagnostic_for_target_file(target_file: &str, fixture: &str) { + let (analysis, file_id, _) = fixture_with_target_file(fixture, target_file); + let diagnostics = analysis.diagnostics(file_id).unwrap(); + assert_eq!(diagnostics.len(), 0); + } + fn check_no_diagnostic(content: &str) { let (analysis, file_id) = single_file(content); let diagnostics = analysis.diagnostics(file_id).unwrap(); @@ -234,8 +249,8 @@ mod tests { #[test] fn test_wrap_return_type() { let before = r#" - enum Result { Ok(T), Err(E) } - struct String { } + //- /main.rs + use std::{string::String, result::Result::{self, Ok, Err}}; fn div(x: i32, y: i32) -> Result { if y == 0 { @@ -243,29 +258,48 @@ mod tests { } x / y } - "#; - let after = r#" - enum Result { Ok(T), Err(E) } - struct String { } - fn div(x: i32, y: i32) -> Result { - if y == 0 { - return Err("div by zero".into()); - } - Ok(x / y) + //- /std/lib.rs + pub mod string { + pub struct String { } + } + pub mod result { + pub enum Result { Ok(T), Err(E) } } "#; - check_apply_diagnostic_fix(before, after); +// The formatting here is a bit odd due to how the parse_fixture function works in test_utils - +// it strips empty lines and leading whitespace. The important part of this test is that the final +// `x / y` expr is now wrapped in `Ok(..)` + let after = r#"use std::{string::String, result::Result::{self, Ok, Err}}; +fn div(x: i32, y: i32) -> Result { + if y == 0 { + return Err("div by zero".into()); + } + Ok(x / y) +} +"#; + check_apply_diagnostic_fix_for_target_file("/main.rs", before, after); } #[test] fn test_wrap_return_type_not_applicable() { let content = r#" + //- /main.rs + use std::{string::String, result::Result::{self, Ok, Err}}; + fn foo() -> Result { 0 } + + //- /std/lib.rs + pub mod string { + pub struct String { } + } + pub mod result { + pub enum Result { Ok(T), Err(E) } + } "#; - check_no_diagnostic(content); + check_no_diagnostic_for_target_file("/main.rs", content); } #[test] -- cgit v1.2.3 From 62c2002e2b21b3a74a4e2205ccc40fa93f722b34 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Sun, 11 Aug 2019 13:47:33 +0100 Subject: Add test that ok-wrapping handles type aliases --- crates/ra_ide_api/src/diagnostics.rs | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'crates/ra_ide_api/src/diagnostics.rs') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 84d2b7fb1..5e25991c6 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -281,6 +281,44 @@ fn div(x: i32, y: i32) -> Result { check_apply_diagnostic_fix_for_target_file("/main.rs", before, after); } + #[test] + fn test_wrap_return_type_handles_type_aliases() { + let before = r#" + //- /main.rs + use std::{string::String, result::Result::{self, Ok, Err}}; + + type MyResult = Result; + + fn div(x: i32, y: i32) -> MyResult { + if y == 0 { + return Err("div by zero".into()); + } + x / y + } + + //- /std/lib.rs + pub mod string { + pub struct String { } + } + pub mod result { + pub enum Result { Ok(T), Err(E) } + } + "#; +// The formatting here is a bit odd due to how the parse_fixture function works in test_utils - +// it strips empty lines and leading whitespace. The important part of this test is that the final +// `x / y` expr is now wrapped in `Ok(..)` + let after = r#"use std::{string::String, result::Result::{self, Ok, Err}}; +type MyResult = Result; +fn div(x: i32, y: i32) -> MyResult { + if y == 0 { + return Err("div by zero".into()); + } + Ok(x / y) +} +"#; + check_apply_diagnostic_fix_for_target_file("/main.rs", before, after); + } + #[test] fn test_wrap_return_type_not_applicable() { let content = r#" -- cgit v1.2.3 From a40e390860987a23f9b899abc5947f1525d3709c Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Sun, 11 Aug 2019 15:00:37 +0100 Subject: Check type rather than just name in ok-wrapping diagnostic. Add test for handling generic functions (which currently fails) --- crates/ra_ide_api/src/diagnostics.rs | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'crates/ra_ide_api/src/diagnostics.rs') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 5e25991c6..57454719c 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -281,6 +281,43 @@ fn div(x: i32, y: i32) -> Result { check_apply_diagnostic_fix_for_target_file("/main.rs", before, after); } + #[test] + fn test_wrap_return_type_handles_generic_functions() { + let before = r#" + //- /main.rs + use std::{default::Default, result::Result::{self, Ok, Err}}; + + fn div(x: i32) -> Result { + if x == 0 { + return Err(7); + } + T::default() + } + + //- /std/lib.rs + pub mod result { + pub enum Result { Ok(T), Err(E) } + } + pub mod default { + pub trait Default { + fn default() -> Self; + } + } + "#; +// The formatting here is a bit odd due to how the parse_fixture function works in test_utils - +// it strips empty lines and leading whitespace. The important part of this test is that the final +// `x / y` expr is now wrapped in `Ok(..)` + let after = r#"use std::{default::Default, result::Result::{self, Ok, Err}}; +fn div(x: i32) -> Result { + if x == 0 { + return Err(7); + } + Ok(T::default()) +} +"#; + check_apply_diagnostic_fix_for_target_file("/main.rs", before, after); + } + #[test] fn test_wrap_return_type_handles_type_aliases() { let before = r#" -- cgit v1.2.3 From 456e72c4e472d9ea0717c64a75bd02e94b6e90e8 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Sun, 11 Aug 2019 15:03:27 +0100 Subject: Change test to not rely on trait inference --- crates/ra_ide_api/src/diagnostics.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'crates/ra_ide_api/src/diagnostics.rs') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 57454719c..9841fbdf3 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -285,34 +285,29 @@ fn div(x: i32, y: i32) -> Result { fn test_wrap_return_type_handles_generic_functions() { let before = r#" //- /main.rs - use std::{default::Default, result::Result::{self, Ok, Err}}; + use std::result::Result::{self, Ok, Err}; - fn div(x: i32) -> Result { + fn div(x: T) -> Result { if x == 0 { return Err(7); } - T::default() + x } //- /std/lib.rs pub mod result { pub enum Result { Ok(T), Err(E) } } - pub mod default { - pub trait Default { - fn default() -> Self; - } - } "#; // The formatting here is a bit odd due to how the parse_fixture function works in test_utils - // it strips empty lines and leading whitespace. The important part of this test is that the final -// `x / y` expr is now wrapped in `Ok(..)` - let after = r#"use std::{default::Default, result::Result::{self, Ok, Err}}; -fn div(x: i32) -> Result { +// expr is now wrapped in `Ok(..)` + let after = r#"use std::result::Result::{self, Ok, Err}; +fn div(x: T) -> Result { if x == 0 { return Err(7); } - Ok(T::default()) + Ok(x) } "#; check_apply_diagnostic_fix_for_target_file("/main.rs", before, after); -- cgit v1.2.3 From 4f6f3933ec04df55a39368d591c91cf81d980237 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Sun, 11 Aug 2019 15:04:08 +0100 Subject: cargo format --- crates/ra_ide_api/src/diagnostics.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'crates/ra_ide_api/src/diagnostics.rs') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 9841fbdf3..0b9bb5a66 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -217,7 +217,8 @@ mod tests { } fn check_apply_diagnostic_fix_for_target_file(target_file: &str, fixture: &str, after: &str) { - let (analysis, file_id, target_file_contents) = fixture_with_target_file(fixture, target_file); + let (analysis, file_id, target_file_contents) = + fixture_with_target_file(fixture, target_file); let diagnostic = analysis.diagnostics(file_id).unwrap().pop().unwrap(); let mut fix = diagnostic.fix.unwrap(); let edit = fix.source_file_edits.pop().unwrap().edit; @@ -267,9 +268,9 @@ mod tests { pub enum Result { Ok(T), Err(E) } } "#; -// The formatting here is a bit odd due to how the parse_fixture function works in test_utils - -// it strips empty lines and leading whitespace. The important part of this test is that the final -// `x / y` expr is now wrapped in `Ok(..)` + // The formatting here is a bit odd due to how the parse_fixture function works in test_utils - + // it strips empty lines and leading whitespace. The important part of this test is that the final + // `x / y` expr is now wrapped in `Ok(..)` let after = r#"use std::{string::String, result::Result::{self, Ok, Err}}; fn div(x: i32, y: i32) -> Result { if y == 0 { @@ -299,9 +300,9 @@ fn div(x: i32, y: i32) -> Result { pub enum Result { Ok(T), Err(E) } } "#; -// The formatting here is a bit odd due to how the parse_fixture function works in test_utils - -// it strips empty lines and leading whitespace. The important part of this test is that the final -// expr is now wrapped in `Ok(..)` + // The formatting here is a bit odd due to how the parse_fixture function works in test_utils - + // it strips empty lines and leading whitespace. The important part of this test is that the final + // expr is now wrapped in `Ok(..)` let after = r#"use std::result::Result::{self, Ok, Err}; fn div(x: T) -> Result { if x == 0 { @@ -336,9 +337,9 @@ fn div(x: T) -> Result { pub enum Result { Ok(T), Err(E) } } "#; -// The formatting here is a bit odd due to how the parse_fixture function works in test_utils - -// it strips empty lines and leading whitespace. The important part of this test is that the final -// `x / y` expr is now wrapped in `Ok(..)` + // The formatting here is a bit odd due to how the parse_fixture function works in test_utils - + // it strips empty lines and leading whitespace. The important part of this test is that the final + // `x / y` expr is now wrapped in `Ok(..)` let after = r#"use std::{string::String, result::Result::{self, Ok, Err}}; type MyResult = Result; fn div(x: i32, y: i32) -> MyResult { -- cgit v1.2.3 From 6620949caee25128416acf285590b0d5558e4597 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Sat, 17 Aug 2019 06:31:53 +0100 Subject: Simplify checking return type, add new test --- crates/ra_ide_api/src/diagnostics.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide_api/src/diagnostics.rs') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 0b9bb5a66..94424dc16 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -79,7 +79,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec .on::(|d| { let node = d.ast(db); let mut builder = TextEditBuilder::default(); - let replacement = format!("Ok({})", node.syntax().text()); + let replacement = format!("Ok({})", node.syntax()); builder.replace(node.syntax().text_range(), replacement); let fix = SourceChange::source_file_edit_from("wrap with ok", file_id, builder.finish()); res.borrow_mut().push(Diagnostic { @@ -353,7 +353,7 @@ fn div(x: i32, y: i32) -> MyResult { } #[test] - fn test_wrap_return_type_not_applicable() { + fn test_wrap_return_type_not_applicable_when_expr_type_does_not_match_ok_type() { let content = r#" //- /main.rs use std::{string::String, result::Result::{self, Ok, Err}}; @@ -373,6 +373,32 @@ fn div(x: i32, y: i32) -> MyResult { check_no_diagnostic_for_target_file("/main.rs", content); } + #[test] + fn test_wrap_return_type_not_applicable_when_return_type_is_not_result() { + let content = r#" + //- /main.rs + use std::{string::String, result::Result::{self, Ok, Err}}; + + enum SomeOtherEnum { + Ok(i32), + Err(String), + } + + fn foo() -> SomeOtherEnum { + 0 + } + + //- /std/lib.rs + pub mod string { + pub struct String { } + } + pub mod result { + pub enum Result { Ok(T), Err(E) } + } + "#; + check_no_diagnostic_for_target_file("/main.rs", content); + } + #[test] fn test_fill_struct_fields_empty() { let before = r" -- cgit v1.2.3 From 59dd30402b7a1924a0f49d1c902e799654a54f5a Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Sat, 17 Aug 2019 07:12:50 +0100 Subject: Specify cursor position in ok-wrapping tests, and switch to using analysis_and_position function --- crates/ra_ide_api/src/diagnostics.rs | 49 +++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'crates/ra_ide_api/src/diagnostics.rs') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 94424dc16..4e1f47db6 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -187,7 +187,7 @@ mod tests { use ra_syntax::SourceFile; use test_utils::assert_eq_text; - use crate::mock_analysis::{fixture_with_target_file, single_file}; + use crate::mock_analysis::{analysis_and_position, single_file}; use super::*; @@ -216,14 +216,25 @@ mod tests { assert_eq_text!(after, &actual); } - fn check_apply_diagnostic_fix_for_target_file(target_file: &str, fixture: &str, after: &str) { - let (analysis, file_id, target_file_contents) = - fixture_with_target_file(fixture, target_file); - let diagnostic = analysis.diagnostics(file_id).unwrap().pop().unwrap(); + /// Takes a multi-file input fixture with annotated cursor positions, + /// and checks that: + /// * a diagnostic is produced + /// * this diagnostic touches the input cursor position + /// * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied + fn check_apply_diagnostic_fix_from_position(fixture: &str, after: &str) { + let (analysis, file_position) = analysis_and_position(fixture); + let diagnostic = analysis.diagnostics(file_position.file_id).unwrap().pop().unwrap(); let mut fix = diagnostic.fix.unwrap(); let edit = fix.source_file_edits.pop().unwrap().edit; + let target_file_contents = analysis.file_text(file_position.file_id).unwrap(); let actual = edit.apply(&target_file_contents); assert_eq_text!(after, &actual); + assert!( + diagnostic.range.start() <= file_position.offset && diagnostic.range.end() >= file_position.offset, + "diagnostic range {} does not touch cursor position {}", + diagnostic.range, + file_position.offset + ); } fn check_apply_diagnostic_fix(before: &str, after: &str) { @@ -235,9 +246,11 @@ mod tests { assert_eq_text!(after, &actual); } - fn check_no_diagnostic_for_target_file(target_file: &str, fixture: &str) { - let (analysis, file_id, _) = fixture_with_target_file(fixture, target_file); - let diagnostics = analysis.diagnostics(file_id).unwrap(); + /// Takes a multi-file input fixture with annotated cursor position and checks that no diagnostics + /// apply to the file containing the cursor. + fn check_no_diagnostic_for_target_file(fixture: &str) { + let (analysis, file_position) = analysis_and_position(fixture); + let diagnostics = analysis.diagnostics(file_position.file_id).unwrap(); assert_eq!(diagnostics.len(), 0); } @@ -257,7 +270,7 @@ mod tests { if y == 0 { return Err("div by zero".into()); } - x / y + x / y<|> } //- /std/lib.rs @@ -279,7 +292,7 @@ fn div(x: i32, y: i32) -> Result { Ok(x / y) } "#; - check_apply_diagnostic_fix_for_target_file("/main.rs", before, after); + check_apply_diagnostic_fix_from_position(before, after); } #[test] @@ -292,7 +305,7 @@ fn div(x: i32, y: i32) -> Result { if x == 0 { return Err(7); } - x + <|>x } //- /std/lib.rs @@ -311,7 +324,7 @@ fn div(x: T) -> Result { Ok(x) } "#; - check_apply_diagnostic_fix_for_target_file("/main.rs", before, after); + check_apply_diagnostic_fix_from_position(before, after); } #[test] @@ -326,7 +339,7 @@ fn div(x: T) -> Result { if y == 0 { return Err("div by zero".into()); } - x / y + x <|>/ y } //- /std/lib.rs @@ -349,7 +362,7 @@ fn div(x: i32, y: i32) -> MyResult { Ok(x / y) } "#; - check_apply_diagnostic_fix_for_target_file("/main.rs", before, after); + check_apply_diagnostic_fix_from_position(before, after); } #[test] @@ -359,7 +372,7 @@ fn div(x: i32, y: i32) -> MyResult { use std::{string::String, result::Result::{self, Ok, Err}}; fn foo() -> Result { - 0 + 0<|> } //- /std/lib.rs @@ -370,7 +383,7 @@ fn div(x: i32, y: i32) -> MyResult { pub enum Result { Ok(T), Err(E) } } "#; - check_no_diagnostic_for_target_file("/main.rs", content); + check_no_diagnostic_for_target_file(content); } #[test] @@ -385,7 +398,7 @@ fn div(x: i32, y: i32) -> MyResult { } fn foo() -> SomeOtherEnum { - 0 + 0<|> } //- /std/lib.rs @@ -396,7 +409,7 @@ fn div(x: i32, y: i32) -> MyResult { pub enum Result { Ok(T), Err(E) } } "#; - check_no_diagnostic_for_target_file("/main.rs", content); + check_no_diagnostic_for_target_file(content); } #[test] -- cgit v1.2.3 From 14a23d1bde8493df9e5196973132144060a61709 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Sat, 17 Aug 2019 07:37:37 +0100 Subject: Strip indents and empty lines in check_apply_diagnostic_fix_from_position --- crates/ra_ide_api/src/diagnostics.rs | 91 ++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 36 deletions(-) (limited to 'crates/ra_ide_api/src/diagnostics.rs') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 4e1f47db6..1a4882824 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -184,6 +184,7 @@ fn check_struct_shorthand_initialization( #[cfg(test)] mod tests { use insta::assert_debug_snapshot_matches; + use join_to_string::join; use ra_syntax::SourceFile; use test_utils::assert_eq_text; @@ -228,9 +229,30 @@ mod tests { let edit = fix.source_file_edits.pop().unwrap().edit; let target_file_contents = analysis.file_text(file_position.file_id).unwrap(); let actual = edit.apply(&target_file_contents); - assert_eq_text!(after, &actual); + + // Strip indent and empty lines from `after`, to match the behaviour of + // `parse_fixture` called from `analysis_and_position`. + let margin = fixture + .lines() + .filter(|it| it.trim_start().starts_with("//-")) + .map(|it| it.len() - it.trim_start().len()) + .next() + .expect("empty fixture"); + let after = join(after.lines().filter_map(|line| { + if line.len() > margin { + Some(&line[margin..]) + } else { + None + } + })) + .separator("\n") + .suffix("\n") + .to_string(); + + assert_eq_text!(&after, &actual); assert!( - diagnostic.range.start() <= file_position.offset && diagnostic.range.end() >= file_position.offset, + diagnostic.range.start() <= file_position.offset + && diagnostic.range.end() >= file_position.offset, "diagnostic range {} does not touch cursor position {}", diagnostic.range, file_position.offset @@ -281,17 +303,16 @@ mod tests { pub enum Result { Ok(T), Err(E) } } "#; - // The formatting here is a bit odd due to how the parse_fixture function works in test_utils - - // it strips empty lines and leading whitespace. The important part of this test is that the final - // `x / y` expr is now wrapped in `Ok(..)` - let after = r#"use std::{string::String, result::Result::{self, Ok, Err}}; -fn div(x: i32, y: i32) -> Result { - if y == 0 { - return Err("div by zero".into()); - } - Ok(x / y) -} -"#; + let after = r#" + use std::{string::String, result::Result::{self, Ok, Err}}; + + fn div(x: i32, y: i32) -> Result { + if y == 0 { + return Err("div by zero".into()); + } + Ok(x / y) + } + "#; check_apply_diagnostic_fix_from_position(before, after); } @@ -313,17 +334,16 @@ fn div(x: i32, y: i32) -> Result { pub enum Result { Ok(T), Err(E) } } "#; - // The formatting here is a bit odd due to how the parse_fixture function works in test_utils - - // it strips empty lines and leading whitespace. The important part of this test is that the final - // expr is now wrapped in `Ok(..)` - let after = r#"use std::result::Result::{self, Ok, Err}; -fn div(x: T) -> Result { - if x == 0 { - return Err(7); - } - Ok(x) -} -"#; + let after = r#" + use std::result::Result::{self, Ok, Err}; + + fn div(x: T) -> Result { + if x == 0 { + return Err(7); + } + Ok(x) + } + "#; check_apply_diagnostic_fix_from_position(before, after); } @@ -350,18 +370,17 @@ fn div(x: T) -> Result { pub enum Result { Ok(T), Err(E) } } "#; - // The formatting here is a bit odd due to how the parse_fixture function works in test_utils - - // it strips empty lines and leading whitespace. The important part of this test is that the final - // `x / y` expr is now wrapped in `Ok(..)` - let after = r#"use std::{string::String, result::Result::{self, Ok, Err}}; -type MyResult = Result; -fn div(x: i32, y: i32) -> MyResult { - if y == 0 { - return Err("div by zero".into()); - } - Ok(x / y) -} -"#; + let after = r#" + use std::{string::String, result::Result::{self, Ok, Err}}; + + type MyResult = Result; + fn div(x: i32, y: i32) -> MyResult { + if y == 0 { + return Err("div by zero".into()); + } + Ok(x / y) + } + "#; check_apply_diagnostic_fix_from_position(before, after); } -- cgit v1.2.3