Repository navigation
Expand file tree
/
Copy pathBaseTreeSitterScopeHandler.ts
More file actions
116 lines (102 loc) · 3.63 KB
/
Copy pathBaseTreeSitterScopeHandler.ts
File metadata and controls
116 lines (102 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { uniqWith } from "lodash-es";
import type {
Direction,
IDE,
Position,
TextEditor,
} from "@cursorless/lib-common";
import { showError } from "@cursorless/lib-common";
import type { TreeSitterQuery } from "../../../../languages/TreeSitterQuery";
import type { QueryMatch } from "../../../../languages/TreeSitterQuery/QueryCapture";
import { BaseScopeHandler } from "../BaseScopeHandler";
import { compareTargetScopes } from "../compareTargetScopes";
import type { TargetScope } from "../scope.types";
import type { ScopeIteratorRequirements } from "../scopeHandler.types";
import { isEveryScopeModifier } from "../util/isHintsEveryScope";
import { getQuerySearchRange } from "./getQuerySearchRange";
import { mergeAdjacentBy } from "./mergeAdjacentBy";
/** Base scope handler to use for both tree-sitter scopes and their iteration scopes */
export abstract class BaseTreeSitterScopeHandler extends BaseScopeHandler {
constructor(
protected ide: IDE,
protected query: TreeSitterQuery,
) {
super();
}
*generateScopeCandidates(
editor: TextEditor,
position: Position,
direction: Direction,
hints: ScopeIteratorRequirements,
): Iterable<TargetScope> {
const { document } = editor;
const isEveryScope = isEveryScopeModifier(hints);
/** Narrow the range within which tree-sitter searches, for performance */
const { start, end } = getQuerySearchRange(
document,
position,
direction,
hints,
);
const scopes = this.query
.matches(document, start, end)
.map((match) => this.matchToScope(editor, match, isEveryScope))
.filter((scope): scope is ExtendedTargetScope => scope != null)
// oxlint-disable-next-line unicorn/no-array-sort
.sort((a, b) => compareTargetScopes(direction, position, a, b));
// Merge scopes that have the same domain into a single scope with multiple
// targets
yield* mergeAdjacentBy(
scopes,
(a, b) => a.domain.isRangeEqual(b.domain),
(equivalentScopes) => {
if (equivalentScopes.length === 1) {
return equivalentScopes[0];
}
const ide = this.ide;
return {
...equivalentScopes[0],
getTargets(isReversed: boolean) {
const targets = uniqWith(
equivalentScopes.flatMap((scope) => scope.getTargets(isReversed)),
(a, b) => a.isEqual(b),
);
if (
targets.length > 1 &&
!equivalentScopes.every((scope) => scope.allowMultiple)
) {
const message =
"Please use #allow-multiple! predicate in your query to allow multiple matches for this scope type";
void showError(
ide.messages,
"BaseTreeSitterScopeHandler.allow-multiple",
message,
);
if (ide.runMode === "test") {
throw new Error(message);
}
}
return targets;
},
};
},
);
}
/**
* Convert a tree-sitter match to a scope, or undefined if the match is not
* relevant to this scope handler
* @param editor The editor in which the match was found
* @param match The match to convert to a scope
* @param isEveryScope Whether the scope is being used in an "every" modifier
* @returns The scope, or undefined if the match is not relevant to this scope
* handler
*/
protected abstract matchToScope(
editor: TextEditor,
match: QueryMatch,
isEveryScope: boolean,
): ExtendedTargetScope | undefined;
}
export interface ExtendedTargetScope extends TargetScope {
allowMultiple: boolean;
}