-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Escape braces that are in string or comments when finding matching brace #3372
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
Escape braces that are in string or comments when finding matching brace #3372
Conversation
9ace8e7 to
8cd69a1
Compare
8cd69a1 to
1ea20dc
Compare
Andriamanitra
left a comment
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.
Some stuff will still slip through (for example regex literals in JS/Ruby/etc) but I don't think there's a good way to handle that (at least without going through all syntaxes and coming up with a consistent way to detect this kind of thing). Just skipping strings and comments probably covers >95% of the circumstances where this is a problem.
internal/buffer/buffer.go
Outdated
| func (b *Buffer) getSortedSyntaxIndices(lineN int) []int { | ||
| keys := make([]int, 0, len(b.Match(lineN))) | ||
| for k := range b.Match(lineN) { | ||
| keys = append(keys, k) | ||
| } | ||
| sort.Ints(keys) | ||
| return keys | ||
| } | ||
|
|
||
| // Returns the Group (syntax highlight group ID) at the specified location and a boolean | ||
| // that indicates if a group is found or not | ||
| func (b *Buffer) GetGroupAtLoc(loc Loc) (highlight.Group, bool) { | ||
| sortedIndices := b.getSortedSyntaxIndices(loc.Y) | ||
| i := sort.SearchInts(sortedIndices, loc.X) | ||
| if i == 0 || i == len(sortedIndices) { | ||
| return 0, false | ||
| } | ||
| if sortedIndices[i] == loc.X && b.Match(loc.Y)[sortedIndices[i]] != 0 { | ||
| return b.Match(loc.Y)[sortedIndices[i]], true | ||
| } | ||
| return b.Match(loc.Y)[sortedIndices[i - 1]], true | ||
| } |
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.
Maybe I'm missing something but is the sorting necessary?
| func (b *Buffer) getSortedSyntaxIndices(lineN int) []int { | |
| keys := make([]int, 0, len(b.Match(lineN))) | |
| for k := range b.Match(lineN) { | |
| keys = append(keys, k) | |
| } | |
| sort.Ints(keys) | |
| return keys | |
| } | |
| // Returns the Group (syntax highlight group ID) at the specified location and a boolean | |
| // that indicates if a group is found or not | |
| func (b *Buffer) GetGroupAtLoc(loc Loc) (highlight.Group, bool) { | |
| sortedIndices := b.getSortedSyntaxIndices(loc.Y) | |
| i := sort.SearchInts(sortedIndices, loc.X) | |
| if i == 0 || i == len(sortedIndices) { | |
| return 0, false | |
| } | |
| if sortedIndices[i] == loc.X && b.Match(loc.Y)[sortedIndices[i]] != 0 { | |
| return b.Match(loc.Y)[sortedIndices[i]], true | |
| } | |
| return b.Match(loc.Y)[sortedIndices[i - 1]], true | |
| } | |
| func (b *Buffer) GetGroupAtLoc(loc Loc) (highlight.Group, bool) { | |
| found := -1 | |
| for i := range b.Match(loc.Y) { | |
| if i > found && i <= loc.X { | |
| found = i | |
| } | |
| } | |
| if found == -1 { | |
| return 0, false | |
| } | |
| return b.Match(loc.Y)[found], true | |
| } |
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.
The suggested change won't work when the cursor is at the end of a syntax change, which has an empty group and needs the previous group id.
The sorted indices are now being reused for the rest of the lines without iterating the whole line again, which will be useful for extreme cases where the line is very long, for example, minified js or json.
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.
The suggested change won't work when the cursor is at the end of a syntax change, which has an empty group and needs the previous group id.
Can you provide an example of this / how to test it? It should be fixable – if the only reason you need the sorted indices is to call sort.SearchInts afterwards you can always do it with a simple loop (less code and no allocations).
|
I doubt this is a good idea, at least because:
So, this is an ad-hoc heuristic, and while it is useful for cases like |
|
@dmaluka Would it work if I keep |
What would be their difference from the existing |
|
@dmaluka |
|
Ok, that was discussed in #3308 (#3308 (comment) and below). I provided a Lua implementation for that in #3308 (comment). Yeah, it might make sense to implement those |
|
The proposed logic for jumping to opening and closing brace is now in #3384 |
Escape braces that are in string or comments when finding matching brace.
To test this, just a filetype that contains brace
({})in string or comment.My test case: