Skip to content

Commit 7e68db3

Browse files
committed
Matching brace prefers brace on cursor's right
1 parent 098284a commit 7e68db3

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

crates/ide/src/matching_brace.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ use syntax::{
1919
pub(crate) fn matching_brace(file: &SourceFile, offset: TextSize) -> Option<TextSize> {
2020
const BRACES: &[SyntaxKind] =
2121
&[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>], T![|], T![|]];
22-
let (brace_token, brace_idx) = file.syntax().token_at_offset(offset).find_map(|node| {
23-
let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
24-
Some((node, idx))
25-
})?;
22+
let (brace_token, brace_idx) = file
23+
.syntax()
24+
.token_at_offset(offset)
25+
.filter_map(|node| {
26+
let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
27+
Some((node, idx))
28+
})
29+
.last()?;
2630
let parent = brace_token.parent()?;
2731
if brace_token.kind() == T![|] && !ast::ParamList::can_cast(parent.kind()) {
2832
cov_mark::hit!(pipes_not_braces);
@@ -58,6 +62,10 @@ mod tests {
5862
do_check("struct Foo { a: i32, }$0", "struct Foo $0{ a: i32, }");
5963
do_check("fn main() { |x: i32|$0 x * 2;}", "fn main() { $0|x: i32| x * 2;}");
6064
do_check("fn main() { $0|x: i32| x * 2;}", "fn main() { |x: i32$0| x * 2;}");
65+
do_check(
66+
"fn func(x) { return (2 * (x + 3)$0) + 5;}",
67+
"fn func(x) { return $0(2 * (x + 3)) + 5;}",
68+
);
6169

6270
{
6371
cov_mark::check!(pipes_not_braces);

0 commit comments

Comments
 (0)