Skip to content

Commit 36c7f89

Browse files
authored
Merge pull request #3 from vim-denops/context
Support context on execute and add Vim.get()
2 parents d018c5d + e8c04a2 commit 36c7f89

File tree

7 files changed

+29
-19
lines changed

7 files changed

+29
-19
lines changed

deps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export type {
22
Context,
33
Dispatcher,
44
} from "https://deno.land/x/denops@v0.7/mod.ts";
5-
export { Denops } from "https://deno.land/x/denops@v0.7/mod.ts";
5+
export { Denops, getCacheOrElse } from "https://deno.land/x/denops@v0.7/mod.ts";

mod.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
export * from "./autocmd.ts";
2-
export * from "./execute.ts";
3-
export * from "./variable.ts";
4-
export * from "./vim.ts";
1+
export { start, Vim } from "./vim/mod.ts";

autocmd.ts renamed to vim/autocmd.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Denops } from "./deps.ts";
1+
import { Denops } from "../deps.ts";
22
import { execute } from "./execute.ts";
33

44
export async function autocmd(
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
import { Denops } from "./deps.ts";
1+
import { Context, Denops } from "../deps.ts";
22

33
/**
44
* Execute Vim script directly
55
*/
66
export async function execute(
77
denops: Denops,
88
command: string | string[],
9+
context: Context = {},
910
): Promise<void> {
1011
if (Array.isArray(command)) {
11-
await denops.cmd("call execute(l:command, '')", {
12-
command: command
12+
context = {
13+
...context,
14+
__denops_internal_command: command
1315
.map((x) => x.replace(/^\s+|\s+$/g, ""))
1416
.filter((x) => !!x),
15-
});
17+
};
18+
await denops.cmd("call execute(l:__denops_internal_command, '')", context);
1619
return;
1720
}
1821
command = command.replace(/\r?\n\s*\\/g, "");
19-
await execute(denops, command.split(/\r?\n/g));
22+
await execute(denops, command.split(/\r?\n/g), context);
2023
}

vim/mod.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./autocmd.ts";
2+
export * from "./execute.ts";
3+
export * from "./variable.ts";
4+
export * from "./vim.ts";

variable.ts renamed to vim/variable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Denops } from "./deps.ts";
1+
import { Denops } from "../deps.ts";
22

33
/**
44
* Vim variable groups

vim.ts renamed to vim/vim.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Context, Denops, Dispatcher } from "./deps.ts";
1+
import { Context, Denops, Dispatcher, getCacheOrElse } from "../deps.ts";
22
import { execute } from "./execute.ts";
33
import { autocmd, AutocmdHelper } from "./autocmd.ts";
44
import { VariableHelper } from "./variable.ts";
@@ -23,6 +23,12 @@ export class Vim {
2323
this.env = new VariableHelper(denops, "env");
2424
}
2525

26+
static get(): Vim {
27+
return getCacheOrElse("vim", () => {
28+
return new Vim(Denops.get());
29+
});
30+
}
31+
2632
get name(): string {
2733
return this.#denops.name;
2834
}
@@ -39,8 +45,11 @@ export class Vim {
3945
return await this.#denops.eval(expr, context);
4046
}
4147

42-
async execute(command: string | string[]): Promise<void> {
43-
await execute(this.#denops, command);
48+
async execute(
49+
command: string | string[],
50+
context: Context = {},
51+
): Promise<void> {
52+
await execute(this.#denops, command, context);
4453
}
4554

4655
async autocmd(
@@ -56,8 +65,5 @@ export class Vim {
5665
}
5766

5867
export function start(main: (vim: Vim) => Promise<void>) {
59-
Denops.start(async (denops) => {
60-
const vim = new Vim(denops);
61-
await main(vim);
62-
});
68+
Denops.start(() => main(Vim.get()));
6369
}

0 commit comments

Comments
 (0)