Skip to content

Commit

Permalink
globでファイルを探せるように
Browse files Browse the repository at this point in the history
  • Loading branch information
kat0h committed Nov 24, 2021
1 parent 9f39a06 commit 970c194
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions denops/bufpreview/lib/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export * as anonymous from "https://deno.land/x/[email protected]/anonymous/mod.
export { open } from "https://deno.land/x/[email protected]/index.ts";
export { v4 } from "https://deno.land/[email protected]/uuid/mod.ts";
export { EventEmitter } from "https://deno.land/x/[email protected]/mod.ts";
export { basename, join } from "https://deno.land/[email protected]/path/mod.ts";

export { Renderer } from "./types.ts";
46 changes: 39 additions & 7 deletions denops/bufpreview/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,52 @@ import {
open,
Renderer,
vars,
basename,
join
} from "./lib/deps.ts";

import Server from "./lib/server.ts";

// 一度に開けるサーバーは一つ
let server: Server | undefined;

const Markdown =
(await import(
(new URL("./@renderer/markdown/main.ts", import.meta.url)).href
)).default;
// レンダラー
interface Renderers {
[key: string]: Renderer;
}
let renderers: Renderers
// renderers.push(
// (await import(
// (new URL("./@renderers/markdown/main.ts", import.meta.url)).href
// )).default,
// );

export async function main(denops: Denops) {
// 全てのレンダラーを探索
async function findRenderer(): Promise<Renderers> {
const runtimepath = (await op.runtimepath.getGlobal(denops)).split(",");
const paths: string[] = []
const ret: Renderers = {}
// さがす
for (const path of runtimepath) {
const i = await fn.globpath(denops, path, "denops/bufpreview/@renderers/*", true, true) as string[]
paths.push(...i)
}
// よみこむ
// TODO: ここ非同期で読み込む
for (const path of paths) {
const name = basename(path)
const codePath = join(path, "main.ts")
ret[name] = (await import(codePath)).default
}
return ret
}

// test
renderers = await findRenderer()

export function main(denops: Denops) {
denops.dispatcher = {
// Vimから呼ばれる
async md(arg: unknown): Promise<void> {
ensureString(arg);

Expand All @@ -40,8 +72,8 @@ export function main(denops: Denops) {

// サーバーを開く
const openServer = async () => {
// レンダラー
const renderer = new Markdown(denops);
// @ts-ignore: これ型エラーにならない方法知っている方教えて
const renderer = new renderers["markdown"](denops)
// サーバーが既に開かれているなら
if (server != undefined) {
server.close();
Expand Down

0 comments on commit 970c194

Please sign in to comment.