Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions crates/ide/src/matching_brace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,37 @@ use syntax::{
pub(crate) fn matching_brace(file: &SourceFile, offset: TextSize) -> Option<TextSize> {
const BRACES: &[SyntaxKind] =
&[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>], T![|], T![|]];
let (brace_token, brace_idx) = file
.syntax()
.token_at_offset(offset)
let current = file.syntax().token_at_offset(offset);
if let Some((brace_token, brace_idx)) = current
.clone()
.filter_map(|node| {
let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
Some((node, idx))
})
.last()?;
let parent = brace_token.parent()?;
if brace_token.kind() == T![|] && !ast::ParamList::can_cast(parent.kind()) {
cov_mark::hit!(pipes_not_braces);
return None;
.last()
{
let parent = brace_token.parent()?;
if brace_token.kind() == T![|] && !ast::ParamList::can_cast(parent.kind()) {
cov_mark::hit!(pipes_not_braces);
return None;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that? I see no reason to exclude pipes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats not my code, its from #5047

}
let matching_kind = BRACES[brace_idx ^ 1];
let matching_node = parent
.children_with_tokens()
.filter_map(|it| it.into_token())
.find(|node| node.kind() == matching_kind && node != &brace_token)?;
Some(matching_node.text_range().start())
} else {
// when the offset is not at a brace, find first parent
current.last()?.parent_ancestors().find_map(|x| {
x.children_with_tokens()
.filter_map(|it| it.into_token())
// with ending brace
.filter(|node| BRACES.contains(&node.kind()))
Comment thread
ChayimFriedman2 marked this conversation as resolved.
.last()
.map(|x| x.text_range().start())
})
}
let matching_kind = BRACES[brace_idx ^ 1];
let matching_node = parent
.children_with_tokens()
.filter_map(|it| it.into_token())
.find(|node| node.kind() == matching_kind && node != &brace_token)?;
Some(matching_node.text_range().start())
}

#[cfg(test)]
Expand Down Expand Up @@ -64,6 +76,14 @@ mod tests {
"fn func(x) { return (2 * (x + 3)$0) + 5;}",
"fn func(x) { return $0(2 * (x + 3)) + 5;}",
);
do_check(
"fn func(x) { return (2 * (x $0+ 3)) + 5;}",
"fn func(x) { return (2 * (x + 3$0)) + 5;}",
);
do_check(
"fn func(x) { re$0turn (2 * (x + 3)) + 5;}",
"fn func(x) { return (2 * (x + 3)) + 5;$0}",
);

{
cov_mark::check!(pipes_not_braces);
Expand Down
Loading