PageRenderTime 137ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/bundle/jedi-vim/jedi_vim.py

https://bitbucket.org/hbhzwj/vim-config
Python | 473 lines | 398 code | 48 blank | 27 comment | 56 complexity | d2df529357ce64771dc89a04de6d1758 MD5 | raw file
Possible License(s): WTFPL
  1. """
  2. The Python parts of the Jedi library for VIM. It is mostly about communicating
  3. with VIM.
  4. """
  5. import traceback # for exception output
  6. import re
  7. import os
  8. from shlex import split as shsplit
  9. import vim
  10. import jedi
  11. import jedi.keywords
  12. from jedi._compatibility import unicode, is_py3k
  13. def catch_and_print_exceptions(func):
  14. def wrapper(*args, **kwargs):
  15. try:
  16. return func(*args, **kwargs)
  17. except (Exception, vim.error):
  18. print(traceback.format_exc())
  19. return None
  20. return wrapper
  21. class VimError(Exception):
  22. def __init__(self, message, throwpoint, executing):
  23. super(type(self), self).__init__(message)
  24. self.throwpoint = throwpoint
  25. self.executing = executing
  26. def __str__(self):
  27. return self.message + '; created by: ' + repr(self.executing)
  28. def _catch_exception(string, is_eval):
  29. """
  30. Interface between vim and python calls back to it.
  31. Necessary, because the exact error message is not given by `vim.error`.
  32. """
  33. e = 'jedi#_vim_exceptions(%s, %s)'
  34. result = vim.eval(e % (repr(PythonToVimStr(string, 'UTF-8')), is_eval))
  35. if 'exception' in result:
  36. raise VimError(result['exception'], result['throwpoint'], string)
  37. return result['result']
  38. def vim_eval(string):
  39. return _catch_exception(string, 1)
  40. def vim_command(string):
  41. _catch_exception(string, 0)
  42. def echo_highlight(msg):
  43. vim_command('echohl WarningMsg | echom "%s" | echohl None' % msg)
  44. class PythonToVimStr(unicode):
  45. """ Vim has a different string implementation of single quotes """
  46. __slots__ = []
  47. def __new__(cls, obj, encoding='UTF-8'):
  48. if is_py3k or isinstance(obj, unicode):
  49. return unicode.__new__(cls, obj)
  50. else:
  51. return unicode.__new__(cls, obj, encoding)
  52. def __repr__(self):
  53. # this is totally stupid and makes no sense but vim/python unicode
  54. # support is pretty bad. don't ask how I came up with this... It just
  55. # works...
  56. # It seems to be related to that bug: http://bugs.python.org/issue5876
  57. if unicode is str:
  58. s = self
  59. else:
  60. s = self.encode('UTF-8')
  61. return '"%s"' % s.replace('\\', '\\\\').replace('"', r'\"')
  62. @catch_and_print_exceptions
  63. def get_script(source=None, column=None):
  64. jedi.settings.additional_dynamic_modules = [b.name for b in vim.buffers
  65. if b.name is not None and b.name.endswith('.py')]
  66. if source is None:
  67. source = '\n'.join(vim.current.buffer)
  68. row = vim.current.window.cursor[0]
  69. if column is None:
  70. column = vim.current.window.cursor[1]
  71. buf_path = vim.current.buffer.name
  72. encoding = vim_eval('&encoding') or 'latin1'
  73. return jedi.Script(source, row, column, buf_path, encoding)
  74. @catch_and_print_exceptions
  75. def completions():
  76. row, column = vim.current.window.cursor
  77. clear_call_signatures()
  78. if vim.eval('a:findstart') == '1':
  79. count = 0
  80. for char in reversed(vim.current.line[:column]):
  81. if not re.match('[\w\d]', char):
  82. break
  83. count += 1
  84. vim.command('return %i' % (column - count))
  85. else:
  86. base = vim.eval('a:base')
  87. source = ''
  88. for i, line in enumerate(vim.current.buffer):
  89. # enter this path again, otherwise source would be incomplete
  90. if i == row - 1:
  91. source += line[:column] + base + line[column:]
  92. else:
  93. source += line
  94. source += '\n'
  95. # here again hacks, because jedi has a different interface than vim
  96. column += len(base)
  97. try:
  98. script = get_script(source=source, column=column)
  99. completions = script.completions()
  100. signatures = script.call_signatures()
  101. out = []
  102. for c in completions:
  103. d = dict(word=PythonToVimStr(c.name[:len(base)] + c.complete),
  104. abbr=PythonToVimStr(c.name),
  105. # stuff directly behind the completion
  106. menu=PythonToVimStr(c.description),
  107. info=PythonToVimStr(c.doc), # docstr
  108. icase=1, # case insensitive
  109. dup=1 # allow duplicates (maybe later remove this)
  110. )
  111. out.append(d)
  112. strout = str(out)
  113. except Exception:
  114. # print to stdout, will be in :messages
  115. print(traceback.format_exc())
  116. strout = ''
  117. completions = []
  118. signatures = []
  119. show_call_signatures(signatures)
  120. vim.command('return ' + strout)
  121. @catch_and_print_exceptions
  122. def goto(is_definition=False, is_related_name=False, no_output=False):
  123. definitions = []
  124. script = get_script()
  125. try:
  126. if is_related_name:
  127. definitions = script.usages()
  128. elif is_definition:
  129. definitions = script.goto_definitions()
  130. else:
  131. definitions = script.goto_assignments()
  132. except jedi.NotFoundError:
  133. echo_highlight( "Cannot follow nothing. Put your cursor on a valid name.")
  134. else:
  135. if no_output:
  136. return definitions
  137. if not definitions:
  138. echo_highlight("Couldn't find any definitions for this.")
  139. elif len(definitions) == 1 and not is_related_name:
  140. # just add some mark to add the current position to the jumplist.
  141. # this is ugly, because it overrides the mark for '`', so if anyone
  142. # has a better idea, let me know.
  143. vim_command('normal! m`')
  144. d = list(definitions)[0]
  145. if d.in_builtin_module():
  146. if d.is_keyword:
  147. echo_highlight(
  148. "Cannot get the definition of Python keywords.")
  149. else:
  150. echo_highlight("Builtin modules cannot be displayed (%s)."
  151. % d.module_path)
  152. else:
  153. if d.module_path != vim.current.buffer.name:
  154. result = new_buffer(d.module_path)
  155. if not result:
  156. return
  157. vim.current.window.cursor = d.line, d.column
  158. vim_command('normal! zt') # cursor at top of screen
  159. else:
  160. # multiple solutions
  161. lst = []
  162. for d in definitions:
  163. if d.in_builtin_module():
  164. lst.append(dict(text=
  165. PythonToVimStr('Builtin ' + d.description)))
  166. else:
  167. lst.append(dict(filename=PythonToVimStr(d.module_path),
  168. lnum=d.line, col=d.column + 1,
  169. text=PythonToVimStr(d.description)))
  170. vim_eval('setqflist(%s)' % repr(lst))
  171. vim_eval('jedi#add_goto_window()')
  172. return definitions
  173. @catch_and_print_exceptions
  174. def show_documentation():
  175. script = get_script()
  176. try:
  177. definitions = script.goto_definitions()
  178. except jedi.NotFoundError:
  179. definitions = []
  180. except Exception:
  181. # print to stdout, will be in :messages
  182. definitions = []
  183. print("Exception, this shouldn't happen.")
  184. print(traceback.format_exc())
  185. if not definitions:
  186. echo_highlight('No documentation found for that.')
  187. vim.command('return')
  188. else:
  189. docs = ['Docstring for %s\n%s\n%s' % (d.desc_with_module, '='*40, d.doc) if d.doc
  190. else '|No Docstring for %s|' % d for d in definitions]
  191. text = ('\n' + '-' * 79 + '\n').join(docs)
  192. vim.command('let l:doc = %s' % repr(PythonToVimStr(text)))
  193. vim.command('let l:doc_lines = %s' % len(text.split('\n')))
  194. @catch_and_print_exceptions
  195. def clear_call_signatures():
  196. cursor = vim.current.window.cursor
  197. e = vim_eval('g:jedi#call_signature_escape')
  198. regex = r'%sjedi=([0-9]+), ([^%s]*)%s.*%sjedi%s'.replace('%s', e)
  199. for i, line in enumerate(vim.current.buffer):
  200. match = re.search(r'%s' % regex, line)
  201. if match is not None:
  202. vim_regex = r'\v' + regex.replace('=', r'\=') + '.{%s}' % \
  203. int(match.group(1))
  204. vim_command(r'try | %s,%ss/%s/\2/g | catch | endtry' \
  205. % (i + 1, i + 1, vim_regex))
  206. vim_eval('histdel("search", -1)')
  207. vim_command('let @/ = histget("search", -1)')
  208. vim.current.window.cursor = cursor
  209. @catch_and_print_exceptions
  210. def show_call_signatures(signatures=()):
  211. if vim_eval("has('conceal') && g:jedi#show_call_signatures") == '0':
  212. return
  213. if signatures == ():
  214. signatures = get_script().call_signatures()
  215. clear_call_signatures()
  216. if not signatures:
  217. return
  218. for i, signature in enumerate(signatures):
  219. line, column = signature.bracket_start
  220. # signatures are listed above each other
  221. line_to_replace = line - i - 1
  222. # because there's a space before the bracket
  223. insert_column = column - 1
  224. if insert_column < 0 or line_to_replace <= 0:
  225. # Edge cases, when the call signature has no space on the screen.
  226. break
  227. # TODO check if completion menu is above or below
  228. line = vim_eval("getline(%s)" % line_to_replace)
  229. params = [p.get_code().replace('\n', '') for p in signature.params]
  230. try:
  231. params[signature.index] = '*%s*' % params[signature.index]
  232. except (IndexError, TypeError):
  233. pass
  234. # This stuff is reaaaaally a hack! I cannot stress enough, that
  235. # this is a stupid solution. But there is really no other yet.
  236. # There is no possibility in VIM to draw on the screen, but there
  237. # will be one (see :help todo Patch to access screen under Python.
  238. # (Marko Mahni, 2010 Jul 18))
  239. text = " (%s) " % ', '.join(params)
  240. text = ' ' * (insert_column - len(line)) + text
  241. end_column = insert_column + len(text) - 2 # -2 due to bold symbols
  242. # Need to decode it with utf8, because vim returns always a python 2
  243. # string even if it is unicode.
  244. e = vim_eval('g:jedi#call_signature_escape')
  245. if hasattr(e, 'decode'):
  246. e = e.decode('UTF-8')
  247. # replace line before with cursor
  248. regex = "xjedi=%sx%sxjedix".replace('x', e)
  249. prefix, replace = line[:insert_column], line[insert_column:end_column]
  250. # Check the replace stuff for strings, to append them
  251. # (don't want to break the syntax)
  252. regex_quotes = r'''\\*["']+'''
  253. # `add` are all the quotation marks.
  254. # join them with a space to avoid producing '''
  255. add = ' '.join(re.findall(regex_quotes, replace))
  256. # search backwards
  257. if add and replace[0] in ['"', "'"]:
  258. a = re.search(regex_quotes + '$', prefix)
  259. add = ('' if a is None else a.group(0)) + add
  260. tup = '%s, %s' % (len(add), replace)
  261. repl = prefix + (regex % (tup, text)) + add + line[end_column:]
  262. vim_eval('setline(%s, %s)' % (line_to_replace, repr(PythonToVimStr(repl))))
  263. @catch_and_print_exceptions
  264. def rename():
  265. if not int(vim.eval('a:0')):
  266. _rename_cursor = vim.current.window.cursor
  267. vim_command('normal A ') # otherwise startinsert doesn't work well
  268. vim.current.window.cursor = _rename_cursor
  269. vim_command('augroup jedi_rename')
  270. vim_command('autocmd InsertLeave <buffer> call jedi#rename(1)')
  271. vim_command('augroup END')
  272. vim_command('normal! diw')
  273. vim_command(':startinsert')
  274. else:
  275. window_path = vim.current.buffer.name
  276. # reset autocommand
  277. vim_command('autocmd! jedi_rename InsertLeave')
  278. replace = vim_eval("expand('<cword>')")
  279. vim_command('normal! u') # undo new word
  280. cursor = vim.current.window.cursor
  281. vim_command('normal! u') # undo the space at the end
  282. vim.current.window.cursor = cursor
  283. if replace is None:
  284. echo_highlight('No rename possible, if no name is given.')
  285. else:
  286. temp_rename = goto(is_related_name=True, no_output=True)
  287. # sort the whole thing reverse (positions at the end of the line
  288. # must be first, because they move the stuff before the position).
  289. temp_rename = sorted(temp_rename, reverse=True,
  290. key=lambda x: (x.module_path, x.start_pos))
  291. for r in temp_rename:
  292. if r.in_builtin_module():
  293. continue
  294. if vim.current.buffer.name != r.module_path:
  295. result = new_buffer(r.module_path)
  296. if not result:
  297. return
  298. vim.current.window.cursor = r.start_pos
  299. vim_command('normal! cw%s' % replace)
  300. result = new_buffer(window_path)
  301. if not result:
  302. return
  303. vim.current.window.cursor = cursor
  304. echo_highlight('Jedi did %s renames!' % len(temp_rename))
  305. @catch_and_print_exceptions
  306. def py_import():
  307. # args are the same as for the :edit command
  308. args = shsplit(vim.eval('a:args'))
  309. import_path = args.pop()
  310. text = 'import %s' % import_path
  311. scr = jedi.Script(text, 1, len(text), '')
  312. try:
  313. completion = scr.goto_assignments()[0]
  314. except IndexError:
  315. echo_highlight('Cannot find %s in sys.path!' % import_path)
  316. else:
  317. if completion.in_builtin_module():
  318. echo_highlight('%s is a builtin module.' % import_path)
  319. else:
  320. cmd_args = ' '.join([a.replace(' ', '\\ ') for a in args])
  321. new_buffer(completion.module_path, cmd_args)
  322. @catch_and_print_exceptions
  323. def py_import_completions():
  324. argl = vim.eval('a:argl')
  325. try:
  326. import jedi
  327. except ImportError:
  328. print('Pyimport completion requires jedi module: https://github.com/davidhalter/jedi')
  329. comps = []
  330. else:
  331. text = 'import %s' % argl
  332. script=jedi.Script(text, 1, len(text), '')
  333. comps = ['%s%s' % (argl, c.complete) for c in script.completions()]
  334. vim.command("return '%s'" % '\n'.join(comps))
  335. @catch_and_print_exceptions
  336. def new_buffer(path, options=''):
  337. # options are what you can to edit the edit options
  338. if vim_eval('g:jedi#use_tabs_not_buffers') == '1':
  339. _tabnew(path, options)
  340. elif not vim_eval('g:jedi#use_splits_not_buffers') == '1':
  341. user_split_option = vim_eval('g:jedi#use_splits_not_buffers')
  342. split_options = {
  343. 'top': 'topleft split',
  344. 'left': 'topleft vsplit',
  345. 'right': 'botright vsplit',
  346. 'bottom': 'botright split'
  347. }
  348. if user_split_option not in split_options:
  349. print('g:jedi#use_splits_not_buffers value is not correct, valid options are: %s' % ','.join(split_options.keys()))
  350. else:
  351. vim_command(split_options[user_split_option] + " %s" % path)
  352. else:
  353. if vim_eval("!&hidden && &modified") == '1':
  354. if vim_eval("bufname('%')") is None:
  355. echo_highlight('Cannot open a new buffer, use `:set hidden` or save your buffer')
  356. return False
  357. else:
  358. vim_command('w')
  359. vim_command('edit %s %s' % (options, escape_file_path(path)))
  360. # sometimes syntax is being disabled and the filetype not set.
  361. if vim_eval('!exists("g:syntax_on")') == '1':
  362. vim_command('syntax enable')
  363. if vim_eval("&filetype != 'python'") == '1':
  364. vim_command('set filetype=python')
  365. return True
  366. @catch_and_print_exceptions
  367. def _tabnew(path, options=''):
  368. """
  369. Open a file in a new tab or switch to an existing one.
  370. :param options: `:tabnew` options, read vim help.
  371. """
  372. path = os.path.abspath(path)
  373. if vim_eval('has("gui")') == '1':
  374. vim_command('tab drop %s %s' % (options, escape_file_path(path)))
  375. return
  376. for tab_nr in range(int(vim_eval("tabpagenr('$')"))):
  377. for buf_nr in vim_eval("tabpagebuflist(%i + 1)" % tab_nr):
  378. buf_nr = int(buf_nr) - 1
  379. try:
  380. buf_path = vim.buffers[buf_nr].name
  381. except (LookupError, ValueError):
  382. # Just do good old asking for forgiveness.
  383. # don't know why this happens :-)
  384. pass
  385. else:
  386. if buf_path == path:
  387. # tab exists, just switch to that tab
  388. vim_command('tabfirst | tabnext %i' % (tab_nr + 1))
  389. break
  390. else:
  391. continue
  392. break
  393. else:
  394. # tab doesn't exist, add a new one.
  395. vim_command('tabnew %s' % escape_file_path(path))
  396. def escape_file_path(path):
  397. return path.replace(' ', r'\ ')
  398. def print_to_stdout(level, str_out):
  399. print(str_out)
  400. if not hasattr(jedi, '__version__') or jedi.__version__ < (0, 7, 0):
  401. echo_highlight('Please update your Jedi version, it is to old.')