Skip to content

Commit 6ea393a

Browse files
committed
Addressed code review
replaced match with let-if variable assignment removed the unnecessary semicolon_on_end variable converted all code and expected test variables to raw strings and inlined them in asserts
1 parent 6ea28c3 commit 6ea393a

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

crates/ra_assists/src/handlers/remove_dbg.rs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,26 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2727
return None;
2828
}
2929

30-
let semicolon_on_end = macro_call.semicolon_token().is_some();
3130
let is_leaf = macro_call.syntax().next_sibling().is_none();
3231

33-
let macro_end = match semicolon_on_end {
34-
true => macro_call.syntax().text_range().end() - TextSize::of(';'),
35-
false => macro_call.syntax().text_range().end(),
32+
let macro_end = if macro_call.semicolon_token().is_some() {
33+
macro_call.syntax().text_range().end() - TextSize::of(';')
34+
} else {
35+
macro_call.syntax().text_range().end()
3636
};
3737

3838
// macro_range determines what will be deleted and replaced with macro_content
3939
let macro_range = TextRange::new(macro_call.syntax().text_range().start(), macro_end);
4040
let paste_instead_of_dbg = {
4141
let text = macro_call.token_tree()?.syntax().text();
4242

43-
// leafines determines if we should include the parenthesis or not
44-
let slice_index: TextRange = match is_leaf {
43+
// leafiness determines if we should include the parenthesis or not
44+
let slice_index: TextRange = if is_leaf {
4545
// leaf means - we can extract the contents of the dbg! in text
46-
true => TextRange::new(TextSize::of('('), text.len() - TextSize::of(')')),
46+
TextRange::new(TextSize::of('('), text.len() - TextSize::of(')'))
47+
} else {
4748
// not leaf - means we should keep the parens
48-
false => TextRange::new(TextSize::from(0 as u32), text.len()),
49+
TextRange::up_to(text.len())
4950
};
5051
text.slice(slice_index).to_string()
5152
};
@@ -147,45 +148,58 @@ fn foo(n: usize) {
147148
// not quite though
148149
// adding a comment at the end of the line makes
149150
// the ast::MacroCall to include the semicolon at the end
150-
let code = "
151-
let res = <|>dbg!(1 * 20); // needless comment
152-
";
153-
let expected = "
154-
let res = 1 * 20; // needless comment
155-
";
156-
check_assist(remove_dbg, code, expected);
151+
check_assist(
152+
remove_dbg,
153+
r#"let res = <|>dbg!(1 * 20); // needless comment"#,
154+
r#"let res = 1 * 20; // needless comment"#,
155+
);
157156
}
158157

159158
#[test]
160159
fn test_remove_dbg_keep_expression() {
161-
let code = "
162-
let res = <|>dbg!(a + b).foo();";
163-
let expected = "let res = (a + b).foo();";
164-
check_assist(remove_dbg, code, expected);
160+
check_assist(
161+
remove_dbg,
162+
r#"let res = <|>dbg!(a + b).foo();"#,
163+
r#"let res = (a + b).foo();"#,
164+
);
165165
}
166166

167167
#[test]
168168
fn test_remove_dbg_from_inside_fn() {
169-
let code = "
169+
check_assist_target(
170+
remove_dbg,
171+
r#"
170172
fn square(x: u32) -> u32 {
171173
x * x
172174
}
173175
174176
fn main() {
175177
let x = square(dbg<|>!(5 + 10));
176-
println!(\"{}\", x);
177-
}";
178+
println!("{}", x);
179+
}"#,
180+
"dbg!(5 + 10)",
181+
);
178182

179-
let expected = "
183+
check_assist(
184+
remove_dbg,
185+
r#"
186+
fn square(x: u32) -> u32 {
187+
x * x
188+
}
189+
190+
fn main() {
191+
let x = square(dbg<|>!(5 + 10));
192+
println!("{}", x);
193+
}"#,
194+
r#"
180195
fn square(x: u32) -> u32 {
181196
x * x
182197
}
183198
184199
fn main() {
185200
let x = square(5 + 10);
186-
println!(\"{}\", x);
187-
}";
188-
check_assist_target(remove_dbg, code, "dbg!(5 + 10)");
189-
check_assist(remove_dbg, code, expected);
201+
println!("{}", x);
202+
}"#,
203+
);
190204
}
191205
}

0 commit comments

Comments
 (0)