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

👍 Make path of GinChaperon and GinPatch optional #141

Merged
merged 3 commits into from
Aug 20, 2024
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
12 changes: 8 additions & 4 deletions denops/gin/command/chaperon/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
validateOpts,
} from "jsr:@denops/std@^7.0.0/argument";
import { fillCmdArgs, normCmdArgs, parseSilent } from "../../util/cmd.ts";
import { ensurePath } from "../../util/ensure_path.ts";
import { exec } from "./command.ts";

export function main(denops: Denops): void {
Expand Down Expand Up @@ -46,8 +47,8 @@ async function command(
...builtinOpts,
]);

const [abspath] = parseResidue(residue);
await exec(denops, abspath, {
const [rawpath] = parseResidue(residue);
await exec(denops, await ensurePath(denops, rawpath), {
worktree: opts.worktree,
opener: opts.opener,
noOurs: "no-ours" in opts,
Expand All @@ -60,9 +61,12 @@ async function command(

function parseResidue(
residue: string[],
): [string] {
// GinChaperon [{options}] {path}
): [string | undefined] {
switch (residue.length) {
// GinChaperon [{options}]
case 0:
return [undefined];
// GinChaperon [{options}] {path}
case 1:
return [residue[0]];
default:
Expand Down
12 changes: 8 additions & 4 deletions denops/gin/command/patch/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
validateOpts,
} from "jsr:@denops/std@^7.0.0/argument";
import { fillCmdArgs, normCmdArgs, parseSilent } from "../../util/cmd.ts";
import { ensurePath } from "../../util/ensure_path.ts";
import { exec } from "./command.ts";

export function main(denops: Denops): void {
Expand Down Expand Up @@ -46,8 +47,8 @@ async function command(
...builtinOpts,
]);

const [abspath] = parseResidue(residue);
await exec(denops, abspath, {
const [rawpath] = parseResidue(residue);
await exec(denops, await ensurePath(denops, rawpath), {
worktree: opts.worktree,
noHead: "no-head" in opts,
noWorktree: "no-worktree" in opts,
Expand All @@ -60,9 +61,12 @@ async function command(

function parseResidue(
residue: string[],
): [string] {
// GinPatch [{options}] {path}
): [string | undefined] {
switch (residue.length) {
// GinPatch [{options}]
case 0:
return [undefined];
// GinPatch [{options}] {path}
case 1:
return [residue[0]];
default:
Expand Down
21 changes: 21 additions & 0 deletions denops/gin/util/ensure_path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Denops } from "jsr:@denops/std@^7.0.0";
import * as fn from "jsr:@denops/std@^7.0.0/function";

/**
* Ensure the path is absolute.
*
* It returns the absolute path of the given path. If the path is not given, it
* returns the absolute path of the current buffer.
*
* @param denops Denops instance.
* @param path Path to ensure.
* @returns Absolute path.
*/
export async function ensurePath(
denops: Denops,
path?: string,
): Promise<string> {
const bufname = await fn.expand(denops, path ?? "%") as string;
const abspath = await fn.fnamemodify(denops, bufname, ":p");
return abspath;
}
10 changes: 6 additions & 4 deletions doc/gin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,10 @@ COMMANDS *gin-commands*
directory. Commands call |cd|, |lcd|, and |tcd| respectively.

*:GinChaperon*
:GinChaperon[!] [++{option}...] {path}
:GinChaperon[!] [++{option}...] [{path}]
Open three main buffers (THEIRS, WORKTREE, and OURS) and three
supplemental buffers to solve conflicts on {path}.
supplemental buffers to solve conflicts on {path}. If no {path} is
specified, the default value is the current buffer.

The following options are valid as {++option}:

Expand Down Expand Up @@ -343,9 +344,10 @@ COMMANDS *gin-commands*
Use a bang (!) to forcibly open a buffer.

*:GinPatch*
:GinPatch[!] [{++option}...] {path}
:GinPatch[!] [{++option}...] [{path}]
Open three buffers (HEAD, INDEX, and WORKTREE) to patch changes of
{path}.
{path}. If no {path} is specified, the default value is the current
buffer.

The following options are valid as {++option}:

Expand Down