Skip to content

Commit 8619058

Browse files
bors[bot]codgician
andauthored
Merge #10538
10538: fix: matching brace should prefer brace on cursor's right r=Veykril a=codgician I observed a brace matching issue with the following Rust code: ```rust let x = (1 + (2 + 3)) * 4; ``` In a situation like `<|>(1 + (2 + 3)) * 4`, the cursor will go to `(1 + (2 + 3)<|>) * 4`, and if user tries to match bracket again it will go like `(1 + <|>(2 + 3)) * 4` while logically the expected result should be `<|>(1 + (2 + 3)) * 4`. This behavior exists in both line cursor style and block cursor style. This PR fixes this by letting `matching_brace` prefer the brace to cursor's right when the cursor lies between multiple consecutive braces. It **does NOT** fix #1942 but could be related. Please review. Co-authored-by: codgician <[email protected]>
2 parents c535487 + 7e68db3 commit 8619058

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)