Skip to content

Commit

Permalink
Add ext-lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Sep 26, 2023
1 parent 2f34d18 commit b8df28e
Show file tree
Hide file tree
Showing 12 changed files with 725 additions and 14 deletions.
443 changes: 443 additions & 0 deletions autoload/dpp/ext/lazy.vim

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions autoload/dpp/source.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function dpp#autoload#_source(plugins) abort
const filetype_before = 'autocmd FileType'->execute()
let &runtimepath = dpp#util#_join_rtp(rtps, &runtimepath, '')

call dpp#call_hook('source', sourced)
call dpp#util#_call_hook('source', sourced)

" Reload script files.
for plugin in sourced
Expand Down Expand Up @@ -100,7 +100,7 @@ function dpp#autoload#_source(plugins) abort
endif

if !has('vim_starting')
call dpp#call_hook('post_source', sourced)
call dpp#util#_call_hook('post_source', sourced)
endif

return sourced
Expand Down
63 changes: 63 additions & 0 deletions denops/@dpp-exts/lazy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Actions, BaseExt, Plugin } from "../dpp/types.ts";

type Params = Record<string, never>;

type MakeStateArgs = {
plugins: Plugin[];
};

const StateLines = [
"augroup dpp",
" autocmd FuncUndefined *",
" \\ : if '<afile>'->expand()->stridx('remote#') != 0",
" \\ | call dpp#ext#lazy#_on_func('<afile>'->expand())",
" \\ | endif",
" autocmd BufRead *? call dpp#ext#lazy#_on_default_event('BufRead')",
" autocmd BufNew,BufNewFile *? call dpp#ext#lazy#_on_default_event('BufNew')",
" autocmd VimEnter *? call dpp#ext#lazy#_on_default_event('VimEnter')",
" autocmd FileType *? call dpp#ext#lazy#_on_default_event('FileType')",
" autocmd BufWritePost *.lua,*.vim,*.toml,vimrc,.vimrc",
" \\ call dpp#util#_check_vimrcs()",
" autocmd CmdUndefined * call dpp#ext#lazy#_on_pre_cmd('<afile>'->expand())",
"augroup END",
"augroup dpp-events | augroup END",
"if !has('nvim') | return | endif",
"lua <<END",
"table.insert(package.loaders, 1, (function()",
" return function(mod_name)",
" mod_root = string.match(mod_name, '^[^./]+')",
" if vim.g['dpp#_on_lua_plugins'][mod_root] then",
" vim.fn['dpp#ext#lazy#_on_lua'](mod_name)",
" end",
" if package.loaded[mod_name] ~= nil then",
" local m = package.loaded[mod_name]",
" return function()",
" return m",
" end",
" end",
" return nil",
" end",
"end)())",
"END",
];

export class Ext extends BaseExt<Params> {
override actions: Actions<Params> = {
makeState: {
description: "Make stateLines",
callback: (args: {
actionParams: unknown;
}) => {
const params = args.actionParams as MakeStateArgs;

// TODO: Support dummy mappings/commands

return StateLines;
},
},
};

override params(): Params {
return {};
}
}
6 changes: 4 additions & 2 deletions denops/dpp/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ export function main(denops: Denops) {
`${toFileUrl(configPath).href}#${performance.now()}`
);
const obj = new mod.Config();
const plugins = await obj.config({
const configReturn = await obj.config({
denops,
basePath,
contextBuilder,
dpp,
});

await dpp.makeState(denops, basePath, plugins);
const [_, options] = await contextBuilder.get(denops);

await dpp.makeState(denops, options, basePath, configReturn);
},
};
}
7 changes: 6 additions & 1 deletion denops/dpp/base/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ export type ConfigArguments = {
dpp: Dpp;
};

export type ConfigReturn = {
plugins: Plugin[];
stateLines: string[];
};

export abstract class BaseConfig {
apiVersion = 1;

config(_args: ConfigArguments): Plugin[] | Promise<Plugin[]> {
config(_args: ConfigArguments): ConfigReturn[] | Promise<ConfigReturn[]> {
return [];
}
}
1 change: 1 addition & 0 deletions denops/dpp/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function defaultDppOptions(): DppOptions {
return {
extOptions: {},
extParams: {},
inlineVimrcs: [],
protocolOptions: {},
protocolParams: {},
};
Expand Down
6 changes: 5 additions & 1 deletion denops/dpp/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export {
assertEquals,
equal,
} from "https://deno.land/[email protected]/assert/mod.ts";
export { parse, toFileUrl } from "https://deno.land/[email protected]/path/mod.ts";
export {
extname,
parse,
toFileUrl,
} from "https://deno.land/[email protected]/path/mod.ts";
export {
deadline,
DeadlineError,
Expand Down
47 changes: 41 additions & 6 deletions denops/dpp/dpp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals, Denops, is, op, vars } from "./deps.ts";
import { assertEquals, Denops, extname, is, op, vars } from "./deps.ts";
import {
ActionName,
BaseExt,
Expand All @@ -19,6 +19,7 @@ import {
} from "./context.ts";
import { Loader } from "./loader.ts";
import { defaultExtOptions } from "./base/ext.ts";
import { ConfigReturn } from "./base/config.ts";
import { errorException, isDirectory } from "./utils.ts";

export class Dpp {
Expand All @@ -44,7 +45,7 @@ export class Dpp {
const action = ext.actions[actionName];
if (!action) {
await denops.call(
"ddu#util#_error",
"dpp#util#_error",
`Not found UI action: ${actionName}`,
);
return;
Expand All @@ -62,10 +63,15 @@ export class Dpp {
return ret;
}

async makeState(denops: Denops, basePath: string, plugins: Plugin[]) {
async makeState(
denops: Denops,
options: DppOptions,
basePath: string,
configReturn: ConfigReturn,
) {
// Initialize plugins
const recordPlugins: Record<string, Plugin> = {};
for (const plugin of plugins) {
for (const plugin of configReturn.plugins) {
recordPlugins[plugin.name] = initPlugin(plugin, basePath);
}

Expand Down Expand Up @@ -134,7 +140,7 @@ export class Dpp {

const cacheVersion = await vars.g.get(denops, "dpp#_cache_version");
const initRuntimepath = await vars.g.get(denops, "dpp#_init_runtimepath");
const stateLines = [
let stateLines = [
`if g:dpp#_cache_version !=# ${cacheVersion} ` +
`|| g:dpp#_init_runtimepath !=# '${initRuntimepath}' | ` +
"throw 'Cache loading error' | endif",
Expand All @@ -146,13 +152,40 @@ export class Dpp {
`let &runtimepath = '${newRuntimepath}'`,
];

if (await vars.g.get(denops, "did_load_filetypes", false)) {
stateLines.push("filetype off");
}
if (
await vars.b.get(denops, "did_indent", false) ||
await vars.b.get(denops, "did_ftplugin", false)
) {
stateLines.push("filetype plugin indent off");
}

for await (
const vimrc of options.inlineVimrcs.map(async (vimrc) =>
await denops.call("dpp#util#_expand", vimrc) as string
)
) {
const vimrcLines = (await Deno.readTextFile(vimrc)).split("\n");
if (extname(vimrc) == "lua") {
stateLines = ["lua <<EOF"].concat(
vimrcLines.filter((line) => !line.match(/^\s*$|^\s*--/)),
).concat(["EOF"]);
} else {
stateLines = stateLines.concat(
vimrcLines.filter((line) => !line.match(/^\s*$|^\s*"/)),
);
}
}

const stateFile = `${basePath}/state_${progname}.vim`;
console.log(stateFile);
await Deno.writeTextFile(stateFile, stateLines.join("\n"));

const cacheFile = `${basePath}/cache_${progname}.vim`;
const cacheLines = [
JSON.stringify([plugins, {}]),
JSON.stringify([configReturn.plugins, {}]),
];
console.log(cacheFile);
await Deno.writeTextFile(cacheFile, cacheLines.join("\n"));
Expand Down Expand Up @@ -318,6 +351,7 @@ Deno.test("initPlugin", () => {
script_type: "foo",
lazy: false,
merged: true,
sourced: false,
},
);

Expand All @@ -334,6 +368,7 @@ Deno.test("initPlugin", () => {
lazy: true,
merged: false,
on_ft: "foo",
sourced: false,
},
);
});
1 change: 1 addition & 0 deletions denops/dpp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type Context = {
export type DppOptions = {
extOptions: Record<ExtName, Partial<ExtOptions>>;
extParams: Record<ExtName, Partial<BaseExtParams>>;
inlineVimrcs: string[];
protocolOptions: Record<ProtocolName, Partial<ExtOptions>>;
protocolParams: Record<ProtocolName, Partial<BaseProtocolParams>>;
};
Expand Down
55 changes: 55 additions & 0 deletions doc/dpp-ext-lazy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
*dpp-ext-lazy.txt* lazy ext for dpp.vim

Author: Shougo <Shougo.Matsu at gmail.com>
License: MIT license

CONTENTS *dpp-ext-lazy-contents*

Introduction |dpp-ext-lazy-introduction|
Install |dpp-ext-lazy-install|
Examples |dpp-ext-lazy-examples|
Actions |dpp-ext-lazy-actions|
Preview params |dpp-ext-lazy-preview-params|
Params |dpp-ext-lazy-params|
Compatibility |dpp-ext-lazy-compatibility|


==============================================================================
INTRODUCTION *dpp-ext-lazy-introduction*

This ext implements lazy loading.


==============================================================================
INSTALL *dpp-ext-lazy-install*

Please install both "dpp.vim" and "denops.vim".

https://github.com/Shougo/dpp.vim
https://github.com/vim-denops/denops.vim


==============================================================================
EXAMPLES *dpp-ext-lazy-examples*
>
<

==============================================================================
ACTIONS *dpp-ext-lazy-actions*

*dpp-ext-lazy-action-makeState*
makeState
Returns state lines for |dpp#make_state()|.

params:
{plugins}: plugins list.
(Required)

==============================================================================
PARAMS *dpp-ext-lazy-params*

==============================================================================
COMPATIBILITY *dpp-ext-lazy-compatibility*

==============================================================================
vim:tw=78:ts=8:ft=help:norl:noet:fen:noet:
2 changes: 1 addition & 1 deletion doc/dpp-ext-toml.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ https://github.com/vim-denops/denops.vim
EXAMPLES *dpp-ext-toml-examples*
>
const plugins = await args.dpp.extAction(args.denops, "toml", "load", {
path: "$BASE_DIR/dein.toml",
path: "$BASE_DIR/dpp.toml",
options: {
lazy: true,
},
Expand Down
Loading

0 comments on commit b8df28e

Please sign in to comment.