Skip to content

Commit 6bfc537

Browse files
authored
Merge pull request #52 from lambdalisue/ref
Feature updates
2 parents a49d891 + feb58da commit 6bfc537

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+980
-562
lines changed

autoload/gin/action.vim

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
let s:range_from_call = v:null
22

33
function! gin#action#fn(callback) abort
4+
let bufnr = bufnr()
45
let range = gin#action#_get_range()
5-
let xs = gin#action#gather_candidates(range)
6+
let xs = gin#action#gather_candidates(bufnr, range)
67
call call(a:callback, [xs])
78
endfunction
89

@@ -16,12 +17,12 @@ function! gin#action#call(name, range) abort
1617
endtry
1718
endfunction
1819

19-
function! gin#action#list_actions() abort
20-
return denops#request('gin', 'action:list_actions', [])
20+
function! gin#action#list_actions(bufnr) abort
21+
return denops#request('gin', 'action:list_actions', [a:bufnr])
2122
endfunction
2223

23-
function! gin#action#gather_candidates(range) abort
24-
return denops#request('gin', 'action:gather_candidates', [a:range])
24+
function! gin#action#gather_candidates(bufnr, range) abort
25+
return denops#request('gin', 'action:gather_candidates', [a:bufnr, a:range])
2526
endfunction
2627

2728
function! gin#action#_get_range() abort

denops/gin/component/branch.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import type { Denops } from "https://deno.land/x/denops_std@v3.3.0/mod.ts";
1+
import type { Denops } from "https://deno.land/x/denops_std@v3.6.0/mod.ts";
22
import { Cache } from "https://deno.land/x/[email protected]/mod.ts";
33
import { decodeUtf8 } from "../util/text.ts";
4-
import { getWorktree } from "../util/worktree.ts";
4+
import {
5+
findWorktreeFromSuspects,
6+
listWorktreeSuspectsFromDenops,
7+
} from "../util/worktree.ts";
58
import { execute } from "../git/process.ts";
69

710
type Data = [string, string];
@@ -14,8 +17,10 @@ async function getData(
1417
if (cache.has("data")) {
1518
return cache.get("data");
1619
}
17-
const cwd = await getWorktree(denops);
18-
const result = await getBranches(cwd);
20+
const worktree = await findWorktreeFromSuspects(
21+
await listWorktreeSuspectsFromDenops(denops),
22+
);
23+
const result = await getBranches(worktree);
1924
cache.set("data", result);
2025
return result;
2126
}

denops/gin/component/traffic.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import type { Denops } from "https://deno.land/x/denops_std@v3.3.0/mod.ts";
1+
import type { Denops } from "https://deno.land/x/denops_std@v3.6.0/mod.ts";
22
import { Cache } from "https://deno.land/x/[email protected]/mod.ts";
33
import { decodeUtf8 } from "../util/text.ts";
4-
import { getWorktree } from "../util/worktree.ts";
4+
import {
5+
findWorktreeFromSuspects,
6+
listWorktreeSuspectsFromDenops,
7+
} from "../util/worktree.ts";
58
import { execute } from "../git/process.ts";
69

710
type Data = [number, number];
@@ -14,10 +17,12 @@ async function getData(
1417
if (cache.has("data")) {
1518
return cache.get("data");
1619
}
17-
const cwd = await getWorktree(denops);
20+
const worktree = await findWorktreeFromSuspects(
21+
await listWorktreeSuspectsFromDenops(denops),
22+
);
1823
const result = await Promise.all([
19-
getAhead(cwd),
20-
getBehind(cwd),
24+
getAhead(worktree),
25+
getBehind(worktree),
2126
]);
2227
cache.set("data", result);
2328
return result;

denops/gin/component/worktree.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import type { Denops } from "https://deno.land/x/denops_std@v3.3.0/mod.ts";
1+
import type { Denops } from "https://deno.land/x/denops_std@v3.6.0/mod.ts";
22
import { Cache } from "https://deno.land/x/[email protected]/mod.ts";
3-
import * as path from "https://deno.land/[email protected]/path/mod.ts";
4-
import { getWorktree } from "../util/worktree.ts";
3+
import * as path from "https://deno.land/[email protected]/path/mod.ts";
4+
import {
5+
findWorktreeFromSuspects,
6+
listWorktreeSuspectsFromDenops,
7+
} from "../util/worktree.ts";
58
import { find } from "../git/finder.ts";
69

710
type Data = string;
@@ -14,8 +17,10 @@ async function getData(
1417
if (cache.has("data")) {
1518
return cache.get("data");
1619
}
17-
const cwd = await getWorktree(denops);
18-
const result = await find(cwd);
20+
const worktree = await findWorktreeFromSuspects(
21+
await listWorktreeSuspectsFromDenops(denops),
22+
);
23+
const result = await find(worktree);
1924
cache.set("data", result);
2025
return result;
2126
}

denops/gin/core/action/action.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import type { Denops } from "https://deno.land/x/[email protected]/mod.ts";
2-
import * as helper from "https://deno.land/x/[email protected]/helper/mod.ts";
3-
import * as mapping from "https://deno.land/x/[email protected]/mapping/mod.ts";
4-
import * as vars from "https://deno.land/x/[email protected]/variable/mod.ts";
1+
import type { Denops } from "https://deno.land/x/[email protected]/mod.ts";
2+
import * as buffer from "https://deno.land/x/[email protected]/buffer/mod.ts";
3+
import * as fn from "https://deno.land/x/[email protected]/function/mod.ts";
4+
import * as helper from "https://deno.land/x/[email protected]/helper/mod.ts";
5+
import * as mapping from "https://deno.land/x/[email protected]/mapping/mod.ts";
6+
import * as vars from "https://deno.land/x/[email protected]/variable/mod.ts";
57

68
export type Action = {
79
name: string;
@@ -11,18 +13,24 @@ export type Action = {
1113

1214
export type Range = [number, number];
1315

14-
export async function list(denops: Denops): Promise<Action[]> {
15-
const ms = await mapping.list(denops, "<Plug>(gin-action-", { mode: "n" });
16-
const cs = ms.flatMap((map) => {
17-
const m = map.lhs.match(/^<Plug>\(gin-action-(.*)\)$/);
18-
if (!m) {
19-
return [];
20-
}
21-
return [{
22-
lhs: map.lhs,
23-
rhs: map.rhs,
24-
name: m[1],
25-
}];
16+
export async function list(
17+
denops: Denops,
18+
bufnr: number,
19+
): Promise<Action[]> {
20+
let cs: Action[] = [];
21+
await buffer.ensure(denops, bufnr, async () => {
22+
const ms = await mapping.list(denops, "<Plug>(gin-action-", { mode: "n" });
23+
cs = ms.flatMap((map) => {
24+
const m = map.lhs.match(/^<Plug>\(gin-action-(.*)\)$/);
25+
if (!m) {
26+
return [];
27+
}
28+
return [{
29+
lhs: map.lhs,
30+
rhs: map.rhs,
31+
name: m[1],
32+
}];
33+
});
2634
});
2735
return cs;
2836
}
@@ -31,7 +39,7 @@ export async function actionChoice(
3139
denops: Denops,
3240
range: Range,
3341
): Promise<void> {
34-
const cs = await list(denops);
42+
const cs = await list(denops, await fn.bufnr(denops));
3543
const name = await helper.input(denops, {
3644
prompt: "action: ",
3745
completion: (

denops/gin/core/action/main.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Denops } from "https://deno.land/x/denops_std@v3.3.0/mod.ts";
1+
import type { Denops } from "https://deno.land/x/denops_std@v3.6.0/mod.ts";
22
import * as unknownutil from "https://deno.land/x/[email protected]/mod.ts";
33
import * as registry from "./registry.ts";
44
import * as action from "./action.ts";
@@ -8,12 +8,14 @@ const rangeRef: [number, number] = [0, 0];
88
export function main(denops: Denops): void {
99
denops.dispatcher = {
1010
...denops.dispatcher,
11-
"action:list_actions": () => {
12-
return action.list(denops);
11+
"action:list_actions": (bufnr) => {
12+
unknownutil.assertNumber(bufnr);
13+
return action.list(denops, bufnr);
1314
},
14-
"action:gather_candidates": (range) => {
15+
"action:gather_candidates": (bufnr, range) => {
16+
unknownutil.assertNumber(bufnr);
1517
unknownutil.assertLike(rangeRef, range);
16-
return registry.gatherCandidates(denops, range);
18+
return registry.gatherCandidates(denops, bufnr, range);
1719
},
1820
"action:action:choice": (range) => {
1921
unknownutil.assertLike(rangeRef, range);

denops/gin/core/action/registry.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { Denops } from "https://deno.land/x/[email protected]/mod.ts";
2-
import * as anonymous from "https://deno.land/x/[email protected]/anonymous/mod.ts";
3-
import * as autocmd from "https://deno.land/x/[email protected]/autocmd/mod.ts";
4-
import * as fn from "https://deno.land/x/[email protected]/function/mod.ts";
1+
import type { Denops } from "https://deno.land/x/[email protected]/mod.ts";
2+
import * as anonymous from "https://deno.land/x/[email protected]/anonymous/mod.ts";
3+
import * as autocmd from "https://deno.land/x/[email protected]/autocmd/mod.ts";
54
import type { Range } from "./action.ts";
65

76
export type { Range };
@@ -13,14 +12,15 @@ export type Candidate = {
1312

1413
export type Gatherer = (
1514
denops: Denops,
15+
bufnr: number,
1616
range: Range,
1717
) => Promise<Candidate[]>;
1818

1919
export async function register(
2020
denops: Denops,
21+
bufnr: number,
2122
gatherer: Gatherer,
2223
): Promise<void> {
23-
const bufnr = await fn.bufnr(denops);
2424
const pat = `<buffer=${bufnr}>`;
2525
const [id] = anonymous.once(denops, () => {
2626
registry.delete(bufnr);
@@ -45,14 +45,14 @@ export async function register(
4545

4646
export async function gatherCandidates(
4747
denops: Denops,
48+
bufnr: number,
4849
range: Range,
4950
): Promise<Candidate[]> {
50-
const bufnr = await fn.bufnr(denops);
5151
const gatherer = registry.get(bufnr);
5252
if (!gatherer) {
5353
throw new Error(`No gatherer is registered on a buffer ${bufnr}`);
5454
}
55-
return await gatherer(denops, range);
55+
return await gatherer(denops, bufnr, range);
5656
}
5757

5858
// Global registry

denops/gin/core/bare/command.ts

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,88 @@
1-
import type { Denops } from "https://deno.land/x/denops_std@v3.3.0/mod.ts";
2-
import * as autocmd from "https://deno.land/x/denops_std@v3.3.0/autocmd/mod.ts";
3-
import * as batch from "https://deno.land/x/denops_std@v3.3.0/batch/mod.ts";
4-
import * as fn from "https://deno.land/x/denops_std@v3.3.0/function/mod.ts";
5-
import * as helper from "https://deno.land/x/denops_std@v3.3.0/helper/mod.ts";
6-
import * as option from "https://deno.land/x/denops_std@v3.3.0/option/mod.ts";
1+
import type { Denops } from "https://deno.land/x/denops_std@v3.6.0/mod.ts";
2+
import * as autocmd from "https://deno.land/x/denops_std@v3.6.0/autocmd/mod.ts";
3+
import * as batch from "https://deno.land/x/denops_std@v3.6.0/batch/mod.ts";
4+
import * as fn from "https://deno.land/x/denops_std@v3.6.0/function/mod.ts";
5+
import * as helper from "https://deno.land/x/denops_std@v3.6.0/helper/mod.ts";
6+
import * as option from "https://deno.land/x/denops_std@v3.6.0/option/mod.ts";
77
import * as unknownutil from "https://deno.land/x/[email protected]/mod.ts";
88
import {
9-
builtinOpts,
109
parseOpts,
1110
validateOpts,
12-
} from "https://deno.land/x/denops_std@v3.3.0/argument/mod.ts";
13-
import { normCmdArgs } from "../../util/cmd.ts";
14-
import * as buffer from "https://deno.land/x/denops_std@v3.3.0/buffer/mod.ts";
11+
} from "https://deno.land/x/denops_std@v3.6.0/argument/mod.ts";
12+
import { expand, normCmdArgs } from "../../util/cmd.ts";
13+
import * as buffer from "https://deno.land/x/denops_std@v3.6.0/buffer/mod.ts";
1514
import {
1615
buildDecorationsFromAnsiEscapeCode,
1716
removeAnsiEscapeCode,
1817
} from "../../util/ansi_escape_code.ts";
19-
import { getWorktreeFromOpts } from "../../util/worktree.ts";
18+
import {
19+
findWorktreeFromSuspects,
20+
listWorktreeSuspectsFromDenops,
21+
} from "../../util/worktree.ts";
2022
import { decodeUtf8 } from "../../util/text.ts";
2123
import { run } from "../../git/process.ts";
2224

25+
export type Options = {
26+
worktree?: string;
27+
buffer?: boolean | string;
28+
monochrome?: boolean;
29+
fileformat?: string;
30+
encoding?: string;
31+
};
32+
2333
export async function command(
2434
denops: Denops,
2535
args: string[],
2636
): Promise<void> {
27-
const [env, verbose, eventignore] = await batch.gather(
37+
const [opts, residue] = parseOpts(await normCmdArgs(denops, args));
38+
validateOpts(opts, [
39+
"worktree",
40+
"buffer",
41+
"monochrome",
42+
"ff",
43+
"fileformat",
44+
"enc",
45+
"encoding",
46+
]);
47+
const buffer = opts["buffer"] === "" ? true : opts["buffer"];
48+
const options = {
49+
worktree: opts["worktree"],
50+
buffer,
51+
monochrome: "monochrome" in opts,
52+
fileformat: opts["ff"] ?? opts["fileformat"],
53+
encoding: opts["enc"] ?? opts["encoding"],
54+
};
55+
await exec(denops, residue, options);
56+
}
57+
58+
export async function exec(
59+
denops: Denops,
60+
args: string[],
61+
options: Options = {},
62+
): Promise<void> {
63+
const [verbose, env, eventignore] = await batch.gather(
2864
denops,
2965
async (denops) => {
30-
await fn.environ(denops);
3166
await option.verbose.get(denops);
67+
await fn.environ(denops);
3268
await option.eventignore.get(denops);
3369
},
3470
);
35-
unknownutil.assertObject(env, unknownutil.isString);
3671
unknownutil.assertNumber(verbose);
72+
unknownutil.assertObject(env, unknownutil.isString);
3773
unknownutil.assertString(eventignore);
38-
const [opts, residue] = parseOpts(await normCmdArgs(denops, args));
39-
validateOpts(opts, [
40-
"worktree",
41-
"buffer",
42-
"monochrome",
43-
...builtinOpts,
44-
]);
45-
const enableColor = "buffer" in opts && !("monochrome" in opts);
74+
75+
const enableColor = options.buffer && !options.monochrome;
4676
const cmd = [
4777
...(enableColor ? ["-c", "color.ui=always"] : []),
48-
...residue,
78+
...args,
4979
];
50-
const worktree = await getWorktreeFromOpts(denops, opts);
80+
const worktree = await findWorktreeFromSuspects(
81+
options.worktree
82+
? [await expand(denops, options.worktree)]
83+
: await listWorktreeSuspectsFromDenops(denops, !!verbose),
84+
!!verbose,
85+
);
5186
const proc = run(cmd, {
5287
printCommand: !!verbose,
5388
stdin: "null",
@@ -63,27 +98,34 @@ export async function command(
6398
proc.stderrOutput(),
6499
]);
65100
proc.close();
66-
if ("buffer" in opts) {
101+
if (options.buffer) {
67102
let decorations: buffer.Decoration[] = [];
68103
const preprocessor = (content: string[]) => {
69104
const [trimmed, decos] = buildDecorationsFromAnsiEscapeCode(content);
70105
decorations = decos;
71106
return trimmed;
72107
};
73-
await denops.cmd("noswapfile enew");
74-
const bufnr = await fn.bufnr(denops);
75-
await buffer.ensure(denops, bufnr, async () => {
76-
await batch.batch(denops, async (denops) => {
77-
await option.modifiable.setLocal(denops, false);
78-
});
108+
let bufnr: number;
109+
if (typeof options.buffer === "string") {
110+
bufnr = await fn.bufnr(denops, options.buffer);
111+
if (bufnr === -1) {
112+
bufnr = await fn.bufnr(denops, Number(options.buffer));
113+
}
114+
await fn.bufload(denops, bufnr);
115+
} else {
116+
await denops.cmd("noswapfile enew");
117+
bufnr = await fn.bufnr(denops);
118+
}
119+
await batch.batch(denops, async (denops) => {
120+
await fn.setbufvar(denops, bufnr, "&modifiable", 0);
79121
});
80122
await buffer.assign(
81123
denops,
82124
bufnr,
83125
new Uint8Array([...stdout, ...stderr]),
84126
{
85-
fileformat: (opts["ff"] ?? opts["fileformat"]),
86-
fileencoding: opts["enc"] ?? opts["fileencoding"],
127+
fileformat: options.fileformat,
128+
fileencoding: options.encoding,
87129
preprocessor,
88130
},
89131
);

0 commit comments

Comments
 (0)