Skip to content

Commit

Permalink
Work around tree-sitter query search bug (#1800)
Browse files Browse the repository at this point in the history
- Fixes #1609
- Required by / tested by
#1448

## Checklist

- [ ] I have added
[tests](https://www.cursorless.org/docs/contributing/test-case-recorder/)
- [ ] I have updated the
[docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and
[cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet)
- [ ] I have not broken the cheatsheet
  • Loading branch information
pokey authored Aug 17, 2023
1 parent 8c9cfc3 commit a50d8ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ export class TreeSitterQuery {

matches(
document: TextDocument,
start: Position,
end: Position,
start?: Position,
end?: Position,
): QueryMatch[] {
return this.query
.matches(
this.treeSitter.getTree(document).rootNode,
positionToPoint(start),
positionToPoint(end),
start == null ? undefined : positionToPoint(start),
end == null ? undefined : positionToPoint(end),
)
.map(
({ pattern, captures }): MutableQueryMatch => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ export abstract class BaseTreeSitterScopeHandler extends BaseScopeHandler {
editor: TextEditor,
position: Position,
direction: Direction,
hints: ScopeIteratorRequirements,
_hints: ScopeIteratorRequirements,
): Iterable<TargetScope> {
const { document } = editor;

/** Narrow the range within which tree-sitter searches, for performance */
const { start, end } = getQuerySearchRange(
document,
position,
direction,
hints,
);
// Due to a tree-sitter bug, we generate all scopes from the entire file
// instead of using `_hints` to restrict the search range to scopes we care
// about. The actual scopes yielded to the client are filtered by
// `BaseScopeHandler` anyway, so there's no impact on correctness, just
// performance. We'd like to roll this back; see #1769.

const scopes = this.query
.matches(document, start, end)
.matches(document)
.map((match) => this.matchToScope(editor, match))
.filter((scope): scope is ExtendedTargetScope => scope != null)
.sort((a, b) => compareTargetScopes(direction, position, a, b));
Expand Down

0 comments on commit a50d8ad

Please sign in to comment.