Skip to content

Commit

Permalink
Add ddu#set_static_import_path()
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Nov 30, 2023
1 parent 9911b66 commit e55877b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions autoload/ddu.vim
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ endfunction
function ddu#load(type, names) abort
call ddu#_notify('loadExtensions', [a:type, a:names])
endfunction
function ddu#set_static_import_path(path) abort
call ddu#_notify('setStaticImportPath', [a:path])
endfunction
function ddu#get_items(options = {}) abort
return ddu#_request('getItems', [a:options])
endfunction
Expand Down
6 changes: 6 additions & 0 deletions denops/ddu/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ export function main(denops: Denops) {
//console.log(`${type} ${names}: ${Date.now() - startTime} ms`);
return Promise.resolve();
},
async setStaticImportPath(arg1: unknown): Promise<void> {
await loader.initStaticImportPath(denops, arg1 as string);
return Promise.resolve();
},
async start(arg1: unknown): Promise<void> {
//const startTime = Date.now();
await lock.lock(async () => {
let userOptions = ensure(arg1, is.Record);
let [context, options] = await contextBuilder.get(denops, userOptions);
Expand Down Expand Up @@ -216,6 +221,7 @@ export function main(denops: Denops) {

await ddu.start(denops, context, options, userOptions);
});
//console.log(`${Date.now() - startTime} ms`);
},
async getItems(arg1: unknown): Promise<DduItem[]> {
let items: DduItem[] = [];
Expand Down
21 changes: 20 additions & 1 deletion denops/ddu/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
UiName,
} from "./types.ts";
import { basename, Denops, fn, Lock, op, parse, toFileUrl } from "./deps.ts";
import { safeStat } from "./utils.ts";

type Mod = {
// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -46,6 +47,22 @@ export class Loader {
private registerLock = new Lock(0);
private cachedPaths: Record<string, string> = {};
private prevRuntimepath = "";
private staticImportMod = null;

async initStaticImportPath(denops: Denops, path: string) {
if (this.staticImportMod) {
return;
}

path = await fn.expand(denops, path) as string;
if (!await safeStat(path)) {
return;
}

//const startTime = Date.now();
this.staticImportMod = (await import(toFileUrl(path).href)).mods;
//console.log(`${Date.now() - startTime} ms`);
}

async autoload(
denops: Denops,
Expand Down Expand Up @@ -149,7 +166,9 @@ export class Loader {
const name = parse(path).name;

const mod: Mod = {
mod: await import(toFileUrl(path).href),
mod: this.staticImportMod
? this.staticImportMod[path]
: await import(toFileUrl(path).href),
path,
};

Expand Down
20 changes: 20 additions & 0 deletions denops/ddu/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,23 @@ export async function errorException(
console.error(e);
}
}

export async function safeStat(path: string): Promise<Deno.FileInfo | null> {
// NOTE: Deno.stat() may be failed
try {
const stat = await Deno.lstat(path);
if (stat.isSymlink) {
try {
const stat = await Deno.stat(path);
stat.isSymlink = true;
return stat;
} catch (_: unknown) {
// Ignore stat exception
}
}
return stat;
} catch (_: unknown) {
// Ignore stat exception
}
return null;
}
6 changes: 6 additions & 0 deletions doc/ddu.txt
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ ddu#start([{options}])
Refer to |ddu-options| about {options}. If you skip a value,
it uses the default value.

*ddu#set_static_import_path()*
ddu#set_static_import_path()
Set the path of staticImport file to optimize load ddu
extensions.
It is generated by "dpp.vim" |dpp-option-convertImportPaths|.

*ddu#ui_async_action()*
ddu#ui_async_action({name}, {action}[, {params}])
Do the {action} action in current UI asynchronously.
Expand Down

0 comments on commit e55877b

Please sign in to comment.