Skip to content

Commit

Permalink
Add local action
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Sep 18, 2023
1 parent 4b35fd0 commit c594925
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
6 changes: 4 additions & 2 deletions autoload/dpp/util.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const s:is_windows = has('win32') || has('win64')

function dpp#util#_error(msg) abort
for mes in s:msg2list(a:msg)
echohl WarningMsg | echomsg '[dpp] ' .. mes | echohl None
Expand Down Expand Up @@ -84,8 +86,8 @@ function dpp#util#_expand(path) abort
\ (a:path =~# '^\$\h\w*') ? a:path
\ ->substitute('^\$\h\w*', '\=eval(submatch(0))', '') :
\ a:path
return (s:is_windows && path =~# '\\') ?
\ dpp#util#_substitute_path(path) : path
return ((s:is_windows && path =~# '\\') ?
\ dpp#util#_substitute_path(path) : path)->substitute('/$', '', '')
endfunction
function dpp#util#_substitute_path(path) abort
return ((s:is_windows || has('win32unix')) && a:path =~# '\\') ?
Expand Down
49 changes: 45 additions & 4 deletions denops/@dpp-exts/local.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,55 @@
import { Actions, BaseExt } from "../dpp/types.ts";
import { Denops } from "../dpp/deps.ts";
import { Actions, BaseExt, Plugin } from "../dpp/types.ts";
import { Denops, fn } from "../dpp/deps.ts";
import { isDirectory } from "../dpp/utils.ts";
import { basename } from "https://deno.land/[email protected]/path/mod.ts";

type Params = Record<string, never>;

type LocalArgs = {
directory: string;
options?: Partial<Plugin>;
includes?: string[];
};

export class Ext extends BaseExt<Params> {
override actions: Actions<Params> = {
local: {
description: "Load local plugins",
callback: async (args: { denops: Denops }) => {
console.log("hello");
callback: async (args: {
denops: Denops;
actionParams: unknown;
}) => {
const params = args.actionParams as LocalArgs;
const base = await args.denops.call(
"dpp#util#_expand",
params.directory,
);

const defaultOptions = params.options ?? {};

let plugins: Plugin[] = [];
for (const include of params.includes ?? ["*"]) {
const dirs = await fn.glob(
args.denops,
base + "/" + include,
1,
1,
) as string[];

plugins = plugins.concat(
dirs.filter(async (dir) => await isDirectory(dir)).map((dir) => {
return {
...defaultOptions,
repo: dir,
local: true,
path: dir,
name: basename(dir),
};
}),
);
}

return plugins;
},
},
};
Expand Down
6 changes: 6 additions & 0 deletions denops/dpp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,11 @@ export type Action<Params extends BaseActionParams> = {
};

export type Plugin = {
frozen?: boolean;
lazy?: boolean;
local?: boolean;
merged?: boolean;
name: string;
path: string;
repo: string;
};
13 changes: 13 additions & 0 deletions denops/dpp/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,16 @@ export async function errorException(
console.error(e);
}
}

export async function isDirectory(path: string) {
// NOTE: Deno.stat() may be failed
try {
if ((await Deno.stat(path)).isDirectory) {
return true;
}
} catch (_e: unknown) {
// Ignore
}

return false;
}

0 comments on commit c594925

Please sign in to comment.