Skip to content

Commit 5e926a4

Browse files
committed
don't try to unwrap functions with comments in
1 parent 5c6aa88 commit 5e926a4

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

compiler-core/src/language_server/code_action.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8411,6 +8411,15 @@ impl<'a> UnwrapAnonymousFunction<'a> {
84118411
_ => return,
84128412
}
84138413

8414+
// We can't apply to functions with comments in (yet)
8415+
if self
8416+
.module
8417+
.extra
8418+
.has_comment_between(location.start, location.end)
8419+
{
8420+
return;
8421+
}
8422+
84148423
// We can only apply to anonymous functions containing a single function call
84158424
let [
84168425
TypedStatement::Expression(TypedExpr::Call {

compiler-core/src/language_server/tests/action.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10059,7 +10059,43 @@ fn op(first a, second b) {
1005910059
);
1006010060
}
1006110061

10062+
#[test]
10063+
fn dont_unwrap_anonymous_function_with_comment_after() {
10064+
assert_no_code_actions!(
10065+
UNWRAP_ANONYMOUS_FUNCTION,
10066+
"pub fn main() {
10067+
fn(a) {
10068+
op(a)
10069+
// look out!
10070+
}
10071+
}
10072+
10073+
fn op(a) {
10074+
todo
10075+
}
10076+
",
10077+
find_position_of("fn(a)").to_selection()
10078+
);
10079+
}
1006210080

10081+
#[test]
10082+
fn dont_unwrap_anonymous_function_with_comment_before() {
10083+
assert_no_code_actions!(
10084+
UNWRAP_ANONYMOUS_FUNCTION,
10085+
"pub fn main() {
10086+
fn(a) {
10087+
// look out!
10088+
op(a)
10089+
}
10090+
}
10091+
10092+
fn op(a) {
10093+
todo
10094+
}
10095+
",
10096+
find_position_of("fn(a)").to_selection()
10097+
);
10098+
}
1006310099

1006410100
#[test]
1006510101
fn unwrap_anonymous_function_unavailable_when_args_discarded() {

compiler-core/src/parse/extra.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl ModuleExtra {
4040
self.first_comment_between(start, end).is_some()
4141
}
4242

43+
// TODO: this won't necessarily find the first comment, just any comment
4344
pub(crate) fn first_comment_between(&self, start: u32, end: u32) -> Option<SrcSpan> {
4445
self.comments
4546
.binary_search_by(|comment| {

0 commit comments

Comments
 (0)