Skip to content

Commit

Permalink
Another attempt to remove circular dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Nov 29, 2023
1 parent 3fe5d81 commit 4359334
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ImplicitStage } from "./marks/ImplicitStage";
import { ContainingTokenIfUntypedEmptyStage } from "./modifiers/ConditionalModifierStages";
import { PlainTarget } from "./targets";
import { uniqWithHash } from "../util/uniqWithHash";
import { createContinuousRangeTarget } from "./createContinuousRangeTarget";

export class TargetPipelineRunner {
constructor(
Expand Down Expand Up @@ -315,8 +316,9 @@ export function targetsToContinuousTarget(
const excludeStart = isReversed ? excludeActive : excludeAnchor;
const excludeEnd = isReversed ? excludeAnchor : excludeActive;

return startTarget.createContinuousRangeTarget(
return createContinuousRangeTarget(
isReversed,
startTarget,
endTarget,
!excludeStart,
!excludeEnd,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Target } from "../typings/target.types";
import {
createContinuousRange,
createContinuousRangeUntypedTarget,
} from "./targetUtil/createContinuousRange";
import { PlainTarget } from "./targets";

export function createContinuousRangeTarget(
isReversed: boolean,
startTarget: Target,
endTarget: Target,
includeStart: boolean,
includeEnd: boolean,
): Target {
const richTarget = startTarget.maybeCreateRichRangeTarget(
isReversed,
endTarget,
includeStart,
includeEnd,
);

if (richTarget != null) {
return richTarget;
}

if (!includeStart || !includeEnd) {
return new PlainTarget({
editor: startTarget.editor,
contentRange: createContinuousRange(
startTarget,
endTarget,
includeStart,
includeEnd,
),
isReversed,
isToken: false,
});
}

return createContinuousRangeUntypedTarget(
isReversed,
startTarget,
endTarget,
includeStart,
includeEnd,
);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Target } from "../../typings/target.types";
import { createContinuousRangeTarget } from "../createContinuousRangeTarget";
import { TargetScope } from "./scopeHandlers/scope.types";

/**
Expand Down Expand Up @@ -41,6 +42,6 @@ export function constructScopeRangeTarget(
: [target2, target1];

return [
startTarget.createContinuousRangeTarget(isReversed, endTarget, true, true),
createContinuousRangeTarget(isReversed, startTarget, endTarget, true, true),
];
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ScopeType } from "@cursorless/common";
import { Target } from "../../typings/target.types";
import { ModifierStageFactory } from "../ModifierStageFactory";
import { createContinuousRangeTarget } from "../createContinuousRangeTarget";

export class OutOfRangeError extends Error {
constructor() {
Expand Down Expand Up @@ -32,8 +33,9 @@ export function createRangeTargetFromIndices(
return targets[startIndex];
}

return targets[startIndex].createContinuousRangeTarget(
return createContinuousRangeTarget(
isReversed,
targets[startIndex],
targets[endIndex],
true,
true,
Expand Down
43 changes: 19 additions & 24 deletions packages/cursorless-engine/src/processTargets/targets/BaseTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import { isEqual } from "lodash";
import type { EditWithRangeUpdater } from "../../typings/Types";
import type { Destination, Target } from "../../typings/target.types";
import { isSameType } from "../../util/typeUtils";
import {
createContinuousRange,
createContinuousRangeUntypedTarget,
} from "../targetUtil/createContinuousRange";
import { createContinuousRange } from "../targetUtil/createContinuousRange";
import { DestinationImpl } from "./DestinationImpl";

/** Parameters supported by all target classes */
Expand Down Expand Up @@ -129,34 +126,32 @@ export abstract class BaseTarget<

protected abstract getCloneParameters(): EnforceUndefined<TParameters>;

createContinuousRangeTarget(
maybeCreateRichRangeTarget(
isReversed: boolean,
endTarget: Target,
includeStart: boolean,
includeEnd: boolean,
): Target {
): Target | undefined {
if (!includeStart || !includeEnd) {
return undefined;
}

if (isSameType(this, endTarget)) {
const constructor = Object.getPrototypeOf(this).constructor;

return new constructor({
...this.getCloneParameters(),
isReversed,
contentRange: createContinuousRange(
this,
endTarget,
includeStart,
includeEnd,
),
});
return this.createRichRangeTarget(isReversed, endTarget);
}
}

protected createRichRangeTarget(
isReversed: boolean,
endTarget: ThisType<this> & Target,
): ThisType<this> & Target {
const { constructor } = Object.getPrototypeOf(this);

return createContinuousRangeUntypedTarget(
return new constructor({
...this.getCloneParameters(),
isReversed,
this,
endTarget,
includeStart,
includeEnd,
);
contentRange: createContinuousRange(this, endTarget, true, true),
});
}

isEqual(otherTarget: Target): boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Range } from "@cursorless/common";
import { BaseTarget, MinimumTargetParameters } from ".";
import { Target } from "../../typings/target.types";
import { shrinkRangeToFitContent } from "../../util/selectionUtils";
import { isSameType } from "../../util/typeUtils";
import {
createContinuousRangeFromRanges,
createContinuousRangeUntypedTarget,
} from "../targetUtil/createContinuousRange";
import { createContinuousRangeFromRanges } from "../targetUtil/createContinuousRange";

export interface InteriorTargetParameters extends MinimumTargetParameters {
readonly fullInteriorRange: Range;
Expand Down Expand Up @@ -39,33 +34,19 @@ export class InteriorTarget extends BaseTarget<InteriorTargetParameters> {
};
}

createContinuousRangeTarget(
createRichRangeTarget(
isReversed: boolean,
endTarget: Target,
includeStart: boolean,
includeEnd: boolean,
): Target {
if (isSameType(this, endTarget)) {
const constructor = Object.getPrototypeOf(this).constructor;

return new constructor({
...this.getCloneParameters(),
isReversed,
fullInteriorRange: createContinuousRangeFromRanges(
this.fullInteriorRange,
endTarget.fullInteriorRange,
includeStart,
includeEnd,
),
});
}

return createContinuousRangeUntypedTarget(
endTarget: InteriorTarget,
): InteriorTarget {
return new InteriorTarget({
...this.getCloneParameters(),
isReversed,
this,
endTarget,
includeStart,
includeEnd,
);
fullInteriorRange: createContinuousRangeFromRanges(
this.fullInteriorRange,
endTarget.fullInteriorRange,
true,
true,
),
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ export class LineTarget extends BaseTarget<CommonTargetParameters> {

getRemovalHighlightRange = () => this.fullLineContentRange;

createContinuousRangeTarget(
maybeCreateRichRangeTarget(
isReversed: boolean,
endTarget: Target,
includeStart: boolean,
includeEnd: boolean,
): Target {
): Target | undefined {
if (endTarget.isLine) {
return new LineTarget({
editor: this.editor,
Expand All @@ -61,12 +61,7 @@ export class LineTarget extends BaseTarget<CommonTargetParameters> {
});
}

return super.createContinuousRangeTarget(
isReversed,
endTarget,
includeStart,
includeEnd,
);
return undefined;
}

protected getCloneParameters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ export class ParagraphTarget extends BaseTarget<CommonTargetParameters> {
: this.fullLineContentRange;
}

createContinuousRangeTarget(
maybeCreateRichRangeTarget(
isReversed: boolean,
endTarget: Target,
includeStart: boolean,
includeEnd: boolean,
): Target {
): Target | undefined {
if (isSameType(this, endTarget)) {
return new ParagraphTarget({
...this.getCloneParameters(),
Expand All @@ -105,12 +105,7 @@ export class ParagraphTarget extends BaseTarget<CommonTargetParameters> {
});
}

return super.createContinuousRangeTarget(
isReversed,
endTarget,
includeStart,
includeEnd,
);
return undefined;
}

protected getCloneParameters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,47 +99,44 @@ export class ScopeTypeTarget extends BaseTarget<ScopeTypeTargetParameters> {
: getTokenRemovalRange(this);
}

createContinuousRangeTarget(
maybeCreateRichRangeTarget(
isReversed: boolean,
endTarget: Target,
includeStart: boolean,
includeEnd: boolean,
): Target {
if (isSameType(this, endTarget)) {
const scopeTarget = endTarget;
if (this.scopeTypeType_ === scopeTarget.scopeTypeType_) {
const contentRemovalRange =
this.removalRange_ != null || scopeTarget.removalRange_ != null
? createContinuousRangeFromRanges(
this.removalRange_ ?? this.contentRange,
scopeTarget.removalRange_ ?? scopeTarget.contentRange,
includeStart,
includeEnd,
)
: undefined;
): Target | undefined {
if (
!includeStart ||
!includeEnd ||
!isSameType(this, endTarget) ||
this.scopeTypeType_ !== endTarget.scopeTypeType_
) {
return undefined;
}

return new ScopeTypeTarget({
...this.getCloneParameters(),
isReversed,
leadingDelimiterRange: this.leadingDelimiterRange_,
trailingDelimiterRange: scopeTarget.trailingDelimiterRange_,
removalRange: contentRemovalRange,
contentRange: createContinuousRange(
this,
endTarget,
const contentRemovalRange =
this.removalRange_ != null || endTarget.removalRange_ != null
? createContinuousRangeFromRanges(
this.removalRange_ ?? this.contentRange,
endTarget.removalRange_ ?? endTarget.contentRange,
includeStart,
includeEnd,
),
});
}
}
)
: undefined;

return super.createContinuousRangeTarget(
return new ScopeTypeTarget({
...this.getCloneParameters(),
isReversed,
endTarget,
includeStart,
includeEnd,
);
leadingDelimiterRange: this.leadingDelimiterRange_,
trailingDelimiterRange: endTarget.trailingDelimiterRange_,
removalRange: contentRemovalRange,
contentRange: createContinuousRange(
this,
endTarget,
includeStart,
includeEnd,
),
});
}

protected getCloneParameters() {
Expand Down
Loading

0 comments on commit 4359334

Please sign in to comment.