Skip to content

Commit

Permalink
Refactor loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Jul 6, 2023
1 parent 6f2600c commit 1421a47
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 57 deletions.
61 changes: 5 additions & 56 deletions denops/ddu/ddu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
equal,
fn,
Lock,
op,
parse,
pathsep,
} from "./deps.ts";
import {
Expand All @@ -31,7 +29,6 @@ import {
ColumnOptions,
Context,
DduEvent,
DduExtType,
DduItem,
DduOptions,
ExpandItem,
Expand Down Expand Up @@ -1320,24 +1317,6 @@ export class Ddu {
});
}

async autoload(
denops: Denops,
type: DduExtType,
name: string,
) {
const paths = await globpath(
denops,
`denops/@ddu-${type}s/`,
this.loader.getAlias(type, name) ?? name,
);

if (paths.length === 0) {
return;
}

await this.loader.registerPath(type, paths[0]);
}

async setInput(denops: Denops, input: string) {
if (this.options.expandInput) {
input = await fn.expand(denops, input) as string;
Expand Down Expand Up @@ -1417,7 +1396,7 @@ export class Ddu {
]
> {
if (!this.loader.getUi(this.options.ui)) {
await this.autoload(denops, "ui", this.options.ui);
await this.loader.autoload(denops, "ui", this.options.ui);
}

const ui = this.loader.getUi(this.options.ui);
Expand Down Expand Up @@ -1453,7 +1432,7 @@ export class Ddu {
]
> {
if (!this.loader.getSource(name)) {
await this.autoload(denops, "source", name);
await this.loader.autoload(denops, "source", name);
}

const source = this.loader.getSource(name);
Expand Down Expand Up @@ -1496,7 +1475,7 @@ export class Ddu {
}

if (!this.loader.getFilter(userFilter.name)) {
await this.autoload(denops, "filter", userFilter.name);
await this.loader.autoload(denops, "filter", userFilter.name);
}

const filter = this.loader.getFilter(userFilter.name);
Expand Down Expand Up @@ -1529,7 +1508,7 @@ export class Ddu {
BaseKind<BaseKindParams> | undefined
> {
if (!this.loader.getKind(name)) {
await this.autoload(denops, "kind", name);
await this.loader.autoload(denops, "kind", name);
}

const kind = this.loader.getKind(name);
Expand Down Expand Up @@ -1557,7 +1536,7 @@ export class Ddu {
]
> {
if (!this.loader.getColumn(name)) {
await this.autoload(denops, "column", name);
await this.loader.autoload(denops, "column", name);
}

const column = this.loader.getColumn(name);
Expand Down Expand Up @@ -2079,36 +2058,6 @@ async function checkColumnOnInit(
}
}

async function globpath(
denops: Denops,
search: string,
file: string,
): Promise<string[]> {
const runtimepath = await op.runtimepath.getGlobal(denops);

const check: Record<string, boolean> = {};
const paths: string[] = [];
const glob = await fn.globpath(
denops,
runtimepath,
search + file + ".ts",
1,
1,
);

for (const path of glob) {
// Skip already added name.
if (parse(path).name in check) {
continue;
}

paths.push(path);
check[parse(path).name] = true;
}

return paths;
}

function convertTreePath(treePath: TreePath) {
return typeof treePath === "string" ? treePath.split(pathsep) : treePath;
}
Expand Down
50 changes: 49 additions & 1 deletion denops/ddu/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
SourceName,
UiName,
} from "./types.ts";
import { Lock, parse, toFileUrl } from "./deps.ts";
import { Denops, fn, Lock, op, parse, toFileUrl } from "./deps.ts";

export class Loader {
private uis: Record<UiName, BaseUi<BaseUiParams>> = {};
Expand All @@ -36,6 +36,24 @@ export class Loader {
private checkPaths: Record<string, boolean> = {};
private registerLock = new Lock(0);

async autoload(
denops: Denops,
type: DduExtType,
name: string,
) {
const paths = await globpath(
denops,
`denops/@ddu-${type}s/`,
this.getAlias(type, name) ?? name,
);

if (paths.length === 0) {
return;
}

await this.registerPath(type, paths[0]);
}

registerAlias(type: DduAliasType, alias: string, base: string) {
this.aliases[type][alias] = base;
}
Expand Down Expand Up @@ -137,3 +155,33 @@ export class Loader {
this.checkPaths[path] = true;
}
}

async function globpath(
denops: Denops,
search: string,
file: string,
): Promise<string[]> {
const runtimepath = await op.runtimepath.getGlobal(denops);

const check: Record<string, boolean> = {};
const paths: string[] = [];
const glob = await fn.globpath(
denops,
runtimepath,
search + file + ".ts",
1,
1,
);

for (const path of glob) {
// Skip already added name.
if (parse(path).name in check) {
continue;
}

paths.push(path);
check[parse(path).name] = true;
}

return paths;
}

0 comments on commit 1421a47

Please sign in to comment.