You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.9 KiB

4 years ago
  1. """
  2. Utilities for end-users.
  3. """
  4. from __future__ import absolute_import
  5. import __main__
  6. from collections import namedtuple
  7. import logging
  8. import traceback
  9. import re
  10. import os
  11. import sys
  12. from parso import split_lines
  13. from jedi import Interpreter
  14. from jedi.api.helpers import get_on_completion_name
  15. READLINE_DEBUG = False
  16. def setup_readline(namespace_module=__main__):
  17. """
  18. Install Jedi completer to :mod:`readline`.
  19. This function setups :mod:`readline` to use Jedi in Python interactive
  20. shell. If you want to use a custom ``PYTHONSTARTUP`` file (typically
  21. ``$HOME/.pythonrc.py``), you can add this piece of code::
  22. try:
  23. from jedi.utils import setup_readline
  24. setup_readline()
  25. except ImportError:
  26. # Fallback to the stdlib readline completer if it is installed.
  27. # Taken from http://docs.python.org/2/library/rlcompleter.html
  28. print("Jedi is not installed, falling back to readline")
  29. try:
  30. import readline
  31. import rlcompleter
  32. readline.parse_and_bind("tab: complete")
  33. except ImportError:
  34. print("Readline is not installed either. No tab completion is enabled.")
  35. This will fallback to the readline completer if Jedi is not installed.
  36. The readline completer will only complete names in the global namespace,
  37. so for example::
  38. ran<TAB>
  39. will complete to ``range``
  40. with both Jedi and readline, but::
  41. range(10).cou<TAB>
  42. will show complete to ``range(10).count`` only with Jedi.
  43. You'll also need to add ``export PYTHONSTARTUP=$HOME/.pythonrc.py`` to
  44. your shell profile (usually ``.bash_profile`` or ``.profile`` if you use
  45. bash).
  46. """
  47. if READLINE_DEBUG:
  48. logging.basicConfig(
  49. filename='/tmp/jedi.log',
  50. filemode='a',
  51. level=logging.DEBUG
  52. )
  53. class JediRL(object):
  54. def complete(self, text, state):
  55. """
  56. This complete stuff is pretty weird, a generator would make
  57. a lot more sense, but probably due to backwards compatibility
  58. this is still the way how it works.
  59. The only important part is stuff in the ``state == 0`` flow,
  60. everything else has been copied from the ``rlcompleter`` std.
  61. library module.
  62. """
  63. if state == 0:
  64. sys.path.insert(0, os.getcwd())
  65. # Calling python doesn't have a path, so add to sys.path.
  66. try:
  67. logging.debug("Start REPL completion: " + repr(text))
  68. interpreter = Interpreter(text, [namespace_module.__dict__])
  69. lines = split_lines(text)
  70. position = (len(lines), len(lines[-1]))
  71. name = get_on_completion_name(
  72. interpreter._module_node,
  73. lines,
  74. position
  75. )
  76. before = text[:len(text) - len(name)]
  77. completions = interpreter.completions()
  78. logging.debug("REPL completions: %s", completions)
  79. except:
  80. logging.error("REPL Completion error:\n" + traceback.format_exc())
  81. raise
  82. finally:
  83. sys.path.pop(0)
  84. self.matches = [before + c.name_with_symbols for c in completions]
  85. try:
  86. return self.matches[state]
  87. except IndexError:
  88. return None
  89. try:
  90. # Need to import this one as well to make sure it's executed before
  91. # this code. This didn't use to be an issue until 3.3. Starting with
  92. # 3.4 this is different, it always overwrites the completer if it's not
  93. # already imported here.
  94. import rlcompleter # noqa: F401
  95. import readline
  96. except ImportError:
  97. print("Jedi: Module readline not available.")
  98. else:
  99. readline.set_completer(JediRL().complete)
  100. readline.parse_and_bind("tab: complete")
  101. # jedi itself does the case matching
  102. readline.parse_and_bind("set completion-ignore-case on")
  103. # because it's easier to hit the tab just once
  104. readline.parse_and_bind("set show-all-if-unmodified")
  105. readline.parse_and_bind("set show-all-if-ambiguous on")
  106. # don't repeat all the things written in the readline all the time
  107. readline.parse_and_bind("set completion-prefix-display-length 2")
  108. # No delimiters, Jedi handles that.
  109. readline.set_completer_delims('')
  110. def version_info():
  111. """
  112. Returns a namedtuple of Jedi's version, similar to Python's
  113. ``sys.version_info``.
  114. """
  115. Version = namedtuple('Version', 'major, minor, micro')
  116. from jedi import __version__
  117. tupl = re.findall(r'[a-z]+|\d+', __version__)
  118. return Version(*[x if i == 3 else int(x) for i, x in enumerate(tupl)])