Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

サブコマンドを使って名前空間を侵さないようにした #82

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions autoload/command.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@

let s:subcommands = #{
\ token: #{
\ args: ['setup', 'delete'],
\ impl: function('command#token'),
\ comp: function('command#tokenComplete'),
\ },
\ channel: #{
\ args: ['open', 'home', 'reload', 'forward', 'back'],
\ impl: function('command#channel'),
\ comp: function('command#channelComplete'),
\ },
\ activity: #{
\ args: ['open', 'reload'],
\ impl: function('command#activity'),
\ comp: function('command#activityComplete'),
\ },
\ message: #{
\ args: ['open', 'send', 'delete', 'edit', 'editApply', 'yankLink', 'yankMarkdown'],
\ impl: function('command#message'),
\ comp: function('command#messageComplete'),
\ },
\ pin: #{
\ args: ['create', 'remove'],
\ impl: function('command#pin'),
\ comp: function('command#pinComplete'),
\ },
\}

function command#token(args) abort
if a:args[0] ==# 'setup'
call denops#request('traqvim', 'setupOAuth', [])
elseif a:args[0] ==# 'delete'
call denops#request('traqvim', 'deleteOAuthToken', [])
endif
endfunction

function command#tokenComplete(arglead, cmdline) abort
if a:cmdline[strlen(a:cmdline)-1] ==# ' ' && len(split(a:cmdline)) >= 3
return []
endif
return s:subcommands.token.args->copy()->filter({_, v -> v =~? '^' . a:arglead})
endfunction

function command#channel(args) abort
if a:args[0] ==# 'open'
" TODO:↓これtimeline APIの実装側が間違ってるのでいったんこのまま
call denops#request('traqvim', 'timeline', [a:args[1]])
elseif a:args[0] ==# 'home'
call denops#request('traqvim', 'home', [])
elseif a:args[0] ==# 'reload'
call denops#request('traqvim', 'reload', [bufnr(), bufname()])
elseif a:args[0] ==# 'forward'
call denops#request('traqvim', 'messageForwad', [bufnr(), bufname()])
elseif a:args[0] ==# 'back'
call denops#request('traqvim', 'messageBack', [bufnr(), bufname()])
endif
endfunction

function command#channelComplete(arglead, cmdline) abort
if a:cmdline[strlen(a:cmdline)-1] ==# ' ' && len(split(a:cmdline)) >= 3
return []
endif
return s:subcommands.channel.args->copy()->filter({_, v -> v =~? '^' . a:arglead})
endfunction

function command#activity(args) abort
if a:args[0] ==# 'open'
call denops#request('traqvim', 'activity', [])
elseif a:args[0] ==# 'reload'
call denops#request('traqvim', 'reload', [bufnr(), bufname()])
endif
endfunction

function command#activityComplete(arglead, cmdline) abort
if a:cmdline[strlen(a:cmdline)-1] ==# ' ' && len(split(a:cmdline)) >= 3
return []
endif
return s:subcommands.activity.args->copy()->filter({_, v -> v =~? '^' . a:arglead})
endfunction

function command#message(args) abort
if a:args[0] ==# 'open'
call denops#request('traqvim', 'messageOpen', [bufnr(), bufname()])
elseif a:args[0] ==# 'send'
call denops#request('traqvim', 'messageSend', [bufnr(), getline(1, '$')])
elseif a:args[0] ==# 'delete'
call denops#request('traqvim', 'messageDelete', [bufnr(), traqvim#get_message()])
elseif a:args[0] ==# 'edit'
call denops#request('traqvim', 'messageEditOpen', [bufnr(), traqvim#get_message()])
elseif a:args[0] ==# 'editApply'
call denops#request('traqvim', 'messageEdit', [getbufvar(bufname("%"), "editSourceBuffer"), getbufvar(bufname("%"), "message"), getline(1, '$')])
elseif a:args[0] ==# 'yankLink'
call denops#request('traqvim', 'yankMessageLink', [traqvim#get_message()])
elseif a:args[0] ==# 'yankMarkdown'
call denops#request('traqvim', 'yankMessageMarkdown', [traqvim#get_message()])
endif
endfunction

function command#messageComplete(arglead, cmdline) abort
if a:cmdline[strlen(a:cmdline)-1] ==# ' ' && len(split(a:cmdline)) >= 3
return []
endif
return s:subcommands.message.args->copy()->filter({_, v -> v =~? '^' . a:arglead})
endfunction

function command#pin(args) abort
if a:args[0] ==# 'create'
call denops#request('traqvim', 'createPin', [bufnr(), traqvim#get_message()])
elseif a:args[0] ==# 'remove'
call denops#request('traqvim', 'removePin', [bufnr(), traqvim#get_message()])
endif
endfunction

function command#pinComplete(arglead, cmdline) abort
if a:cmdline[strlen(a:cmdline)-1] ==# ' ' && len(split(a:cmdline)) >= 3
return []
endif
return s:subcommands.pin.args->copy()->filter({_, v -> v =~? '^' . a:arglead})
endfunction

function command#complete(arglead, cmdline, cursorpos) abort
" subcommandを取得
let subcmd = matchstr(a:cmdline, '^Traq\s\+\zs\w\+')
let subcmdArgLead = matchstr(a:cmdline, '^Traq\s\+\w\+\s\+\zs\w\+')
" subcommandの引数を補完
if subcmd != ''
\ && has_key(s:subcommands, subcmd) == 1
\ && has_key(s:subcommands[subcmd], 'comp') == 1
return s:subcommands[subcmd].comp(a:arglead, a:cmdline)
endif
" subcommandを補完
let cmdline = matchstr(a:cmdline, '^Traq\s\+\w*$')
if cmdline != ''
let subcmdKeys = s:subcommands->keys()
return subcmdKeys->filter({_, v -> v =~? '^' . a:arglead})
endif
endfunction

function command#call(rargs) abort
let args = split(a:rargs)
let subcommandKey = args[0]
let subcommand = s:subcommands->get(subcommandKey)
if type(subcommand) ==# type(0) && subcommand ==# 0
echomsg 'subcommand not found: ' . subcommandKey
return
endif
call subcommand.impl(args[1:])
endfunction

5 changes: 0 additions & 5 deletions ftplugin/traqvim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ nnoremap <buffer><expr> <Plug>(traqvim-operator-pin-toggle)
onoremap <buffer><silent> <Plug>(traqvim-motion-message)
\ :<C-u>call traqvim#message_motion()<CR>

command! -buffer -nargs=0 TraqYankMessageLink
\ call denops#request('traqvim', 'yankMessageLink', [traqvim#get_message()])
command! -buffer -nargs=0 TraqYankMessageMarkdown
\ call denops#request('traqvim', 'yankMessageMarkdown', [traqvim#get_message()])

" filetypeがtraqvimの時かつ、ウィンドウのサイズが変更された時だけ実行

augroup traqvim
Expand Down
35 changes: 5 additions & 30 deletions plugin/traqvim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,11 @@
" endif
" let g:loaded_traq = 1
"
" :Traq setup でdenopsのsetupAPIを叩く
command! TraqSetup call denops#request('traqvim', 'setupOAuth', [])
" :Traq setup でdenopsのsetupAPIを叩く
command! TraqDeleteToken call denops#request('traqvim', 'deleteOAuthToken', [])
" homeChannelを開く
command! TraqHome call denops#request('traqvim', 'home', [])
" :Traq timeline でdenopsのtimelineAPIを叩く
command! -nargs=1 TraqTimeline call denops#request('traqvim', 'timeline', [<q-args>])
" activity
command! TraqActivity call denops#request('traqvim', 'activity', [])
" reload
command! TraqReload call denops#request('traqvim', 'reload', [bufnr(), bufname()])
" fetch forward
command! TraqFetchForward call denops#request('traqvim', 'messageForward', [bufnr(), bufname()])
" fetch backward
command! TraqFetchBack call denops#request('traqvim', 'messageBack', [bufnr(), bufname()])
" messageバッファの作成
command! TraqMessageOpen call denops#request('traqvim', 'messageOpen', [bufnr(), bufname()])
" messageの送信
command! TraqMessageSend call denops#request('traqvim', 'messageSend', [bufnr(), getline(1, '$')])
" messageの削除
command! TraqMessageDelete call denops#request('traqvim', 'messageDelete', [bufnr(), traqvim#get_message()])
" messageの編集
command! TraqMessageEdit call denops#request('traqvim', 'messageEditOpen', [bufnr(), traqvim#get_message()])
" messageの編集を適用
command! TraqMessageEditApply call denops#request('traqvim', 'messageEdit', [getbufvar(bufname("%"), "editSourceBuffer"), getbufvar(bufname("%"), "message"), getline(1, '$')])
" pinを作成
command! TraqCreatePin call denops#request('traqvim', 'createPin', [bufnr(), traqvim#get_message()])
" pinを削除
command! TraqRemovePin call denops#request('traqvim', 'removePin', [bufnr(), traqvim#get_message()])

command! -nargs=+
\ -complete=customlist,command#complete
\ Traq
\ call command#call(<q-args>)

call helper#define_highlight()

Expand Down
Loading