Skip to content

Commit

Permalink
Change ddc#set_static_import_path() spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Dec 6, 2023
1 parent 6aa1d07 commit 663f89f
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:

- name: Check Type
run: |
echo "export const mods = {};" > denops/ddc/_mods.js
find denops -name "*.ts"| xargs deno test --unstable --no-run -A
- name: Check with deno lint
Expand All @@ -44,5 +45,6 @@ jobs:

- name: Test
run: |
echo "export const mods = {};" > denops/ddc/_mods.js
grep -rl Deno.test denops| xargs deno test --unstable -A
timeout-minutes: 5
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
vim-themis/
denops/ddc/_mods.js
15 changes: 13 additions & 2 deletions autoload/ddc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,20 @@ function ddc#enable(opts = {}) abort
silent! call context_filetype#get_filetype()
endif

" Create default mods file.
const mods = [s:root_dir, 'denops', 'ddc', '_mods.js']->join(s:sep)
if !(mods->filereadable())
call writefile([
\ 'export const mods = {};',
\ ], mods)
endif

let g:ddc#_started = reltime()
let g:ddc#_context_filetype = context_filetype
let g:ddc#_skip_next_complete = 0
if !('g:ddc#_mods'->exists())
const g:ddc#_mods = mods
endif

" NOTE: ddc.vim must be registered manually.
autocmd ddc User DenopsReady silent! call ddc#_register()
Expand Down Expand Up @@ -99,8 +110,8 @@ 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])
function ddc#set_static_import_path() abort
call ddc#_notify('setStaticImportPath', [])
endfunction

function ddc#on_event(event) abort
Expand Down
4 changes: 2 additions & 2 deletions denops/ddc/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ 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);
async setStaticImportPath(): Promise<void> {
await loader.initStaticImportPath(denops);
return Promise.resolve();
},
async getPreviewer(arg1: unknown, arg2: unknown): Promise<Previewer> {
Expand Down
52 changes: 38 additions & 14 deletions denops/ddc/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
SourceName,
UiName,
} from "./types.ts";
import { basename, Denops, fn, Lock, op, parse, toFileUrl } from "./deps.ts";
import { safeStat } from "./utils.ts";
import { basename, Denops, fn, Lock, op, parse, toFileUrl, vars } from "./deps.ts";
import { mods } from "./_mods.js";

export class Loader {
private uis: Record<UiName, BaseUi<BaseUiParams>> = {};
Expand All @@ -26,21 +26,45 @@ 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;
async initStaticImportPath(denops: Denops) {
// Generate _mods.ts
let mods: string[] = [];
const runtimepath = await op.runtimepath.getGlobal(denops);
for (
const glob of [
"denops/@ddu-columns/*.ts",
"denops/@ddu-filters/*.ts",
"denops/@ddu-kinds/*.ts",
"denops/@ddu-sources/*.ts",
"denops/@ddu-uis/*.ts",
]
) {
mods = mods.concat(
await fn.globpath(
denops,
runtimepath,
glob,
1,
1,
),
);
}

path = await fn.expand(denops, path) as string;
if (!await safeStat(path)) {
return;
const staticLines = [];
for (const [index, path] of mods.entries()) {
staticLines.push(`import * as mod${index} from "file://${path}"`);
}

//const startTime = Date.now();
this.staticImportMod = (await import(toFileUrl(path).href)).mods;
//console.log(`${Date.now() - startTime} ms`);
staticLines.push("export const mods = {");
for (const [index, path] of mods.entries()) {
staticLines.push(` "${path}":`);
staticLines.push(` mod${index},`);
}
staticLines.push("};");
await Deno.writeTextFile(
await vars.g.get(denops, "ddc#_mods"),
staticLines.join("\n"),
);
}

async autoload(
Expand Down Expand Up @@ -99,7 +123,7 @@ export class Loader {

const name = parse(path).name;

const mod = this.staticImportMod[path] ??
const mod = (mods as Record<string, unknown>)[path] ??
await import(toFileUrl(path).href);

let add;
Expand Down
15 changes: 11 additions & 4 deletions doc/ddc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,9 @@ ddc#register({type}, {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|.
Make static import file to optimize load ddu extensions from
current 'runtimepath'.
NOTE: It should be called if you have updated plugins.

*ddc#skip_next_complete()*
ddc#skip_next_complete()
Expand Down Expand Up @@ -1530,11 +1530,15 @@ https://github.com/Shougo/pum.vim
*ddc-faq-21*
Q: ddc.vim starting is slow.

A: It may be slow in Windows environment. You should use denops shared server
A: It may be slow in Windows environment. You can use denops shared server
feature.

https://github.com/vim-denops/denops.vim#shared-server

Or you can create static import file by |ddc#set_static_import_path()|.

Or ignore deno cache directory "%LOCALAPPDATA%\deno" from anti virus scan.

*ddc-faq-22*
Q: ddc.vim does not work in |command-line-window|.

Expand Down Expand Up @@ -1629,6 +1633,9 @@ https://github.com/Shougo/ddc-ui-pum
==============================================================================
COMPATIBILITY *ddc-compatibility*

2023.12.06
* Change "ddc#set_static_import_path()" spec.

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

Expand Down

0 comments on commit 663f89f

Please sign in to comment.