Skip to content

Commit

Permalink
Merge pull request #142 from lambdalisue/refine-gin-browse
Browse files Browse the repository at this point in the history
💥 Refine `GinBrowse` command
  • Loading branch information
lambdalisue authored Aug 20, 2024
2 parents a5922b5 + 5e44a6e commit 4b80ce3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
3 changes: 2 additions & 1 deletion denops/gin/action/browse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ async function doBrowse(
for (const x of xs) {
await denops.dispatch("gin", "browse:command", [
...extraArgs,
...(x.path ? [`--path=${x.path}`] : []),
...(x.path ? [] : ["++repository"]),
x.commit,
...(x.path ? [x.path] : []),
]);
}
}
38 changes: 19 additions & 19 deletions denops/gin/command/browse/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Denops } from "jsr:@denops/std@^7.0.0";
import { unnullish } from "jsr:@lambdalisue/unnullish@^1.0.0";
import { assert, ensure, is } from "jsr:@core/unknownutil@^4.0.0";
import * as batch from "jsr:@denops/std@^7.0.0/batch";
import * as fn from "jsr:@denops/std@^7.0.0/function";
Expand Down Expand Up @@ -44,22 +43,20 @@ async function command(
const [opts, flags, residue] = parse(args);
validateOpts(opts, [
"worktree",
"repository",
"yank",
]);

const commitish = parseResidue(residue);
const path = unnullish(
await ensurePath(denops, flags["path"]),
(p) => formatPath(p, options.range),
);
const [commitish, rawpath] = parseResidue(residue);
const path = formatPath(await ensurePath(denops, rawpath), options.range);
await exec(denops, commitish ?? "HEAD", {
worktree: opts.worktree,
yank: opts.yank === "" ? true : (opts.yank ?? false),
noBrowser: ("n" in flags || "no-browser" in flags),
remote: ensure(flags.remote, is.UnionOf([is.Undefined, is.String]), {
"message": "REMOTE in --remote={REMOTE} must be string",
}),
path,
path: "repository" in opts ? undefined : path,
home: "home" in flags,
commit: "commit" in flags,
pr: "pr" in flags,
Expand All @@ -69,33 +66,36 @@ async function command(

function parseResidue(
residue: string[],
): string | undefined {
// GinBrowse [{options}] {commitish}
): [string | undefined, string | undefined] {
switch (residue.length) {
// GinBrowse [{options}]
case 0:
return undefined;
return [undefined, undefined];
// GinBrowse [{options}] {commitish}
case 1:
return residue[0];
return [residue[0], undefined];
// GinBrowse [{options}] {commitish} {path}
case 2:
return [residue[0], residue[1]];
default:
throw new Error("Invalid number of arguments");
}
}

async function ensurePath(
denops: Denops,
path?: unknown,
): Promise<string | undefined> {
path?: string,
): Promise<string> {
const bufname = await fn.expand(denops, path ?? "%") as string;
const abspath = await fn.fnamemodify(denops, bufname, ":p");
if (path) {
return ensure(path, is.String, {
message: "PATH in --path={PATH} must be string",
});
return abspath;
}
const [bufname, buftype, cwd] = await batch.collect(denops, (denops) => [
fn.expand(denops, "%:p") as Promise<string>,
const [buftype, cwd] = await batch.collect(denops, (denops) => [
fn.getbufvar(denops, "%", "&buftype") as Promise<string>,
fn.getcwd(denops),
]);
return buftype ? cwd : bufname;
return buftype ? cwd : abspath;
}

function formatPath(path: string, range?: Range): string {
Expand Down
13 changes: 9 additions & 4 deletions doc/gin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,18 @@ COMMANDS *gin-commands*
Use a bang (!) to forcibly open a buffer.

*:GinBrowse*
:GinBrowse [{++option}...] [{flags}] [{commitish}]
:[range]GinBrowse [{++option}...] [{flags}] [{commitish}] [{path}]
Open a system browser to visit the hosting service webpage of the
repository. If no {commitish} is given, it defaults to HEAD.
repository. If no {commitish} is given, it defaults to HEAD. If no
{path} is given, it defaults to the current buffer or the current
working directory if the current buffer is not a file.

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

++repository
Open the repository page instead of the blob or commit page.
If this option is given, {path} is ignored.

++yank={regname}
Yank the URL to the clipboard.
{regname} is optional and defaults to |v:register|.
Expand All @@ -190,15 +196,14 @@ COMMANDS *gin-commands*
--commit
--home
-n, --no-browser
--path={PATH}
--permalink
--pr
--remote={REMOTE}

It uses "git-browse" command as a module internally so see usage of
that for detail about each {flags}.

https://deno.land/x/git_browse
https://jsr.io/@lambdalisue/git-browse

See |g:gin_browse_aliases| to define aliases of REMOTE to support
arbitrary domain (e.g. GitHub Enterprise).
Expand Down

0 comments on commit 4b80ce3

Please sign in to comment.