Skip to content

Commit 14a23d1

Browse files
theotherphilmatklad
authored andcommitted
Strip indents and empty lines in check_apply_diagnostic_fix_from_position
1 parent 59dd304 commit 14a23d1

File tree

1 file changed

+55
-36
lines changed

1 file changed

+55
-36
lines changed

crates/ra_ide_api/src/diagnostics.rs

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ fn check_struct_shorthand_initialization(
184184
#[cfg(test)]
185185
mod tests {
186186
use insta::assert_debug_snapshot_matches;
187+
use join_to_string::join;
187188
use ra_syntax::SourceFile;
188189
use test_utils::assert_eq_text;
189190

@@ -228,9 +229,30 @@ mod tests {
228229
let edit = fix.source_file_edits.pop().unwrap().edit;
229230
let target_file_contents = analysis.file_text(file_position.file_id).unwrap();
230231
let actual = edit.apply(&target_file_contents);
231-
assert_eq_text!(after, &actual);
232+
233+
// Strip indent and empty lines from `after`, to match the behaviour of
234+
// `parse_fixture` called from `analysis_and_position`.
235+
let margin = fixture
236+
.lines()
237+
.filter(|it| it.trim_start().starts_with("//-"))
238+
.map(|it| it.len() - it.trim_start().len())
239+
.next()
240+
.expect("empty fixture");
241+
let after = join(after.lines().filter_map(|line| {
242+
if line.len() > margin {
243+
Some(&line[margin..])
244+
} else {
245+
None
246+
}
247+
}))
248+
.separator("\n")
249+
.suffix("\n")
250+
.to_string();
251+
252+
assert_eq_text!(&after, &actual);
232253
assert!(
233-
diagnostic.range.start() <= file_position.offset && diagnostic.range.end() >= file_position.offset,
254+
diagnostic.range.start() <= file_position.offset
255+
&& diagnostic.range.end() >= file_position.offset,
234256
"diagnostic range {} does not touch cursor position {}",
235257
diagnostic.range,
236258
file_position.offset
@@ -281,17 +303,16 @@ mod tests {
281303
pub enum Result<T, E> { Ok(T), Err(E) }
282304
}
283305
"#;
284-
// The formatting here is a bit odd due to how the parse_fixture function works in test_utils -
285-
// it strips empty lines and leading whitespace. The important part of this test is that the final
286-
// `x / y` expr is now wrapped in `Ok(..)`
287-
let after = r#"use std::{string::String, result::Result::{self, Ok, Err}};
288-
fn div(x: i32, y: i32) -> Result<i32, String> {
289-
if y == 0 {
290-
return Err("div by zero".into());
291-
}
292-
Ok(x / y)
293-
}
294-
"#;
306+
let after = r#"
307+
use std::{string::String, result::Result::{self, Ok, Err}};
308+
309+
fn div(x: i32, y: i32) -> Result<i32, String> {
310+
if y == 0 {
311+
return Err("div by zero".into());
312+
}
313+
Ok(x / y)
314+
}
315+
"#;
295316
check_apply_diagnostic_fix_from_position(before, after);
296317
}
297318

@@ -313,17 +334,16 @@ fn div(x: i32, y: i32) -> Result<i32, String> {
313334
pub enum Result<T, E> { Ok(T), Err(E) }
314335
}
315336
"#;
316-
// The formatting here is a bit odd due to how the parse_fixture function works in test_utils -
317-
// it strips empty lines and leading whitespace. The important part of this test is that the final
318-
// expr is now wrapped in `Ok(..)`
319-
let after = r#"use std::result::Result::{self, Ok, Err};
320-
fn div<T>(x: T) -> Result<T, i32> {
321-
if x == 0 {
322-
return Err(7);
323-
}
324-
Ok(x)
325-
}
326-
"#;
337+
let after = r#"
338+
use std::result::Result::{self, Ok, Err};
339+
340+
fn div<T>(x: T) -> Result<T, i32> {
341+
if x == 0 {
342+
return Err(7);
343+
}
344+
Ok(x)
345+
}
346+
"#;
327347
check_apply_diagnostic_fix_from_position(before, after);
328348
}
329349

@@ -350,18 +370,17 @@ fn div<T>(x: T) -> Result<T, i32> {
350370
pub enum Result<T, E> { Ok(T), Err(E) }
351371
}
352372
"#;
353-
// The formatting here is a bit odd due to how the parse_fixture function works in test_utils -
354-
// it strips empty lines and leading whitespace. The important part of this test is that the final
355-
// `x / y` expr is now wrapped in `Ok(..)`
356-
let after = r#"use std::{string::String, result::Result::{self, Ok, Err}};
357-
type MyResult<T> = Result<T, String>;
358-
fn div(x: i32, y: i32) -> MyResult<i32> {
359-
if y == 0 {
360-
return Err("div by zero".into());
361-
}
362-
Ok(x / y)
363-
}
364-
"#;
373+
let after = r#"
374+
use std::{string::String, result::Result::{self, Ok, Err}};
375+
376+
type MyResult<T> = Result<T, String>;
377+
fn div(x: i32, y: i32) -> MyResult<i32> {
378+
if y == 0 {
379+
return Err("div by zero".into());
380+
}
381+
Ok(x / y)
382+
}
383+
"#;
365384
check_apply_diagnostic_fix_from_position(before, after);
366385
}
367386

0 commit comments

Comments
 (0)