Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
maralla committed Sep 14, 2016
0 parents commit f945db3
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.pyc
.cache
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 <maralla.ai@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
80 changes: 80 additions & 0 deletions autoload/completor.vim
@@ -0,0 +1,80 @@
" vim: et ts=2 sts=2 sw=2

let s:save_cpo = &cpo
set cpo&vim

let s:completions = []


function! completor#omnifunc(findstart, base)
if a:findstart
let word = matchstr(getline('.'), '\w*\%'.col('.').'c')
return col('.') - 1 - len(word)
endif
return s:completions
endfunction


function! s:handle(ch)
let msg = []
while ch_status(a:ch) == 'buffered'
call add(msg, ch_read(a:ch))
endwhile

Py << EOF
import completor, vim
completer = completor.load_completer(vim.current.buffer.options['ft'])
result = completer.parse(vim.eval('msg')) if completer else []
EOF

let s:completions = Pyeval('result')
setlocal omnifunc=completor#omnifunc
if !empty(s:completions)
setlocal completeopt-=longest
setlocal completeopt+=menuone
setlocal completeopt-=menu
if &completeopt !~# 'noinsert\|noselect'
setlocal completeopt+=noselect
endif
call feedkeys("\<C-x>\<C-o>", 'n')
endif
endfunction


function! s:execute(cmd)
let job = job_start(a:cmd, {"close_cb": {c->s:handle(c)}, "in_io": 'null', "err_io": 'out'})
endfunction


function! s:complete()
Py << EOF
import completor, vim
completer = completor.load_completer(vim.current.buffer.options['ft'])
cmd = completer.format_cmd() if completer else ''
EOF

let cmd = Pyeval('cmd')
if !empty(cmd)
call s:execute(cmd)
endif
endfunction


function! s:set_events()
augroup completor
autocmd!
autocmd TextChangedI * call s:complete()
augroup END
endfunction


function! completor#enable()
if &diff
return
endif

call s:set_events()
endfunction

let &cpo = s:save_cpo
unlet s:save_cpo
5 changes: 5 additions & 0 deletions autoload/completor/utils.vim
@@ -0,0 +1,5 @@
function! completor#utils#tempname()
let tmp = escape(tempname(), ' ')
call writefile(getline(1, '$'), tmp)
return tmp
endfunction
43 changes: 43 additions & 0 deletions plugin/completor.vim
@@ -0,0 +1,43 @@
" vim: et ts=2 sts=2 sw=2

let s:save_cpo = &cpo
set cpo&vim

function! s:restore_cpo()
let &cpo = s:save_cpo
unlet s:save_cpo
endfunction

if exists("g:loaded_completor_plugin")
call s:restore_cpo()
finish
elseif !(has('python') || has('python3')) || !(has('job') && has('timers') && has('lambda'))
echohl WarningMsg |
\ echomsg "Completor requires vim compiled with python or python3 and has features `job`, `timers` and `lambda`" |
\ echohl None
call s:restore_cpo()
finish
endif


let g:loaded_completor_plugin = 1

if has("python3")
command! -nargs=1 Py py3 <args>
function! Pyeval(arg)
return py3eval(a:arg)
endfunction
else
command! -nargs=1 Py py <args>
function! Pyeval(arg)
return pyeval(a:arg)
endfunction
endif

augroup completor
autocmd!
autocmd VimEnter * call completor#enable()
augroup END


call s:restore_cpo()
Empty file added pythonx/completers/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions pythonx/completers/rust.py
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-

from completor import Completor

type_map = {
'Struct' : 's', 'Module' : 'M', 'Function' : 'f',
'Crate' : 'C', 'Let' : 'v', 'StructField' : 'm',
'Impl' : 'i', 'Enum' : 'e', 'EnumVariant' : 'E',
'Type' : 't', 'FnArg' : 'v', 'Trait' : 'T'
}

class Racer(Completor):
__filetype__ = 'rust'

name = 'racer'

def format_cmd(self):
line, col = self.cursor
return "{} complete {} {} {} {}".format(self.name, line, col,
self.filename, self.tempname)

def parse(self, items):
completions = []
for item in items:
if not item.startswith('MATCH'):
continue

parts = item.split(',')
name = parts[0][6:]
kind = type_map.get(parts[4], '')
spec = ', '.join(parts[5:])

completions.append({
'kind': kind,
'word': name,
'menu': spec,
'dup': 1
})
return completions
52 changes: 52 additions & 0 deletions pythonx/completor/__init__.py
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-

import importlib
import vim


class Meta(type):
def __init__(cls, name, bases, attrs):
if name not in ('Completor', 'Base'):
Completor._registry[cls.__filetype__] = cls()

return super(Meta, cls).__init__(name, bases, attrs)

Base = Meta('Base', (object,), {})


class Unusable(object):
def __get__(self, inst, owner):
raise RuntimeError('unusable')


class Completor(Base):
_registry = {}

__filetype__ = Unusable()
name = Unusable()

@property
def tempname(self):
return vim.eval('completor#utils#tempname()')

@property
def filename(self):
return vim.current.buffer.name

@property
def cursor(self):
return vim.current.window.cursor

_completor = Completor()


def load_completer(ft):
if not ft:
return None

if ft not in _completor._registry:
try:
importlib.import_module("completers.{}".format(ft))
except ImportError:
return None
return _completor._registry[ft]

0 comments on commit f945db3

Please sign in to comment.