Skip to content

Instantly share code, notes, and snippets.

@aliou
Created April 28, 2018 12:28
Show Gist options
  • Save aliou/36abeb31885c34d60ad1810270e371bc to your computer and use it in GitHub Desktop.
Save aliou/36abeb31885c34d60ad1810270e371bc to your computer and use it in GitHub Desktop.
autoload/swiftcomplete.vim
let s:cpo_save = &cpoptions
set cpoptions&vim
function! s:reduce(fct, list) abort
let [l:acc; l:tail] = a:list
while !empty(l:tail)
let [l:head; l:tail] = l:tail
let l:acc = a:fct(l:acc, l:head)
endwhile
return l:acc
endfunction
function! s:add(a, b) abort
return a:a + a:b
endfunction
function! s:offset() abort
if line('.') == 1
return col('.')
endif
let l:columns = map(range(1, line('.') - 1), 'col([v:val, ''$''])')
let l:offset = s:reduce(function('s:add'), l:columns) + col('.')
return l:offset
endfunction
function! s:run_command(cmd) abort
let l:term_options = {
\ 'term_rows': 10,
\ }
" call term_start(['/bin/sh', '-c', a:cmd], l:term_options)
" let l:job = job_start(['/bin/sh', '-c', a:cmd],
silent let l:result = system(a:cmd)
try
return json_decode(l:result)
catch
endtry
throw 'invalid response from sourcekitten'
endfunction
function! swiftcomplete#Complete(findstart, base)
" Start completion on current position.
if a:findstart
return col('.')
endif
let l:filepath = expand('%:p')
" Detect ios sdk and target
let l:command = 'sourcekitten complete --file ' . l:filepath . ' --offset ' . s:offset()
let l:completion_results = s:run_command(l:command)
" Some filtering is needed here.
let l:completions = map(l:completion_results, 'v:val.sourcetext')
return l:completions
return ['music', 'book', 'place']
endfunction
let &cpoptions = s:cpo_save
unlet s:cpo_save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment