Skip to content

Commit

Permalink
Merge pull request #93 from kamecha/refacter/update-unkownutil-library
Browse files Browse the repository at this point in the history
unkownutil周りのライブラリを上げて、型判定周りを整理
  • Loading branch information
kamecha authored Jul 27, 2024
2 parents 3452e46 + 603b5a2 commit cf5101e
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 78 deletions.
12 changes: 7 additions & 5 deletions denops/@ddc-sources/stamp.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import {
assert,
ddcVim,
ddcVimSource,
Denops,
ensureString,
ensure,
is,
traq,
vars,
} from "../traqvim/deps.ts";

import { getStamps } from "../traqvim/model.ts";
import { api } from "../traqvim/api.ts";
import { isDdcItem } from "../traqvim/type_check.ts";

type Params = Record<never, never>;

export class Source extends ddcVim.BaseSource<Params> {
async onInit(args: ddcVimSource.OnInitArguments<Params>): Promise<void> {
const path = await vars.globals.get(args.denops, "traqvim#token_file_path");
ensureString(path);
assert(path, is.String);
api.tokenFilePath = path;
return Promise.resolve();
}
Expand All @@ -25,9 +27,9 @@ export class Source extends ddcVim.BaseSource<Params> {
return stamps
.filter((stamp) => stamp.name)
.map((stamp) => {
return {
return ensure({
word: ":" + stamp.name + ":",
} as ddcVim.Item;
}, isDdcItem);
});
}

Expand Down
4 changes: 2 additions & 2 deletions denops/@ddu-columns/channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dduVim, dduVimColumn, fn } from "../traqvim/deps.ts";
import { dduVim, dduVimColumn, ensure, fn, is } from "../traqvim/deps.ts";

export type Params = {
collapsedParentIcon: string;
Expand Down Expand Up @@ -36,7 +36,7 @@ export class Column extends dduVim.BaseColumn<Params> {
const text =
" ".repeat(args.columnParams.indentationWidth * args.item.__level) +
icon + " " + args.item.word;
const width = await fn.strwidth(args.denops, text) as number;
const width = ensure(await fn.strwidth(args.denops, text), is.Number);
const padding = " ".repeat(args.endCol - args.startCol - width);
return Promise.resolve({
text: text + padding,
Expand Down
26 changes: 18 additions & 8 deletions denops/@ddu-kinds/channel.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import {
assert,
dduVim,
Denops,
ensureArray,
ensureNumber,
ensure,
is,
Predicate,
vars,
} from "../traqvim/deps.ts";
import { channelMessageOptions, channelTimeline } from "../traqvim/model.ts";
import { actionOpenChannel } from "../traqvim/action.ts";
import { Message } from "../traqvim/type.d.ts";

// TODO: unkownutilのアプデしたらtype.d.tsのChannelとかに変更する
// ChannelとUnreadChannelをUnion型にできなかったので、kindごと分ける
export interface ActionData {
id: string;
}

export const isActionData: Predicate<ActionData> = is.ObjectOf({
id: is.String,
});

type Params = Record<never, never>;

type OpenParams = {
command: string;
};

const isOpenParams: Predicate<OpenParams> = is.ObjectOf({
command: is.String,
});

export class Kind extends dduVim.BaseKind<Params> {
actions: dduVim.Actions<Params> = {
open: async (args: {
Expand All @@ -31,23 +42,22 @@ export class Kind extends dduVim.BaseKind<Params> {
if (!item.action) {
continue;
}
// TODO: unkownutilのアプデしたらasをensureに変更
const action = item.action as ActionData;
const action = ensure(item.action, isActionData);
const channelPath: string = item.word;
const channelID: string = action.id;
const limit = await vars.globals.get(
args.denops,
"traqvim#fetch_limit",
);
ensureNumber(limit);
assert(limit, is.Number);
const timelineOption: channelMessageOptions = {
id: channelID,
channelPath: channelPath,
limit: limit,
until: new Date().toISOString(),
order: "desc",
};
const params = args.actionParams as OpenParams;
const params = ensure(args.actionParams, isOpenParams);
if (params.command) {
await args.denops.cmd(params.command);
}
Expand All @@ -65,7 +75,7 @@ export class Kind extends dduVim.BaseKind<Params> {
item: dduVim.DduItem;
},
): Promise<dduVim.Previewer | undefined> {
const action = args.item.action as ActionData;
const action = ensure(args.item.action, isActionData);
if (!action) {
return undefined;
}
Expand All @@ -80,7 +90,7 @@ export class Kind extends dduVim.BaseKind<Params> {
message,
args.previewContext.width,
);
ensureArray<string>(ret);
assert(ret, is.ArrayOf(is.String));
return ret;
}),
);
Expand Down
5 changes: 3 additions & 2 deletions denops/@ddu-sources/channel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
assert,
dduVim,
dduVimSource,
Denops,
ensureString,
helper,
is,
vars,
} from "../traqvim/deps.ts";
import { ActionData } from "../@ddu-kinds/channel.ts";
Expand All @@ -17,7 +18,7 @@ export class Source extends dduVim.BaseSource<Params> {
kind = "channel";
async onInit(args: dduVimSource.OnInitArguments<Params>): Promise<void> {
const path = await vars.globals.get(args.denops, "traqvim#token_file_path");
ensureString(path);
assert(path, is.String);
api.tokenFilePath = path;
return Promise.resolve();
}
Expand Down
5 changes: 3 additions & 2 deletions denops/@ddu-sources/channel_rec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {
assert,
dduVim,
dduVimSource,
Denops,
ensureString,
is,
vars,
} from "../traqvim/deps.ts";
import { ActionData } from "../@ddu-kinds/channel.ts";
Expand All @@ -18,7 +19,7 @@ export class Source extends dduVim.BaseSource<Params> {
kind = "channel";
async onInit(args: dduVimSource.OnInitArguments<Params>): Promise<void> {
const path = await vars.globals.get(args.denops, "traqvim#token_file_path");
ensureString(path);
assert(path, is.String);
api.tokenFilePath = path;
return Promise.resolve();
}
Expand Down
15 changes: 8 additions & 7 deletions denops/traqvim/action.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bufname, Denops, ensureArray, fn, helper, vars } from "./deps.ts";
import { assert, bufname, Denops, fn, helper, is, vars } from "./deps.ts";
import { ChannelBuffer, Message } from "./type.d.ts";
import {
activity,
Expand All @@ -9,6 +9,7 @@ import {
editMessage,
removePin,
} from "./model.ts";
import { isMessage } from "./type_check.ts";

export const actionOpenChannel = async (
denops: Denops,
Expand Down Expand Up @@ -56,7 +57,7 @@ export const actionForwardChannelMessage = async (
): Promise<void> => {
// 既存メッセージの取得
const timeline = await vars.buffers.get(denops, "channelTimeline");
ensureArray<Message>(timeline);
assert(timeline, is.ArrayOf(isMessage));
// 追記したものをセット
await vars.buffers.set(
denops,
Expand All @@ -79,7 +80,7 @@ export const actionBackChannelMessage = async (
): Promise<void> => {
// 既存メッセージの取得
const timeline = await vars.buffers.get(denops, "channelTimeline");
ensureArray<Message>(timeline);
assert(timeline, is.ArrayOf(isMessage));
// 追記したものをセット
await vars.buffers.set(
denops,
Expand All @@ -103,7 +104,7 @@ export const actionDeleteMessage = async (
}
// 既存メッセージの取得
const timeline = await vars.buffers.get(denops, "channelTimeline");
ensureArray<Message>(timeline);
assert(timeline, is.ArrayOf(isMessage));
// 削除したものをセット
await vars.buffers.set(
denops,
Expand All @@ -128,7 +129,7 @@ export const actionEditMessage = async (
// 既存メッセージの取得
// const timeline = await vars.buffers.get(denops, "channelTimeline");
const timeline = await fn.getbufvar(denops, bufNum, "channelTimeline");
ensureArray<Message>(timeline);
assert(timeline, is.ArrayOf(isMessage));
const editedTimeline = timeline.map((m) => {
if (m.id === message.id) {
return {
Expand Down Expand Up @@ -209,7 +210,7 @@ export const actionCreatePin = async (
}
// 既存メッセージの取得
const timeline = await vars.buffers.get(denops, "channelTimeline");
ensureArray<Message>(timeline);
assert(timeline, is.ArrayOf(isMessage));
message.pinned = true;
// ピン留めしたものをセット
await vars.buffers.set(
Expand Down Expand Up @@ -239,7 +240,7 @@ export const actionRemovePin = async (
}
// 既存メッセージの取得
const timeline = await vars.buffers.get(denops, "channelTimeline");
ensureArray<Message>(timeline);
assert(timeline, is.ArrayOf(isMessage));
message.pinned = false;
// ピン留め解除したものをセット
await vars.buffers.set(
Expand Down
10 changes: 5 additions & 5 deletions denops/traqvim/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ export * as oauth2Client from "https://deno.land/x/oauth2_client@v1.0.0/mod.ts";
export * as oak from "https://deno.land/x/oak@v6.3.0/mod.ts";
export * as path from "https://deno.land/std@0.177.0/path/mod.ts";
export {
ensureArray,
ensureNumber,
ensureObject,
ensureString,
} from "https://deno.land/x/unknownutil@v1.0.0/mod.ts";
assert,
ensure,
is,
} from "https://deno.land/x/unknownutil@v3.18.1/mod.ts";
export type { Predicate } from "https://deno.land/x/unknownutil@v3.18.1/mod.ts";
export type { Denops } from "https://deno.land/x/denops_std@v4.0.0/mod.ts";
export * as fn from "https://deno.land/x/denops_std@v4.0.0/function/mod.ts";
export * as vars from "https://deno.land/x/denops_std@v4.0.0/variable/mod.ts";
Expand Down
Loading

0 comments on commit cf5101e

Please sign in to comment.