Skip to content

Commit 39bc49a

Browse files
authored
Merge pull request #92 from vim-denops/imp-load
👍 Do NOT reload Vim script files on `load()`
2 parents cac3aff + ff85932 commit 39bc49a

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

denops_std/helper/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,56 @@ export async function main(denops: Denops): Promise<void> {
167167
await load(denops, new URL("https://example.com/foo.vim"));
168168
}
169169
```
170+
171+
It does nothing if the `url` is already loaded unless `force` option is
172+
specified like:
173+
174+
```typescript
175+
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
176+
import { load } from "https://deno.land/x/denops_std/helper/mod.ts";
177+
178+
export async function main(denops: Denops): Promise<void> {
179+
const url = new URL("../../foo.vim", import.meta.url);
180+
181+
// Line below loads a script
182+
await load(denops, url);
183+
184+
// Line below does nothing while `url` is already loaded.
185+
await load(denops, url);
186+
187+
// Line below loads the script while `force` option is specified.
188+
await load(denops, url, { force: true });
189+
}
190+
```
191+
192+
It returns `true` when the script is loaded. Otherwise, it returns `false` like:
193+
194+
```typescript
195+
import { Denops } from "https://deno.land/x/denops_std/mod.ts";
196+
import { load } from "https://deno.land/x/denops_std/helper/mod.ts";
197+
198+
export async function main(denops: Denops): Promise<void> {
199+
const url = new URL("../../foo.vim", import.meta.url);
200+
201+
console.log(await load(denops, url));
202+
// -> true
203+
204+
console.log(await load(denops, url));
205+
// -> false
206+
207+
console.log(await load(denops, url, { force: true }));
208+
// -> true
209+
}
210+
```
211+
212+
Note that denops plugins works on individual threads so developers should add a
213+
source guard on a Vim script as well like:
214+
215+
```vim
216+
if has('g:loaded_xxxxx')
217+
finish
218+
endif
219+
let g:loaded_xxxxx = 1
220+
221+
" Define functions or whatever
222+
```

denops_std/helper/load.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
import { Denops, fs, hash, path } from "../deps.ts";
22
import { execute } from "./execute.ts";
33

4+
const loaded = new Set<URL>();
5+
6+
export type LoadOptions = {
7+
force?: boolean;
8+
};
9+
410
/**
5-
* Load Vim script in local/remote URL
11+
* Load a Vim script in a local/remote URL
12+
*
13+
* It does nothing if the `url` is already loaded unless `force` option is specified.
14+
* It returns `true` when the script is loaded. Otherwise, it returns `false`.
615
*/
716
export async function load(
817
denops: Denops,
918
url: URL,
10-
): Promise<void> {
19+
options: LoadOptions = {},
20+
): Promise<boolean> {
21+
if (!options.force && loaded.has(url)) {
22+
return false;
23+
}
1124
const scriptPath = await ensureLocalFile(url);
1225
await execute(
1326
denops,
1427
"execute printf('source %s', fnameescape(scriptPath)) ",
1528
{ scriptPath },
1629
);
30+
loaded.add(url);
31+
return true;
1732
}
1833

1934
async function ensureLocalFile(url: URL): Promise<string> {

0 commit comments

Comments
 (0)