Skip to content

Commit

Permalink
Merge pull request #114 from kamecha/feature/telescope-channel-source
Browse files Browse the repository at this point in the history
telescopeでチャンネルを検索できるようにした
  • Loading branch information
kamecha authored Nov 7, 2024
2 parents fadec62 + ee034c2 commit 9b85409
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 18 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [ddu.vim](https://github.com/Shougo/ddu.vim) (Optional)
- [ddc.vim](https://github.com/Shougo/ddc.vim) (Optional)
- [open-browser.vim](https://github.com/tyru/open-browser.vim) (Optional)
- [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) (Optional)

## 他プラグインとの連携

Expand Down Expand Up @@ -57,6 +58,12 @@ function s:traqvim_setting()
endfunction
```

telescope.nvimとの連携

```lua
require("telescope").load_extension "traqvim"
```

## 今後の展望

WebSocketとか実装して、手動リロードしなくてもいいようにしたいなぁ...
17 changes: 14 additions & 3 deletions denops/traqvim/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { OAuth } from "./oauth.ts";
import {
channelMessageOptions,
channelsRecursive,
channelTimeline,
channelUUID,
Expand Down Expand Up @@ -38,9 +37,14 @@ import {
actionYankMessageLink,
actionYankMessageMarkdown,
} from "./action.ts";
import { Channel, ChannelMessageBuffer, Message } from "./type.d.ts";
import {
Channel,
ChannelMessageBuffer,
channelMessageOptions,
Message,
} from "./type.d.ts";
import { api } from "./api.ts";
import { isMessage } from "./type_check.ts";
import { isChannelMessageOptions, isMessage } from "./type_check.ts";

export async function main(denops: Denops) {
const path = await vars.globals.get(denops, "traqvim#token_file_path");
Expand Down Expand Up @@ -470,6 +474,13 @@ export async function main(denops: Denops) {
const channels = await channelsRecursive();
return channels;
};
denops.dispatcher["channelMessage"] = async (
option: unknown,
): Promise<Message[]> => {
assert(option, isChannelMessageOptions);
const timeline = await channelTimeline(option);
return timeline;
};
denops.dispatcher["convertDate"] = (date: unknown): Promise<string> => {
assert(date, is.String);
const d = new Date(date);
Expand Down
20 changes: 6 additions & 14 deletions denops/traqvim/model.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { api } from "./api.ts";
import { Channel, Message, UnreadChannel } from "./type.d.ts";
import {
Channel,
channelMessageOptions,
Message,
UnreadChannel,
} from "./type.d.ts";
import { ensure, is, traq } from "./deps.ts";

export type channelMessageOptions = {
// channelUUID
id: string;
// #gps/time/kamecha
channelPath?: string;
limit?: number;
offset?: number;
since?: string;
until?: string;
inclusive?: boolean;
order?: "asc" | "desc";
};

// TODO: ↓チャンネル周りはclassに分離しても良いかも
const channelMapCache: Map<string, traq.Channel> = new Map();

Expand Down
13 changes: 13 additions & 0 deletions denops/traqvim/type.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { traq } from "./deps.ts";

export type channelMessageOptions = {
// channelUUID
id: string;
// #gps/time/kamecha
channelPath?: string;
limit?: number;
offset?: number;
since?: string;
until?: string;
inclusive?: boolean;
order?: "asc" | "desc";
};

export interface Channel extends traq.Channel {
path: string;
}
Expand Down
14 changes: 13 additions & 1 deletion denops/traqvim/type_check.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ddcVim, is, Predicate, traq } from "./deps.ts";
import { Message } from "./type.d.ts";
import { channelMessageOptions, Message } from "./type.d.ts";

const isPumHighlight: Predicate<ddcVim.PumHighlight> = is.ObjectOf({
name: is.String,
Expand Down Expand Up @@ -73,3 +73,15 @@ export const isMessage: Predicate<Message> = is.ObjectOf({
// quote?: Message[];
quote: is.OptionalOf(is.ArrayOf((x: unknown): x is Message => isMessage(x))),
});

export const isChannelMessageOptions: Predicate<channelMessageOptions> = is
.ObjectOf({
id: is.String,
channelPath: is.OptionalOf(is.String),
limit: is.OptionalOf(is.Number),
offset: is.OptionalOf(is.Number),
since: is.OptionalOf(is.String),
until: is.OptionalOf(is.String),
inclusive: is.OptionalOf(is.Boolean),
order: is.OptionalOf(is.LiteralOneOf(["asc", "desc"] as const)),
});
11 changes: 11 additions & 0 deletions doc/traqvim.jax
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ type (string)
Default: "all"


==============================================================================
TELESCOPE *traqvim-telescope*

|telescope.nvim|と連携したtraQのチャンネル名のソースを提供します。
チャンネルのメッセージをプレビューとして表示します。

>lua
local telescope = require('telescope')
telescope.load_extension('traqvim')
<

==============================================================================
FAQ *traqvim-faq*

Expand Down
62 changes: 62 additions & 0 deletions lua/telescope/_extensions/traqvim.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
local pickers = require "telescope.pickers"
local finders = require "telescope.finders"
local actions = require "telescope.actions"
local action_state = require "telescope.actions.state"
local previewers = require "telescope.previewers"
local conf = require("telescope.config").values

local channel_rec = function()
local channels = vim.fn["denops#request"]('traqvim', 'channelList', {})
return channels
end

return require("telescope").register_extension {
exports = {
traqvim = function(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = "channel",
finder = finders.new_table {
results = channel_rec(),
entry_maker = function(entry)
return {
value = entry,
display = entry["path"],
ordinal = entry["path"],
}
end,
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr, _)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
local action = selection["value"]
local path = action["path"]
vim.fn["denops#request"]('traqvim', 'timeline', { path })
end)
return true
end,
previewer = previewers.new_buffer_previewer({
define_preview = function(self, entry, _)
local value = entry["value"]
local option = {
id = value["id"],
limit = vim.g["traqvim#fetch_limit"],
}
vim.fn.setbufvar(self.state.bufnr, "&filetype", "traqvim")
local timeline = vim.fn["denops#request"]('traqvim', 'channelMessage', { option })
local start_line = 1
for _, v in ipairs(timeline) do
local width = vim.fn.winwidth(self.state.winid)
local mes = vim.fn["traqvim#view#make_message_body"](v, width)
vim.fn.setbufline(self.state.bufnr, start_line, mes["body"])
start_line = start_line + #mes["body"]
end
end,
title = "timeline preview",
}),
}):find()
end
},
}

0 comments on commit 9b85409

Please sign in to comment.