ptutils.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """prompt-toolkit utilities
  2. Everything in this module is a private API,
  3. not to be used outside IPython.
  4. """
  5. # Copyright (c) IPython Development Team.
  6. # Distributed under the terms of the Modified BSD License.
  7. import unicodedata
  8. from wcwidth import wcwidth
  9. from IPython.utils.py3compat import PY3
  10. from IPython.core.completer import IPCompleter
  11. from prompt_toolkit.completion import Completer, Completion
  12. from prompt_toolkit.layout.lexers import Lexer
  13. from prompt_toolkit.layout.lexers import PygmentsLexer
  14. import pygments.lexers as pygments_lexers
  15. class IPythonPTCompleter(Completer):
  16. """Adaptor to provide IPython completions to prompt_toolkit"""
  17. def __init__(self, ipy_completer=None, shell=None):
  18. if shell is None and ipy_completer is None:
  19. raise TypeError("Please pass shell=an InteractiveShell instance.")
  20. self._ipy_completer = ipy_completer
  21. self.shell = shell
  22. @property
  23. def ipy_completer(self):
  24. if self._ipy_completer:
  25. return self._ipy_completer
  26. else:
  27. return self.shell.Completer
  28. def get_completions(self, document, complete_event):
  29. if not document.current_line.strip():
  30. return
  31. used, matches = self.ipy_completer.complete(
  32. line_buffer=document.current_line,
  33. cursor_pos=document.cursor_position_col
  34. )
  35. start_pos = -len(used)
  36. for m in matches:
  37. if not m:
  38. # Guard against completion machinery giving us an empty string.
  39. continue
  40. m = unicodedata.normalize('NFC', m)
  41. # When the first character of the completion has a zero length,
  42. # then it's probably a decomposed unicode character. E.g. caused by
  43. # the "\dot" completion. Try to compose again with the previous
  44. # character.
  45. if wcwidth(m[0]) == 0:
  46. if document.cursor_position + start_pos > 0:
  47. char_before = document.text[document.cursor_position + start_pos - 1]
  48. m = unicodedata.normalize('NFC', char_before + m)
  49. # Yield the modified completion instead, if this worked.
  50. if wcwidth(m[0:1]) == 1:
  51. yield Completion(m, start_position=start_pos - 1)
  52. continue
  53. # TODO: Use Jedi to determine meta_text
  54. # (Jedi currently has a bug that results in incorrect information.)
  55. # meta_text = ''
  56. # yield Completion(m, start_position=start_pos,
  57. # display_meta=meta_text)
  58. yield Completion(m, start_position=start_pos)
  59. class IPythonPTLexer(Lexer):
  60. """
  61. Wrapper around PythonLexer and BashLexer.
  62. """
  63. def __init__(self):
  64. l = pygments_lexers
  65. self.python_lexer = PygmentsLexer(l.Python3Lexer if PY3 else l.PythonLexer)
  66. self.shell_lexer = PygmentsLexer(l.BashLexer)
  67. self.magic_lexers = {
  68. 'HTML': PygmentsLexer(l.HtmlLexer),
  69. 'html': PygmentsLexer(l.HtmlLexer),
  70. 'javascript': PygmentsLexer(l.JavascriptLexer),
  71. 'js': PygmentsLexer(l.JavascriptLexer),
  72. 'perl': PygmentsLexer(l.PerlLexer),
  73. 'ruby': PygmentsLexer(l.RubyLexer),
  74. 'latex': PygmentsLexer(l.TexLexer),
  75. }
  76. def lex_document(self, cli, document):
  77. text = document.text.lstrip()
  78. lexer = self.python_lexer
  79. if text.startswith('!') or text.startswith('%%bash'):
  80. lexer = self.shell_lexer
  81. elif text.startswith('%%'):
  82. for magic, l in self.magic_lexers.items():
  83. if text.startswith('%%' + magic):
  84. lexer = l
  85. break
  86. return lexer.lex_document(cli, document)