Skip to content

Commit

Permalink
Clean up types
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Jan 19, 2025
1 parent 3817301 commit b248af6
Show file tree
Hide file tree
Showing 3 changed files with 554 additions and 47 deletions.
72 changes: 25 additions & 47 deletions packages/common/src/types/command/legacy/ActionDescriptorV6.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { DestinationDescriptorV6 } from "./DestinationDescriptorV6.types";
import type {
PartialTargetDescriptor,
ScopeType,
} from "../PartialTargetDescriptor.types";
import type { DestinationDescriptor } from "../DestinationDescriptor.types";
PartialTargetDescriptorV6,
ScopeTypeV6,
} from "./PartialTargetDescriptorV6.types";

/**
* A simple action takes only a single target and no other arguments.
*/
const simpleActionNames = [
const _simpleActionNames = [
"addSelection",
"addSelectionAfter",
"addSelectionBefore",
Expand Down Expand Up @@ -60,42 +60,20 @@ const simpleActionNames = [
"private.showParseTree",
] as const;

const complexActionNames = [
"callAsFunction",
"editNew",
"executeCommand",
"generateSnippet",
"getText",
"highlight",
"insertSnippet",
"moveToTarget",
"pasteFromClipboard",
"replace",
"replaceWithTarget",
"rewrapWithPairedDelimiter",
"swapTargets",
"wrapWithPairedDelimiter",
"wrapWithSnippet",
"parsed",
] as const;

const actionNames = [...simpleActionNames, ...complexActionNames] as const;

type SimpleActionName = (typeof simpleActionNames)[number];
type ActionType = (typeof actionNames)[number];
type SimpleActionName = (typeof _simpleActionNames)[number];

/**
* A simple action takes only a single target and no other arguments.
*/
interface SimpleActionDescriptor {
name: SimpleActionName;
target: PartialTargetDescriptor;
target: PartialTargetDescriptorV6;
}

interface BringMoveActionDescriptor {
name: "replaceWithTarget" | "moveToTarget";
source: PartialTargetDescriptor;
destination: DestinationDescriptor;
source: PartialTargetDescriptorV6;
destination: DestinationDescriptorV6;
}

interface CallActionDescriptor {
Expand All @@ -104,37 +82,37 @@ interface CallActionDescriptor {
/**
* The target to use as the function to be called.
*/
callee: PartialTargetDescriptor;
callee: PartialTargetDescriptorV6;

/**
* The target to wrap in a function call.
*/
argument: PartialTargetDescriptor;
argument: PartialTargetDescriptorV6;
}

interface SwapActionDescriptor {
name: "swapTargets";
target1: PartialTargetDescriptor;
target2: PartialTargetDescriptor;
target1: PartialTargetDescriptorV6;
target2: PartialTargetDescriptorV6;
}

interface WrapWithPairedDelimiterActionDescriptor {
name: "wrapWithPairedDelimiter" | "rewrapWithPairedDelimiter";
left: string;
right: string;
target: PartialTargetDescriptor;
target: PartialTargetDescriptorV6;
}

interface PasteActionDescriptor {
name: "pasteFromClipboard";
destination: DestinationDescriptor;
destination: DestinationDescriptorV6;
}

interface GenerateSnippetActionDescriptor {
name: "generateSnippet";
dirPath?: string;
snippetName?: string;
target: PartialTargetDescriptor;
target: PartialTargetDescriptorV6;
}

interface NamedInsertSnippetArg {
Expand All @@ -145,15 +123,15 @@ interface NamedInsertSnippetArg {
interface CustomInsertSnippetArg {
type: "custom";
body: string;
scopeTypes?: ScopeType[];
scopeTypes?: ScopeTypeV6[];
substitutions?: Record<string, string>;
}
type InsertSnippetArg = NamedInsertSnippetArg | CustomInsertSnippetArg;

interface InsertSnippetActionDescriptor {
name: "insertSnippet";
snippetDescription: InsertSnippetArg;
destination: DestinationDescriptor;
destination: DestinationDescriptorV6;
}

interface NamedWrapWithSnippetArg {
Expand All @@ -165,14 +143,14 @@ interface CustomWrapWithSnippetArg {
type: "custom";
body: string;
variableName?: string;
scopeType?: ScopeType;
scopeType?: ScopeTypeV6;
}
type WrapWithSnippetArg = NamedWrapWithSnippetArg | CustomWrapWithSnippetArg;

interface WrapWithSnippetActionDescriptor {
name: "wrapWithSnippet";
snippetDescription: WrapWithSnippetArg;
target: PartialTargetDescriptor;
target: PartialTargetDescriptorV6;
}

interface ExecuteCommandOptions {
Expand All @@ -187,26 +165,26 @@ interface ExecuteCommandActionDescriptor {
name: "executeCommand";
commandId: string;
options?: ExecuteCommandOptions;
target: PartialTargetDescriptor;
target: PartialTargetDescriptorV6;
}

type ReplaceWith = string[] | { start: number };

interface ReplaceActionDescriptor {
name: "replace";
replaceWith: ReplaceWith;
destination: DestinationDescriptor;
destination: DestinationDescriptorV6;
}

interface HighlightActionDescriptor {
name: "highlight";
highlightId?: string;
target: PartialTargetDescriptor;
target: PartialTargetDescriptorV6;
}

interface EditNewActionDescriptor {
name: "editNew";
destination: DestinationDescriptor;
destination: DestinationDescriptorV6;
}

interface GetTextActionOptions {
Expand All @@ -217,7 +195,7 @@ interface GetTextActionOptions {
interface GetTextActionDescriptor {
name: "getText";
options?: GetTextActionOptions;
target: PartialTargetDescriptor;
target: PartialTargetDescriptorV6;
}

interface ParsedActionDescriptor {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type {
PartialListTargetDescriptorV6,
PartialPrimitiveTargetDescriptorV6,
PartialRangeTargetDescriptorV6,
} from "./PartialTargetDescriptorV6.types";

/**
* The insertion mode to use when inserting relative to a target.
* - `before` inserts before the target. Depending on the target, a delimiter
* may be inserted after the inserted text.
* - `after` inserts after the target. Depending on the target, a delimiter may
* be inserted before the inserted text.
* - `to` replaces the target. However, this insertion mode may also be used
* when the target is really only a pseudo-target. For example, you could say
* `"bring type air to bat"` even if `bat` doesn't already have a type. In
* that case, `"take type bat"` wouldn't work, so `"type bat"` is really just
* a pseudo-target in that situation.
*/
type InsertionMode = "before" | "after" | "to";

interface PrimitiveDestinationDescriptor {
type: "primitive";

/**
* The insertion mode to use when inserting relative to {@link target}.
*/
insertionMode: InsertionMode;

target:
| PartialPrimitiveTargetDescriptorV6
| PartialRangeTargetDescriptorV6
| PartialListTargetDescriptorV6;
}

/**
* A list of destinations. This is used when the user uses more than one insertion mode
* in a single command. For example, `"bring air after bat and before cap"`.
*/
interface ListDestinationDescriptor {
type: "list";
destinations: PrimitiveDestinationDescriptor[];
}

/**
* An implicit destination. This is used for e.g. `"bring air"` (note the user
* doesn't explicitly specify the destination), or `"snip funk"`.
*/
interface ImplicitDestinationDescriptor {
type: "implicit";
}

export type DestinationDescriptorV6 =
| ListDestinationDescriptor
| PrimitiveDestinationDescriptor
| ImplicitDestinationDescriptor;
Loading

0 comments on commit b248af6

Please sign in to comment.