Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leading/trailing infers token on empty range #836

Merged
merged 7 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
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;
}
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}]}]
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}]}]