Unnamed repository; edit this file 'description' to name the repository.
make matching brace almost always proc
bendn 2 months ago
parent 3e832f3 · commit b54a6f3
-rw-r--r--crates/ide/src/matching_brace.rs39
1 files changed, 27 insertions, 12 deletions
diff --git a/crates/ide/src/matching_brace.rs b/crates/ide/src/matching_brace.rs
index b2b91d6e3c..defd8aae8a 100644
--- a/crates/ide/src/matching_brace.rs
+++ b/crates/ide/src/matching_brace.rs
@@ -17,25 +17,40 @@ 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
+
+ if let Some((brace_token, brace_idx)) = file
.syntax()
.token_at_offset(offset)
.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;
+ }
+ 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
+ let thingy = file.syntax().token_at_offset(offset).last()?;
+ // find first parent
+ thingy.parent_ancestors().find_map(|x| {
+ x.children_with_tokens()
+ .filter_map(|it| it.into_token())
+ // with ending brace
+ .filter(|node| BRACES.contains(&node.kind()))
+ .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)]