Skip to content

Commit

Permalink
Add ddc#set_static_import_path()
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Dec 4, 2023
1 parent f1d675a commit 6aa1d07
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
4 changes: 4 additions & 0 deletions autoload/ddc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ function ddc#update_items(name, items) abort
call ddc#_notify('updateItems', [a:name, a:items])
endfunction

function ddc#set_static_import_path(path) abort
call ddc#_notify('setStaticImportPath', [a:path])
endfunction

function ddc#on_event(event) abort
" NOTE: If denops isn't running, stop
if !ddc#_denops_running()
Expand Down
4 changes: 4 additions & 0 deletions denops/ddc/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export function main(denops: Denops) {
getCurrent(): Promise<DdcOptions> {
return Promise.resolve(contextBuilder.getCurrent(denops));
},
async setStaticImportPath(arg1: unknown): Promise<void> {
await loader.initStaticImportPath(denops, arg1 as string);
return Promise.resolve();
},
async getPreviewer(arg1: unknown, arg2: unknown): Promise<Previewer> {
const [_skip, context, options] = await contextBuilder
.createContext(denops, "Manual");
Expand Down
2 changes: 1 addition & 1 deletion denops/ddc/base/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SourceOptions,
} from "../types.ts";
import { Denops } from "../deps.ts";
import { convertKeywordPattern } from "../util.ts";
import { convertKeywordPattern } from "../utils.ts";
import { Loader } from "../loader.ts";

export type BaseSourceParams = Record<string, unknown>;
Expand Down
2 changes: 1 addition & 1 deletion denops/ddc/ddc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
TimeoutError,
vars,
} from "./deps.ts";
import { errorException } from "./util.ts";
import { errorException } from "./utils.ts";

type DdcResult = {
items: Item[];
Expand Down
20 changes: 19 additions & 1 deletion denops/ddc/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
UiName,
} from "./types.ts";
import { basename, Denops, fn, Lock, op, parse, toFileUrl } from "./deps.ts";
import { safeStat } from "./utils.ts";

export class Loader {
private uis: Record<UiName, BaseUi<BaseUiParams>> = {};
Expand All @@ -25,6 +26,22 @@ export class Loader {
private registerLock = new Lock(0);
private cachedPaths: Record<string, string> = {};
private prevRuntimepath = "";
private staticImportMod: Record<string, unknown> = {};

async initStaticImportPath(denops: Denops, path: string) {
if (Object.values(this.staticImportMod).length !== 0) {
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 @@ -82,7 +99,8 @@ export class Loader {

const name = parse(path).name;

const mod = await import(toFileUrl(path).href);
const mod = this.staticImportMod[path] ??
await import(toFileUrl(path).href);

let add;
switch (type) {
Expand Down
20 changes: 20 additions & 0 deletions denops/ddc/util.ts → denops/ddc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ export async function errorException(
}
}

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;
}

Deno.test("vimoption2ts", () => {
assertEquals(vimoption2ts("@,48-57,_,\\"), "a-zA-Z0-9_\\\\");
assertEquals(vimoption2ts("@,-,48-57,_"), "a-zA-Z0-9_-");
Expand Down
10 changes: 10 additions & 0 deletions doc/ddc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ ddc#hide([{event}])
It must be list of |autocmd-events| string or "Manual" or
"Initialize" or "Update".
NOTE: It is used to cancel the completion.
NOTE: It does not restore inserted text.
>
inoremap <C-e> <Cmd>call ddc#hide()<CR>
<
Expand All @@ -352,6 +353,12 @@ ddc#register({type}, {path})
{path} is ddc extension path.
NOTE: {path} must be full path.

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

*ddc#skip_next_complete()*
ddc#skip_next_complete()
Skip the next auto completion.
Expand Down Expand Up @@ -1622,6 +1629,9 @@ https://github.com/Shougo/ddc-ui-pum
==============================================================================
COMPATIBILITY *ddc-compatibility*

2023.12.04
* Rename "util.ts" to "utils.ts".

2023.11.08
* Remove keywordPattern backward compatibility.

Expand Down

0 comments on commit 6aa1d07

Please sign in to comment.