PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/vim_runtime/sources_non_forked/jedi-vim/pythonx/jedi_vim.py

https://gitlab.com/lokiexinferis/vim-configs
Python | 1159 lines | 955 code | 106 blank | 98 comment | 146 complexity | ceb541bd30a7c1e36b936c542ea2e8ed MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """
  3. The Python parts of the Jedi library for VIM. It is mostly about communicating
  4. with VIM.
  5. """
  6. import traceback # for exception output
  7. import re
  8. import os
  9. import sys
  10. from shlex import split as shsplit
  11. from contextlib import contextmanager
  12. try:
  13. from itertools import zip_longest
  14. except ImportError:
  15. from itertools import izip_longest as zip_longest # Python 2
  16. import vim
  17. is_py3 = sys.version_info[0] >= 3
  18. if is_py3:
  19. ELLIPSIS = "…"
  20. unicode = str
  21. else:
  22. ELLIPSIS = u"…"
  23. try:
  24. # Somehow sys.prefix is set in combination with VIM and virtualenvs.
  25. # However the sys path is not affected. Just reset it to the normal value.
  26. sys.prefix = sys.base_prefix
  27. sys.exec_prefix = sys.base_exec_prefix
  28. except AttributeError:
  29. # If we're not in a virtualenv we don't care. Everything is fine.
  30. pass
  31. class PythonToVimStr(unicode):
  32. """ Vim has a different string implementation of single quotes """
  33. __slots__ = []
  34. def __new__(cls, obj, encoding='UTF-8'):
  35. if not (is_py3 or isinstance(obj, unicode)):
  36. obj = unicode.__new__(cls, obj, encoding)
  37. # Vim cannot deal with zero bytes:
  38. obj = obj.replace('\0', '\\0')
  39. return unicode.__new__(cls, obj)
  40. def __repr__(self):
  41. # this is totally stupid and makes no sense but vim/python unicode
  42. # support is pretty bad. don't ask how I came up with this... It just
  43. # works...
  44. # It seems to be related to that bug: http://bugs.python.org/issue5876
  45. if unicode is str:
  46. s = self
  47. else:
  48. s = self.encode('UTF-8')
  49. return '"%s"' % s.replace('\\', '\\\\').replace('"', r'\"')
  50. class VimError(Exception):
  51. def __init__(self, message, throwpoint, executing):
  52. super(type(self), self).__init__(message)
  53. self.message = message
  54. self.throwpoint = throwpoint
  55. self.executing = executing
  56. def __str__(self):
  57. return "{}; created by {!r} (in {})".format(
  58. self.message, self.executing, self.throwpoint
  59. )
  60. def _catch_exception(string, is_eval):
  61. """
  62. Interface between vim and python calls back to it.
  63. Necessary, because the exact error message is not given by `vim.error`.
  64. """
  65. result = vim.eval('jedi#_vim_exceptions({0}, {1})'.format(
  66. repr(PythonToVimStr(string, 'UTF-8')), int(is_eval)))
  67. if 'exception' in result:
  68. raise VimError(result['exception'], result['throwpoint'], string)
  69. return result['result']
  70. def vim_command(string):
  71. _catch_exception(string, is_eval=False)
  72. def vim_eval(string):
  73. return _catch_exception(string, is_eval=True)
  74. def no_jedi_warning(error=None):
  75. vim.command('echohl WarningMsg')
  76. vim.command('echom "Please install Jedi if you want to use jedi-vim."')
  77. if error:
  78. vim.command('echom "The error was: {0}"'.format(error))
  79. vim.command('echohl None')
  80. def echo_highlight(msg):
  81. vim_command('echohl WarningMsg | echom "jedi-vim: {0}" | echohl None'.format(
  82. str(msg).replace('"', '\\"')))
  83. jedi_path = os.path.join(os.path.dirname(__file__), 'jedi')
  84. sys.path.insert(0, jedi_path)
  85. parso_path = os.path.join(os.path.dirname(__file__), 'parso')
  86. sys.path.insert(0, parso_path)
  87. try:
  88. import jedi
  89. except ImportError:
  90. jedi = None
  91. jedi_import_error = sys.exc_info()
  92. else:
  93. try:
  94. version = jedi.__version__
  95. except Exception as e: # e.g. AttributeError
  96. echo_highlight(
  97. "Error when loading the jedi python module ({0}). "
  98. "Please ensure that Jedi is installed correctly (see Installation "
  99. "in the README.".format(e))
  100. jedi = None
  101. else:
  102. if isinstance(version, str):
  103. # the normal use case, now.
  104. from jedi import utils
  105. version = utils.version_info()
  106. if version < (0, 7):
  107. echo_highlight('Please update your Jedi version, it is too old.')
  108. finally:
  109. sys.path.remove(jedi_path)
  110. sys.path.remove(parso_path)
  111. class VimCompat:
  112. _eval_cache = {}
  113. _func_cache = {}
  114. @classmethod
  115. def has(cls, what):
  116. try:
  117. return cls._eval_cache[what]
  118. except KeyError:
  119. ret = cls._eval_cache[what] = cls.call('has', what)
  120. return ret
  121. @classmethod
  122. def call(cls, func, *args):
  123. try:
  124. f = cls._func_cache[func]
  125. except KeyError:
  126. if IS_NVIM:
  127. f = cls._func_cache[func] = getattr(vim.funcs, func)
  128. else:
  129. f = cls._func_cache[func] = vim.Function(func)
  130. return f(*args)
  131. @classmethod
  132. def setqflist(cls, items, title, context):
  133. if cls.has('patch-7.4.2200'): # can set qf title.
  134. what = {'title': title}
  135. if cls.has('patch-8.0.0590'): # can set qf context
  136. what['context'] = {'jedi_usages': context}
  137. if cls.has('patch-8.0.0657'): # can set items via "what".
  138. what['items'] = items
  139. cls.call('setqflist', [], ' ', what)
  140. else:
  141. # Can set title (and maybe context), but needs two calls.
  142. cls.call('setqflist', items)
  143. cls.call('setqflist', items, 'a', what)
  144. else:
  145. cls.call('setqflist', items)
  146. @classmethod
  147. def setqflist_title(cls, title):
  148. if cls.has('patch-7.4.2200'):
  149. cls.call('setqflist', [], 'a', {'title': title})
  150. @classmethod
  151. def can_update_current_qflist_for_context(cls, context):
  152. if cls.has('patch-8.0.0590'): # can set qf context
  153. return cls.call('getqflist', {'context': 1})['context'] == {
  154. 'jedi_usages': context,
  155. }
  156. def catch_and_print_exceptions(func):
  157. def wrapper(*args, **kwargs):
  158. try:
  159. return func(*args, **kwargs)
  160. except (Exception, vim.error):
  161. print(traceback.format_exc())
  162. return None
  163. return wrapper
  164. def _check_jedi_availability(show_error=False):
  165. def func_receiver(func):
  166. def wrapper(*args, **kwargs):
  167. if jedi is None:
  168. if show_error:
  169. no_jedi_warning()
  170. return
  171. else:
  172. return func(*args, **kwargs)
  173. return wrapper
  174. return func_receiver
  175. current_environment = (None, None)
  176. def get_environment(use_cache=True):
  177. global current_environment
  178. vim_force_python_version = vim_eval("g:jedi#force_py_version")
  179. if use_cache and vim_force_python_version == current_environment[0]:
  180. return current_environment[1]
  181. environment = None
  182. if vim_force_python_version == "auto":
  183. environment = jedi.api.environment.get_cached_default_environment()
  184. else:
  185. force_python_version = vim_force_python_version
  186. if '0000' in force_python_version or '9999' in force_python_version:
  187. # It's probably a float that wasn't shortened.
  188. try:
  189. force_python_version = "{:.1f}".format(float(force_python_version))
  190. except ValueError:
  191. pass
  192. elif isinstance(force_python_version, float):
  193. force_python_version = "{:.1f}".format(force_python_version)
  194. try:
  195. environment = jedi.get_system_environment(force_python_version)
  196. except jedi.InvalidPythonEnvironment as exc:
  197. environment = jedi.api.environment.get_cached_default_environment()
  198. echo_highlight(
  199. "force_python_version=%s is not supported: %s - using %s." % (
  200. vim_force_python_version, str(exc), str(environment)))
  201. current_environment = (vim_force_python_version, environment)
  202. return environment
  203. def get_known_environments():
  204. """Get known Jedi environments."""
  205. envs = list(jedi.api.environment.find_virtualenvs())
  206. envs.extend(jedi.api.environment.find_system_environments())
  207. return envs
  208. @catch_and_print_exceptions
  209. def get_script(source=None, column=None):
  210. jedi.settings.additional_dynamic_modules = [
  211. b.name for b in vim.buffers if (
  212. b.name is not None and
  213. b.name.endswith('.py') and
  214. b.options['buflisted'])]
  215. if source is None:
  216. source = '\n'.join(vim.current.buffer)
  217. row = vim.current.window.cursor[0]
  218. if column is None:
  219. column = vim.current.window.cursor[1]
  220. buf_path = vim.current.buffer.name
  221. return jedi.Script(
  222. source, row, column, buf_path,
  223. encoding=vim_eval('&encoding') or 'latin1',
  224. environment=get_environment(),
  225. )
  226. @_check_jedi_availability(show_error=False)
  227. @catch_and_print_exceptions
  228. def completions():
  229. row, column = vim.current.window.cursor
  230. # Clear call signatures in the buffer so they aren't seen by the completer.
  231. # Call signatures in the command line can stay.
  232. if int(vim_eval("g:jedi#show_call_signatures")) == 1:
  233. clear_call_signatures()
  234. if vim.eval('a:findstart') == '1':
  235. count = 0
  236. for char in reversed(vim.current.line[:column]):
  237. if not re.match(r'[\w\d]', char):
  238. break
  239. count += 1
  240. vim.command('return %i' % (column - count))
  241. else:
  242. base = vim.eval('a:base')
  243. source = ''
  244. for i, line in enumerate(vim.current.buffer):
  245. # enter this path again, otherwise source would be incomplete
  246. if i == row - 1:
  247. source += line[:column] + base + line[column:]
  248. else:
  249. source += line
  250. source += '\n'
  251. # here again hacks, because jedi has a different interface than vim
  252. column += len(base)
  253. try:
  254. script = get_script(source=source, column=column)
  255. completions = script.completions()
  256. signatures = script.call_signatures()
  257. add_info = "preview" in vim.eval("&completeopt").split(",")
  258. out = []
  259. for c in completions:
  260. d = dict(word=PythonToVimStr(c.name[:len(base)] + c.complete),
  261. abbr=PythonToVimStr(c.name_with_symbols),
  262. # stuff directly behind the completion
  263. menu=PythonToVimStr(c.description),
  264. icase=1, # case insensitive
  265. dup=1 # allow duplicates (maybe later remove this)
  266. )
  267. if add_info:
  268. try:
  269. d["info"] = PythonToVimStr(c.docstring())
  270. except Exception:
  271. print("jedi-vim: error with docstring for %r: %s" % (
  272. c, traceback.format_exc()))
  273. out.append(d)
  274. strout = str(out)
  275. except Exception:
  276. # print to stdout, will be in :messages
  277. print(traceback.format_exc())
  278. strout = ''
  279. completions = []
  280. signatures = []
  281. show_call_signatures(signatures)
  282. vim.command('return ' + strout)
  283. @contextmanager
  284. def tempfile(content):
  285. # Using this instead of the tempfile module because Windows won't read
  286. # from a file not yet written to disk
  287. with open(vim_eval('tempname()'), 'w') as f:
  288. f.write(content)
  289. try:
  290. yield f
  291. finally:
  292. os.unlink(f.name)
  293. @_check_jedi_availability(show_error=True)
  294. @catch_and_print_exceptions
  295. def goto(mode="goto"):
  296. """
  297. :param str mode: "definition", "assignment", "goto"
  298. :return: list of definitions/assignments
  299. :rtype: list
  300. """
  301. script = get_script()
  302. if mode == "goto":
  303. definitions = script.goto_assignments(follow_imports=True)
  304. elif mode == "definition":
  305. definitions = script.goto_definitions()
  306. elif mode == "assignment":
  307. definitions = script.goto_assignments()
  308. elif mode == "stubs":
  309. definitions = script.goto_assignments(follow_imports=True, only_stubs=True)
  310. if not definitions:
  311. echo_highlight("Couldn't find any definitions for this.")
  312. elif len(definitions) == 1 and mode != "related_name":
  313. d = list(definitions)[0]
  314. if d.column is None:
  315. if d.is_keyword:
  316. echo_highlight("Cannot get the definition of Python keywords.")
  317. else:
  318. echo_highlight("Builtin modules cannot be displayed (%s)."
  319. % d.desc_with_module)
  320. else:
  321. using_tagstack = int(vim_eval('g:jedi#use_tag_stack')) == 1
  322. if (d.module_path or '') != vim.current.buffer.name:
  323. result = new_buffer(d.module_path,
  324. using_tagstack=using_tagstack)
  325. if not result:
  326. return []
  327. if (using_tagstack and d.module_path and
  328. os.path.exists(d.module_path)):
  329. tagname = d.name
  330. with tempfile('{0}\t{1}\t{2}'.format(
  331. tagname, d.module_path, 'call cursor({0}, {1})'.format(
  332. d.line, d.column + 1))) as f:
  333. old_tags = vim.eval('&tags')
  334. old_wildignore = vim.eval('&wildignore')
  335. try:
  336. # Clear wildignore to ensure tag file isn't ignored
  337. vim.command('set wildignore=')
  338. vim.command('let &tags = %s' %
  339. repr(PythonToVimStr(f.name)))
  340. vim.command('tjump %s' % tagname)
  341. finally:
  342. vim.command('let &tags = %s' %
  343. repr(PythonToVimStr(old_tags)))
  344. vim.command('let &wildignore = %s' %
  345. repr(PythonToVimStr(old_wildignore)))
  346. vim.current.window.cursor = d.line, d.column
  347. else:
  348. show_goto_multi_results(definitions, mode)
  349. return definitions
  350. def relpath(path):
  351. """Make path relative to cwd if it is below."""
  352. abspath = os.path.abspath(path)
  353. if abspath.startswith(os.getcwd()):
  354. return os.path.relpath(path)
  355. return path
  356. def annotate_description(d):
  357. code = d.get_line_code().strip()
  358. if d.type == 'statement':
  359. return code
  360. if d.type == 'function':
  361. if code.startswith('def'):
  362. return code
  363. typ = 'def'
  364. else:
  365. typ = d.type
  366. return '[%s] %s' % (typ, code)
  367. def show_goto_multi_results(definitions, mode):
  368. """Create (or reuse) a quickfix list for multiple definitions."""
  369. global _current_definitions
  370. lst = []
  371. (row, col) = vim.current.window.cursor
  372. current_idx = None
  373. current_def = None
  374. for d in definitions:
  375. if d.column is None:
  376. # Typically a namespace, in the future maybe other things as
  377. # well.
  378. lst.append(dict(text=PythonToVimStr(d.description)))
  379. else:
  380. text = annotate_description(d)
  381. lst.append(dict(filename=PythonToVimStr(relpath(d.module_path)),
  382. lnum=d.line, col=d.column + 1,
  383. text=PythonToVimStr(text)))
  384. # Select current/nearest entry via :cc later.
  385. if d.line == row and d.column <= col:
  386. if (current_idx is None
  387. or (abs(lst[current_idx]["col"] - col)
  388. > abs(d.column - col))):
  389. current_idx = len(lst)
  390. current_def = d
  391. # Build qflist title.
  392. qf_title = mode
  393. if current_def is not None:
  394. if current_def.full_name:
  395. qf_title += ": " + current_def.full_name
  396. else:
  397. qf_title += ": " + str(current_def)
  398. select_entry = current_idx
  399. else:
  400. select_entry = 0
  401. qf_context = id(definitions)
  402. if (_current_definitions
  403. and VimCompat.can_update_current_qflist_for_context(qf_context)):
  404. # Same list, only adjust title/selected entry.
  405. VimCompat.setqflist_title(qf_title)
  406. vim_command('%dcc' % select_entry)
  407. else:
  408. VimCompat.setqflist(lst, title=qf_title, context=qf_context)
  409. for_usages = mode == "usages"
  410. vim_eval('jedi#add_goto_window(%d, %d)' % (for_usages, len(lst)))
  411. vim_command('%d' % select_entry)
  412. def _same_definitions(a, b):
  413. """Compare without _inference_state.
  414. Ref: https://github.com/davidhalter/jedi-vim/issues/952)
  415. """
  416. return all(
  417. x._name.start_pos == y._name.start_pos
  418. and x.module_path == y.module_path
  419. and x.name == y.name
  420. for x, y in zip(a, b)
  421. )
  422. @catch_and_print_exceptions
  423. def usages(visuals=True):
  424. script = get_script()
  425. definitions = script.usages()
  426. if not definitions:
  427. echo_highlight("No usages found here.")
  428. return definitions
  429. if visuals:
  430. global _current_definitions
  431. if _current_definitions:
  432. if _same_definitions(_current_definitions, definitions):
  433. definitions = _current_definitions
  434. else:
  435. clear_usages()
  436. assert not _current_definitions
  437. show_goto_multi_results(definitions, "usages")
  438. if not _current_definitions:
  439. _current_definitions = definitions
  440. highlight_usages()
  441. else:
  442. assert definitions is _current_definitions # updated above
  443. return definitions
  444. _current_definitions = None
  445. """Current definitions to use for highlighting."""
  446. _pending_definitions = {}
  447. """Pending definitions for unloaded buffers."""
  448. _placed_definitions_in_buffers = set()
  449. """Set of buffers for faster cleanup."""
  450. IS_NVIM = hasattr(vim, 'from_nvim')
  451. if IS_NVIM:
  452. vim_prop_add = None
  453. else:
  454. vim_prop_type_added = False
  455. try:
  456. vim_prop_add = vim.Function("prop_add")
  457. except ValueError:
  458. vim_prop_add = None
  459. else:
  460. vim_prop_remove = vim.Function("prop_remove")
  461. def clear_usages():
  462. """Clear existing highlights."""
  463. global _current_definitions
  464. if _current_definitions is None:
  465. return
  466. _current_definitions = None
  467. if IS_NVIM:
  468. for buf in _placed_definitions_in_buffers:
  469. src_ids = buf.vars.get('_jedi_usages_src_ids')
  470. if src_ids is not None:
  471. for src_id in src_ids:
  472. buf.clear_highlight(src_id)
  473. elif vim_prop_add:
  474. for buf in _placed_definitions_in_buffers:
  475. vim_prop_remove({
  476. 'type': 'jediUsage',
  477. 'all': 1,
  478. 'bufnr': buf.number,
  479. })
  480. else:
  481. # Unset current window only.
  482. assert _current_definitions is None
  483. highlight_usages_for_vim_win()
  484. _placed_definitions_in_buffers.clear()
  485. def highlight_usages():
  486. """Set definitions to be highlighted.
  487. With Neovim it will use the nvim_buf_add_highlight API to highlight all
  488. buffers already.
  489. With Vim without support for text-properties only the current window is
  490. highlighted via matchaddpos, and autocommands are setup to highlight other
  491. windows on demand. Otherwise Vim's text-properties are used.
  492. """
  493. global _current_definitions, _pending_definitions
  494. definitions = _current_definitions
  495. _pending_definitions = {}
  496. if IS_NVIM or vim_prop_add:
  497. bufs = {x.name: x for x in vim.buffers}
  498. defs_per_buf = {}
  499. for definition in definitions:
  500. try:
  501. buf = bufs[definition.module_path]
  502. except KeyError:
  503. continue
  504. defs_per_buf.setdefault(buf, []).append(definition)
  505. if IS_NVIM:
  506. # We need to remember highlight ids with Neovim's API.
  507. buf_src_ids = {}
  508. for buf, definitions in defs_per_buf.items():
  509. buf_src_ids[buf] = []
  510. for definition in definitions:
  511. src_id = _add_highlight_definition(buf, definition)
  512. buf_src_ids[buf].append(src_id)
  513. for buf, src_ids in buf_src_ids.items():
  514. buf.vars['_jedi_usages_src_ids'] = src_ids
  515. else:
  516. for buf, definitions in defs_per_buf.items():
  517. try:
  518. for definition in definitions:
  519. _add_highlight_definition(buf, definition)
  520. except vim.error as exc:
  521. if exc.args[0].startswith('Vim:E275:'):
  522. # "Cannot add text property to unloaded buffer"
  523. _pending_definitions.setdefault(buf.name, []).extend(
  524. definitions)
  525. else:
  526. highlight_usages_for_vim_win()
  527. def _handle_pending_usages_for_buf():
  528. """Add (pending) highlights for the current buffer (Vim with textprops)."""
  529. buf = vim.current.buffer
  530. bufname = buf.name
  531. try:
  532. buf_defs = _pending_definitions[bufname]
  533. except KeyError:
  534. return
  535. for definition in buf_defs:
  536. _add_highlight_definition(buf, definition)
  537. del _pending_definitions[bufname]
  538. def _add_highlight_definition(buf, definition):
  539. lnum = definition.line
  540. start_col = definition.column
  541. # Skip highlighting of module definitions that point to the start
  542. # of the file.
  543. if definition.type == 'module' and lnum == 1 and start_col == 0:
  544. return
  545. _placed_definitions_in_buffers.add(buf)
  546. # TODO: validate that definition.name is at this position?
  547. # Would skip the module definitions from above already.
  548. length = len(definition.name)
  549. if vim_prop_add:
  550. # XXX: needs jediUsage highlight (via after/syntax/python.vim).
  551. global vim_prop_type_added
  552. if not vim_prop_type_added:
  553. vim.eval("prop_type_add('jediUsage', {'highlight': 'jediUsage'})")
  554. vim_prop_type_added = True
  555. vim_prop_add(lnum, start_col+1, {
  556. 'type': 'jediUsage',
  557. 'bufnr': buf.number,
  558. 'length': length,
  559. })
  560. return
  561. assert IS_NVIM
  562. end_col = definition.column + length
  563. src_id = buf.add_highlight('jediUsage', lnum-1, start_col, end_col,
  564. src_id=0)
  565. return src_id
  566. def highlight_usages_for_vim_win():
  567. """Highlight usages in the current window.
  568. It stores the matchids in a window-local variable.
  569. (matchaddpos() only works for the current window.)
  570. """
  571. global _current_definitions
  572. definitions = _current_definitions
  573. win = vim.current.window
  574. cur_matchids = win.vars.get('_jedi_usages_vim_matchids')
  575. if cur_matchids:
  576. if cur_matchids[0] == vim.current.buffer.number:
  577. return
  578. # Need to clear non-matching highlights.
  579. for matchid in cur_matchids[1:]:
  580. expr = 'matchdelete(%d)' % int(matchid)
  581. vim.eval(expr)
  582. matchids = []
  583. if definitions:
  584. buffer_path = vim.current.buffer.name
  585. for definition in definitions:
  586. if (definition.module_path or '') == buffer_path:
  587. positions = [
  588. [definition.line,
  589. definition.column + 1,
  590. len(definition.name)]
  591. ]
  592. expr = "matchaddpos('jediUsage', %s)" % repr(positions)
  593. matchids.append(int(vim_eval(expr)))
  594. if matchids:
  595. vim.current.window.vars['_jedi_usages_vim_matchids'] = [
  596. vim.current.buffer.number] + matchids
  597. elif cur_matchids is not None:
  598. # Always set it (uses an empty list for "unset", which is not possible
  599. # using del).
  600. vim.current.window.vars['_jedi_usages_vim_matchids'] = []
  601. # Remember if clearing is needed for later buffer autocommands.
  602. vim.current.buffer.vars['_jedi_usages_needs_clear'] = bool(matchids)
  603. @_check_jedi_availability(show_error=True)
  604. @catch_and_print_exceptions
  605. def show_documentation():
  606. script = get_script()
  607. try:
  608. definitions = script.goto_definitions()
  609. except Exception:
  610. # print to stdout, will be in :messages
  611. definitions = []
  612. print("Exception, this shouldn't happen.")
  613. print(traceback.format_exc())
  614. if not definitions:
  615. echo_highlight('No documentation found for that.')
  616. vim.command('return')
  617. return
  618. docs = []
  619. for d in definitions:
  620. doc = d.docstring()
  621. if doc:
  622. title = 'Docstring for %s' % d.desc_with_module
  623. underline = '=' * len(title)
  624. docs.append('%s\n%s\n%s' % (title, underline, doc))
  625. else:
  626. docs.append('|No Docstring for %s|' % d)
  627. text = ('\n' + '-' * 79 + '\n').join(docs)
  628. vim.command('let l:doc = %s' % repr(PythonToVimStr(text)))
  629. vim.command('let l:doc_lines = %s' % len(text.split('\n')))
  630. return True
  631. @catch_and_print_exceptions
  632. def clear_call_signatures():
  633. # Check if using command line call signatures
  634. if int(vim_eval("g:jedi#show_call_signatures")) == 2:
  635. vim_command('echo ""')
  636. return
  637. cursor = vim.current.window.cursor
  638. e = vim_eval('g:jedi#call_signature_escape')
  639. # We need two turns here to search and replace certain lines:
  640. # 1. Search for a line with a call signature and save the appended
  641. # characters
  642. # 2. Actually replace the line and redo the status quo.
  643. py_regex = r'%sjedi=([0-9]+), (.*?)%s.*?%sjedi%s'.replace(
  644. '%s', re.escape(e))
  645. for i, line in enumerate(vim.current.buffer):
  646. match = re.search(py_regex, line)
  647. if match is not None:
  648. # Some signs were added to minimize syntax changes due to call
  649. # signatures. We have to remove them again. The number of them is
  650. # specified in `match.group(1)`.
  651. after = line[match.end() + int(match.group(1)):]
  652. line = line[:match.start()] + match.group(2) + after
  653. vim.current.buffer[i] = line
  654. vim.current.window.cursor = cursor
  655. @_check_jedi_availability(show_error=False)
  656. @catch_and_print_exceptions
  657. def show_call_signatures(signatures=()):
  658. if int(vim_eval("has('conceal') && g:jedi#show_call_signatures")) == 0:
  659. return
  660. # We need to clear the signatures before we calculate them again. The
  661. # reason for this is that call signatures are unfortunately written to the
  662. # buffer.
  663. clear_call_signatures()
  664. if signatures == ():
  665. signatures = get_script().call_signatures()
  666. if not signatures:
  667. return
  668. if int(vim_eval("g:jedi#show_call_signatures")) == 2:
  669. return cmdline_call_signatures(signatures)
  670. seen_sigs = []
  671. for i, signature in enumerate(signatures):
  672. line, column = signature.bracket_start
  673. # signatures are listed above each other
  674. line_to_replace = line - i - 1
  675. # because there's a space before the bracket
  676. insert_column = column - 1
  677. if insert_column < 0 or line_to_replace <= 0:
  678. # Edge cases, when the call signature has no space on the screen.
  679. break
  680. # TODO check if completion menu is above or below
  681. line = vim_eval("getline(%s)" % line_to_replace)
  682. # Descriptions are usually looking like `param name`, remove the param.
  683. params = [p.description.replace('\n', '').replace('param ', '', 1)
  684. for p in signature.params]
  685. try:
  686. # *_*PLACEHOLDER*_* makes something fat. See after/syntax file.
  687. params[signature.index] = '*_*%s*_*' % params[signature.index]
  688. except (IndexError, TypeError):
  689. pass
  690. # Skip duplicates.
  691. if params in seen_sigs:
  692. continue
  693. seen_sigs.append(params)
  694. # This stuff is reaaaaally a hack! I cannot stress enough, that
  695. # this is a stupid solution. But there is really no other yet.
  696. # There is no possibility in VIM to draw on the screen, but there
  697. # will be one (see :help todo Patch to access screen under Python.
  698. # (Marko Mahni, 2010 Jul 18))
  699. text = " (%s) " % ', '.join(params)
  700. text = ' ' * (insert_column - len(line)) + text
  701. end_column = insert_column + len(text) - 2 # -2 due to bold symbols
  702. # Need to decode it with utf8, because vim returns always a python 2
  703. # string even if it is unicode.
  704. e = vim_eval('g:jedi#call_signature_escape')
  705. if hasattr(e, 'decode'):
  706. e = e.decode('UTF-8')
  707. # replace line before with cursor
  708. regex = "xjedi=%sx%sxjedix".replace('x', e)
  709. prefix, replace = line[:insert_column], line[insert_column:end_column]
  710. # Check the replace stuff for strings, to append them
  711. # (don't want to break the syntax)
  712. regex_quotes = r'''\\*["']+'''
  713. # `add` are all the quotation marks.
  714. # join them with a space to avoid producing '''
  715. add = ' '.join(re.findall(regex_quotes, replace))
  716. # search backwards
  717. if add and replace[0] in ['"', "'"]:
  718. a = re.search(regex_quotes + '$', prefix)
  719. add = ('' if a is None else a.group(0)) + add
  720. tup = '%s, %s' % (len(add), replace)
  721. repl = prefix + (regex % (tup, text)) + add + line[end_column:]
  722. vim_eval('setline(%s, %s)' % (line_to_replace, repr(PythonToVimStr(repl))))
  723. @catch_and_print_exceptions
  724. def cmdline_call_signatures(signatures):
  725. def get_params(s):
  726. return [p.description.replace('\n', '').replace('param ', '', 1) for p in s.params]
  727. def escape(string):
  728. return string.replace('"', '\\"').replace(r'\n', r'\\n')
  729. def join():
  730. return ', '.join(filter(None, (left, center, right)))
  731. def too_long():
  732. return len(join()) > max_msg_len
  733. if len(signatures) > 1:
  734. params = zip_longest(*map(get_params, signatures), fillvalue='_')
  735. params = ['(' + ', '.join(p) + ')' for p in params]
  736. else:
  737. params = get_params(signatures[0])
  738. index = next(iter(s.index for s in signatures if s.index is not None), None)
  739. # Allow 12 characters for showcmd plus 18 for ruler - setting
  740. # noruler/noshowcmd here causes incorrect undo history
  741. max_msg_len = int(vim_eval('&columns')) - 12
  742. if int(vim_eval('&ruler')):
  743. max_msg_len -= 18
  744. max_msg_len -= len(signatures[0].name) + 2 # call name + parentheses
  745. if max_msg_len < (1 if params else 0):
  746. return
  747. elif index is None:
  748. text = escape(', '.join(params))
  749. if params and len(text) > max_msg_len:
  750. text = ELLIPSIS
  751. elif max_msg_len < len(ELLIPSIS):
  752. return
  753. else:
  754. left = escape(', '.join(params[:index]))
  755. center = escape(params[index])
  756. right = escape(', '.join(params[index + 1:]))
  757. while too_long():
  758. if left and left != ELLIPSIS:
  759. left = ELLIPSIS
  760. continue
  761. if right and right != ELLIPSIS:
  762. right = ELLIPSIS
  763. continue
  764. if (left or right) and center != ELLIPSIS:
  765. left = right = None
  766. center = ELLIPSIS
  767. continue
  768. if too_long():
  769. # Should never reach here
  770. return
  771. max_num_spaces = max_msg_len
  772. if index is not None:
  773. max_num_spaces -= len(join())
  774. _, column = signatures[0].bracket_start
  775. spaces = min(int(vim_eval('g:jedi#first_col +'
  776. 'wincol() - col(".")')) +
  777. column - len(signatures[0].name),
  778. max_num_spaces) * ' '
  779. if index is not None:
  780. vim_command(' echon "%s" | '
  781. 'echohl Function | echon "%s" | '
  782. 'echohl None | echon "(" | '
  783. 'echohl jediFunction | echon "%s" | '
  784. 'echohl jediFat | echon "%s" | '
  785. 'echohl jediFunction | echon "%s" | '
  786. 'echohl None | echon ")"'
  787. % (spaces, signatures[0].name,
  788. left + ', ' if left else '',
  789. center, ', ' + right if right else ''))
  790. else:
  791. vim_command(' echon "%s" | '
  792. 'echohl Function | echon "%s" | '
  793. 'echohl None | echon "(%s)"'
  794. % (spaces, signatures[0].name, text))
  795. @_check_jedi_availability(show_error=True)
  796. @catch_and_print_exceptions
  797. def rename():
  798. if not int(vim.eval('a:0')):
  799. # Need to save the cursor position before insert mode
  800. cursor = vim.current.window.cursor
  801. changenr = vim.eval('changenr()') # track undo tree
  802. vim_command('augroup jedi_rename')
  803. vim_command('autocmd InsertLeave <buffer> call jedi#rename'
  804. '({}, {}, {})'.format(cursor[0], cursor[1], changenr))
  805. vim_command('augroup END')
  806. vim_command("let s:jedi_replace_orig = expand('<cword>')")
  807. line = vim_eval('getline(".")')
  808. vim_command('normal! diw')
  809. if re.match(r'\w+$', line[cursor[1]:]):
  810. # In case the deleted word is at the end of the line we need to
  811. # move the cursor to the end.
  812. vim_command('startinsert!')
  813. else:
  814. vim_command('startinsert')
  815. else:
  816. # Remove autocommand.
  817. vim_command('autocmd! jedi_rename InsertLeave')
  818. args = vim.eval('a:000')
  819. cursor = tuple(int(x) for x in args[:2])
  820. changenr = args[2]
  821. # Get replacement, if there is something on the cursor.
  822. # This won't be the case when the user ends insert mode right away,
  823. # and `<cword>` would pick up the nearest word instead.
  824. if vim_eval('getline(".")[getpos(".")[2]-1]') != ' ':
  825. replace = vim_eval("expand('<cword>')")
  826. else:
  827. replace = None
  828. vim_command('undo {}'.format(changenr))
  829. vim.current.window.cursor = cursor
  830. if replace:
  831. return do_rename(replace)
  832. def rename_visual():
  833. replace = vim.eval('input("Rename to: ")')
  834. orig = vim.eval('getline(".")[(getpos("\'<")[2]-1):getpos("\'>")[2]]')
  835. do_rename(replace, orig)
  836. def do_rename(replace, orig=None):
  837. if not len(replace):
  838. echo_highlight('No rename possible without name.')
  839. return
  840. if orig is None:
  841. orig = vim_eval('s:jedi_replace_orig')
  842. # Save original window / tab.
  843. saved_tab = int(vim_eval('tabpagenr()'))
  844. saved_win = int(vim_eval('winnr()'))
  845. temp_rename = usages(visuals=False)
  846. # Sort the whole thing reverse (positions at the end of the line
  847. # must be first, because they move the stuff before the position).
  848. temp_rename = sorted(temp_rename, reverse=True,
  849. key=lambda x: (x.module_path, x.line, x.column))
  850. buffers = set()
  851. for r in temp_rename:
  852. if r.in_builtin_module():
  853. continue
  854. if os.path.abspath(vim.current.buffer.name) != r.module_path:
  855. assert r.module_path is not None
  856. result = new_buffer(r.module_path)
  857. if not result:
  858. echo_highlight('Failed to create buffer window for %s!' % (
  859. r.module_path))
  860. continue
  861. buffers.add(vim.current.buffer.name)
  862. # Replace original word.
  863. r_line = vim.current.buffer[r.line - 1]
  864. vim.current.buffer[r.line - 1] = (r_line[:r.column] + replace +
  865. r_line[r.column + len(orig):])
  866. # Restore previous tab and window.
  867. vim_command('tabnext {0:d}'.format(saved_tab))
  868. vim_command('{0:d}wincmd w'.format(saved_win))
  869. if len(buffers) > 1:
  870. echo_highlight('Jedi did {0:d} renames in {1:d} buffers!'.format(
  871. len(temp_rename), len(buffers)))
  872. else:
  873. echo_highlight('Jedi did {0:d} renames!'.format(len(temp_rename)))
  874. @_check_jedi_availability(show_error=True)
  875. @catch_and_print_exceptions
  876. def py_import():
  877. # args are the same as for the :edit command
  878. args = shsplit(vim.eval('a:args'))
  879. import_path = args.pop()
  880. text = 'import %s' % import_path
  881. scr = jedi.Script(text, 1, len(text), '', environment=get_environment())
  882. try:
  883. completion = scr.goto_assignments()[0]
  884. except IndexError:
  885. echo_highlight('Cannot find %s in sys.path!' % import_path)
  886. else:
  887. if completion.column is None: # Python modules always have a line number.
  888. echo_highlight('%s is a builtin module.' % import_path)
  889. else:
  890. cmd_args = ' '.join([a.replace(' ', '\\ ') for a in args])
  891. new_buffer(completion.module_path, cmd_args)
  892. @catch_and_print_exceptions
  893. def py_import_completions():
  894. argl = vim.eval('a:argl')
  895. try:
  896. import jedi
  897. except ImportError:
  898. print('Pyimport completion requires jedi module: https://github.com/davidhalter/jedi')
  899. comps = []
  900. else:
  901. text = 'import %s' % argl
  902. script = jedi.Script(text, 1, len(text), '', environment=get_environment())
  903. comps = ['%s%s' % (argl, c.complete) for c in script.completions()]
  904. vim.command("return '%s'" % '\n'.join(comps))
  905. @catch_and_print_exceptions
  906. def new_buffer(path, options='', using_tagstack=False):
  907. # options are what you can to edit the edit options
  908. if int(vim_eval('g:jedi#use_tabs_not_buffers')) == 1:
  909. _tabnew(path, options)
  910. elif not vim_eval('g:jedi#use_splits_not_buffers') in [1, '1']:
  911. user_split_option = vim_eval('g:jedi#use_splits_not_buffers')
  912. split_options = {
  913. 'top': 'topleft split',
  914. 'left': 'topleft vsplit',
  915. 'right': 'botright vsplit',
  916. 'bottom': 'botright split',
  917. 'winwidth': 'vs'
  918. }
  919. if (user_split_option == 'winwidth' and
  920. vim.current.window.width <= 2 * int(vim_eval(
  921. "&textwidth ? &textwidth : 80"))):
  922. split_options['winwidth'] = 'sp'
  923. if user_split_option not in split_options:
  924. print('Unsupported value for g:jedi#use_splits_not_buffers: {0}. '
  925. 'Valid options are: {1}.'.format(
  926. user_split_option, ', '.join(split_options.keys())))
  927. else:
  928. vim_command(split_options[user_split_option] + " %s" % escape_file_path(path))
  929. else:
  930. if int(vim_eval("!&hidden && &modified")) == 1:
  931. if not vim_eval("bufname('%')"):
  932. echo_highlight('Cannot open a new buffer, use `:set hidden` or save your buffer')
  933. return False
  934. else:
  935. vim_command('w')
  936. if using_tagstack:
  937. return True
  938. vim_command('edit %s %s' % (options, escape_file_path(path)))
  939. # sometimes syntax is being disabled and the filetype not set.
  940. if int(vim_eval('!exists("g:syntax_on")')) == 1:
  941. vim_command('syntax enable')
  942. if int(vim_eval("&filetype != 'python'")) == 1:
  943. vim_command('set filetype=python')
  944. return True
  945. @catch_and_print_exceptions
  946. def _tabnew(path, options=''):
  947. """
  948. Open a file in a new tab or switch to an existing one.
  949. :param options: `:tabnew` options, read vim help.
  950. """
  951. path = os.path.abspath(path)
  952. if int(vim_eval('has("gui")')) == 1:
  953. vim_command('tab drop %s %s' % (options, escape_file_path(path)))
  954. return
  955. for tab_nr in range(int(vim_eval("tabpagenr('$')"))):
  956. for buf_nr in vim_eval("tabpagebuflist(%i + 1)" % tab_nr):
  957. buf_nr = int(buf_nr) - 1
  958. try:
  959. buf_path = vim.buffers[buf_nr].name
  960. except (LookupError, ValueError):
  961. # Just do good old asking for forgiveness.
  962. # don't know why this happens :-)
  963. pass
  964. else:
  965. if buf_path == path:
  966. # tab exists, just switch to that tab
  967. vim_command('tabfirst | tabnext %i' % (tab_nr + 1))
  968. # Goto the buffer's window.
  969. vim_command('exec bufwinnr(%i) . " wincmd w"' % (buf_nr + 1))
  970. break
  971. else:
  972. continue
  973. break
  974. else:
  975. # tab doesn't exist, add a new one.
  976. vim_command('tabnew %s' % escape_file_path(path))
  977. def escape_file_path(path):
  978. return path.replace(' ', r'\ ')
  979. def print_to_stdout(level, str_out):
  980. print(str_out)