Skip to content

Commit bc8f8d3

Browse files
authored
feat: add colorscheme lazy loading (#170)
1 parent 9753b09 commit bc8f8d3

5 files changed

Lines changed: 85 additions & 3 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ export type Lazy = {
341341
ft?: string | string[];
342342
// Load the plugin when the key is pressed. (Optional)
343343
keys?: string | string[] | KeyMap | KeyMap[];
344+
// Load the plugin when the colorscheme is applied. (Optional)
345+
colorscheme?: string | string[];
344346
};
345347

346348
export type Command = {
@@ -583,6 +585,14 @@ e.g.
583585
},
584586
});
585587

588+
// Load on colorscheme.
589+
await dvpm.add({
590+
url: "folke/tokyonight.nvim",
591+
lazy: {
592+
colorscheme: "tokyonight",
593+
},
594+
});
595+
586596
// Load on keys.
587597
await dvpm.add({
588598
url: "mbbill/undotree",

dvpm.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,18 @@ export class Dvpm {
879879
);
880880
}
881881
}
882+
if (lazy.colorscheme) {
883+
const colorschemes = Array.isArray(lazy.colorscheme)
884+
? lazy.colorscheme
885+
: [lazy.colorscheme];
886+
await autocmd.define(
887+
denops,
888+
"ColorSchemePre",
889+
colorschemes,
890+
`call denops#request('${denops.name}', 'load', ['${p.info.url}', 'colorscheme', '<amatch>'])`,
891+
{ once: true },
892+
);
893+
}
882894
if (lazy.event) {
883895
const events = Array.isArray(lazy.event) ? lazy.event : [lazy.event];
884896
await autocmd.define(

plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class Plugin {
125125

126126
private async initLazy() {
127127
const lazy = this.info.lazy;
128-
if (lazy.cmd || lazy.event || lazy.ft || lazy.keys) {
128+
if (lazy.cmd || lazy.event || lazy.ft || lazy.keys || lazy.colorscheme) {
129129
lazy.enabled = true;
130130
}
131131
lazy.enabled = await this.is(lazy.enabled ?? false);

tests/colorscheme_lazy_test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// =============================================================================
2+
// File : colorscheme_lazy_test.ts
3+
// Author : yukimemi
4+
// Last Change : 2026/04/01 00:00:00.
5+
// =============================================================================
6+
7+
import { assertEquals } from "@std/assert";
8+
import { test } from "@denops/test";
9+
import { Dvpm } from "../dvpm.ts";
10+
11+
test({
12+
mode: "all",
13+
name: "Lazy Loading: colorscheme trigger defines ColorSchemePre autocmd",
14+
fn: async (denops) => {
15+
const base = await Deno.makeTempDir();
16+
// Use Dvpm.begin to register dispatcher
17+
const dvpm = await Dvpm.begin(denops, { base, health: false });
18+
19+
const colorName = "mycolor";
20+
const pluginUrl = "color/plugin";
21+
await dvpm.add({
22+
url: pluginUrl,
23+
lazy: {
24+
enabled: true,
25+
colorscheme: colorName,
26+
},
27+
});
28+
29+
const plugin = dvpm.plugins[0];
30+
31+
try {
32+
await dvpm.end();
33+
34+
// Check if ColorSchemePre autocmd for mycolor is defined
35+
const isDefined = await denops.call("exists", `#ColorSchemePre#${colorName}`);
36+
assertEquals(isDefined, 1, `ColorSchemePre autocmd for ${colorName} should be defined`);
37+
38+
// Manually trigger ColorSchemePre to simulate colorscheme command
39+
await denops.cmd(`doautocmd ColorSchemePre ${colorName}`);
40+
41+
// Verify the plugin was loaded into runtimepath
42+
const rtp = await denops.eval("&runtimepath") as string;
43+
assertEquals(
44+
rtp.includes(plugin.info.dst),
45+
true,
46+
"Plugin destination should be in runtimepath after trigger",
47+
);
48+
} finally {
49+
// Clean up
50+
await denops.cmd(`autocmd! ColorSchemePre ${colorName}`);
51+
try {
52+
await Deno.remove(base, { recursive: true });
53+
} catch {
54+
// Ignore
55+
}
56+
}
57+
},
58+
});

types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export type LazyParams = {
9797
event?: string | string[];
9898
ft?: string | string[];
9999
keys?: string | string[] | KeyMap | KeyMap[];
100+
colorscheme?: string | string[];
100101
};
101102

102103
const _LazyParamsSchema = type({
@@ -105,14 +106,15 @@ const _LazyParamsSchema = type({
105106
"event?": "string | string[]",
106107
"ft?": "string | string[]",
107108
"keys?": type(KeyMapSchema, "|", "string").array().or(KeyMapSchema).or("string"),
109+
"colorscheme?": "string | string[]",
108110
});
109111

110112
export const LazyParamsSchema: Type<LazyParams> = _LazyParamsSchema as unknown as Type<LazyParams>;
111113

112114
/**
113115
* Represents the type of load trigger.
114116
*/
115-
export type LoadType = "cmd" | "keys" | "ft" | "event";
117+
export type LoadType = "cmd" | "keys" | "ft" | "event" | "colorscheme";
116118

117119
/**
118120
* Schema for the arguments of the load method.
@@ -146,7 +148,7 @@ const _CmdParamsSchema = type({
146148

147149
const _LoadArgsSchema = type({
148150
url: "string",
149-
"loadType?": type("'cmd' | 'keys' | 'ft' | 'event'").or("undefined"),
151+
"loadType?": type("'cmd' | 'keys' | 'ft' | 'event' | 'colorscheme'").or("undefined"),
150152
"arg?": type("string").or("undefined"),
151153
"params?": _CmdParamsSchema.or("undefined"),
152154
});

0 commit comments

Comments
 (0)