MCPcopy Index your code
hub / github.com/ipython/ipython / IPythonPTCompleter

Class IPythonPTCompleter

IPython/terminal/ptutils.py:98–194  ·  view source on GitHub ↗

Adaptor to provide IPython completions to prompt_toolkit

Source from the content-addressed store, hash-verified

96
97
98class IPythonPTCompleter(Completer):
99 """Adaptor to provide IPython completions to prompt_toolkit"""
100 def __init__(self, ipy_completer=None, shell=None):
101 if shell is None and ipy_completer is None:
102 raise TypeError("Please pass shell=an InteractiveShell instance.")
103 self._ipy_completer = ipy_completer
104 self.shell = shell
105
106 @property
107 def ipy_completer(self):
108 if self._ipy_completer:
109 return self._ipy_completer
110 else:
111 return self.shell.Completer
112
113 def get_completions(self, document, complete_event):
114 if not document.current_line.strip():
115 return
116 # Some bits of our completion system may print stuff (e.g. if a module
117 # is imported). This context manager ensures that doesn't interfere with
118 # the prompt.
119
120 with patch_stdout(), provisionalcompleter():
121 body = document.text
122 cursor_row = document.cursor_position_row
123 cursor_col = document.cursor_position_col
124 cursor_position = document.cursor_position
125 offset = cursor_to_position(body, cursor_row, cursor_col)
126 try:
127 yield from self._get_completions(body, offset, cursor_position, self.ipy_completer)
128 except Exception as e:
129 try:
130 exc_type, exc_value, exc_tb = sys.exc_info()
131 traceback.print_exception(exc_type, exc_value, exc_tb)
132 except AttributeError:
133 print('Unrecoverable Error in completions')
134
135 def _get_completions(self, body, offset, cursor_position, ipyc):
136 """
137 Private equivalent of get_completions() use only for unit_testing.
138 """
139 debug = getattr(ipyc, 'debug', False)
140 completions = _deduplicate_completions(
141 body, ipyc.completions(body, offset))
142 for c in completions:
143 if not c.text:
144 # Guard against completion machinery giving us an empty string.
145 continue
146 text = unicodedata.normalize('NFC', c.text)
147 # When the first character of the completion has a zero length,
148 # then it's probably a decomposed unicode character. E.g. caused by
149 # the "\dot" completion. Try to compose again with the previous
150 # character.
151 if wcwidth(text[0]) == 0:
152 if cursor_position + c.start > 0:
153 char_before = body[c.start - 1]
154 fixed_text = unicodedata.normalize(
155 'NFC', char_before + text)

Callers 2

pt_initMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…