Skip to content

Commit b54a6f3

Browse files
committed
make matching brace almost always proc
1 parent 3e832f3 commit b54a6f3

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

crates/ide/src/matching_brace.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,40 @@ use syntax::{
1717
pub(crate) fn matching_brace(file: &SourceFile, offset: TextSize) -> Option<TextSize> {
1818
const BRACES: &[SyntaxKind] =
1919
&[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>], T![|], T![|]];
20-
let (brace_token, brace_idx) = file
20+
21+
if let Some((brace_token, brace_idx)) = file
2122
.syntax()
2223
.token_at_offset(offset)
2324
.filter_map(|node| {
2425
let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
2526
Some((node, idx))
2627
})
27-
.last()?;
28-
let parent = brace_token.parent()?;
29-
if brace_token.kind() == T![|] && !ast::ParamList::can_cast(parent.kind()) {
30-
cov_mark::hit!(pipes_not_braces);
31-
return None;
28+
.last()
29+
{
30+
let parent = brace_token.parent()?;
31+
if brace_token.kind() == T![|] && !ast::ParamList::can_cast(parent.kind()) {
32+
cov_mark::hit!(pipes_not_braces);
33+
return None;
34+
}
35+
let matching_kind = BRACES[brace_idx ^ 1];
36+
let matching_node = parent
37+
.children_with_tokens()
38+
.filter_map(|it| it.into_token())
39+
.find(|node| node.kind() == matching_kind && node != &brace_token)?;
40+
Some(matching_node.text_range().start())
41+
} else {
42+
// when the offset is not at a brace
43+
let thingy = file.syntax().token_at_offset(offset).last()?;
44+
// find first parent
45+
thingy.parent_ancestors().find_map(|x| {
46+
x.children_with_tokens()
47+
.filter_map(|it| it.into_token())
48+
// with ending brace
49+
.filter(|node| BRACES.contains(&node.kind()))
50+
.last()
51+
.map(|x| x.text_range().start())
52+
})
3253
}
33-
let matching_kind = BRACES[brace_idx ^ 1];
34-
let matching_node = parent
35-
.children_with_tokens()
36-
.filter_map(|it| it.into_token())
37-
.find(|node| node.kind() == matching_kind && node != &brace_token)?;
38-
Some(matching_node.text_range().start())
3954
}
4055

4156
#[cfg(test)]

0 commit comments

Comments
 (0)