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 all commits
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
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";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried replacing these lines with import { TokenTarget, WeakTarget } from "../targets"; but that gives me the following error on extension activation: Activating extension 'pokey.cursorless' failed: Class extends value undefined is not a constructor or null.. I tried replacing all references to targets to go through the target/index file but no go.

Copy link
Member

@pokey pokey Jul 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. I can take a look in a follow-up. I wonder if we need to do the same trick for our stages

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" },
},
});
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
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}]}]
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