-
Notifications
You must be signed in to change notification settings - Fork 321
Fix that ViFindBrace doesn't search for brace when current char is not a brace #4862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -254,7 +254,28 @@ private int ViFindBrace(int i) | |||||||||||||||||||||||||
| case ')': | ||||||||||||||||||||||||||
| return ViFindBackward(i, '(', withoutPassing: ')'); | ||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||
| return i; | ||||||||||||||||||||||||||
| ReadOnlySpan<char> parenthese = stackalloc char[] { '{', '}', '(', ')', '[', ']' }; | ||||||||||||||||||||||||||
| int nextParen = i; | ||||||||||||||||||||||||||
| // find next of any kind of paren | ||||||||||||||||||||||||||
| for (; nextParen < _buffer.Length; nextParen++) | ||||||||||||||||||||||||||
| for (int idx = 0; idx < parenthese.Length; idx++) | ||||||||||||||||||||||||||
| if (parenthese[idx] == _buffer[nextParen]) goto Outer; | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rare great use of
Comment on lines
+257
to
+262
|
||||||||||||||||||||||||||
| ReadOnlySpan<char> parenthese = stackalloc char[] { '{', '}', '(', ')', '[', ']' }; | |
| int nextParen = i; | |
| // find next of any kind of paren | |
| for (; nextParen < _buffer.Length; nextParen++) | |
| for (int idx = 0; idx < parenthese.Length; idx++) | |
| if (parenthese[idx] == _buffer[nextParen]) goto Outer; | |
| ReadOnlySpan<char> parentheses = stackalloc char[] { '{', '}', '(', ')', '[', ']' }; | |
| int nextParen = i; | |
| // find next of any kind of paren | |
| for (; nextParen < _buffer.Length; nextParen++) | |
| for (int idx = 0; idx < parentheses.Length; idx++) | |
| if (parentheses[idx] == _buffer[nextParen]) goto Outer; |
daxian-dbw marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Apr 6, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the default case, if no brace/paren/bracket is found from the current position to the end of the buffer, nextParen will end up equal to _buffer.Length, and _buffer[nextParen] will throw IndexOutOfRangeException. Add an explicit check after the scan (e.g., if nextParen >= _buffer.Length return i) before indexing into _buffer.
| Outer: | |
| Outer: | |
| if (nextParen >= _buffer.Length) | |
| { | |
| return i; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the default case, if no brace/paren/bracket is found from the current position to the end of the buffer,
nextParenwill end up equal to_buffer.Length, and_buffer[nextParen]will throwIndexOutOfRangeException
This is a real problem. I prefer to not using the Outer label here. For the goto statement, we should avoid using it unless it's really needed.
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||||
| using Xunit; | ||||||||
| using System.Collections.Generic; | ||||||||
| using Xunit; | ||||||||
|
Comment on lines
+1
to
+2
|
||||||||
| using System.Collections.Generic; | |
| using Xunit; | |
| using Xunit; |
Copilot
AI
Apr 6, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the "Closing paren without backward match" case, % should still trigger a ding (consistent with other unmatched-brace cases in this test and with ViGotoBrace dinging when ViFindBrace returns the current position). Consider using TestMustDing here so the test actually asserts the expected ding behavior, not just that the cursor stays still.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can find the next parenthesis by the code below, instead of using nested loop here: