Skip to content

Commit

Permalink
More simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Nov 30, 2023
1 parent 91be349 commit 6976c5f
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Target } from "../typings/target.types";
import { createContinuousRange } from "./targetUtil/createContinuousRange";
import { PlainTarget, UntypedTarget } from "./targets";
import { isSameType } from "../util/typeUtils";
import {
createContinuousLineRange,
createContinuousRange,
} from "./targetUtil/createContinuousRange";
import { LineTarget, UntypedTarget } from "./targets";

export function createContinuousRangeTarget(
isReversed: boolean,
Expand All @@ -9,28 +13,27 @@ export function createContinuousRangeTarget(
includeStart: boolean,
includeEnd: boolean,
): Target {
const richTarget = startTarget.maybeCreateRichRangeTarget(
isReversed,
endTarget,
includeStart,
includeEnd,
);
if (includeStart && includeEnd && isSameType(startTarget, endTarget)) {
const richTarget = startTarget.maybeCreateRichRangeTarget(
isReversed,
endTarget,
);

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

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

Expand All @@ -44,6 +47,7 @@ export function createContinuousRangeTarget(
includeStart,
includeEnd,
),
isToken: startTarget.isToken && endTarget.isToken,
isToken:
includeStart && includeEnd && startTarget.isToken && endTarget.isToken,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import {
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 } from "../targetUtil/createContinuousRange";
import { DestinationImpl } from "./DestinationImpl";
import { createContinuousRange } from "../targetUtil/createContinuousRange";

/** Parameters supported by all target classes */
export interface MinimumTargetParameters {
Expand Down Expand Up @@ -127,22 +126,9 @@ export abstract class BaseTarget<
protected abstract getCloneParameters(): EnforceUndefined<TParameters>;

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

return this.createRichRangeTarget(isReversed, endTarget);
}

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

return new constructor({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class InteriorTarget extends BaseTarget<InteriorTargetParameters> {
};
}

createRichRangeTarget(
maybeCreateRichRangeTarget(
isReversed: boolean,
endTarget: InteriorTarget,
): InteriorTarget {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Position, Range, TextEditor } from "@cursorless/common";
import { BaseTarget, CommonTargetParameters } from ".";
import { Target } from "../../typings/target.types";
import { expandToFullLine } from "../../util/rangeUtils";
import { tryConstructPlainTarget } from "../../util/tryConstructTarget";
import { createContinuousLineRange } from "../targetUtil/createContinuousRange";
Expand Down Expand Up @@ -44,24 +43,13 @@ export class LineTarget extends BaseTarget<CommonTargetParameters> {

maybeCreateRichRangeTarget(
isReversed: boolean,
endTarget: Target,
includeStart: boolean,
includeEnd: boolean,
): Target | undefined {
if (endTarget.isLine) {
return new LineTarget({
editor: this.editor,
isReversed,
contentRange: createContinuousLineRange(
this,
endTarget,
includeStart,
includeEnd,
),
});
}

return undefined;
endTarget: LineTarget,
): LineTarget {
return new LineTarget({
editor: this.editor,
isReversed,
contentRange: createContinuousLineRange(this, endTarget, true, true),
});
}

protected getCloneParameters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import {
TextLine,
} from "@cursorless/common";
import { BaseTarget, CommonTargetParameters, LineTarget } from ".";
import { Target } from "../../typings/target.types";
import { expandToFullLine } from "../../util/rangeUtils";
import { constructLineTarget } from "../../util/tryConstructTarget";
import { isSameType } from "../../util/typeUtils";
import { createContinuousLineRange } from "../targetUtil/createContinuousRange";

export class ParagraphTarget extends BaseTarget<CommonTargetParameters> {
Expand Down Expand Up @@ -75,37 +73,13 @@ export class ParagraphTarget extends BaseTarget<CommonTargetParameters> {

maybeCreateRichRangeTarget(
isReversed: boolean,
endTarget: Target,
includeStart: boolean,
includeEnd: boolean,
): Target | undefined {
if (isSameType(this, endTarget)) {
return new ParagraphTarget({
...this.getCloneParameters(),
isReversed,
contentRange: createContinuousLineRange(
this,
endTarget,
includeStart,
includeEnd,
),
});
}

if (endTarget.isLine) {
return new LineTarget({
editor: this.editor,
isReversed,
contentRange: createContinuousLineRange(
this,
endTarget,
includeStart,
includeEnd,
),
});
}

return undefined;
endTarget: ParagraphTarget,
): ParagraphTarget {
return new ParagraphTarget({
...this.getCloneParameters(),
isReversed,
contentRange: createContinuousLineRange(this, endTarget, true, true),
});
}

protected getCloneParameters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
PlainTarget,
} from ".";
import { Target } from "../../typings/target.types";
import { isSameType } from "../../util/typeUtils";
import {
createContinuousRange,
createContinuousRangeFromRanges,
Expand Down Expand Up @@ -101,26 +100,19 @@ export class ScopeTypeTarget extends BaseTarget<ScopeTypeTargetParameters> {

maybeCreateRichRangeTarget(
isReversed: boolean,
endTarget: Target,
includeStart: boolean,
includeEnd: boolean,
): Target | undefined {
if (
!includeStart ||
!includeEnd ||
!isSameType(this, endTarget) ||
this.scopeTypeType_ !== endTarget.scopeTypeType_
) {
return undefined;
endTarget: ScopeTypeTarget,
): ScopeTypeTarget | null {
if (this.scopeTypeType_ !== endTarget.scopeTypeType_) {
return null;
}

const contentRemovalRange =
this.removalRange_ != null || endTarget.removalRange_ != null
? createContinuousRangeFromRanges(
this.removalRange_ ?? this.contentRange,
endTarget.removalRange_ ?? endTarget.contentRange,
includeStart,
includeEnd,
true,
true,
)
: undefined;

Expand All @@ -130,12 +122,7 @@ export class ScopeTypeTarget extends BaseTarget<ScopeTypeTargetParameters> {
leadingDelimiterRange: this.leadingDelimiterRange_,
trailingDelimiterRange: endTarget.trailingDelimiterRange_,
removalRange: contentRemovalRange,
contentRange: createContinuousRange(
this,
endTarget,
includeStart,
includeEnd,
),
contentRange: createContinuousRange(this, endTarget, true, true),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class SubTokenWordTarget extends BaseTarget<SubTokenTargetParameters> {
return getDelimitedSequenceRemovalRange(this);
}

createRichRangeTarget(
maybeCreateRichRangeTarget(
isReversed: boolean,
endTarget: SubTokenWordTarget,
): SubTokenWordTarget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export class UntypedTarget extends BaseTarget<UntypedTargetParameters> {
: getTokenRemovalRange(this);
}

maybeCreateRichRangeTarget(): undefined {
return undefined;
maybeCreateRichRangeTarget(): null {
return null;
}

protected getCloneParameters() {
Expand Down
35 changes: 22 additions & 13 deletions packages/cursorless-engine/src/typings/target.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,33 @@ export interface Target {
getRemovalHighlightRange(): Range;
withThatTarget(thatTarget: Target): Target;
withContentRange(contentRange: Range): Target;

/**
* Attempt to create a range target that preserves some of the semantics of
* this target. Most targets will return `undefined` for targets not of the
* same type, and for targets of the same type, inherit some of the args from
* the two targets. Trailing delimiter should come from end target, leading
* from start target, etc.
* Targets use this function to determine what happens when a range target is
* created from two targets of the same type. This function is called by
* {@link createContinuousRangeTarget} to create the range target if both
* sides of the range are included and are of the same type.
*
* The newly created range target can inherit some of the args from the two
* targets. Trailing delimiter should come from end target, leading from start
* target, etc.
*
* Note that you likely don't want to call this function directly; it is
* designed to be used by {@link createContinuousRangeTarget}.
* @param isReversed
* @param endTarget
* If for whatever reason it doesn't make sense to create a rich range target
* from the two targets, this function should return null. For example,
* {@link ScopeTypeTarget} returns null if the two targets have different
* scope types, and {@link UntypedTarget} returns null because it never makes
* sense to create a rich range target from two untyped targets.
*
* @param isReversed Indicates whether the range is reversed.
* @param endTarget The end target of the range.
* @returns The new target of the same type as the two targets, corresponding
* to an inclusive range between the two targets.
*/
maybeCreateRichRangeTarget(
isReversed: boolean,
endTarget: Target,
includeStart: boolean,
includeEnd: boolean,
): Target | undefined;
endTarget: ThisType<this> & Target,
): (ThisType<this> & Target) | null;

/** Constructs removal edit */
constructRemovalEdit(): EditWithRangeUpdater;
isEqual(target: Target): boolean;
Expand Down

0 comments on commit 6976c5f

Please sign in to comment.