Skip to content

Instantly share code, notes, and snippets.

@nobuoka
Created November 4, 2012 14:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobuoka/4012062 to your computer and use it in GitHub Desktop.
Save nobuoka/4012062 to your computer and use it in GitHub Desktop.
" filetype が typescript の場合にのみ動作する neocomplcache プラグイン
" jsx_complete.vim を参考にしてます.
" https://github.com/osyo-manga/neocomplcache-jsx/blob/master/autoload/neocomplcache/sources/jsx_complete.vim
let s:source = {
\ 'name': 'typescript_complete',
\ 'kind': 'ftplugin',
\ 'filetypes': { 'typescript': 1 },
\ }
function! s:source.initialize()
endfunction
function! s:source.finalize()
endfunction
" 補完を開始する桁位置 (0 が行頭)
" とりあえず今はプロパティ名の補完だけに対応するので, カーソル位置の
" 左側にあるドットを探す. 行内にドットがなければ補完しない (-1 を返す).
function! s:source.get_keyword_pos(cur_text)
let line = getline('.')
let wsstart = col('.') - 1
" カーソルの左側にあるドットの位置を探す
while wsstart >= 0
" ドットより先に空白文字かセミコロンが見つかった場合は補完をしない
if line[wsstart] =~ '[\s;]'
let wsstart = -1
break
endif
" ドットを見つけたらループ終了
if line[wsstart-1] == '.'
break
endif
let wsstart -= 1
endwhile
return wsstart
endfunction
function! s:source.get_complete_words(cur_keyword_pos, cur_keyword_str)
if bufname('%') == ''
return []
endif
" 一時ファイルに保存
let buf = getline(1, '$')
let tempfile = expand('%:p:h') . '/' . localtime() . expand('%:t')
call writefile(buf, tempfile)
"let escaped_tempfile = shellescape(tempfile)
" 補完候補の取得は, ローカルホストで動かしている HTTP サーバーに任せる
" HTTP サーバーには一時ファイルのパスと, 補完を行う位置 (プロパティ名の補完は
" ドットの左側の位置で行われるので, 与えられた pos から 1 ひいている) を渡す.
" TODO URL のクエリ文字列をパーセントエンコーディングする必要あり
" TODO 一時ファイルに保存しないでいいようにしたい
let command = 'curl -s "http://localhost:10080/'
\ . '?' . 'filepath=' . tempfile
\ . '&' . 'line=' . line('.')
\ . '&' . 'col=' . (a:cur_keyword_pos - 1)
\ . '"'
" HTTP サーバーにリクエストして, 取得した結果の文字列を辞書のリストに変換
" (eval すればそのまま値になるような形式の文字列で HTTP サーバーが結果を返すようにしておく.)
let result = system(command)
sandbox let output = eval(result)
call delete(tempfile)
return output
endfunction
function! neocomplcache#sources#typescript_complete#define()
return s:source
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment