mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
Strip indents and empty lines in check_apply_diagnostic_fix_from_position
This commit is contained in:
parent
59dd30402b
commit
14a23d1bde
1 changed files with 55 additions and 36 deletions
|
@ -184,6 +184,7 @@ fn check_struct_shorthand_initialization(
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use insta::assert_debug_snapshot_matches;
|
use insta::assert_debug_snapshot_matches;
|
||||||
|
use join_to_string::join;
|
||||||
use ra_syntax::SourceFile;
|
use ra_syntax::SourceFile;
|
||||||
use test_utils::assert_eq_text;
|
use test_utils::assert_eq_text;
|
||||||
|
|
||||||
|
@ -228,9 +229,30 @@ mod tests {
|
||||||
let edit = fix.source_file_edits.pop().unwrap().edit;
|
let edit = fix.source_file_edits.pop().unwrap().edit;
|
||||||
let target_file_contents = analysis.file_text(file_position.file_id).unwrap();
|
let target_file_contents = analysis.file_text(file_position.file_id).unwrap();
|
||||||
let actual = edit.apply(&target_file_contents);
|
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!(
|
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 {} does not touch cursor position {}",
|
||||||
diagnostic.range,
|
diagnostic.range,
|
||||||
file_position.offset
|
file_position.offset
|
||||||
|
@ -281,17 +303,16 @@ mod tests {
|
||||||
pub enum Result<T, E> { Ok(T), Err(E) }
|
pub enum Result<T, E> { Ok(T), Err(E) }
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
// The formatting here is a bit odd due to how the parse_fixture function works in test_utils -
|
let after = r#"
|
||||||
// it strips empty lines and leading whitespace. The important part of this test is that the final
|
use std::{string::String, result::Result::{self, Ok, Err}};
|
||||||
// `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<i32, String> {
|
||||||
fn div(x: i32, y: i32) -> Result<i32, String> {
|
|
||||||
if y == 0 {
|
if y == 0 {
|
||||||
return Err("div by zero".into());
|
return Err("div by zero".into());
|
||||||
}
|
}
|
||||||
Ok(x / y)
|
Ok(x / y)
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
check_apply_diagnostic_fix_from_position(before, after);
|
check_apply_diagnostic_fix_from_position(before, after);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -313,17 +334,16 @@ fn div(x: i32, y: i32) -> Result<i32, String> {
|
||||||
pub enum Result<T, E> { Ok(T), Err(E) }
|
pub enum Result<T, E> { Ok(T), Err(E) }
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
// The formatting here is a bit odd due to how the parse_fixture function works in test_utils -
|
let after = r#"
|
||||||
// it strips empty lines and leading whitespace. The important part of this test is that the final
|
use std::result::Result::{self, Ok, Err};
|
||||||
// expr is now wrapped in `Ok(..)`
|
|
||||||
let after = r#"use std::result::Result::{self, Ok, Err};
|
fn div<T>(x: T) -> Result<T, i32> {
|
||||||
fn div<T>(x: T) -> Result<T, i32> {
|
|
||||||
if x == 0 {
|
if x == 0 {
|
||||||
return Err(7);
|
return Err(7);
|
||||||
}
|
}
|
||||||
Ok(x)
|
Ok(x)
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
check_apply_diagnostic_fix_from_position(before, after);
|
check_apply_diagnostic_fix_from_position(before, after);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -350,18 +370,17 @@ fn div<T>(x: T) -> Result<T, i32> {
|
||||||
pub enum Result<T, E> { Ok(T), Err(E) }
|
pub enum Result<T, E> { Ok(T), Err(E) }
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
// The formatting here is a bit odd due to how the parse_fixture function works in test_utils -
|
let after = r#"
|
||||||
// it strips empty lines and leading whitespace. The important part of this test is that the final
|
use std::{string::String, result::Result::{self, Ok, Err}};
|
||||||
// `x / y` expr is now wrapped in `Ok(..)`
|
|
||||||
let after = r#"use std::{string::String, result::Result::{self, Ok, Err}};
|
type MyResult<T> = Result<T, String>;
|
||||||
type MyResult<T> = Result<T, String>;
|
fn div(x: i32, y: i32) -> MyResult<i32> {
|
||||||
fn div(x: i32, y: i32) -> MyResult<i32> {
|
|
||||||
if y == 0 {
|
if y == 0 {
|
||||||
return Err("div by zero".into());
|
return Err("div by zero".into());
|
||||||
}
|
}
|
||||||
Ok(x / y)
|
Ok(x / y)
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
check_apply_diagnostic_fix_from_position(before, after);
|
check_apply_diagnostic_fix_from_position(before, after);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue