Advertisement
Guest User

RobM

a guest
Sep 23rd, 2010
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. from ropevim import _interface
  2. from ropemode.interface import _CodeAssist
  3. from rope.base.exceptions import BadIdentifierError
  4. import vim
  5.  
  6. class Completer(object):
  7.     def create_code_assist(self):
  8.         return _CodeAssist(_interface, _interface.env)
  9.  
  10.     def __call__(self, findstart, base):
  11.         try:
  12.             if findstart:
  13.                 self.code_assist = self.create_code_assist()
  14.                 base_len = self.code_assist.offset - \
  15.                            self.code_assist.starting_offset
  16.                 return int(vim.eval("col('.')")) - base_len - 1
  17.             else:
  18.                 try:
  19.                     proposals = self.code_assist._calculate_proposals()
  20.                 except Exception:
  21.                     return []
  22.                 if vim.eval("complete_check()") != "0":
  23.                     return []
  24.                 ps = []
  25.                 for proposal in proposals:
  26.                     ci = _interface.env._completion_text(proposal)
  27.                     p = {}
  28.                     args = proposal.parameters or []
  29.                     p['word'] = "%s(" % ci
  30.                     p['info'] = proposal.get_doc() or 'x.' + str(ci) + '(' + ', '.join(map(str, args)) + ')'
  31.                     p['abbr'] = ci
  32.                     ps.append(p)
  33.                 del self.code_assist
  34.                 return ps
  35.         except BadIdentifierError:
  36.             del self.code_assist
  37.             if findstart:
  38.                 return -1
  39.             else:
  40.                 return []
  41.  
  42. completer = Completer()
  43.  
  44. # Note: Don't use str() to prepare the result for consumption by vim. Vim syntax treats
  45. # single and double quoted strings different. A good work around is to use the json
  46. # module to format the data:
  47. #
  48. # function! RopeCompleteFunc(findstart, base)
  49. # python <<EOF
  50. # findstart = int(vim.eval("a:findstart"))
  51. # base = vim.eval("a:base")
  52. # import json
  53. # vim.command("return %s" % json.dumps(completer(findstart, base)))
  54. # EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement