Skip to content

Commit

Permalink
Treat leading and trailing modifier on empty content range as a token
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Arvidsson committed Jul 6, 2022
1 parent d9fa044 commit 9fc6923
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/processTargets/modifiers/LeadingTrailingStages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import {
TrailingModifier,
} from "../../typings/targetDescriptor.types";
import { ProcessedTargetsContext } from "../../typings/Types";
import getModifierStage from "../getModifierStage";
import { ModifierStage } from "../PipelineStages.types";

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

run(context: ProcessedTargetsContext, target: Target): Target[] {
const leading = target.getLeadingDelimiterTarget();
const leading = getTargetToUse(context, target).getLeadingDelimiterTarget();
if (leading == null) {
throw Error("No available leading range");
throw Error("No available leading delimiter range");
}
return [leading];
}
Expand All @@ -22,10 +23,25 @@ export class TrailingStage implements ModifierStage {
constructor(private modifier: TrailingModifier) {}

run(context: ProcessedTargetsContext, target: Target): Target[] {
const trailing = target.getTrailingDelimiterTarget();
const trailing = getTargetToUse(
context,
target
).getTrailingDelimiterTarget();
if (trailing == null) {
throw Error("No available trailing range");
throw Error("No available trailing delimiter range");
}
return [trailing];
}
}

/**If the content range of the given target is empty convert it to a token target. If not just return it unmodified. */
function getTargetToUse(context: ProcessedTargetsContext, target: Target) {
if (target.contentRange.isEmpty) {
const tokenStage = getModifierStage({
type: "containingScope",
scopeType: { type: "token" },
});
return tokenStage.run(context, target)[0];
}
return target;
}
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}]}]

0 comments on commit 9fc6923

Please sign in to comment.