Skip to content

Commit

Permalink
👍 Use dispatch to execute command in actions
Browse files Browse the repository at this point in the history
To support persistent arguments in actions as well.
  • Loading branch information
lambdalisue committed Sep 24, 2023
1 parent a64111e commit 05762a2
Show file tree
Hide file tree
Showing 22 changed files with 84 additions and 108 deletions.
5 changes: 2 additions & 3 deletions denops/gin/action/add.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts";
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts";
import { define, GatherCandidates, Range } from "./core.ts";
import { exec as execBare } from "../command/bare/command.ts";

export type Candidate = { path: string };

Expand Down Expand Up @@ -36,12 +35,12 @@ async function doAdd(
gatherCandidates: GatherCandidates<Candidate>,
): Promise<void> {
const xs = await gatherCandidates(denops, bufnr, range);
await execBare(denops, [
await denops.dispatch("gin", "command", "silent", [
"add",
"--ignore-errors",
"--force",
...extraArgs,
"--",
...xs.map((x) => x.path),
], { stdoutIndicator: "null" });
]);
}
5 changes: 2 additions & 3 deletions denops/gin/action/branch_delete.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts";
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts";
import { define, GatherCandidates, Range } from "./core.ts";
import { exec as execBare } from "../command/bare/command.ts";

export type Candidate =
| { kind: "remote"; branch: string; remote: string }
Expand Down Expand Up @@ -43,15 +42,15 @@ async function doDelete(
case "alias":
continue;
case "remote":
await execBare(denops, [
await denops.dispatch("gin", "command", "", [
"push",
"--delete",
x.remote,
x.branch,
]);
break;
default:
await execBare(denops, [
await denops.dispatch("gin", "command", "", [
"branch",
force ? "-D" : "-d",
x.branch,
Expand Down
3 changes: 1 addition & 2 deletions denops/gin/action/branch_move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts";
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts";
import * as helper from "https://deno.land/x/denops_std@v5.0.1/helper/mod.ts";
import { define, GatherCandidates, Range } from "./core.ts";
import { exec as execBare } from "../command/bare/command.ts";

export type Candidate = { branch: string };

Expand Down Expand Up @@ -51,7 +50,7 @@ async function doMove(
await helper.echoerr(denops, "Cancelled");
return;
}
await execBare(denops, [
await denops.dispatch("gin", "command", "", [
"branch",
...(force ? ["--force"] : []),
"--move",
Expand Down
5 changes: 2 additions & 3 deletions denops/gin/action/branch_new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts";
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts";
import * as helper from "https://deno.land/x/denops_std@v5.0.1/helper/mod.ts";
import { define, GatherCandidates, Range } from "./core.ts";
import { exec as execBare } from "../command/bare/command.ts";

export type Candidate = { target?: string };

Expand Down Expand Up @@ -54,7 +53,7 @@ async function doNew(
await helper.echoerr(denops, "Cancelled");
return;
}
await execBare(denops, [
await denops.dispatch("gin", "command", "", [
"switch",
force ? "-C" : "-c",
name,
Expand All @@ -76,5 +75,5 @@ async function doNewOrphan(
await helper.echoerr(denops, "Cancelled");
return;
}
await execBare(denops, ["switch", "--orphan", name]);
await denops.dispatch("gin", "command", "", ["switch", "--orphan", name]);
}
21 changes: 9 additions & 12 deletions denops/gin/action/chaperon.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts";
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts";
import { alias, define, GatherCandidates, Range } from "./core.ts";
import {
exec as execChaperon,
ExecOptions as ExecChaperonOptions,
} from "../command/chaperon/command.ts";

export type Candidate = { path: string };

Expand All @@ -19,21 +15,21 @@ export async function init(
bufnr,
"chaperon:both",
(denops, bufnr, range) =>
doChaperon(denops, bufnr, range, {}, gatherCandidates),
doChaperon(denops, bufnr, range, [], gatherCandidates),
);
await define(
denops,
bufnr,
"chaperon:theirs",
(denops, bufnr, range) =>
doChaperon(denops, bufnr, range, { noOurs: true }, gatherCandidates),
doChaperon(denops, bufnr, range, ["++no-ours"], gatherCandidates),
);
await define(
denops,
bufnr,
"chaperon:ours",
(denops, bufnr, range) =>
doChaperon(denops, bufnr, range, { noTheirs: true }, gatherCandidates),
doChaperon(denops, bufnr, range, ["++no-theirs"], gatherCandidates),
);
await alias(
denops,
Expand All @@ -48,14 +44,15 @@ async function doChaperon(
denops: Denops,
bufnr: number,
range: Range,
options: ExecChaperonOptions,
extraArgs: string[],
gatherCandidates: GatherCandidates<Candidate>,
): Promise<void> {
const xs = await gatherCandidates(denops, bufnr, range);
for (const x of xs) {
await execChaperon(denops, x.path, {
opener: "tabedit",
...options,
});
await denops.dispatch("gin", "chaperon:command", "", "", [
"++opener=tabedit",
...extraArgs,
x.path,
]);
}
}
3 changes: 1 addition & 2 deletions denops/gin/action/cherry_pick.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts";
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts";
import { define, GatherCandidates, Range } from "./core.ts";
import { exec as execBare } from "../command/bare/command.ts";

export type Candidate = { commit: string };

Expand Down Expand Up @@ -47,7 +46,7 @@ async function doCherryPick(
if (!x) {
return;
}
await execBare(denops, [
await denops.dispatch("gin", "command", "", [
"cherry-pick",
...(mainline ? [] : ["--mainline", mainline]),
x.commit,
Expand Down
23 changes: 10 additions & 13 deletions denops/gin/action/diff.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts";
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts";
import { alias, define, GatherCandidates, Range } from "./core.ts";
import {
exec as execDiff,
ExecOptions as ExecDiffOptions,
} from "../command/diff/command.ts";

export type Candidate = { path: string };

Expand All @@ -26,7 +22,7 @@ export async function init(
bufnr,
`diff:local:${opener}`,
(denops, bufnr, range) =>
doDiff(denops, bufnr, range, opener, {}, gatherCandidates),
doDiff(denops, bufnr, range, opener, [], gatherCandidates),
);
await define(
denops,
Expand All @@ -38,7 +34,7 @@ export async function init(
bufnr,
range,
opener,
{ flags: { "cached": "" } },
["--cached"],
gatherCandidates,
),
);
Expand All @@ -52,7 +48,7 @@ export async function init(
bufnr,
range,
opener,
{ commitish: "HEAD" },
["HEAD"],
gatherCandidates,
),
);
Expand Down Expand Up @@ -89,15 +85,16 @@ async function doDiff(
bufnr: number,
range: Range,
opener: string,
options: ExecDiffOptions,
extraArgs: string[],
gatherCandidates: GatherCandidates<Candidate>,
): Promise<void> {
const xs = await gatherCandidates(denops, bufnr, range);
for (const x of xs) {
await execDiff(denops, {
opener,
paths: [x.path],
...options,
});
await denops.dispatch("gin", "diff:command", "", "", [
`++opener=${opener}`,
...extraArgs,
"--",
x.path,
]);
}
}
23 changes: 11 additions & 12 deletions denops/gin/action/diff_smart.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts";
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts";
import { alias, define, GatherCandidates, Range } from "./core.ts";
import { exec as execDiff } from "../command/diff/command.ts";

export type Candidate = { path: string; XY: string };

Expand Down Expand Up @@ -51,18 +50,18 @@ async function doDiffSmart(
const xs = await gatherCandidates(denops, bufnr, range);
for (const x of xs) {
if (x.XY.startsWith(".")) {
await execDiff(denops, {
opener,
paths: [x.path],
});
await denops.dispatch("gin", "diff:command", "", "", [
`++opener=${opener}`,
"--",
x.path,
]);
} else {
await execDiff(denops, {
opener,
paths: [x.path],
flags: {
"cached": "",
},
});
await denops.dispatch("gin", "diff:command", "", "", [
`++opener=${opener}`,
"--cached",
"--",
x.path,
]);
}
}
}
19 changes: 8 additions & 11 deletions denops/gin/action/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts";
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts";
import * as buffer from "https://deno.land/x/denops_std@v5.0.1/buffer/mod.ts";
import { alias, define, GatherCandidates, Range } from "./core.ts";
import {
exec as execEdit,
ExecOptions as ExecEditOptions,
} from "../command/edit/command.ts";

export type Candidate = { path: string };

Expand Down Expand Up @@ -34,7 +30,7 @@ export async function init(
bufnr,
`edit:cached:${opener}`,
(denops, bufnr, range) =>
doEdit(denops, bufnr, range, opener, {}, gatherCandidates),
doEdit(denops, bufnr, range, opener, [], gatherCandidates),
);
await define(
denops,
Expand All @@ -46,7 +42,7 @@ export async function init(
bufnr,
range,
opener,
{ commitish: "HEAD" },
["HEAD"],
gatherCandidates,
),
);
Expand Down Expand Up @@ -83,15 +79,16 @@ async function doEdit(
bufnr: number,
range: Range,
opener: string,
options: ExecEditOptions,
extraArgs: string[],
gatherCandidates: GatherCandidates<Candidate>,
): Promise<void> {
const xs = await gatherCandidates(denops, bufnr, range);
for (const x of xs) {
await execEdit(denops, x.path, {
opener,
...options,
});
await denops.dispatch("gin", "edit:command", "", "", [
`++opener=${opener}`,
...extraArgs,
x.path,
]);
}
}

Expand Down
3 changes: 1 addition & 2 deletions denops/gin/action/merge.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts";
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts";
import { alias, define, GatherCandidates, Range } from "./core.ts";
import { exec as execBare } from "../command/bare/command.ts";

export type Candidate = { commit: string };

Expand Down Expand Up @@ -53,7 +52,7 @@ async function doMerge(
if (!x) {
return;
}
await execBare(denops, [
await denops.dispatch("gin", "command", "", [
"merge",
`--${method}`,
x.commit,
Expand Down
21 changes: 9 additions & 12 deletions denops/gin/action/patch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts";
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts";
import { alias, define, GatherCandidates, Range } from "./core.ts";
import {
exec as execPatch,
ExecOptions as ExecPatchOptions,
} from "../command/patch/command.ts";

export type Candidate = { path: string };

Expand All @@ -19,21 +15,21 @@ export async function init(
bufnr,
"patch:both",
(denops, bufnr, range) =>
doPatch(denops, bufnr, range, {}, gatherCandidates),
doPatch(denops, bufnr, range, [], gatherCandidates),
);
await define(
denops,
bufnr,
"patch:head",
(denops, bufnr, range) =>
doPatch(denops, bufnr, range, { noWorktree: true }, gatherCandidates),
doPatch(denops, bufnr, range, ["++no-worktree"], gatherCandidates),
);
await define(
denops,
bufnr,
"patch:worktree",
(denops, bufnr, range) =>
doPatch(denops, bufnr, range, { noHead: true }, gatherCandidates),
doPatch(denops, bufnr, range, ["++no-head"], gatherCandidates),
);
await alias(
denops,
Expand All @@ -48,14 +44,15 @@ async function doPatch(
denops: Denops,
bufnr: number,
range: Range,
options: ExecPatchOptions,
extraArgs: string[],
gatherCandidates: GatherCandidates<Candidate>,
): Promise<void> {
const xs = await gatherCandidates(denops, bufnr, range);
for (const x of xs) {
await execPatch(denops, x.path, {
opener: "tabedit",
...options,
});
await denops.dispatch("gin", "patch:command", "", "", [
`++opener=tabedit`,
...extraArgs,
x.path,
]);
}
}
Loading

0 comments on commit 05762a2

Please sign in to comment.