Skip to content

Commit

Permalink
Improve loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Dec 3, 2024
1 parent 8f4bb41 commit b839d19
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 73 deletions.
8 changes: 4 additions & 4 deletions denops/@ddu-sources/dpp.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Plugin } from "../dpp/types.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import * as vars from "jsr:@denops/std@~7.3.0/variable";
import type { Denops } from "jsr:@denops/std@~7.4.0";
import * as vars from "jsr:@denops/std@~7.4.0/variable";

import type {
ActionArguments,
ActionFlags,
Item,
} from "jsr:@shougo/ddu-vim@6.4.0/types";
import { BaseSource } from "jsr:@shougo/ddu-vim@6.4.0/source";
} from "jsr:@shougo/ddu-vim@9.0.0/types";
import { BaseSource } from "jsr:@shougo/ddu-vim@9.0.0/source";
import type { ActionData } from "jsr:@shougo/ddu-kind-file@0.9.0";

type Params = {
Expand Down
37 changes: 34 additions & 3 deletions denops/dpp/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import { ContextBuilderImpl } from "./context.ts";
import { DppImpl } from "./dpp.ts";
import type {
BaseParams,
DppExtType,
DppOptions,
ExtOptions,
ProtocolName,
} from "./types.ts";
import type { BaseExt } from "./base/ext.ts";
import type { Protocol } from "./base/protocol.ts";
import type { BaseProtocol, Protocol } from "./base/protocol.ts";
import { Loader } from "./loader.ts";
import { extAction } from "./ext.ts";
import { isDenoCacheIssueError } from "./utils.ts";

import type { Denops, Entrypoint } from "jsr:@denops/std@~7.3.0";
import * as vars from "jsr:@denops/std@~7.3.0/variable";
import type { Denops, Entrypoint } from "jsr:@denops/std@~7.4.0";
import * as vars from "jsr:@denops/std@~7.4.0/variable";

import { ensure } from "jsr:@core/unknownutil@~4.3.0/ensure";
import { is } from "jsr:@core/unknownutil@~4.3.0/is";
Expand All @@ -25,6 +26,36 @@ export const main: Entrypoint = (denops: Denops) => {
const contextBuilder = new ContextBuilderImpl();

denops.dispatcher = {
async registerPath(arg1: unknown, arg2: unknown): Promise<void> {
await loader.registerPath(
ensure(arg1, is.String) as DppExtType,
ensure(arg2, is.String),
);
return Promise.resolve();
},
registerExtension(
arg1: unknown,
arg2: unknown,
arg3: unknown,
): Promise<void> {
const type = ensure(arg1, is.String);
const extName = ensure(arg2, is.String);

switch (type) {
case "ext":
loader.registerExtension(type, extName, arg3 as BaseExt<BaseParams>);
break;
case "protocol":
loader.registerExtension(
type,
extName,
arg3 as BaseProtocol<BaseParams>,
);
break;
}

return Promise.resolve();
},
async extAction(
arg1: unknown,
arg2: unknown,
Expand Down
2 changes: 1 addition & 1 deletion denops/dpp/base/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ContextBuilder } from "../types.ts";
import type { Plugin } from "../types.ts";
import type { Dpp } from "./dpp.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import type { Denops } from "jsr:@denops/std@~7.4.0";

export type ConfigArguments = {
contextBuilder: ContextBuilder;
Expand Down
2 changes: 1 addition & 1 deletion denops/dpp/base/dpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
import type { BaseExt } from "./ext.ts";
import type { Protocol } from "./protocol.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import type { Denops } from "jsr:@denops/std@~7.4.0";

export interface Dpp {
extAction(
Expand Down
2 changes: 1 addition & 1 deletion denops/dpp/base/ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
} from "../types.ts";
import type { Protocol } from "./protocol.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import type { Denops } from "jsr:@denops/std@~7.4.0";

export type OnInitArguments<Params extends BaseParams> = {
denops: Denops;
Expand Down
2 changes: 1 addition & 1 deletion denops/dpp/base/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BaseParams, Plugin, ProtocolOptions } from "../types.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import type { Denops } from "jsr:@denops/std@~7.4.0";

export type OnInitArguments<Params extends BaseParams> = {
denops: Denops;
Expand Down
2 changes: 1 addition & 1 deletion denops/dpp/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
} from "./types.ts";
import { printError } from "./utils.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import type { Denops } from "jsr:@denops/std@~7.4.0";

// where
// T: Object
Expand Down
6 changes: 3 additions & 3 deletions denops/dpp/dpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import {
printError,
} from "./utils.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import * as fn from "jsr:@denops/std@~7.3.0/function";
import * as vars from "jsr:@denops/std@~7.3.0/variable";
import type { Denops } from "jsr:@denops/std@~7.4.0";
import * as fn from "jsr:@denops/std@~7.4.0/function";
import * as vars from "jsr:@denops/std@~7.4.0/variable";

import { dirname } from "jsr:@std/path@~1.0.2/dirname";
import { extname } from "jsr:@std/path@~1.0.2/extname";
Expand Down
4 changes: 2 additions & 2 deletions denops/dpp/ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { type BaseProtocol, defaultProtocolOptions } from "./base/protocol.ts";
import type { Protocol } from "./base/protocol.ts";
import { printError } from "./utils.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import * as fn from "jsr:@denops/std@~7.3.0/function";
import type { Denops } from "jsr:@denops/std@~7.4.0";
import * as fn from "jsr:@denops/std@~7.4.0/function";

export async function getProtocols(
denops: Denops,
Expand Down
107 changes: 53 additions & 54 deletions denops/dpp/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ import type { BaseExt } from "./base/ext.ts";
import type { BaseProtocol } from "./base/protocol.ts";
import { isDenoCacheIssueError } from "./utils.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import * as op from "jsr:@denops/std@~7.3.0/option";
import * as fn from "jsr:@denops/std@~7.3.0/function";
import type { Denops } from "jsr:@denops/std@~7.4.0";
import * as op from "jsr:@denops/std@~7.4.0/option";
import * as fn from "jsr:@denops/std@~7.4.0/function";

import { basename } from "jsr:@std/path@~1.0.2/basename";
import { parse } from "jsr:@std/path@~1.0.2/parse";
import { toFileUrl } from "jsr:@std/path@~1.0.2/to-file-url";
import { Lock } from "jsr:@core/asyncutil@~1.2.0/lock";

type Mod = {
// deno-lint-ignore no-explicit-any
mod: any;
path: string;
type Ext = {
ext: Record<string, BaseExt<BaseParams>>;
protocol: Record<string, BaseProtocol<BaseParams>>;
};

export class Loader {
#extension: Extension = new Extension();
#mods: Record<DppExtType, Record<string, Mod>> = {
#exts: Ext = {
ext: {},
protocol: {},
};
Expand Down Expand Up @@ -76,68 +74,69 @@ export class Loader {
});
}

getExt(name: ExtName): BaseExt<BaseParams> | null {
const mod = this.#mods.ext[name];
if (!mod) {
return null;
}
registerExtension(
type: "ext",
name: string,
ext: BaseExt<BaseParams>,
): void;
registerExtension(
type: "protocol",
name: string,
ext: BaseProtocol<BaseParams>,
): void;
registerExtension(
type: DppExtType,
name: string,
ext:
| BaseExt<BaseParams>
| BaseProtocol<BaseParams>,
) {
ext.name = name;
this.#exts[type][name] = ext;
}

return this.#extension.getExt(mod, name);
getExt(name: ExtName): BaseExt<BaseParams> | null {
return this.#exts.ext[name];
}
getProtocol(name: ProtocolName): BaseProtocol<BaseParams> | null {
const mod = this.#mods.protocol[name];
if (!mod) {
return null;
}

return this.#extension.getProtocol(mod, name);
return this.#exts.protocol[name];
}

async #register(type: DppExtType, path: string) {
if (path in this.#checkPaths) {
return;
}

const mods = this.#mods[type];

const name = parse(path).name;
const mod = await import(toFileUrl(path).href);

const typeExt = this.#exts[type];
let add;
switch (type) {
case "ext":
add = (name: string) => {
const ext = new mod.Ext();
ext.name = name;
ext.path = path;
typeExt[name] = ext;
};
break;
case "protocol":
add = (name: string) => {
const ext = new mod.Protocol();
ext.name = name;
ext.path = path;
typeExt[name] = ext;
};
break;
}

const mod: Mod = {
mod: await import(toFileUrl(path).href),
path,
};

mods[name] = mod;
add(name);

this.#checkPaths[path] = true;
}
}

class Extension {
#exts: Record<ExtName, BaseExt<BaseParams>> = {};
#protocols: Record<ProtocolName, BaseProtocol<BaseParams>> = {};

getExt(mod: Mod, name: string): BaseExt<BaseParams> {
if (!this.#exts[name]) {
const obj = new mod.mod.Ext();
obj.name = name;
obj.path = mod.path;
this.#exts[obj.name] = obj;
}
return this.#exts[name];
}

getProtocol(mod: Mod, name: string): BaseProtocol<BaseParams> {
if (!this.#protocols[name]) {
const obj = new mod.mod.Protocol();
obj.name = name;
obj.path = mod.path;
this.#protocols[obj.name] = obj;
}
return this.#protocols[name];
}
}

async function globpath(
denops: Denops,
search: string,
Expand Down
2 changes: 1 addition & 1 deletion denops/dpp/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Denops } from "jsr:@denops/std@~7.3.0";
import type { Denops } from "jsr:@denops/std@~7.4.0";

export type DppExtType = "ext" | "protocol";

Expand Down
2 changes: 1 addition & 1 deletion denops/dpp/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Plugin } from "./types.ts";

import type { Denops } from "jsr:@denops/std@~7.3.0";
import type { Denops } from "jsr:@denops/std@~7.4.0";

import { join } from "jsr:@std/path@~1.0.2/join";
import { assertEquals } from "jsr:@std/assert@~1.0.2/equals";
Expand Down

0 comments on commit b839d19

Please sign in to comment.