diff --git a/crates/ide-assists/src/handlers/remove_parentheses.rs b/crates/ide-assists/src/handlers/remove_parentheses.rs index f2d1828e678..0281b29cd42 100644 --- a/crates/ide-assists/src/handlers/remove_parentheses.rs +++ b/crates/ide-assists/src/handlers/remove_parentheses.rs @@ -34,27 +34,24 @@ pub(crate) fn remove_parentheses(acc: &mut Assists, ctx: &AssistContext<'_>) -> return None; } - // we should use `find_node_at_offset` at `SourceFile` level to get expectant `Between` - let token_at_offset = ctx - .find_node_at_offset::()? - .syntax() - .token_at_offset(parens.syntax().text_range().start()); - let need_to_add_ws = match token_at_offset { - syntax::TokenAtOffset::Between(before, _after) => { - // anyother `SyntaxKind` we missing here? - let tokens = vec![T![&], T![!], T!['('], T!['['], T!['{']]; - before.kind() != SyntaxKind::WHITESPACE && !tokens.contains(&before.kind()) - } - _ => false, - }; - let expr = if need_to_add_ws { format!(" {}", expr) } else { expr.to_string() }; - let target = parens.syntax().text_range(); acc.add( AssistId("remove_parentheses", AssistKind::Refactor), "Remove redundant parentheses", target, - |builder| builder.replace(parens.syntax().text_range(), expr), + |builder| { + let prev_token = parens.syntax().first_token().and_then(|it| it.prev_token()); + let need_to_add_ws = match prev_token { + Some(it) => { + let tokens = vec![T![&], T![!], T!['('], T!['['], T!['{']]; + it.kind() != SyntaxKind::WHITESPACE && !tokens.contains(&it.kind()) + } + None => false, + }; + let expr = if need_to_add_ws { format!(" {}", expr) } else { expr.to_string() }; + + builder.replace(parens.syntax().text_range(), expr) + }, ) }