From cb765abb1055e970ab84b25bb27761567b9d5620 Mon Sep 17 00:00:00 2001 From: Pokey Rule <755842+pokey@users.noreply.github.com> Date: Fri, 11 Aug 2023 15:08:01 +0100 Subject: [PATCH] Work around tree-sitter query search bug --- .../languages/TreeSitterQuery/TreeSitterQuery.ts | 8 ++++---- .../BaseTreeSitterScopeHandler.ts | 16 +++++++--------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/cursorless-engine/src/languages/TreeSitterQuery/TreeSitterQuery.ts b/packages/cursorless-engine/src/languages/TreeSitterQuery/TreeSitterQuery.ts index 9600624137..3ecd56b1a5 100644 --- a/packages/cursorless-engine/src/languages/TreeSitterQuery/TreeSitterQuery.ts +++ b/packages/cursorless-engine/src/languages/TreeSitterQuery/TreeSitterQuery.ts @@ -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 => ({ diff --git a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/TreeSitterScopeHandler/BaseTreeSitterScopeHandler.ts b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/TreeSitterScopeHandler/BaseTreeSitterScopeHandler.ts index d12ffd0a67..a03fe61217 100644 --- a/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/TreeSitterScopeHandler/BaseTreeSitterScopeHandler.ts +++ b/packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/TreeSitterScopeHandler/BaseTreeSitterScopeHandler.ts @@ -26,20 +26,18 @@ export abstract class BaseTreeSitterScopeHandler extends BaseScopeHandler { editor: TextEditor, position: Position, direction: Direction, - hints: ScopeIteratorRequirements, + _hints: ScopeIteratorRequirements, ): Iterable { 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));