Skip to content

Commit

Permalink
Merge pull request #19 from mcepl/omnicomplete
Browse files Browse the repository at this point in the history
Omnicomplete
  • Loading branch information
mcepl committed Dec 3, 2013
2 parents 989a0ea + 571183e commit 255dac5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.rst
Expand Up @@ -321,6 +321,15 @@ C-c f RopeFindOccurrences
================ ============================


Support for Omni completion
---------------------------

You can enable using Rope as providing for Omni completion by setting
omnifunc variable to ``RopeCompleteFunc``. E.g., by putting something
like this in your ``~/.vimrc``::
autocmd FileType python setlocal omnifunc=RopeCompleteFunc


Contributing
============

Expand Down
56 changes: 56 additions & 0 deletions rope_omni.py
@@ -0,0 +1,56 @@
import vim
import ropevim
import ropemode.interface


class RopeOmniCompleter(object):
""" The class used to complete python code. """

def __init__(self, base=""):
self.assist = None
self.start = self.get_start(base)

def vim_string(self, inp):
""" Creates a vim-friendly string from a group of
dicts, lists and strings.
"""
def conv(obj):
if isinstance(obj, list):
return u'[' + u",".join([conv(o) for o in obj]) + u']'
elif isinstance(obj, dict):
return u'{' + u','.join([
u"%s:%s" % (conv(key), conv(value))
for key, value in obj.iteritems()]) + u'}'
else:
return u'"%s"' % str(obj).replace(u'"', u'\\"')
return conv(inp)

def _get_dict(self, prop):
ci = vim.eval(ropevim._env._extended_completion(prop))
ci['info'] = prop.get_doc() or " "
return ci

def complete(self, base):
""" Gets a completion list using a given base string. """
if vim.eval("complete_check()") != "0":
return []

try:
proposals = self.assist._calculate_proposals()
except Exception: # a bunch of rope stuff
return []

ps = [self._get_dict(p) for p in proposals]
return self.vim_string(ps)

def get_start(self, base):
""" Gets the starting column for vim completion. """
try:
inf = ropevim._interface
self.assist = ropemode.interface._CodeAssist(inf, inf.env)

base_len = self.assist.offset - self.assist.starting_offset
return int(vim.eval("col('.')")) - base_len - 1

except Exception:
return -1
18 changes: 18 additions & 0 deletions ropevim.vim
@@ -1,7 +1,25 @@
if !has("python")
finish
endif

function! LoadRope()
python << EOF
import ropevim
from rope_omni import RopeOmniCompleter
EOF
endfunction

call LoadRope()

" The code below is an omni-completer for python using rope and ropevim.
" Created by Ryan Wooden (rygwdn@gmail.com)

function! RopeCompleteFunc(findstart, base)
" A completefunc for python code using rope
if (a:findstart)
py ropecompleter = RopeOmniCompleter(vim.eval("a:base"))
py vim.command("return %s" % ropecompleter.start)
else
py vim.command("return %s" % ropecompleter.complete(vim.eval("a:base")))
endif
endfunction
9 changes: 5 additions & 4 deletions setup.py
Expand Up @@ -7,7 +7,7 @@
from distutils.core import setup


classifiers=[
classifiers = [
'Development Status :: 4 - Beta',
'Operating System :: OS Independent',
'Environment :: X11 Applications',
Expand All @@ -20,16 +20,17 @@
'Programming Language :: Python',
'Topic :: Software Development']


def get_long_description():
lines = open('README.rst').read().splitlines(False)
end = lines.index('Setting Up')
return '\n' + '\n'.join(lines[:end]) + '\n'

setup(name='ropevim',
version='0.3-rc',
version='0.3-rc.1.omni',
description='A vim plugin for using rope python refactoring library',
long_description=get_long_description(),
py_modules=['ropevim'],
py_modules=['ropevim', 'rope_omni'],
author='Ali Gholami Rudi',
author_email='aligrudi@users.sourceforge.net',
url='http://rope.sf.net/ropevim.html',
Expand All @@ -38,4 +39,4 @@ def get_long_description():
requires=['ropemode'],
data_files=[('share/vim/plugin', ['ropevim.vim'])],
**extra_kwargs
)
)

0 comments on commit 255dac5

Please sign in to comment.