Skip to content

Commit 5baeecf

Browse files
Draft off one of compound scope type
1 parent a4cee96 commit 5baeecf

File tree

5 files changed

+106
-4
lines changed

5 files changed

+106
-4
lines changed

src/processTargets/modifiers/scopeHandlers/LineScopeHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { range } from "lodash";
22
import { Position, Range, TextEditor } from "vscode";
3-
import { Direction } from "../../../typings/targetDescriptor.types";
3+
import { Direction, ScopeType } from "../../../typings/targetDescriptor.types";
44
import { getDocumentRange } from "../../../util/range";
55
import { LineTarget } from "../../targets";
66
import { OutOfRangeError } from "../targetSequenceUtils";
@@ -12,7 +12,7 @@ export default class LineScopeHandler implements ScopeHandler {
1212
public readonly iterationScopeType = { type: "document" } as const;
1313

1414
constructor(
15-
public readonly scopeType: { type: "line" },
15+
public readonly scopeType: ScopeType,
1616
protected languageId: string
1717
) {}
1818

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { Position, Range, TextEditor } from "vscode";
2+
import { getScopeHandler } from ".";
3+
import {
4+
Direction,
5+
OneOfScopeType,
6+
} from "../../../typings/targetDescriptor.types";
7+
import type { IterationScope, Scope, TargetScope } from "./scope.types";
8+
import { ScopeHandler } from "./scopeHandler.types";
9+
10+
export default class OneOfScopeHandler implements ScopeHandler {
11+
iterationScopeType = undefined;
12+
13+
constructor(
14+
public readonly scopeType: OneOfScopeType,
15+
private languageId: string
16+
) {}
17+
18+
/** Return smallest target scope touching position */
19+
getScopesTouchingPosition(
20+
editor: TextEditor,
21+
position: Position,
22+
ancestorIndex?: number
23+
): TargetScope[] {
24+
const targetScopes = this.getsScopeHandlers().flatMap((scopeHandler) =>
25+
scopeHandler.getScopesTouchingPosition(editor, position, ancestorIndex)
26+
);
27+
targetScopes.sort(scopeComparator);
28+
return [targetScopes[0]];
29+
}
30+
31+
/** Return all scopes overlapping range not contained by another scope */
32+
getScopesOverlappingRange(editor: TextEditor, range: Range): TargetScope[] {
33+
const targetScopes = this.getsScopeHandlers().flatMap((scopeHandler) =>
34+
scopeHandler.getScopesOverlappingRange(editor, range)
35+
);
36+
return targetScopes.filter(
37+
(targetScope) =>
38+
!targetScopes.find((s) => s.domain.contains(targetScope.domain))
39+
);
40+
}
41+
42+
/** Returns smallest iteration scope touching position */
43+
getIterationScopesTouchingPosition(
44+
editor: TextEditor,
45+
position: Position
46+
): IterationScope[] {
47+
const iterationScopes = this.getsScopeHandlers().flatMap((scopeHandler) =>
48+
scopeHandler.getIterationScopesTouchingPosition(editor, position)
49+
);
50+
iterationScopes.sort(scopeComparator);
51+
return [iterationScopes[0]];
52+
}
53+
54+
getScopeRelativeToPosition(
55+
editor: TextEditor,
56+
position: Position,
57+
offset: number,
58+
direction: Direction
59+
): TargetScope {
60+
throw new Error("not implemented");
61+
// const scopeType=this.scopeType.scopeTypes[offset]
62+
// const targetScopes = this.getsScopeHandlers().map((scopeHandler) =>
63+
// scopeHandler.getScopeRelativeToPosition(
64+
// editor,
65+
// position,
66+
// offset,
67+
// direction
68+
// )
69+
// )
70+
}
71+
72+
private getsScopeHandlers(): ScopeHandler[] {
73+
return this.scopeType.scopeTypes.map((scopeType) => {
74+
const handler = getScopeHandler(scopeType, this.languageId);
75+
if (handler == null) {
76+
throw new Error(`No available scope handler for '${scopeType.type}'`);
77+
}
78+
return handler;
79+
});
80+
}
81+
}
82+
83+
function scopeComparator(a: Scope, b: Scope): number {
84+
if (a.domain.contains(b.domain)) {
85+
return 1;
86+
}
87+
if (b.domain.contains(a.domain)) {
88+
return -1;
89+
}
90+
return 0;
91+
}

src/processTargets/modifiers/scopeHandlers/getScopeHandler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
LineScopeHandler,
55
TokenScopeHandler,
66
WordScopeHandler,
7+
OneOfScopeHandler,
78
} from ".";
89
import type { ScopeType } from "../../../typings/targetDescriptor.types";
910
import type { ScopeHandler } from "./scopeHandler.types";
@@ -39,7 +40,9 @@ export default function getScopeHandler(
3940
case "identifier":
4041
return new IdentifierScopeHandler(scopeType, languageId);
4142
case "line":
42-
return new LineScopeHandler(scopeType as { type: "line" }, languageId);
43+
return new LineScopeHandler(scopeType, languageId);
44+
case "oneOf":
45+
return new OneOfScopeHandler(scopeType, languageId);
4346
default:
4447
return undefined;
4548
}

src/processTargets/modifiers/scopeHandlers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ export * from "./WordScopeHandler";
1010
export { default as WordScopeHandler } from "./WordScopeHandler";
1111
export * from "./TokenScopeHandler";
1212
export { default as TokenScopeHandler } from "./TokenScopeHandler";
13+
export * from "./OneOfScopeHandler";
14+
export { default as OneOfScopeHandler } from "./OneOfScopeHandler";
1315
export * from "./getScopeHandler";
1416
export { default as getScopeHandler } from "./getScopeHandler";

src/typings/targetDescriptor.types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,16 @@ export interface SurroundingPairScopeType {
156156
requireStrongContainment?: boolean;
157157
}
158158

159+
export interface OneOfScopeType {
160+
type: "oneOf";
161+
scopeTypes: ScopeType[];
162+
}
163+
159164
export type ScopeType =
160165
| SimpleScopeType
161166
| SurroundingPairScopeType
162-
| CustomRegexScopeType;
167+
| CustomRegexScopeType
168+
| OneOfScopeType;
163169

164170
export interface ContainingSurroundingPairModifier
165171
extends ContainingScopeModifier {

0 commit comments

Comments
 (0)