diff --git a/README.rst b/README.rst index b94238e..9b7a24e 100644 --- a/README.rst +++ b/README.rst @@ -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 ============ diff --git a/rope_omni.py b/rope_omni.py new file mode 100644 index 0000000..ee963b7 --- /dev/null +++ b/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 diff --git a/ropevim.vim b/ropevim.vim index f3a7f87..2edeab4 100644 --- a/ropevim.vim +++ b/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 diff --git a/setup.py b/setup.py index 4edac24..28dc27d 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ from distutils.core import setup -classifiers=[ +classifiers = [ 'Development Status :: 4 - Beta', 'Operating System :: OS Independent', 'Environment :: X11 Applications', @@ -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', @@ -38,4 +39,4 @@ def get_long_description(): requires=['ropemode'], data_files=[('share/vim/plugin', ['ropevim.vim'])], **extra_kwargs -) + )