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 <15964984+codgician@users.noreply.github.com>
This commit is contained in:
bors[bot] 2021-10-14 11:50:14 +00:00 committed by GitHub
commit 8619058d3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,10 +19,14 @@
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).find_map(|node| {
let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
Some((node, idx))
})?;
let (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);
@ -58,6 +62,10 @@ fn do_check(before: &str, after: &str) {
do_check("struct Foo { a: i32, }$0", "struct Foo $0{ a: i32, }");
do_check("fn main() { |x: i32|$0 x * 2;}", "fn main() { $0|x: i32| x * 2;}");
do_check("fn main() { $0|x: i32| x * 2;}", "fn main() { |x: i32$0| x * 2;}");
do_check(
"fn func(x) { return (2 * (x + 3)$0) + 5;}",
"fn func(x) { return $0(2 * (x + 3)) + 5;}",
);
{
cov_mark::check!(pipes_not_braces);