Skip to content

Commit 06f3c99

Browse files
committed
Add support for domain, leading, trailing, interior
1 parent ecaf27f commit 06f3c99

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

packages/cursorless-engine/src/languages/ruby.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ const nodeMatchers: Partial<
175175
"argument_list",
176176
),
177177
collectionKey: trailingMatcher(["pair[key]"], [":"]),
178-
className: "class[name]",
179178
name: [
180179
"assignment[left]",
181180
"operator_assignment[left]",

packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/TreeSitterScopeHandler.ts

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,45 @@ export class TreeSitterScopeHandler extends BaseScopeHandler {
5959
}
6060

6161
private matchToScope(editor: TextEditor, match: QueryMatch): TargetScope {
62+
const scopeTypeType = this.scopeType.type;
63+
6264
const contentRange = getNodeRange(
63-
match.captures.find((capture) => capture.name === this.scopeType.type)!
64-
.node,
65+
match.captures.find((capture) => capture.name === scopeTypeType)!.node,
66+
);
67+
68+
const domain =
69+
getRelatedRange(match, scopeTypeType, "domain") ?? contentRange;
70+
71+
const removalRange = getRelatedRange(match, scopeTypeType, "removal");
72+
73+
const leadingDelimiterRange = getRelatedRange(
74+
match,
75+
scopeTypeType,
76+
"leading",
77+
);
78+
79+
const trailingDelimiterRange = getRelatedRange(
80+
match,
81+
scopeTypeType,
82+
"trailing",
6583
);
6684

85+
const interiorRange = getRelatedRange(match, scopeTypeType, "interior");
86+
6787
return {
6888
editor,
69-
// FIXME: Actually get domain
70-
domain: contentRange,
89+
domain,
7190
getTarget: (isReversed) =>
7291
new ScopeTypeTarget({
73-
scopeTypeType: this.scopeType.type,
92+
scopeTypeType,
7493
editor,
7594
isReversed,
7695
contentRange,
77-
// FIXME: Actually get removalRange
78-
removalRange: contentRange,
79-
// FIXME: Other fields here
96+
removalRange,
97+
leadingDelimiterRange,
98+
trailingDelimiterRange,
99+
interiorRange,
100+
// FIXME: Add delimiter text
80101
}),
81102
};
82103
}
@@ -136,6 +157,29 @@ function getQueryRange(
136157
};
137158
}
138159

160+
/**
161+
* Gets the range of a node that is related to the scope. For example, if the
162+
* scope is "class name", the `domain` node would be the containing class.
163+
*
164+
* @param match The match to get the range from
165+
* @param scopeTypeType The type of the scope
166+
* @param relationship The relationship to get the range for, eg "domain", or "removal"
167+
* @returns A range or undefined if no range was found
168+
*/
169+
function getRelatedRange(
170+
match: QueryMatch,
171+
scopeTypeType: string,
172+
relationship: string,
173+
) {
174+
const relatedNode = match.captures.find(
175+
(capture) =>
176+
capture.name === `${scopeTypeType}.${relationship}` ||
177+
capture.name === relationship,
178+
)?.node;
179+
180+
return relatedNode == null ? undefined : getNodeRange(relatedNode);
181+
}
182+
139183
function positionToPoint(start: Position): Point | undefined {
140184
return { row: start.line, column: start.character };
141185
}

queries/ruby.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
(regex) @regularExpression
1414

1515
(class) @class
16+
(class name: (_) @className) @domain

0 commit comments

Comments
 (0)