-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from lambdalisue/gin-browse
👍 Add `GinBrowse` command
- Loading branch information
Showing
6 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts"; | ||
import { unnullish } from "https://deno.land/x/unnullish@v1.0.1/mod.ts"; | ||
import { assert, is } from "https://deno.land/x/unknownutil@v3.4.0/mod.ts"; | ||
import { systemopen } from "https://deno.land/x/systemopen@v0.2.0/mod.ts"; | ||
import { | ||
getURL, | ||
Options, | ||
} from "https://deno.land/x/git_browse@v0.3.0/bin/browse.ts"; | ||
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts"; | ||
import * as vars from "https://deno.land/x/denops_std@v5.0.1/variable/mod.ts"; | ||
import * as path from "https://deno.land/std@0.192.0/path/mod.ts"; | ||
import * as option from "https://deno.land/x/denops_std@v5.0.1/option/mod.ts"; | ||
import { findWorktreeFromDenops } from "../../git/worktree.ts"; | ||
import { yank } from "../../util/yank.ts"; | ||
|
||
export type ExecOptions = Omit<Options, "cwd" | "aliases"> & { | ||
worktree?: string; | ||
yank?: boolean; | ||
noBrowser?: boolean; | ||
}; | ||
|
||
export async function exec( | ||
denops: Denops, | ||
commitish: string, | ||
options: ExecOptions = {}, | ||
): Promise<void> { | ||
const [verbose, aliases] = await batch.collect(denops, (denops) => [ | ||
option.verbose.get(denops), | ||
vars.g.get(denops, "git_browse_aliases", {}), | ||
]); | ||
assert(aliases, is.RecordOf(is.String), { | ||
message: "g:git_browse_aliases must be a string dictionary", | ||
}); | ||
|
||
const worktree = await findWorktreeFromDenops(denops, { | ||
worktree: options.worktree, | ||
verbose: !!verbose, | ||
}); | ||
|
||
options.path = unnullish( | ||
options.path, | ||
(p) => path.isAbsolute(p) ? path.relative(worktree, p) : p, | ||
); | ||
options.path = unnullish( | ||
options.path, | ||
(p) => p === "" ? "." : p, | ||
); | ||
const url = await getURL(commitish, { | ||
cwd: worktree, | ||
remote: options.remote, | ||
path: options.path, | ||
home: options.home, | ||
commit: options.commit, | ||
pr: options.pr, | ||
permalink: options.permalink, | ||
aliases, | ||
}); | ||
|
||
if (options.yank) { | ||
await yank(denops, url.href); | ||
} | ||
if (options.noBrowser) { | ||
await denops.cmd("echomsg url", { url: url.href }); | ||
} else { | ||
await systemopen(url.href); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts"; | ||
import { unnullish } from "https://deno.land/x/unnullish@v1.0.1/mod.ts"; | ||
import { | ||
assert, | ||
ensure, | ||
is, | ||
} from "https://deno.land/x/unknownutil@v3.0.0/mod.ts#^"; | ||
import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts"; | ||
import * as fn from "https://deno.land/x/denops_std@v5.0.1/function/mod.ts"; | ||
import * as vars from "https://deno.land/x/denops_std@v5.0.1/variable/mod.ts"; | ||
import * as helper from "https://deno.land/x/denops_std@v5.0.1/helper/mod.ts"; | ||
import { | ||
parse, | ||
validateFlags, | ||
validateOpts, | ||
} from "https://deno.land/x/denops_std@v5.0.1/argument/mod.ts"; | ||
import { normCmdArgs, parseDisableDefaultArgs } from "../../util/cmd.ts"; | ||
import { exec } from "./command.ts"; | ||
|
||
type Range = readonly [number, number]; | ||
|
||
const isRange = is.TupleOf([is.Number, is.Number] as const); | ||
|
||
export function main(denops: Denops): void { | ||
denops.dispatcher = { | ||
...denops.dispatcher, | ||
"browse:command": (args, range) => { | ||
assert(args, is.ArrayOf(is.String), { message: "args must be string[]" }); | ||
assert(range, is.OneOf([is.Undefined, isRange]), { | ||
message: "range must be undefined | [number, number]", | ||
}); | ||
const [disableDefaultArgs, realArgs] = parseDisableDefaultArgs(args); | ||
return helper.friendlyCall( | ||
denops, | ||
() => | ||
command(denops, realArgs, { | ||
disableDefaultArgs, | ||
range, | ||
}), | ||
); | ||
}, | ||
}; | ||
} | ||
|
||
type CommandOptions = { | ||
disableDefaultArgs?: boolean; | ||
range?: Range; | ||
}; | ||
|
||
async function command( | ||
denops: Denops, | ||
args: string[], | ||
options: CommandOptions = {}, | ||
): Promise<void> { | ||
if (!options.disableDefaultArgs) { | ||
const defaultArgs = await vars.g.get( | ||
denops, | ||
"gin_browse_default_args", | ||
[], | ||
); | ||
assert(defaultArgs, is.ArrayOf(is.String), { | ||
message: "g:gin_browse_default_args must be string[]", | ||
}); | ||
args = [...defaultArgs, ...args]; | ||
} | ||
const [opts, flags, residue] = parse(await normCmdArgs(denops, args)); | ||
validateFlags(flags, [ | ||
"remote", | ||
"permalink", | ||
"path", | ||
"home", | ||
"commit", | ||
"pr", | ||
"n", | ||
"no-browser", | ||
]); | ||
validateOpts(opts, [ | ||
"worktree", | ||
"yank", | ||
]); | ||
const commitish = parseResidue(residue); | ||
const path = unnullish( | ||
await ensurePath(denops, opts.path), | ||
(p) => formatPath(p, options.range), | ||
); | ||
await exec(denops, commitish ?? "HEAD", { | ||
worktree: opts.worktree, | ||
yank: "yank" in opts, | ||
noBrowser: ("n" in flags || "no-browser" in flags), | ||
remote: ensure(flags.remote, is.OneOf([is.Undefined, is.String]), { | ||
"message": "REMOTE in --remote={REMOTE} must be string", | ||
}), | ||
path, | ||
home: "home" in flags, | ||
commit: "commit" in flags, | ||
pr: "pr" in flags, | ||
permalink: "permalink" in flags, | ||
}); | ||
} | ||
|
||
function parseResidue( | ||
residue: string[], | ||
): string | undefined { | ||
// GinBrowse [{options}] {commitish} | ||
switch (residue.length) { | ||
case 0: | ||
return undefined; | ||
case 1: | ||
return residue[0]; | ||
default: | ||
throw new Error("Invalid number of arguments"); | ||
} | ||
} | ||
|
||
async function ensurePath( | ||
denops: Denops, | ||
path?: string, | ||
): Promise<string | undefined> { | ||
if (path) { | ||
return ensure(path, is.String, { | ||
message: "PATH in --path={PATH} must be string", | ||
}); | ||
} | ||
const [bufname, buftype, cwd] = await batch.collect(denops, (denops) => [ | ||
fn.expand(denops, "%:p") as Promise<string>, | ||
fn.getbufvar(denops, "%", "&buftype") as Promise<string>, | ||
fn.getcwd(denops), | ||
]); | ||
return buftype ? cwd : bufname; | ||
} | ||
|
||
function formatPath(path: string, range?: Range): string { | ||
if (!range) { | ||
return path; | ||
} | ||
const [start, end] = range; | ||
if (start === end) { | ||
return `${path}:${start}`; | ||
} | ||
return `${path}:${start}:${end}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
if exists('g:loaded_gin_browse') | ||
finish | ||
endif | ||
let g:loaded_gin_browse = 1 | ||
|
||
function! s:command(args, range, range_given) abort | ||
let l:Callback = function('denops#notify', [ | ||
\ 'gin', | ||
\ 'browse:command', | ||
\ a:range_given ? [a:args, a:range] : [a:args], | ||
\]) | ||
call denops#plugin#wait_async('gin', l:Callback) | ||
endfunction | ||
|
||
command! -bar -range=0 -nargs=* GinBrowse call s:command([<f-args>], [<line1>, <line2>], <count>) |