Skip to content

Commit

Permalink
Assert creation of scope handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Nov 19, 2024
1 parent 8cf3464 commit ed7591e
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,11 @@ export class PreferredScopeStage implements ModifierStage {
}
}

const scopeHandler = this.scopeHandlerFactory.create(
const scopeHandler = this.scopeHandlerFactory.createWithAssert(
this.modifier.scopeType,
target.editor.document.languageId,
);

if (scopeHandler == null) {
throw Error(`Couldn't create scope handler for: ${scopeType.type}`);
}

const closestTargets = getClosestScopeTargets(target, scopeHandler);

if (closestTargets == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ abstract class BoundedBaseScopeHandler extends BaseScopeHandler {
) {
super();

this.targetScopeHandler = this.scopeHandlerFactory.create(
this.targetScopeHandler = this.scopeHandlerFactory.createWithAssert(
this.targetScopeType,
this.languageId,
)!;
this.surroundingPairInteriorScopeHandler = this.scopeHandlerFactory.create(
{
type: "surroundingPairInterior",
delimiter: "any",
},
this.languageId,
)!;
);
this.surroundingPairInteriorScopeHandler =
this.scopeHandlerFactory.createWithAssert(
{
type: "surroundingPairInterior",
delimiter: "any",
},
this.languageId,
);
}

get iterationScopeType(): ScopeType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export abstract class NestedScopeHandler extends BaseScopeHandler {

private get searchScopeHandler(): ScopeHandler {
if (this._searchScopeHandler == null) {
this._searchScopeHandler = this.scopeHandlerFactory.create(
this._searchScopeHandler = this.scopeHandlerFactory.createWithAssert(
this.searchScopeType,
this.languageId,
)!;
);
}

return this._searchScopeHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,19 @@ export class OneOfScopeHandler extends BaseScopeHandler {
languageId: string,
): ScopeHandler {
const scopeHandlers: ScopeHandler[] = scopeType.scopeTypes.map(
(scopeType) => {
const handler = scopeHandlerFactory.create(scopeType, languageId);
if (handler == null) {
throw new Error(`No available scope handler for '${scopeType.type}'`);
}
return handler;
},
(scopeType) =>
scopeHandlerFactory.createWithAssert(scopeType, languageId),
);

const iterationScopeType = (): CustomScopeType => ({
type: "custom",
scopeHandler: new OneOfScopeHandler(
undefined,
scopeHandlers.map(
(scopeHandler) =>
scopeHandlerFactory.create(
scopeHandler.iterationScopeType,
languageId,
)!,
scopeHandlers.map((scopeHandler) =>
scopeHandlerFactory.createWithAssert(
scopeHandler.iterationScopeType,
languageId,
),
),
() => {
throw new Error("Not implemented");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ export interface ScopeHandlerFactory {
scopeType: ScopeType | CustomScopeType,
languageId: string,
): ScopeHandler | undefined;

createWithAssert(
scopeType: ScopeType | CustomScopeType,
languageId: string,
): ScopeHandler;
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,15 @@ export class ScopeHandlerFactoryImpl implements ScopeHandlerFactory {
?.getScopeHandler(scopeType);
}
}

createWithAssert(
scopeType: ScopeType | CustomScopeType,
languageId: string,
): ScopeHandler {
const handler = this.create(scopeType, languageId);
if (handler == null) {
throw new Error(`Couldn't create scope handler for '${scopeType.type}'`);
}
return handler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ export class SurroundingPairInteriorScopeHandler extends BaseScopeHandler {
) {
super();

this.surroundingPairScopeHandler = this.scopeHandlerFactory.create(
{
type: "surroundingPair",
delimiter: this.scopeType.delimiter,
requireStrongContainment: true,
},
this.languageId,
)!;
this.surroundingPairScopeHandler =
this.scopeHandlerFactory.createWithAssert(
{
type: "surroundingPair",
delimiter: this.scopeType.delimiter,
requireStrongContainment: true,
},
this.languageId,
);
}

get iterationScopeType() {
Expand Down

0 comments on commit ed7591e

Please sign in to comment.