Skip to content

Commit

Permalink
Leading/trailing infers token on empty range (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Jul 12, 2022
1 parent c3e1ffb commit ca132db
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/actions/Clear.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PlainTarget from "../processTargets/targets/PlainTarget";
import { PlainTarget } from "../processTargets/targets";
import { Target } from "../typings/target.types";
import { Graph } from "../typings/Types";
import { setSelectionsAndFocusEditor } from "../util/setSelectionsAndFocusEditor";
Expand Down
2 changes: 1 addition & 1 deletion src/actions/CutCopy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PlainTarget from "../processTargets/targets/PlainTarget";
import { PlainTarget } from "../processTargets/targets";
import { Target } from "../typings/target.types";
import { Graph } from "../typings/Types";
import { getOutsideOverflow } from "../util/targetUtils";
Expand Down
2 changes: 1 addition & 1 deletion src/actions/EditNew/runNotebookCellTargets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { commands, Selection } from "vscode";
import { NotebookCellPositionTarget } from "../../processTargets/targets/NotebookCellTarget";
import { NotebookCellPositionTarget } from "../../processTargets/targets";
import { Target } from "../../typings/target.types";
import { Graph } from "../../typings/Types";
import { createThatMark, ensureSingleTarget } from "../../util/targetUtils";
Expand Down
8 changes: 6 additions & 2 deletions src/processTargets/marks/CursorStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import { ProcessedTargetsContext } from "../../typings/Types";
import { isReversed } from "../../util/selectionUtils";
import { MarkStage } from "../PipelineStages.types";
import WeakTarget from "../targets/WeakTarget";
import TokenTarget from "../targets/TokenTarget";

export default class CursorStage implements MarkStage {
constructor(_modifier: CursorMark) {}

run(context: ProcessedTargetsContext): Target[] {
return context.currentSelections.map((selection) => {
return new WeakTarget({
const parameters = {
editor: selection.editor,
isReversed: isReversed(selection.selection),
contentRange: selection.selection,
});
};
return selection.selection.isEmpty
? new WeakTarget(parameters)
: new TokenTarget(parameters);
});
}
}
2 changes: 1 addition & 1 deletion src/processTargets/marks/DecoratedSymbolStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Target } from "../../typings/target.types";
import { DecoratedSymbolMark } from "../../typings/targetDescriptor.types";
import { ProcessedTargetsContext } from "../../typings/Types";
import { MarkStage } from "../PipelineStages.types";
import WeakTarget from "../targets/WeakTarget";
import { WeakTarget } from "../targets";

export default class implements MarkStage {
constructor(private modifier: DecoratedSymbolMark) {}
Expand Down
2 changes: 1 addition & 1 deletion src/processTargets/modifiers/ItemStage/ItemStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ProcessedTargetsContext } from "../../../typings/Types";
import { getInsertionDelimiter } from "../../../util/nodeSelectors";
import { getRangeLength } from "../../../util/rangeUtils";
import { ModifierStage } from "../../PipelineStages.types";
import ScopeTypeTarget from "../../targets/ScopeTypeTarget";
import { ScopeTypeTarget } from "../../targets";
import ContainingSyntaxScopeStage, {
SimpleContainingScopeModifier,
} from "../scopeTypeStages/ContainingSyntaxScopeStage";
Expand Down
25 changes: 15 additions & 10 deletions src/processTargets/modifiers/LeadingTrailingStages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,32 @@ import {
} from "../../typings/targetDescriptor.types";
import { ProcessedTargetsContext } from "../../typings/Types";
import { ModifierStage } from "../PipelineStages.types";
import { weakContainingTokenStage } from "./commonWeakContainingScopeStages";

export class LeadingStage implements ModifierStage {
constructor(private modifier: LeadingModifier) {}

run(context: ProcessedTargetsContext, target: Target): Target[] {
const leading = target.getLeadingDelimiterTarget();
if (leading == null) {
throw Error("No available leading range");
}
return [leading];
return weakContainingTokenStage.run(context, target).map((target) => {
const leading = target.getLeadingDelimiterTarget();
if (leading == null) {
throw Error("No available leading delimiter range");
}
return leading;
});
}
}

export class TrailingStage implements ModifierStage {
constructor(private modifier: TrailingModifier) {}

run(context: ProcessedTargetsContext, target: Target): Target[] {
const trailing = target.getTrailingDelimiterTarget();
if (trailing == null) {
throw Error("No available trailing range");
}
return [trailing];
return weakContainingTokenStage.run(context, target).map((target) => {
const trailing = target.getTrailingDelimiterTarget();
if (trailing == null) {
throw Error("No available trailing delimiter range");
}
return trailing;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ export const weakContainingLineStage = new ModifyIfWeakStage({
scopeType: { type: "line" },
},
});

export const weakContainingTokenStage = new ModifyIfWeakStage({
type: "modifyIfWeak",
modifier: {
type: "containingScope",
scopeType: { type: "token" },
},
});
2 changes: 1 addition & 1 deletion src/processTargets/modifiers/scopeTypeStages/TokenStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { ProcessedTargetsContext } from "../../../typings/Types";
import { getTokensInRange, PartialToken } from "../../../util/getTokensInRange";
import { ModifierStage } from "../../PipelineStages.types";
import TokenTarget from "../../targets/TokenTarget";
import { TokenTarget } from "../../targets";

export default class implements ModifierStage {
constructor(private modifier: ContainingScopeModifier | EveryScopeModifier) {}
Expand Down
3 changes: 1 addition & 2 deletions src/processTargets/processTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { ensureSingleEditor } from "../util/targetUtils";
import getMarkStage from "./getMarkStage";
import getModifierStage from "./getModifierStage";
import { ModifierStage } from "./PipelineStages.types";
import PlainTarget from "./targets/PlainTarget";
import PositionTarget from "./targets/PositionTarget";
import { PlainTarget, PositionTarget } from "./targets";

/**
* Converts the abstract target descriptions provided by the user to a concrete
Expand Down
2 changes: 1 addition & 1 deletion src/processTargets/targets/TokenTarget.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Range } from "vscode";
import { BaseTarget } from ".";
import { Target } from "../../typings/target.types";
import {
getTokenLeadingDelimiterTarget,
getTokenRemovalRange,
getTokenTrailingDelimiterTarget,
} from "../targetUtil/insertionRemovalBehaviors/TokenInsertionRemovalBehavior";
import BaseTarget from "./BaseTarget";

export default class TokenTarget extends BaseTarget {
insertionDelimiter = " ";
Expand Down
25 changes: 25 additions & 0 deletions src/test/suite/fixtures/recorded/selectionTypes/clearLeading.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
languageId: plaintext
command:
spokenForm: clear leading
version: 2
targets:
- type: primitive
modifiers:
- {type: leading}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: aaa bbb ccc
selections:
- anchor: {line: 0, character: 6}
active: {line: 0, character: 6}
marks: {}
finalState:
documentContents: aaabbb ccc
selections:
- anchor: {line: 0, character: 3}
active: {line: 0, character: 3}
thatMark:
- anchor: {line: 0, character: 3}
active: {line: 0, character: 3}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: leading}]}]
25 changes: 25 additions & 0 deletions src/test/suite/fixtures/recorded/selectionTypes/clearTrailing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
languageId: plaintext
command:
spokenForm: clear trailing
version: 2
targets:
- type: primitive
modifiers:
- {type: trailing}
usePrePhraseSnapshot: true
action: {name: clearAndSetSelection}
initialState:
documentContents: aaa bbb ccc
selections:
- anchor: {line: 0, character: 6}
active: {line: 0, character: 6}
marks: {}
finalState:
documentContents: aaa bbbccc
selections:
- anchor: {line: 0, character: 7}
active: {line: 0, character: 7}
thatMark:
- anchor: {line: 0, character: 7}
active: {line: 0, character: 7}
fullTargets: [{type: primitive, mark: {type: cursor}, modifiers: [{type: trailing}]}]
8 changes: 5 additions & 3 deletions src/util/tryConstructTarget.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Range, TextEditor } from "vscode";
import { CommonTargetParameters } from "../processTargets/targets/BaseTarget";
import LineTarget from "../processTargets/targets/LineTarget";
import PlainTarget from "../processTargets/targets/PlainTarget";
import {
CommonTargetParameters,
LineTarget,
PlainTarget,
} from "../processTargets/targets";
import { Target } from "../typings/target.types";

type TargetConstructor<T extends Target> = new (
Expand Down

0 comments on commit ca132db

Please sign in to comment.