##// END OF EJS Templates
Lazy load unicode names
zzzz-qq -
Show More
@@ -1,2089 +1,2092 b''
1 """Completion for IPython.
1 """Completion for IPython.
2
2
3 This module started as fork of the rlcompleter module in the Python standard
3 This module started as fork of the rlcompleter module in the Python standard
4 library. The original enhancements made to rlcompleter have been sent
4 library. The original enhancements made to rlcompleter have been sent
5 upstream and were accepted as of Python 2.3,
5 upstream and were accepted as of Python 2.3,
6
6
7 This module now support a wide variety of completion mechanism both available
7 This module now support a wide variety of completion mechanism both available
8 for normal classic Python code, as well as completer for IPython specific
8 for normal classic Python code, as well as completer for IPython specific
9 Syntax like magics.
9 Syntax like magics.
10
10
11 Latex and Unicode completion
11 Latex and Unicode completion
12 ============================
12 ============================
13
13
14 IPython and compatible frontends not only can complete your code, but can help
14 IPython and compatible frontends not only can complete your code, but can help
15 you to input a wide range of characters. In particular we allow you to insert
15 you to input a wide range of characters. In particular we allow you to insert
16 a unicode character using the tab completion mechanism.
16 a unicode character using the tab completion mechanism.
17
17
18 Forward latex/unicode completion
18 Forward latex/unicode completion
19 --------------------------------
19 --------------------------------
20
20
21 Forward completion allows you to easily type a unicode character using its latex
21 Forward completion allows you to easily type a unicode character using its latex
22 name, or unicode long description. To do so type a backslash follow by the
22 name, or unicode long description. To do so type a backslash follow by the
23 relevant name and press tab:
23 relevant name and press tab:
24
24
25
25
26 Using latex completion:
26 Using latex completion:
27
27
28 .. code::
28 .. code::
29
29
30 \\alpha<tab>
30 \\alpha<tab>
31 Ξ±
31 Ξ±
32
32
33 or using unicode completion:
33 or using unicode completion:
34
34
35
35
36 .. code::
36 .. code::
37
37
38 \\greek small letter alpha<tab>
38 \\greek small letter alpha<tab>
39 Ξ±
39 Ξ±
40
40
41
41
42 Only valid Python identifiers will complete. Combining characters (like arrow or
42 Only valid Python identifiers will complete. Combining characters (like arrow or
43 dots) are also available, unlike latex they need to be put after the their
43 dots) are also available, unlike latex they need to be put after the their
44 counterpart that is to say, `F\\\\vec<tab>` is correct, not `\\\\vec<tab>F`.
44 counterpart that is to say, `F\\\\vec<tab>` is correct, not `\\\\vec<tab>F`.
45
45
46 Some browsers are known to display combining characters incorrectly.
46 Some browsers are known to display combining characters incorrectly.
47
47
48 Backward latex completion
48 Backward latex completion
49 -------------------------
49 -------------------------
50
50
51 It is sometime challenging to know how to type a character, if you are using
51 It is sometime challenging to know how to type a character, if you are using
52 IPython, or any compatible frontend you can prepend backslash to the character
52 IPython, or any compatible frontend you can prepend backslash to the character
53 and press `<tab>` to expand it to its latex form.
53 and press `<tab>` to expand it to its latex form.
54
54
55 .. code::
55 .. code::
56
56
57 \\Ξ±<tab>
57 \\Ξ±<tab>
58 \\alpha
58 \\alpha
59
59
60
60
61 Both forward and backward completions can be deactivated by setting the
61 Both forward and backward completions can be deactivated by setting the
62 ``Completer.backslash_combining_completions`` option to ``False``.
62 ``Completer.backslash_combining_completions`` option to ``False``.
63
63
64
64
65 Experimental
65 Experimental
66 ============
66 ============
67
67
68 Starting with IPython 6.0, this module can make use of the Jedi library to
68 Starting with IPython 6.0, this module can make use of the Jedi library to
69 generate completions both using static analysis of the code, and dynamically
69 generate completions both using static analysis of the code, and dynamically
70 inspecting multiple namespaces. Jedi is an autocompletion and static analysis
70 inspecting multiple namespaces. Jedi is an autocompletion and static analysis
71 for Python. The APIs attached to this new mechanism is unstable and will
71 for Python. The APIs attached to this new mechanism is unstable and will
72 raise unless use in an :any:`provisionalcompleter` context manager.
72 raise unless use in an :any:`provisionalcompleter` context manager.
73
73
74 You will find that the following are experimental:
74 You will find that the following are experimental:
75
75
76 - :any:`provisionalcompleter`
76 - :any:`provisionalcompleter`
77 - :any:`IPCompleter.completions`
77 - :any:`IPCompleter.completions`
78 - :any:`Completion`
78 - :any:`Completion`
79 - :any:`rectify_completions`
79 - :any:`rectify_completions`
80
80
81 .. note::
81 .. note::
82
82
83 better name for :any:`rectify_completions` ?
83 better name for :any:`rectify_completions` ?
84
84
85 We welcome any feedback on these new API, and we also encourage you to try this
85 We welcome any feedback on these new API, and we also encourage you to try this
86 module in debug mode (start IPython with ``--Completer.debug=True``) in order
86 module in debug mode (start IPython with ``--Completer.debug=True``) in order
87 to have extra logging information if :any:`jedi` is crashing, or if current
87 to have extra logging information if :any:`jedi` is crashing, or if current
88 IPython completer pending deprecations are returning results not yet handled
88 IPython completer pending deprecations are returning results not yet handled
89 by :any:`jedi`
89 by :any:`jedi`
90
90
91 Using Jedi for tab completion allow snippets like the following to work without
91 Using Jedi for tab completion allow snippets like the following to work without
92 having to execute any code:
92 having to execute any code:
93
93
94 >>> myvar = ['hello', 42]
94 >>> myvar = ['hello', 42]
95 ... myvar[1].bi<tab>
95 ... myvar[1].bi<tab>
96
96
97 Tab completion will be able to infer that ``myvar[1]`` is a real number without
97 Tab completion will be able to infer that ``myvar[1]`` is a real number without
98 executing any code unlike the previously available ``IPCompleter.greedy``
98 executing any code unlike the previously available ``IPCompleter.greedy``
99 option.
99 option.
100
100
101 Be sure to update :any:`jedi` to the latest stable version or to try the
101 Be sure to update :any:`jedi` to the latest stable version or to try the
102 current development version to get better completions.
102 current development version to get better completions.
103 """
103 """
104
104
105
105
106 # Copyright (c) IPython Development Team.
106 # Copyright (c) IPython Development Team.
107 # Distributed under the terms of the Modified BSD License.
107 # Distributed under the terms of the Modified BSD License.
108 #
108 #
109 # Some of this code originated from rlcompleter in the Python standard library
109 # Some of this code originated from rlcompleter in the Python standard library
110 # Copyright (C) 2001 Python Software Foundation, www.python.org
110 # Copyright (C) 2001 Python Software Foundation, www.python.org
111
111
112
112
113 import __main__
113 import __main__
114 import builtins as builtin_mod
114 import builtins as builtin_mod
115 import glob
115 import glob
116 import time
116 import time
117 import inspect
117 import inspect
118 import itertools
118 import itertools
119 import keyword
119 import keyword
120 import os
120 import os
121 import re
121 import re
122 import sys
122 import sys
123 import unicodedata
123 import unicodedata
124 import string
124 import string
125 import warnings
125 import warnings
126
126
127 from contextlib import contextmanager
127 from contextlib import contextmanager
128 from importlib import import_module
128 from importlib import import_module
129 from typing import Iterator, List, Tuple, Iterable, Union
129 from typing import Iterator, List, Tuple, Iterable, Union
130 from types import SimpleNamespace
130 from types import SimpleNamespace
131
131
132 from traitlets.config.configurable import Configurable
132 from traitlets.config.configurable import Configurable
133 from IPython.core.error import TryNext
133 from IPython.core.error import TryNext
134 from IPython.core.inputtransformer2 import ESC_MAGIC
134 from IPython.core.inputtransformer2 import ESC_MAGIC
135 from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol
135 from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol
136 from IPython.core.oinspect import InspectColors
136 from IPython.core.oinspect import InspectColors
137 from IPython.utils import generics
137 from IPython.utils import generics
138 from IPython.utils.dir2 import dir2, get_real_method
138 from IPython.utils.dir2 import dir2, get_real_method
139 from IPython.utils.process import arg_split
139 from IPython.utils.process import arg_split
140 from traitlets import Bool, Enum, observe, Int
140 from traitlets import Bool, Enum, observe, Int
141
141
142 # skip module docstests
142 # skip module docstests
143 skip_doctest = True
143 skip_doctest = True
144
144
145 try:
145 try:
146 import jedi
146 import jedi
147 jedi.settings.case_insensitive_completion = False
147 jedi.settings.case_insensitive_completion = False
148 import jedi.api.helpers
148 import jedi.api.helpers
149 import jedi.api.classes
149 import jedi.api.classes
150 JEDI_INSTALLED = True
150 JEDI_INSTALLED = True
151 except ImportError:
151 except ImportError:
152 JEDI_INSTALLED = False
152 JEDI_INSTALLED = False
153 #-----------------------------------------------------------------------------
153 #-----------------------------------------------------------------------------
154 # Globals
154 # Globals
155 #-----------------------------------------------------------------------------
155 #-----------------------------------------------------------------------------
156
156
157 # Public API
157 # Public API
158 __all__ = ['Completer','IPCompleter']
158 __all__ = ['Completer','IPCompleter']
159
159
160 if sys.platform == 'win32':
160 if sys.platform == 'win32':
161 PROTECTABLES = ' '
161 PROTECTABLES = ' '
162 else:
162 else:
163 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
163 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
164
164
165 # Protect against returning an enormous number of completions which the frontend
165 # Protect against returning an enormous number of completions which the frontend
166 # may have trouble processing.
166 # may have trouble processing.
167 MATCHES_LIMIT = 500
167 MATCHES_LIMIT = 500
168
168
169 _deprecation_readline_sentinel = object()
169 _deprecation_readline_sentinel = object()
170
170
171 names = []
172 for c in range(0,0x10FFFF + 1):
173 try:
174 names.append(unicodedata.name(chr(c)))
175 except ValueError:
176 pass
177
171
178 class ProvisionalCompleterWarning(FutureWarning):
172 class ProvisionalCompleterWarning(FutureWarning):
179 """
173 """
180 Exception raise by an experimental feature in this module.
174 Exception raise by an experimental feature in this module.
181
175
182 Wrap code in :any:`provisionalcompleter` context manager if you
176 Wrap code in :any:`provisionalcompleter` context manager if you
183 are certain you want to use an unstable feature.
177 are certain you want to use an unstable feature.
184 """
178 """
185 pass
179 pass
186
180
187 warnings.filterwarnings('error', category=ProvisionalCompleterWarning)
181 warnings.filterwarnings('error', category=ProvisionalCompleterWarning)
188
182
189 @contextmanager
183 @contextmanager
190 def provisionalcompleter(action='ignore'):
184 def provisionalcompleter(action='ignore'):
191 """
185 """
192
186
193
187
194 This contest manager has to be used in any place where unstable completer
188 This contest manager has to be used in any place where unstable completer
195 behavior and API may be called.
189 behavior and API may be called.
196
190
197 >>> with provisionalcompleter():
191 >>> with provisionalcompleter():
198 ... completer.do_experimetal_things() # works
192 ... completer.do_experimetal_things() # works
199
193
200 >>> completer.do_experimental_things() # raises.
194 >>> completer.do_experimental_things() # raises.
201
195
202 .. note:: Unstable
196 .. note:: Unstable
203
197
204 By using this context manager you agree that the API in use may change
198 By using this context manager you agree that the API in use may change
205 without warning, and that you won't complain if they do so.
199 without warning, and that you won't complain if they do so.
206
200
207 You also understand that if the API is not to you liking you should report
201 You also understand that if the API is not to you liking you should report
208 a bug to explain your use case upstream and improve the API and will loose
202 a bug to explain your use case upstream and improve the API and will loose
209 credibility if you complain after the API is make stable.
203 credibility if you complain after the API is make stable.
210
204
211 We'll be happy to get your feedback , feature request and improvement on
205 We'll be happy to get your feedback , feature request and improvement on
212 any of the unstable APIs !
206 any of the unstable APIs !
213 """
207 """
214 with warnings.catch_warnings():
208 with warnings.catch_warnings():
215 warnings.filterwarnings(action, category=ProvisionalCompleterWarning)
209 warnings.filterwarnings(action, category=ProvisionalCompleterWarning)
216 yield
210 yield
217
211
218
212
219 def has_open_quotes(s):
213 def has_open_quotes(s):
220 """Return whether a string has open quotes.
214 """Return whether a string has open quotes.
221
215
222 This simply counts whether the number of quote characters of either type in
216 This simply counts whether the number of quote characters of either type in
223 the string is odd.
217 the string is odd.
224
218
225 Returns
219 Returns
226 -------
220 -------
227 If there is an open quote, the quote character is returned. Else, return
221 If there is an open quote, the quote character is returned. Else, return
228 False.
222 False.
229 """
223 """
230 # We check " first, then ', so complex cases with nested quotes will get
224 # We check " first, then ', so complex cases with nested quotes will get
231 # the " to take precedence.
225 # the " to take precedence.
232 if s.count('"') % 2:
226 if s.count('"') % 2:
233 return '"'
227 return '"'
234 elif s.count("'") % 2:
228 elif s.count("'") % 2:
235 return "'"
229 return "'"
236 else:
230 else:
237 return False
231 return False
238
232
239
233
240 def protect_filename(s, protectables=PROTECTABLES):
234 def protect_filename(s, protectables=PROTECTABLES):
241 """Escape a string to protect certain characters."""
235 """Escape a string to protect certain characters."""
242 if set(s) & set(protectables):
236 if set(s) & set(protectables):
243 if sys.platform == "win32":
237 if sys.platform == "win32":
244 return '"' + s + '"'
238 return '"' + s + '"'
245 else:
239 else:
246 return "".join(("\\" + c if c in protectables else c) for c in s)
240 return "".join(("\\" + c if c in protectables else c) for c in s)
247 else:
241 else:
248 return s
242 return s
249
243
250
244
251 def expand_user(path:str) -> Tuple[str, bool, str]:
245 def expand_user(path:str) -> Tuple[str, bool, str]:
252 """Expand ``~``-style usernames in strings.
246 """Expand ``~``-style usernames in strings.
253
247
254 This is similar to :func:`os.path.expanduser`, but it computes and returns
248 This is similar to :func:`os.path.expanduser`, but it computes and returns
255 extra information that will be useful if the input was being used in
249 extra information that will be useful if the input was being used in
256 computing completions, and you wish to return the completions with the
250 computing completions, and you wish to return the completions with the
257 original '~' instead of its expanded value.
251 original '~' instead of its expanded value.
258
252
259 Parameters
253 Parameters
260 ----------
254 ----------
261 path : str
255 path : str
262 String to be expanded. If no ~ is present, the output is the same as the
256 String to be expanded. If no ~ is present, the output is the same as the
263 input.
257 input.
264
258
265 Returns
259 Returns
266 -------
260 -------
267 newpath : str
261 newpath : str
268 Result of ~ expansion in the input path.
262 Result of ~ expansion in the input path.
269 tilde_expand : bool
263 tilde_expand : bool
270 Whether any expansion was performed or not.
264 Whether any expansion was performed or not.
271 tilde_val : str
265 tilde_val : str
272 The value that ~ was replaced with.
266 The value that ~ was replaced with.
273 """
267 """
274 # Default values
268 # Default values
275 tilde_expand = False
269 tilde_expand = False
276 tilde_val = ''
270 tilde_val = ''
277 newpath = path
271 newpath = path
278
272
279 if path.startswith('~'):
273 if path.startswith('~'):
280 tilde_expand = True
274 tilde_expand = True
281 rest = len(path)-1
275 rest = len(path)-1
282 newpath = os.path.expanduser(path)
276 newpath = os.path.expanduser(path)
283 if rest:
277 if rest:
284 tilde_val = newpath[:-rest]
278 tilde_val = newpath[:-rest]
285 else:
279 else:
286 tilde_val = newpath
280 tilde_val = newpath
287
281
288 return newpath, tilde_expand, tilde_val
282 return newpath, tilde_expand, tilde_val
289
283
290
284
291 def compress_user(path:str, tilde_expand:bool, tilde_val:str) -> str:
285 def compress_user(path:str, tilde_expand:bool, tilde_val:str) -> str:
292 """Does the opposite of expand_user, with its outputs.
286 """Does the opposite of expand_user, with its outputs.
293 """
287 """
294 if tilde_expand:
288 if tilde_expand:
295 return path.replace(tilde_val, '~')
289 return path.replace(tilde_val, '~')
296 else:
290 else:
297 return path
291 return path
298
292
299
293
300 def completions_sorting_key(word):
294 def completions_sorting_key(word):
301 """key for sorting completions
295 """key for sorting completions
302
296
303 This does several things:
297 This does several things:
304
298
305 - Demote any completions starting with underscores to the end
299 - Demote any completions starting with underscores to the end
306 - Insert any %magic and %%cellmagic completions in the alphabetical order
300 - Insert any %magic and %%cellmagic completions in the alphabetical order
307 by their name
301 by their name
308 """
302 """
309 prio1, prio2 = 0, 0
303 prio1, prio2 = 0, 0
310
304
311 if word.startswith('__'):
305 if word.startswith('__'):
312 prio1 = 2
306 prio1 = 2
313 elif word.startswith('_'):
307 elif word.startswith('_'):
314 prio1 = 1
308 prio1 = 1
315
309
316 if word.endswith('='):
310 if word.endswith('='):
317 prio1 = -1
311 prio1 = -1
318
312
319 if word.startswith('%%'):
313 if word.startswith('%%'):
320 # If there's another % in there, this is something else, so leave it alone
314 # If there's another % in there, this is something else, so leave it alone
321 if not "%" in word[2:]:
315 if not "%" in word[2:]:
322 word = word[2:]
316 word = word[2:]
323 prio2 = 2
317 prio2 = 2
324 elif word.startswith('%'):
318 elif word.startswith('%'):
325 if not "%" in word[1:]:
319 if not "%" in word[1:]:
326 word = word[1:]
320 word = word[1:]
327 prio2 = 1
321 prio2 = 1
328
322
329 return prio1, word, prio2
323 return prio1, word, prio2
330
324
331
325
332 class _FakeJediCompletion:
326 class _FakeJediCompletion:
333 """
327 """
334 This is a workaround to communicate to the UI that Jedi has crashed and to
328 This is a workaround to communicate to the UI that Jedi has crashed and to
335 report a bug. Will be used only id :any:`IPCompleter.debug` is set to true.
329 report a bug. Will be used only id :any:`IPCompleter.debug` is set to true.
336
330
337 Added in IPython 6.0 so should likely be removed for 7.0
331 Added in IPython 6.0 so should likely be removed for 7.0
338
332
339 """
333 """
340
334
341 def __init__(self, name):
335 def __init__(self, name):
342
336
343 self.name = name
337 self.name = name
344 self.complete = name
338 self.complete = name
345 self.type = 'crashed'
339 self.type = 'crashed'
346 self.name_with_symbols = name
340 self.name_with_symbols = name
347 self.signature = ''
341 self.signature = ''
348 self._origin = 'fake'
342 self._origin = 'fake'
349
343
350 def __repr__(self):
344 def __repr__(self):
351 return '<Fake completion object jedi has crashed>'
345 return '<Fake completion object jedi has crashed>'
352
346
353
347
354 class Completion:
348 class Completion:
355 """
349 """
356 Completion object used and return by IPython completers.
350 Completion object used and return by IPython completers.
357
351
358 .. warning:: Unstable
352 .. warning:: Unstable
359
353
360 This function is unstable, API may change without warning.
354 This function is unstable, API may change without warning.
361 It will also raise unless use in proper context manager.
355 It will also raise unless use in proper context manager.
362
356
363 This act as a middle ground :any:`Completion` object between the
357 This act as a middle ground :any:`Completion` object between the
364 :any:`jedi.api.classes.Completion` object and the Prompt Toolkit completion
358 :any:`jedi.api.classes.Completion` object and the Prompt Toolkit completion
365 object. While Jedi need a lot of information about evaluator and how the
359 object. While Jedi need a lot of information about evaluator and how the
366 code should be ran/inspected, PromptToolkit (and other frontend) mostly
360 code should be ran/inspected, PromptToolkit (and other frontend) mostly
367 need user facing information.
361 need user facing information.
368
362
369 - Which range should be replaced replaced by what.
363 - Which range should be replaced replaced by what.
370 - Some metadata (like completion type), or meta information to displayed to
364 - Some metadata (like completion type), or meta information to displayed to
371 the use user.
365 the use user.
372
366
373 For debugging purpose we can also store the origin of the completion (``jedi``,
367 For debugging purpose we can also store the origin of the completion (``jedi``,
374 ``IPython.python_matches``, ``IPython.magics_matches``...).
368 ``IPython.python_matches``, ``IPython.magics_matches``...).
375 """
369 """
376
370
377 __slots__ = ['start', 'end', 'text', 'type', 'signature', '_origin']
371 __slots__ = ['start', 'end', 'text', 'type', 'signature', '_origin']
378
372
379 def __init__(self, start: int, end: int, text: str, *, type: str=None, _origin='', signature='') -> None:
373 def __init__(self, start: int, end: int, text: str, *, type: str=None, _origin='', signature='') -> None:
380 warnings.warn("``Completion`` is a provisional API (as of IPython 6.0). "
374 warnings.warn("``Completion`` is a provisional API (as of IPython 6.0). "
381 "It may change without warnings. "
375 "It may change without warnings. "
382 "Use in corresponding context manager.",
376 "Use in corresponding context manager.",
383 category=ProvisionalCompleterWarning, stacklevel=2)
377 category=ProvisionalCompleterWarning, stacklevel=2)
384
378
385 self.start = start
379 self.start = start
386 self.end = end
380 self.end = end
387 self.text = text
381 self.text = text
388 self.type = type
382 self.type = type
389 self.signature = signature
383 self.signature = signature
390 self._origin = _origin
384 self._origin = _origin
391
385
392 def __repr__(self):
386 def __repr__(self):
393 return '<Completion start=%s end=%s text=%r type=%r, signature=%r,>' % \
387 return '<Completion start=%s end=%s text=%r type=%r, signature=%r,>' % \
394 (self.start, self.end, self.text, self.type or '?', self.signature or '?')
388 (self.start, self.end, self.text, self.type or '?', self.signature or '?')
395
389
396 def __eq__(self, other)->Bool:
390 def __eq__(self, other)->Bool:
397 """
391 """
398 Equality and hash do not hash the type (as some completer may not be
392 Equality and hash do not hash the type (as some completer may not be
399 able to infer the type), but are use to (partially) de-duplicate
393 able to infer the type), but are use to (partially) de-duplicate
400 completion.
394 completion.
401
395
402 Completely de-duplicating completion is a bit tricker that just
396 Completely de-duplicating completion is a bit tricker that just
403 comparing as it depends on surrounding text, which Completions are not
397 comparing as it depends on surrounding text, which Completions are not
404 aware of.
398 aware of.
405 """
399 """
406 return self.start == other.start and \
400 return self.start == other.start and \
407 self.end == other.end and \
401 self.end == other.end and \
408 self.text == other.text
402 self.text == other.text
409
403
410 def __hash__(self):
404 def __hash__(self):
411 return hash((self.start, self.end, self.text))
405 return hash((self.start, self.end, self.text))
412
406
413
407
414 _IC = Iterable[Completion]
408 _IC = Iterable[Completion]
415
409
416
410
417 def _deduplicate_completions(text: str, completions: _IC)-> _IC:
411 def _deduplicate_completions(text: str, completions: _IC)-> _IC:
418 """
412 """
419 Deduplicate a set of completions.
413 Deduplicate a set of completions.
420
414
421 .. warning:: Unstable
415 .. warning:: Unstable
422
416
423 This function is unstable, API may change without warning.
417 This function is unstable, API may change without warning.
424
418
425 Parameters
419 Parameters
426 ----------
420 ----------
427 text: str
421 text: str
428 text that should be completed.
422 text that should be completed.
429 completions: Iterator[Completion]
423 completions: Iterator[Completion]
430 iterator over the completions to deduplicate
424 iterator over the completions to deduplicate
431
425
432 Yields
426 Yields
433 ------
427 ------
434 `Completions` objects
428 `Completions` objects
435
429
436
430
437 Completions coming from multiple sources, may be different but end up having
431 Completions coming from multiple sources, may be different but end up having
438 the same effect when applied to ``text``. If this is the case, this will
432 the same effect when applied to ``text``. If this is the case, this will
439 consider completions as equal and only emit the first encountered.
433 consider completions as equal and only emit the first encountered.
440
434
441 Not folded in `completions()` yet for debugging purpose, and to detect when
435 Not folded in `completions()` yet for debugging purpose, and to detect when
442 the IPython completer does return things that Jedi does not, but should be
436 the IPython completer does return things that Jedi does not, but should be
443 at some point.
437 at some point.
444 """
438 """
445 completions = list(completions)
439 completions = list(completions)
446 if not completions:
440 if not completions:
447 return
441 return
448
442
449 new_start = min(c.start for c in completions)
443 new_start = min(c.start for c in completions)
450 new_end = max(c.end for c in completions)
444 new_end = max(c.end for c in completions)
451
445
452 seen = set()
446 seen = set()
453 for c in completions:
447 for c in completions:
454 new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
448 new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
455 if new_text not in seen:
449 if new_text not in seen:
456 yield c
450 yield c
457 seen.add(new_text)
451 seen.add(new_text)
458
452
459
453
460 def rectify_completions(text: str, completions: _IC, *, _debug=False)->_IC:
454 def rectify_completions(text: str, completions: _IC, *, _debug=False)->_IC:
461 """
455 """
462 Rectify a set of completions to all have the same ``start`` and ``end``
456 Rectify a set of completions to all have the same ``start`` and ``end``
463
457
464 .. warning:: Unstable
458 .. warning:: Unstable
465
459
466 This function is unstable, API may change without warning.
460 This function is unstable, API may change without warning.
467 It will also raise unless use in proper context manager.
461 It will also raise unless use in proper context manager.
468
462
469 Parameters
463 Parameters
470 ----------
464 ----------
471 text: str
465 text: str
472 text that should be completed.
466 text that should be completed.
473 completions: Iterator[Completion]
467 completions: Iterator[Completion]
474 iterator over the completions to rectify
468 iterator over the completions to rectify
475
469
476
470
477 :any:`jedi.api.classes.Completion` s returned by Jedi may not have the same start and end, though
471 :any:`jedi.api.classes.Completion` s returned by Jedi may not have the same start and end, though
478 the Jupyter Protocol requires them to behave like so. This will readjust
472 the Jupyter Protocol requires them to behave like so. This will readjust
479 the completion to have the same ``start`` and ``end`` by padding both
473 the completion to have the same ``start`` and ``end`` by padding both
480 extremities with surrounding text.
474 extremities with surrounding text.
481
475
482 During stabilisation should support a ``_debug`` option to log which
476 During stabilisation should support a ``_debug`` option to log which
483 completion are return by the IPython completer and not found in Jedi in
477 completion are return by the IPython completer and not found in Jedi in
484 order to make upstream bug report.
478 order to make upstream bug report.
485 """
479 """
486 warnings.warn("`rectify_completions` is a provisional API (as of IPython 6.0). "
480 warnings.warn("`rectify_completions` is a provisional API (as of IPython 6.0). "
487 "It may change without warnings. "
481 "It may change without warnings. "
488 "Use in corresponding context manager.",
482 "Use in corresponding context manager.",
489 category=ProvisionalCompleterWarning, stacklevel=2)
483 category=ProvisionalCompleterWarning, stacklevel=2)
490
484
491 completions = list(completions)
485 completions = list(completions)
492 if not completions:
486 if not completions:
493 return
487 return
494 starts = (c.start for c in completions)
488 starts = (c.start for c in completions)
495 ends = (c.end for c in completions)
489 ends = (c.end for c in completions)
496
490
497 new_start = min(starts)
491 new_start = min(starts)
498 new_end = max(ends)
492 new_end = max(ends)
499
493
500 seen_jedi = set()
494 seen_jedi = set()
501 seen_python_matches = set()
495 seen_python_matches = set()
502 for c in completions:
496 for c in completions:
503 new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
497 new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
504 if c._origin == 'jedi':
498 if c._origin == 'jedi':
505 seen_jedi.add(new_text)
499 seen_jedi.add(new_text)
506 elif c._origin == 'IPCompleter.python_matches':
500 elif c._origin == 'IPCompleter.python_matches':
507 seen_python_matches.add(new_text)
501 seen_python_matches.add(new_text)
508 yield Completion(new_start, new_end, new_text, type=c.type, _origin=c._origin, signature=c.signature)
502 yield Completion(new_start, new_end, new_text, type=c.type, _origin=c._origin, signature=c.signature)
509 diff = seen_python_matches.difference(seen_jedi)
503 diff = seen_python_matches.difference(seen_jedi)
510 if diff and _debug:
504 if diff and _debug:
511 print('IPython.python matches have extras:', diff)
505 print('IPython.python matches have extras:', diff)
512
506
513
507
514 if sys.platform == 'win32':
508 if sys.platform == 'win32':
515 DELIMS = ' \t\n`!@#$^&*()=+[{]}|;\'",<>?'
509 DELIMS = ' \t\n`!@#$^&*()=+[{]}|;\'",<>?'
516 else:
510 else:
517 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
511 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
518
512
519 GREEDY_DELIMS = ' =\r\n'
513 GREEDY_DELIMS = ' =\r\n'
520
514
521
515
522 class CompletionSplitter(object):
516 class CompletionSplitter(object):
523 """An object to split an input line in a manner similar to readline.
517 """An object to split an input line in a manner similar to readline.
524
518
525 By having our own implementation, we can expose readline-like completion in
519 By having our own implementation, we can expose readline-like completion in
526 a uniform manner to all frontends. This object only needs to be given the
520 a uniform manner to all frontends. This object only needs to be given the
527 line of text to be split and the cursor position on said line, and it
521 line of text to be split and the cursor position on said line, and it
528 returns the 'word' to be completed on at the cursor after splitting the
522 returns the 'word' to be completed on at the cursor after splitting the
529 entire line.
523 entire line.
530
524
531 What characters are used as splitting delimiters can be controlled by
525 What characters are used as splitting delimiters can be controlled by
532 setting the ``delims`` attribute (this is a property that internally
526 setting the ``delims`` attribute (this is a property that internally
533 automatically builds the necessary regular expression)"""
527 automatically builds the necessary regular expression)"""
534
528
535 # Private interface
529 # Private interface
536
530
537 # A string of delimiter characters. The default value makes sense for
531 # A string of delimiter characters. The default value makes sense for
538 # IPython's most typical usage patterns.
532 # IPython's most typical usage patterns.
539 _delims = DELIMS
533 _delims = DELIMS
540
534
541 # The expression (a normal string) to be compiled into a regular expression
535 # The expression (a normal string) to be compiled into a regular expression
542 # for actual splitting. We store it as an attribute mostly for ease of
536 # for actual splitting. We store it as an attribute mostly for ease of
543 # debugging, since this type of code can be so tricky to debug.
537 # debugging, since this type of code can be so tricky to debug.
544 _delim_expr = None
538 _delim_expr = None
545
539
546 # The regular expression that does the actual splitting
540 # The regular expression that does the actual splitting
547 _delim_re = None
541 _delim_re = None
548
542
549 def __init__(self, delims=None):
543 def __init__(self, delims=None):
550 delims = CompletionSplitter._delims if delims is None else delims
544 delims = CompletionSplitter._delims if delims is None else delims
551 self.delims = delims
545 self.delims = delims
552
546
553 @property
547 @property
554 def delims(self):
548 def delims(self):
555 """Return the string of delimiter characters."""
549 """Return the string of delimiter characters."""
556 return self._delims
550 return self._delims
557
551
558 @delims.setter
552 @delims.setter
559 def delims(self, delims):
553 def delims(self, delims):
560 """Set the delimiters for line splitting."""
554 """Set the delimiters for line splitting."""
561 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
555 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
562 self._delim_re = re.compile(expr)
556 self._delim_re = re.compile(expr)
563 self._delims = delims
557 self._delims = delims
564 self._delim_expr = expr
558 self._delim_expr = expr
565
559
566 def split_line(self, line, cursor_pos=None):
560 def split_line(self, line, cursor_pos=None):
567 """Split a line of text with a cursor at the given position.
561 """Split a line of text with a cursor at the given position.
568 """
562 """
569 l = line if cursor_pos is None else line[:cursor_pos]
563 l = line if cursor_pos is None else line[:cursor_pos]
570 return self._delim_re.split(l)[-1]
564 return self._delim_re.split(l)[-1]
571
565
572
566
573
567
574 class Completer(Configurable):
568 class Completer(Configurable):
575
569
576 greedy = Bool(False,
570 greedy = Bool(False,
577 help="""Activate greedy completion
571 help="""Activate greedy completion
578 PENDING DEPRECTION. this is now mostly taken care of with Jedi.
572 PENDING DEPRECTION. this is now mostly taken care of with Jedi.
579
573
580 This will enable completion on elements of lists, results of function calls, etc.,
574 This will enable completion on elements of lists, results of function calls, etc.,
581 but can be unsafe because the code is actually evaluated on TAB.
575 but can be unsafe because the code is actually evaluated on TAB.
582 """
576 """
583 ).tag(config=True)
577 ).tag(config=True)
584
578
585 use_jedi = Bool(default_value=JEDI_INSTALLED,
579 use_jedi = Bool(default_value=JEDI_INSTALLED,
586 help="Experimental: Use Jedi to generate autocompletions. "
580 help="Experimental: Use Jedi to generate autocompletions. "
587 "Default to True if jedi is installed.").tag(config=True)
581 "Default to True if jedi is installed.").tag(config=True)
588
582
589 jedi_compute_type_timeout = Int(default_value=400,
583 jedi_compute_type_timeout = Int(default_value=400,
590 help="""Experimental: restrict time (in milliseconds) during which Jedi can compute types.
584 help="""Experimental: restrict time (in milliseconds) during which Jedi can compute types.
591 Set to 0 to stop computing types. Non-zero value lower than 100ms may hurt
585 Set to 0 to stop computing types. Non-zero value lower than 100ms may hurt
592 performance by preventing jedi to build its cache.
586 performance by preventing jedi to build its cache.
593 """).tag(config=True)
587 """).tag(config=True)
594
588
595 debug = Bool(default_value=False,
589 debug = Bool(default_value=False,
596 help='Enable debug for the Completer. Mostly print extra '
590 help='Enable debug for the Completer. Mostly print extra '
597 'information for experimental jedi integration.')\
591 'information for experimental jedi integration.')\
598 .tag(config=True)
592 .tag(config=True)
599
593
600 backslash_combining_completions = Bool(True,
594 backslash_combining_completions = Bool(True,
601 help="Enable unicode completions, e.g. \\alpha<tab> . "
595 help="Enable unicode completions, e.g. \\alpha<tab> . "
602 "Includes completion of latex commands, unicode names, and expanding "
596 "Includes completion of latex commands, unicode names, and expanding "
603 "unicode characters back to latex commands.").tag(config=True)
597 "unicode characters back to latex commands.").tag(config=True)
604
598
605
599
606
600
607 def __init__(self, namespace=None, global_namespace=None, **kwargs):
601 def __init__(self, namespace=None, global_namespace=None, **kwargs):
608 """Create a new completer for the command line.
602 """Create a new completer for the command line.
609
603
610 Completer(namespace=ns, global_namespace=ns2) -> completer instance.
604 Completer(namespace=ns, global_namespace=ns2) -> completer instance.
611
605
612 If unspecified, the default namespace where completions are performed
606 If unspecified, the default namespace where completions are performed
613 is __main__ (technically, __main__.__dict__). Namespaces should be
607 is __main__ (technically, __main__.__dict__). Namespaces should be
614 given as dictionaries.
608 given as dictionaries.
615
609
616 An optional second namespace can be given. This allows the completer
610 An optional second namespace can be given. This allows the completer
617 to handle cases where both the local and global scopes need to be
611 to handle cases where both the local and global scopes need to be
618 distinguished.
612 distinguished.
619 """
613 """
620
614
621 # Don't bind to namespace quite yet, but flag whether the user wants a
615 # Don't bind to namespace quite yet, but flag whether the user wants a
622 # specific namespace or to use __main__.__dict__. This will allow us
616 # specific namespace or to use __main__.__dict__. This will allow us
623 # to bind to __main__.__dict__ at completion time, not now.
617 # to bind to __main__.__dict__ at completion time, not now.
624 if namespace is None:
618 if namespace is None:
625 self.use_main_ns = True
619 self.use_main_ns = True
626 else:
620 else:
627 self.use_main_ns = False
621 self.use_main_ns = False
628 self.namespace = namespace
622 self.namespace = namespace
629
623
630 # The global namespace, if given, can be bound directly
624 # The global namespace, if given, can be bound directly
631 if global_namespace is None:
625 if global_namespace is None:
632 self.global_namespace = {}
626 self.global_namespace = {}
633 else:
627 else:
634 self.global_namespace = global_namespace
628 self.global_namespace = global_namespace
635
629
636 super(Completer, self).__init__(**kwargs)
630 super(Completer, self).__init__(**kwargs)
637
631
638 def complete(self, text, state):
632 def complete(self, text, state):
639 """Return the next possible completion for 'text'.
633 """Return the next possible completion for 'text'.
640
634
641 This is called successively with state == 0, 1, 2, ... until it
635 This is called successively with state == 0, 1, 2, ... until it
642 returns None. The completion should begin with 'text'.
636 returns None. The completion should begin with 'text'.
643
637
644 """
638 """
645 if self.use_main_ns:
639 if self.use_main_ns:
646 self.namespace = __main__.__dict__
640 self.namespace = __main__.__dict__
647
641
648 if state == 0:
642 if state == 0:
649 if "." in text:
643 if "." in text:
650 self.matches = self.attr_matches(text)
644 self.matches = self.attr_matches(text)
651 else:
645 else:
652 self.matches = self.global_matches(text)
646 self.matches = self.global_matches(text)
653 try:
647 try:
654 return self.matches[state]
648 return self.matches[state]
655 except IndexError:
649 except IndexError:
656 return None
650 return None
657
651
658 def global_matches(self, text):
652 def global_matches(self, text):
659 """Compute matches when text is a simple name.
653 """Compute matches when text is a simple name.
660
654
661 Return a list of all keywords, built-in functions and names currently
655 Return a list of all keywords, built-in functions and names currently
662 defined in self.namespace or self.global_namespace that match.
656 defined in self.namespace or self.global_namespace that match.
663
657
664 """
658 """
665 matches = []
659 matches = []
666 match_append = matches.append
660 match_append = matches.append
667 n = len(text)
661 n = len(text)
668 for lst in [keyword.kwlist,
662 for lst in [keyword.kwlist,
669 builtin_mod.__dict__.keys(),
663 builtin_mod.__dict__.keys(),
670 self.namespace.keys(),
664 self.namespace.keys(),
671 self.global_namespace.keys()]:
665 self.global_namespace.keys()]:
672 for word in lst:
666 for word in lst:
673 if word[:n] == text and word != "__builtins__":
667 if word[:n] == text and word != "__builtins__":
674 match_append(word)
668 match_append(word)
675
669
676 snake_case_re = re.compile(r"[^_]+(_[^_]+)+?\Z")
670 snake_case_re = re.compile(r"[^_]+(_[^_]+)+?\Z")
677 for lst in [self.namespace.keys(),
671 for lst in [self.namespace.keys(),
678 self.global_namespace.keys()]:
672 self.global_namespace.keys()]:
679 shortened = {"_".join([sub[0] for sub in word.split('_')]) : word
673 shortened = {"_".join([sub[0] for sub in word.split('_')]) : word
680 for word in lst if snake_case_re.match(word)}
674 for word in lst if snake_case_re.match(word)}
681 for word in shortened.keys():
675 for word in shortened.keys():
682 if word[:n] == text and word != "__builtins__":
676 if word[:n] == text and word != "__builtins__":
683 match_append(shortened[word])
677 match_append(shortened[word])
684 return matches
678 return matches
685
679
686 def attr_matches(self, text):
680 def attr_matches(self, text):
687 """Compute matches when text contains a dot.
681 """Compute matches when text contains a dot.
688
682
689 Assuming the text is of the form NAME.NAME....[NAME], and is
683 Assuming the text is of the form NAME.NAME....[NAME], and is
690 evaluatable in self.namespace or self.global_namespace, it will be
684 evaluatable in self.namespace or self.global_namespace, it will be
691 evaluated and its attributes (as revealed by dir()) are used as
685 evaluated and its attributes (as revealed by dir()) are used as
692 possible completions. (For class instances, class members are
686 possible completions. (For class instances, class members are
693 also considered.)
687 also considered.)
694
688
695 WARNING: this can still invoke arbitrary C code, if an object
689 WARNING: this can still invoke arbitrary C code, if an object
696 with a __getattr__ hook is evaluated.
690 with a __getattr__ hook is evaluated.
697
691
698 """
692 """
699
693
700 # Another option, seems to work great. Catches things like ''.<tab>
694 # Another option, seems to work great. Catches things like ''.<tab>
701 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
695 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
702
696
703 if m:
697 if m:
704 expr, attr = m.group(1, 3)
698 expr, attr = m.group(1, 3)
705 elif self.greedy:
699 elif self.greedy:
706 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
700 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
707 if not m2:
701 if not m2:
708 return []
702 return []
709 expr, attr = m2.group(1,2)
703 expr, attr = m2.group(1,2)
710 else:
704 else:
711 return []
705 return []
712
706
713 try:
707 try:
714 obj = eval(expr, self.namespace)
708 obj = eval(expr, self.namespace)
715 except:
709 except:
716 try:
710 try:
717 obj = eval(expr, self.global_namespace)
711 obj = eval(expr, self.global_namespace)
718 except:
712 except:
719 return []
713 return []
720
714
721 if self.limit_to__all__ and hasattr(obj, '__all__'):
715 if self.limit_to__all__ and hasattr(obj, '__all__'):
722 words = get__all__entries(obj)
716 words = get__all__entries(obj)
723 else:
717 else:
724 words = dir2(obj)
718 words = dir2(obj)
725
719
726 try:
720 try:
727 words = generics.complete_object(obj, words)
721 words = generics.complete_object(obj, words)
728 except TryNext:
722 except TryNext:
729 pass
723 pass
730 except AssertionError:
724 except AssertionError:
731 raise
725 raise
732 except Exception:
726 except Exception:
733 # Silence errors from completion function
727 # Silence errors from completion function
734 #raise # dbg
728 #raise # dbg
735 pass
729 pass
736 # Build match list to return
730 # Build match list to return
737 n = len(attr)
731 n = len(attr)
738 return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ]
732 return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ]
739
733
740
734
741 def get__all__entries(obj):
735 def get__all__entries(obj):
742 """returns the strings in the __all__ attribute"""
736 """returns the strings in the __all__ attribute"""
743 try:
737 try:
744 words = getattr(obj, '__all__')
738 words = getattr(obj, '__all__')
745 except:
739 except:
746 return []
740 return []
747
741
748 return [w for w in words if isinstance(w, str)]
742 return [w for w in words if isinstance(w, str)]
749
743
750
744
751 def match_dict_keys(keys: List[str], prefix: str, delims: str):
745 def match_dict_keys(keys: List[str], prefix: str, delims: str):
752 """Used by dict_key_matches, matching the prefix to a list of keys
746 """Used by dict_key_matches, matching the prefix to a list of keys
753
747
754 Parameters
748 Parameters
755 ==========
749 ==========
756 keys:
750 keys:
757 list of keys in dictionary currently being completed.
751 list of keys in dictionary currently being completed.
758 prefix:
752 prefix:
759 Part of the text already typed by the user. e.g. `mydict[b'fo`
753 Part of the text already typed by the user. e.g. `mydict[b'fo`
760 delims:
754 delims:
761 String of delimiters to consider when finding the current key.
755 String of delimiters to consider when finding the current key.
762
756
763 Returns
757 Returns
764 =======
758 =======
765
759
766 A tuple of three elements: ``quote``, ``token_start``, ``matched``, with
760 A tuple of three elements: ``quote``, ``token_start``, ``matched``, with
767 ``quote`` being the quote that need to be used to close current string.
761 ``quote`` being the quote that need to be used to close current string.
768 ``token_start`` the position where the replacement should start occurring,
762 ``token_start`` the position where the replacement should start occurring,
769 ``matches`` a list of replacement/completion
763 ``matches`` a list of replacement/completion
770
764
771 """
765 """
772 if not prefix:
766 if not prefix:
773 return None, 0, [repr(k) for k in keys
767 return None, 0, [repr(k) for k in keys
774 if isinstance(k, (str, bytes))]
768 if isinstance(k, (str, bytes))]
775 quote_match = re.search('["\']', prefix)
769 quote_match = re.search('["\']', prefix)
776 quote = quote_match.group()
770 quote = quote_match.group()
777 try:
771 try:
778 prefix_str = eval(prefix + quote, {})
772 prefix_str = eval(prefix + quote, {})
779 except Exception:
773 except Exception:
780 return None, 0, []
774 return None, 0, []
781
775
782 pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$'
776 pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$'
783 token_match = re.search(pattern, prefix, re.UNICODE)
777 token_match = re.search(pattern, prefix, re.UNICODE)
784 token_start = token_match.start()
778 token_start = token_match.start()
785 token_prefix = token_match.group()
779 token_prefix = token_match.group()
786
780
787 matched = []
781 matched = []
788 for key in keys:
782 for key in keys:
789 try:
783 try:
790 if not key.startswith(prefix_str):
784 if not key.startswith(prefix_str):
791 continue
785 continue
792 except (AttributeError, TypeError, UnicodeError):
786 except (AttributeError, TypeError, UnicodeError):
793 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
787 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
794 continue
788 continue
795
789
796 # reformat remainder of key to begin with prefix
790 # reformat remainder of key to begin with prefix
797 rem = key[len(prefix_str):]
791 rem = key[len(prefix_str):]
798 # force repr wrapped in '
792 # force repr wrapped in '
799 rem_repr = repr(rem + '"') if isinstance(rem, str) else repr(rem + b'"')
793 rem_repr = repr(rem + '"') if isinstance(rem, str) else repr(rem + b'"')
800 if rem_repr.startswith('u') and prefix[0] not in 'uU':
794 if rem_repr.startswith('u') and prefix[0] not in 'uU':
801 # Found key is unicode, but prefix is Py2 string.
795 # Found key is unicode, but prefix is Py2 string.
802 # Therefore attempt to interpret key as string.
796 # Therefore attempt to interpret key as string.
803 try:
797 try:
804 rem_repr = repr(rem.encode('ascii') + '"')
798 rem_repr = repr(rem.encode('ascii') + '"')
805 except UnicodeEncodeError:
799 except UnicodeEncodeError:
806 continue
800 continue
807
801
808 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
802 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
809 if quote == '"':
803 if quote == '"':
810 # The entered prefix is quoted with ",
804 # The entered prefix is quoted with ",
811 # but the match is quoted with '.
805 # but the match is quoted with '.
812 # A contained " hence needs escaping for comparison:
806 # A contained " hence needs escaping for comparison:
813 rem_repr = rem_repr.replace('"', '\\"')
807 rem_repr = rem_repr.replace('"', '\\"')
814
808
815 # then reinsert prefix from start of token
809 # then reinsert prefix from start of token
816 matched.append('%s%s' % (token_prefix, rem_repr))
810 matched.append('%s%s' % (token_prefix, rem_repr))
817 return quote, token_start, matched
811 return quote, token_start, matched
818
812
819
813
820 def cursor_to_position(text:str, line:int, column:int)->int:
814 def cursor_to_position(text:str, line:int, column:int)->int:
821 """
815 """
822
816
823 Convert the (line,column) position of the cursor in text to an offset in a
817 Convert the (line,column) position of the cursor in text to an offset in a
824 string.
818 string.
825
819
826 Parameters
820 Parameters
827 ----------
821 ----------
828
822
829 text : str
823 text : str
830 The text in which to calculate the cursor offset
824 The text in which to calculate the cursor offset
831 line : int
825 line : int
832 Line of the cursor; 0-indexed
826 Line of the cursor; 0-indexed
833 column : int
827 column : int
834 Column of the cursor 0-indexed
828 Column of the cursor 0-indexed
835
829
836 Return
830 Return
837 ------
831 ------
838 Position of the cursor in ``text``, 0-indexed.
832 Position of the cursor in ``text``, 0-indexed.
839
833
840 See Also
834 See Also
841 --------
835 --------
842 position_to_cursor: reciprocal of this function
836 position_to_cursor: reciprocal of this function
843
837
844 """
838 """
845 lines = text.split('\n')
839 lines = text.split('\n')
846 assert line <= len(lines), '{} <= {}'.format(str(line), str(len(lines)))
840 assert line <= len(lines), '{} <= {}'.format(str(line), str(len(lines)))
847
841
848 return sum(len(l) + 1 for l in lines[:line]) + column
842 return sum(len(l) + 1 for l in lines[:line]) + column
849
843
850 def position_to_cursor(text:str, offset:int)->Tuple[int, int]:
844 def position_to_cursor(text:str, offset:int)->Tuple[int, int]:
851 """
845 """
852 Convert the position of the cursor in text (0 indexed) to a line
846 Convert the position of the cursor in text (0 indexed) to a line
853 number(0-indexed) and a column number (0-indexed) pair
847 number(0-indexed) and a column number (0-indexed) pair
854
848
855 Position should be a valid position in ``text``.
849 Position should be a valid position in ``text``.
856
850
857 Parameters
851 Parameters
858 ----------
852 ----------
859
853
860 text : str
854 text : str
861 The text in which to calculate the cursor offset
855 The text in which to calculate the cursor offset
862 offset : int
856 offset : int
863 Position of the cursor in ``text``, 0-indexed.
857 Position of the cursor in ``text``, 0-indexed.
864
858
865 Return
859 Return
866 ------
860 ------
867 (line, column) : (int, int)
861 (line, column) : (int, int)
868 Line of the cursor; 0-indexed, column of the cursor 0-indexed
862 Line of the cursor; 0-indexed, column of the cursor 0-indexed
869
863
870
864
871 See Also
865 See Also
872 --------
866 --------
873 cursor_to_position : reciprocal of this function
867 cursor_to_position : reciprocal of this function
874
868
875
869
876 """
870 """
877
871
878 assert 0 <= offset <= len(text) , "0 <= %s <= %s" % (offset , len(text))
872 assert 0 <= offset <= len(text) , "0 <= %s <= %s" % (offset , len(text))
879
873
880 before = text[:offset]
874 before = text[:offset]
881 blines = before.split('\n') # ! splitnes trim trailing \n
875 blines = before.split('\n') # ! splitnes trim trailing \n
882 line = before.count('\n')
876 line = before.count('\n')
883 col = len(blines[-1])
877 col = len(blines[-1])
884 return line, col
878 return line, col
885
879
886
880
887 def _safe_isinstance(obj, module, class_name):
881 def _safe_isinstance(obj, module, class_name):
888 """Checks if obj is an instance of module.class_name if loaded
882 """Checks if obj is an instance of module.class_name if loaded
889 """
883 """
890 return (module in sys.modules and
884 return (module in sys.modules and
891 isinstance(obj, getattr(import_module(module), class_name)))
885 isinstance(obj, getattr(import_module(module), class_name)))
892
886
893
887
894 def back_unicode_name_matches(text):
888 def back_unicode_name_matches(text):
895 u"""Match unicode characters back to unicode name
889 u"""Match unicode characters back to unicode name
896
890
897 This does ``β˜ƒ`` -> ``\\snowman``
891 This does ``β˜ƒ`` -> ``\\snowman``
898
892
899 Note that snowman is not a valid python3 combining character but will be expanded.
893 Note that snowman is not a valid python3 combining character but will be expanded.
900 Though it will not recombine back to the snowman character by the completion machinery.
894 Though it will not recombine back to the snowman character by the completion machinery.
901
895
902 This will not either back-complete standard sequences like \\n, \\b ...
896 This will not either back-complete standard sequences like \\n, \\b ...
903
897
904 Used on Python 3 only.
898 Used on Python 3 only.
905 """
899 """
906 if len(text)<2:
900 if len(text)<2:
907 return u'', ()
901 return u'', ()
908 maybe_slash = text[-2]
902 maybe_slash = text[-2]
909 if maybe_slash != '\\':
903 if maybe_slash != '\\':
910 return u'', ()
904 return u'', ()
911
905
912 char = text[-1]
906 char = text[-1]
913 # no expand on quote for completion in strings.
907 # no expand on quote for completion in strings.
914 # nor backcomplete standard ascii keys
908 # nor backcomplete standard ascii keys
915 if char in string.ascii_letters or char in ['"',"'"]:
909 if char in string.ascii_letters or char in ['"',"'"]:
916 return u'', ()
910 return u'', ()
917 try :
911 try :
918 unic = unicodedata.name(char)
912 unic = unicodedata.name(char)
919 return '\\'+char,['\\'+unic]
913 return '\\'+char,['\\'+unic]
920 except KeyError:
914 except KeyError:
921 pass
915 pass
922 return u'', ()
916 return u'', ()
923
917
924 def back_latex_name_matches(text:str):
918 def back_latex_name_matches(text:str):
925 """Match latex characters back to unicode name
919 """Match latex characters back to unicode name
926
920
927 This does ``\\β„΅`` -> ``\\aleph``
921 This does ``\\β„΅`` -> ``\\aleph``
928
922
929 Used on Python 3 only.
923 Used on Python 3 only.
930 """
924 """
931 if len(text)<2:
925 if len(text)<2:
932 return u'', ()
926 return u'', ()
933 maybe_slash = text[-2]
927 maybe_slash = text[-2]
934 if maybe_slash != '\\':
928 if maybe_slash != '\\':
935 return u'', ()
929 return u'', ()
936
930
937
931
938 char = text[-1]
932 char = text[-1]
939 # no expand on quote for completion in strings.
933 # no expand on quote for completion in strings.
940 # nor backcomplete standard ascii keys
934 # nor backcomplete standard ascii keys
941 if char in string.ascii_letters or char in ['"',"'"]:
935 if char in string.ascii_letters or char in ['"',"'"]:
942 return u'', ()
936 return u'', ()
943 try :
937 try :
944 latex = reverse_latex_symbol[char]
938 latex = reverse_latex_symbol[char]
945 # '\\' replace the \ as well
939 # '\\' replace the \ as well
946 return '\\'+char,[latex]
940 return '\\'+char,[latex]
947 except KeyError:
941 except KeyError:
948 pass
942 pass
949 return u'', ()
943 return u'', ()
950
944
951
945
952 def _formatparamchildren(parameter) -> str:
946 def _formatparamchildren(parameter) -> str:
953 """
947 """
954 Get parameter name and value from Jedi Private API
948 Get parameter name and value from Jedi Private API
955
949
956 Jedi does not expose a simple way to get `param=value` from its API.
950 Jedi does not expose a simple way to get `param=value` from its API.
957
951
958 Parameter
952 Parameter
959 =========
953 =========
960
954
961 parameter:
955 parameter:
962 Jedi's function `Param`
956 Jedi's function `Param`
963
957
964 Returns
958 Returns
965 =======
959 =======
966
960
967 A string like 'a', 'b=1', '*args', '**kwargs'
961 A string like 'a', 'b=1', '*args', '**kwargs'
968
962
969
963
970 """
964 """
971 description = parameter.description
965 description = parameter.description
972 if not description.startswith('param '):
966 if not description.startswith('param '):
973 raise ValueError('Jedi function parameter description have change format.'
967 raise ValueError('Jedi function parameter description have change format.'
974 'Expected "param ...", found %r".' % description)
968 'Expected "param ...", found %r".' % description)
975 return description[6:]
969 return description[6:]
976
970
977 def _make_signature(completion)-> str:
971 def _make_signature(completion)-> str:
978 """
972 """
979 Make the signature from a jedi completion
973 Make the signature from a jedi completion
980
974
981 Parameter
975 Parameter
982 =========
976 =========
983
977
984 completion: jedi.Completion
978 completion: jedi.Completion
985 object does not complete a function type
979 object does not complete a function type
986
980
987 Returns
981 Returns
988 =======
982 =======
989
983
990 a string consisting of the function signature, with the parenthesis but
984 a string consisting of the function signature, with the parenthesis but
991 without the function name. example:
985 without the function name. example:
992 `(a, *args, b=1, **kwargs)`
986 `(a, *args, b=1, **kwargs)`
993
987
994 """
988 """
995
989
996 return '(%s)'% ', '.join([f for f in (_formatparamchildren(p) for p in completion.params) if f])
990 return '(%s)'% ', '.join([f for f in (_formatparamchildren(p) for p in completion.params) if f])
997
991
998 class IPCompleter(Completer):
992 class IPCompleter(Completer):
999 """Extension of the completer class with IPython-specific features"""
993 """Extension of the completer class with IPython-specific features"""
1000
994
995 _names = None
996
1001 @observe('greedy')
997 @observe('greedy')
1002 def _greedy_changed(self, change):
998 def _greedy_changed(self, change):
1003 """update the splitter and readline delims when greedy is changed"""
999 """update the splitter and readline delims when greedy is changed"""
1004 if change['new']:
1000 if change['new']:
1005 self.splitter.delims = GREEDY_DELIMS
1001 self.splitter.delims = GREEDY_DELIMS
1006 else:
1002 else:
1007 self.splitter.delims = DELIMS
1003 self.splitter.delims = DELIMS
1008
1004
1009 dict_keys_only = Bool(False,
1005 dict_keys_only = Bool(False,
1010 help="""Whether to show dict key matches only""")
1006 help="""Whether to show dict key matches only""")
1011
1007
1012 merge_completions = Bool(True,
1008 merge_completions = Bool(True,
1013 help="""Whether to merge completion results into a single list
1009 help="""Whether to merge completion results into a single list
1014
1010
1015 If False, only the completion results from the first non-empty
1011 If False, only the completion results from the first non-empty
1016 completer will be returned.
1012 completer will be returned.
1017 """
1013 """
1018 ).tag(config=True)
1014 ).tag(config=True)
1019 omit__names = Enum((0,1,2), default_value=2,
1015 omit__names = Enum((0,1,2), default_value=2,
1020 help="""Instruct the completer to omit private method names
1016 help="""Instruct the completer to omit private method names
1021
1017
1022 Specifically, when completing on ``object.<tab>``.
1018 Specifically, when completing on ``object.<tab>``.
1023
1019
1024 When 2 [default]: all names that start with '_' will be excluded.
1020 When 2 [default]: all names that start with '_' will be excluded.
1025
1021
1026 When 1: all 'magic' names (``__foo__``) will be excluded.
1022 When 1: all 'magic' names (``__foo__``) will be excluded.
1027
1023
1028 When 0: nothing will be excluded.
1024 When 0: nothing will be excluded.
1029 """
1025 """
1030 ).tag(config=True)
1026 ).tag(config=True)
1031 limit_to__all__ = Bool(False,
1027 limit_to__all__ = Bool(False,
1032 help="""
1028 help="""
1033 DEPRECATED as of version 5.0.
1029 DEPRECATED as of version 5.0.
1034
1030
1035 Instruct the completer to use __all__ for the completion
1031 Instruct the completer to use __all__ for the completion
1036
1032
1037 Specifically, when completing on ``object.<tab>``.
1033 Specifically, when completing on ``object.<tab>``.
1038
1034
1039 When True: only those names in obj.__all__ will be included.
1035 When True: only those names in obj.__all__ will be included.
1040
1036
1041 When False [default]: the __all__ attribute is ignored
1037 When False [default]: the __all__ attribute is ignored
1042 """,
1038 """,
1043 ).tag(config=True)
1039 ).tag(config=True)
1044
1040
1045 @observe('limit_to__all__')
1041 @observe('limit_to__all__')
1046 def _limit_to_all_changed(self, change):
1042 def _limit_to_all_changed(self, change):
1047 warnings.warn('`IPython.core.IPCompleter.limit_to__all__` configuration '
1043 warnings.warn('`IPython.core.IPCompleter.limit_to__all__` configuration '
1048 'value has been deprecated since IPython 5.0, will be made to have '
1044 'value has been deprecated since IPython 5.0, will be made to have '
1049 'no effects and then removed in future version of IPython.',
1045 'no effects and then removed in future version of IPython.',
1050 UserWarning)
1046 UserWarning)
1051
1047
1052 def __init__(self, shell=None, namespace=None, global_namespace=None,
1048 def __init__(self, shell=None, namespace=None, global_namespace=None,
1053 use_readline=_deprecation_readline_sentinel, config=None, **kwargs):
1049 use_readline=_deprecation_readline_sentinel, config=None, **kwargs):
1054 """IPCompleter() -> completer
1050 """IPCompleter() -> completer
1055
1051
1056 Return a completer object.
1052 Return a completer object.
1057
1053
1058 Parameters
1054 Parameters
1059 ----------
1055 ----------
1060
1056
1061 shell
1057 shell
1062 a pointer to the ipython shell itself. This is needed
1058 a pointer to the ipython shell itself. This is needed
1063 because this completer knows about magic functions, and those can
1059 because this completer knows about magic functions, and those can
1064 only be accessed via the ipython instance.
1060 only be accessed via the ipython instance.
1065
1061
1066 namespace : dict, optional
1062 namespace : dict, optional
1067 an optional dict where completions are performed.
1063 an optional dict where completions are performed.
1068
1064
1069 global_namespace : dict, optional
1065 global_namespace : dict, optional
1070 secondary optional dict for completions, to
1066 secondary optional dict for completions, to
1071 handle cases (such as IPython embedded inside functions) where
1067 handle cases (such as IPython embedded inside functions) where
1072 both Python scopes are visible.
1068 both Python scopes are visible.
1073
1069
1074 use_readline : bool, optional
1070 use_readline : bool, optional
1075 DEPRECATED, ignored since IPython 6.0, will have no effects
1071 DEPRECATED, ignored since IPython 6.0, will have no effects
1076 """
1072 """
1077
1073
1078 self.magic_escape = ESC_MAGIC
1074 self.magic_escape = ESC_MAGIC
1079 self.splitter = CompletionSplitter()
1075 self.splitter = CompletionSplitter()
1080
1076
1081 if use_readline is not _deprecation_readline_sentinel:
1077 if use_readline is not _deprecation_readline_sentinel:
1082 warnings.warn('The `use_readline` parameter is deprecated and ignored since IPython 6.0.',
1078 warnings.warn('The `use_readline` parameter is deprecated and ignored since IPython 6.0.',
1083 DeprecationWarning, stacklevel=2)
1079 DeprecationWarning, stacklevel=2)
1084
1080
1085 # _greedy_changed() depends on splitter and readline being defined:
1081 # _greedy_changed() depends on splitter and readline being defined:
1086 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
1082 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
1087 config=config, **kwargs)
1083 config=config, **kwargs)
1088
1084
1089 # List where completion matches will be stored
1085 # List where completion matches will be stored
1090 self.matches = []
1086 self.matches = []
1091 self.shell = shell
1087 self.shell = shell
1092 # Regexp to split filenames with spaces in them
1088 # Regexp to split filenames with spaces in them
1093 self.space_name_re = re.compile(r'([^\\] )')
1089 self.space_name_re = re.compile(r'([^\\] )')
1094 # Hold a local ref. to glob.glob for speed
1090 # Hold a local ref. to glob.glob for speed
1095 self.glob = glob.glob
1091 self.glob = glob.glob
1096
1092
1097 # Determine if we are running on 'dumb' terminals, like (X)Emacs
1093 # Determine if we are running on 'dumb' terminals, like (X)Emacs
1098 # buffers, to avoid completion problems.
1094 # buffers, to avoid completion problems.
1099 term = os.environ.get('TERM','xterm')
1095 term = os.environ.get('TERM','xterm')
1100 self.dumb_terminal = term in ['dumb','emacs']
1096 self.dumb_terminal = term in ['dumb','emacs']
1101
1097
1102 # Special handling of backslashes needed in win32 platforms
1098 # Special handling of backslashes needed in win32 platforms
1103 if sys.platform == "win32":
1099 if sys.platform == "win32":
1104 self.clean_glob = self._clean_glob_win32
1100 self.clean_glob = self._clean_glob_win32
1105 else:
1101 else:
1106 self.clean_glob = self._clean_glob
1102 self.clean_glob = self._clean_glob
1107
1103
1108 #regexp to parse docstring for function signature
1104 #regexp to parse docstring for function signature
1109 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
1105 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
1110 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
1106 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
1111 #use this if positional argument name is also needed
1107 #use this if positional argument name is also needed
1112 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
1108 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
1113
1109
1114 self.magic_arg_matchers = [
1110 self.magic_arg_matchers = [
1115 self.magic_config_matches,
1111 self.magic_config_matches,
1116 self.magic_color_matches,
1112 self.magic_color_matches,
1117 ]
1113 ]
1118
1114
1119 # This is set externally by InteractiveShell
1115 # This is set externally by InteractiveShell
1120 self.custom_completers = None
1116 self.custom_completers = None
1121
1117
1122 @property
1118 @property
1123 def matchers(self):
1119 def matchers(self):
1124 """All active matcher routines for completion"""
1120 """All active matcher routines for completion"""
1125 if self.dict_keys_only:
1121 if self.dict_keys_only:
1126 return [self.dict_key_matches]
1122 return [self.dict_key_matches]
1127
1123
1128 if self.use_jedi:
1124 if self.use_jedi:
1129 return [
1125 return [
1130 self.file_matches,
1126 self.file_matches,
1131 self.magic_matches,
1127 self.magic_matches,
1132 self.dict_key_matches,
1128 self.dict_key_matches,
1133 ]
1129 ]
1134 else:
1130 else:
1135 return [
1131 return [
1136 self.python_matches,
1132 self.python_matches,
1137 self.file_matches,
1133 self.file_matches,
1138 self.magic_matches,
1134 self.magic_matches,
1139 self.python_func_kw_matches,
1135 self.python_func_kw_matches,
1140 self.dict_key_matches,
1136 self.dict_key_matches,
1141 ]
1137 ]
1142
1138
1143 def all_completions(self, text) -> List[str]:
1139 def all_completions(self, text) -> List[str]:
1144 """
1140 """
1145 Wrapper around the completions method for the benefit of emacs.
1141 Wrapper around the completions method for the benefit of emacs.
1146 """
1142 """
1147 prefix = text[:text.rfind(".") + 1]
1143 prefix = text[:text.rfind(".") + 1]
1148 with provisionalcompleter():
1144 with provisionalcompleter():
1149 return list(map(lambda c: prefix + c.text,
1145 return list(map(lambda c: prefix + c.text,
1150 self.completions(text, len(text))))
1146 self.completions(text, len(text))))
1151
1147
1152 return self.complete(text)[1]
1148 return self.complete(text)[1]
1153
1149
1154 def _clean_glob(self, text):
1150 def _clean_glob(self, text):
1155 return self.glob("%s*" % text)
1151 return self.glob("%s*" % text)
1156
1152
1157 def _clean_glob_win32(self,text):
1153 def _clean_glob_win32(self,text):
1158 return [f.replace("\\","/")
1154 return [f.replace("\\","/")
1159 for f in self.glob("%s*" % text)]
1155 for f in self.glob("%s*" % text)]
1160
1156
1161 def file_matches(self, text):
1157 def file_matches(self, text):
1162 """Match filenames, expanding ~USER type strings.
1158 """Match filenames, expanding ~USER type strings.
1163
1159
1164 Most of the seemingly convoluted logic in this completer is an
1160 Most of the seemingly convoluted logic in this completer is an
1165 attempt to handle filenames with spaces in them. And yet it's not
1161 attempt to handle filenames with spaces in them. And yet it's not
1166 quite perfect, because Python's readline doesn't expose all of the
1162 quite perfect, because Python's readline doesn't expose all of the
1167 GNU readline details needed for this to be done correctly.
1163 GNU readline details needed for this to be done correctly.
1168
1164
1169 For a filename with a space in it, the printed completions will be
1165 For a filename with a space in it, the printed completions will be
1170 only the parts after what's already been typed (instead of the
1166 only the parts after what's already been typed (instead of the
1171 full completions, as is normally done). I don't think with the
1167 full completions, as is normally done). I don't think with the
1172 current (as of Python 2.3) Python readline it's possible to do
1168 current (as of Python 2.3) Python readline it's possible to do
1173 better."""
1169 better."""
1174
1170
1175 # chars that require escaping with backslash - i.e. chars
1171 # chars that require escaping with backslash - i.e. chars
1176 # that readline treats incorrectly as delimiters, but we
1172 # that readline treats incorrectly as delimiters, but we
1177 # don't want to treat as delimiters in filename matching
1173 # don't want to treat as delimiters in filename matching
1178 # when escaped with backslash
1174 # when escaped with backslash
1179 if text.startswith('!'):
1175 if text.startswith('!'):
1180 text = text[1:]
1176 text = text[1:]
1181 text_prefix = u'!'
1177 text_prefix = u'!'
1182 else:
1178 else:
1183 text_prefix = u''
1179 text_prefix = u''
1184
1180
1185 text_until_cursor = self.text_until_cursor
1181 text_until_cursor = self.text_until_cursor
1186 # track strings with open quotes
1182 # track strings with open quotes
1187 open_quotes = has_open_quotes(text_until_cursor)
1183 open_quotes = has_open_quotes(text_until_cursor)
1188
1184
1189 if '(' in text_until_cursor or '[' in text_until_cursor:
1185 if '(' in text_until_cursor or '[' in text_until_cursor:
1190 lsplit = text
1186 lsplit = text
1191 else:
1187 else:
1192 try:
1188 try:
1193 # arg_split ~ shlex.split, but with unicode bugs fixed by us
1189 # arg_split ~ shlex.split, but with unicode bugs fixed by us
1194 lsplit = arg_split(text_until_cursor)[-1]
1190 lsplit = arg_split(text_until_cursor)[-1]
1195 except ValueError:
1191 except ValueError:
1196 # typically an unmatched ", or backslash without escaped char.
1192 # typically an unmatched ", or backslash without escaped char.
1197 if open_quotes:
1193 if open_quotes:
1198 lsplit = text_until_cursor.split(open_quotes)[-1]
1194 lsplit = text_until_cursor.split(open_quotes)[-1]
1199 else:
1195 else:
1200 return []
1196 return []
1201 except IndexError:
1197 except IndexError:
1202 # tab pressed on empty line
1198 # tab pressed on empty line
1203 lsplit = ""
1199 lsplit = ""
1204
1200
1205 if not open_quotes and lsplit != protect_filename(lsplit):
1201 if not open_quotes and lsplit != protect_filename(lsplit):
1206 # if protectables are found, do matching on the whole escaped name
1202 # if protectables are found, do matching on the whole escaped name
1207 has_protectables = True
1203 has_protectables = True
1208 text0,text = text,lsplit
1204 text0,text = text,lsplit
1209 else:
1205 else:
1210 has_protectables = False
1206 has_protectables = False
1211 text = os.path.expanduser(text)
1207 text = os.path.expanduser(text)
1212
1208
1213 if text == "":
1209 if text == "":
1214 return [text_prefix + protect_filename(f) for f in self.glob("*")]
1210 return [text_prefix + protect_filename(f) for f in self.glob("*")]
1215
1211
1216 # Compute the matches from the filesystem
1212 # Compute the matches from the filesystem
1217 if sys.platform == 'win32':
1213 if sys.platform == 'win32':
1218 m0 = self.clean_glob(text)
1214 m0 = self.clean_glob(text)
1219 else:
1215 else:
1220 m0 = self.clean_glob(text.replace('\\', ''))
1216 m0 = self.clean_glob(text.replace('\\', ''))
1221
1217
1222 if has_protectables:
1218 if has_protectables:
1223 # If we had protectables, we need to revert our changes to the
1219 # If we had protectables, we need to revert our changes to the
1224 # beginning of filename so that we don't double-write the part
1220 # beginning of filename so that we don't double-write the part
1225 # of the filename we have so far
1221 # of the filename we have so far
1226 len_lsplit = len(lsplit)
1222 len_lsplit = len(lsplit)
1227 matches = [text_prefix + text0 +
1223 matches = [text_prefix + text0 +
1228 protect_filename(f[len_lsplit:]) for f in m0]
1224 protect_filename(f[len_lsplit:]) for f in m0]
1229 else:
1225 else:
1230 if open_quotes:
1226 if open_quotes:
1231 # if we have a string with an open quote, we don't need to
1227 # if we have a string with an open quote, we don't need to
1232 # protect the names beyond the quote (and we _shouldn't_, as
1228 # protect the names beyond the quote (and we _shouldn't_, as
1233 # it would cause bugs when the filesystem call is made).
1229 # it would cause bugs when the filesystem call is made).
1234 matches = m0 if sys.platform == "win32" else\
1230 matches = m0 if sys.platform == "win32" else\
1235 [protect_filename(f, open_quotes) for f in m0]
1231 [protect_filename(f, open_quotes) for f in m0]
1236 else:
1232 else:
1237 matches = [text_prefix +
1233 matches = [text_prefix +
1238 protect_filename(f) for f in m0]
1234 protect_filename(f) for f in m0]
1239
1235
1240 # Mark directories in input list by appending '/' to their names.
1236 # Mark directories in input list by appending '/' to their names.
1241 return [x+'/' if os.path.isdir(x) else x for x in matches]
1237 return [x+'/' if os.path.isdir(x) else x for x in matches]
1242
1238
1243 def magic_matches(self, text):
1239 def magic_matches(self, text):
1244 """Match magics"""
1240 """Match magics"""
1245 # Get all shell magics now rather than statically, so magics loaded at
1241 # Get all shell magics now rather than statically, so magics loaded at
1246 # runtime show up too.
1242 # runtime show up too.
1247 lsm = self.shell.magics_manager.lsmagic()
1243 lsm = self.shell.magics_manager.lsmagic()
1248 line_magics = lsm['line']
1244 line_magics = lsm['line']
1249 cell_magics = lsm['cell']
1245 cell_magics = lsm['cell']
1250 pre = self.magic_escape
1246 pre = self.magic_escape
1251 pre2 = pre+pre
1247 pre2 = pre+pre
1252
1248
1253 explicit_magic = text.startswith(pre)
1249 explicit_magic = text.startswith(pre)
1254
1250
1255 # Completion logic:
1251 # Completion logic:
1256 # - user gives %%: only do cell magics
1252 # - user gives %%: only do cell magics
1257 # - user gives %: do both line and cell magics
1253 # - user gives %: do both line and cell magics
1258 # - no prefix: do both
1254 # - no prefix: do both
1259 # In other words, line magics are skipped if the user gives %% explicitly
1255 # In other words, line magics are skipped if the user gives %% explicitly
1260 #
1256 #
1261 # We also exclude magics that match any currently visible names:
1257 # We also exclude magics that match any currently visible names:
1262 # https://github.com/ipython/ipython/issues/4877, unless the user has
1258 # https://github.com/ipython/ipython/issues/4877, unless the user has
1263 # typed a %:
1259 # typed a %:
1264 # https://github.com/ipython/ipython/issues/10754
1260 # https://github.com/ipython/ipython/issues/10754
1265 bare_text = text.lstrip(pre)
1261 bare_text = text.lstrip(pre)
1266 global_matches = self.global_matches(bare_text)
1262 global_matches = self.global_matches(bare_text)
1267 if not explicit_magic:
1263 if not explicit_magic:
1268 def matches(magic):
1264 def matches(magic):
1269 """
1265 """
1270 Filter magics, in particular remove magics that match
1266 Filter magics, in particular remove magics that match
1271 a name present in global namespace.
1267 a name present in global namespace.
1272 """
1268 """
1273 return ( magic.startswith(bare_text) and
1269 return ( magic.startswith(bare_text) and
1274 magic not in global_matches )
1270 magic not in global_matches )
1275 else:
1271 else:
1276 def matches(magic):
1272 def matches(magic):
1277 return magic.startswith(bare_text)
1273 return magic.startswith(bare_text)
1278
1274
1279 comp = [ pre2+m for m in cell_magics if matches(m)]
1275 comp = [ pre2+m for m in cell_magics if matches(m)]
1280 if not text.startswith(pre2):
1276 if not text.startswith(pre2):
1281 comp += [ pre+m for m in line_magics if matches(m)]
1277 comp += [ pre+m for m in line_magics if matches(m)]
1282
1278
1283 return comp
1279 return comp
1284
1280
1285 def magic_config_matches(self, text:str) -> List[str]:
1281 def magic_config_matches(self, text:str) -> List[str]:
1286 """ Match class names and attributes for %config magic """
1282 """ Match class names and attributes for %config magic """
1287 texts = text.strip().split()
1283 texts = text.strip().split()
1288
1284
1289 if len(texts) > 0 and (texts[0] == 'config' or texts[0] == '%config'):
1285 if len(texts) > 0 and (texts[0] == 'config' or texts[0] == '%config'):
1290 # get all configuration classes
1286 # get all configuration classes
1291 classes = sorted(set([ c for c in self.shell.configurables
1287 classes = sorted(set([ c for c in self.shell.configurables
1292 if c.__class__.class_traits(config=True)
1288 if c.__class__.class_traits(config=True)
1293 ]), key=lambda x: x.__class__.__name__)
1289 ]), key=lambda x: x.__class__.__name__)
1294 classnames = [ c.__class__.__name__ for c in classes ]
1290 classnames = [ c.__class__.__name__ for c in classes ]
1295
1291
1296 # return all classnames if config or %config is given
1292 # return all classnames if config or %config is given
1297 if len(texts) == 1:
1293 if len(texts) == 1:
1298 return classnames
1294 return classnames
1299
1295
1300 # match classname
1296 # match classname
1301 classname_texts = texts[1].split('.')
1297 classname_texts = texts[1].split('.')
1302 classname = classname_texts[0]
1298 classname = classname_texts[0]
1303 classname_matches = [ c for c in classnames
1299 classname_matches = [ c for c in classnames
1304 if c.startswith(classname) ]
1300 if c.startswith(classname) ]
1305
1301
1306 # return matched classes or the matched class with attributes
1302 # return matched classes or the matched class with attributes
1307 if texts[1].find('.') < 0:
1303 if texts[1].find('.') < 0:
1308 return classname_matches
1304 return classname_matches
1309 elif len(classname_matches) == 1 and \
1305 elif len(classname_matches) == 1 and \
1310 classname_matches[0] == classname:
1306 classname_matches[0] == classname:
1311 cls = classes[classnames.index(classname)].__class__
1307 cls = classes[classnames.index(classname)].__class__
1312 help = cls.class_get_help()
1308 help = cls.class_get_help()
1313 # strip leading '--' from cl-args:
1309 # strip leading '--' from cl-args:
1314 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
1310 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
1315 return [ attr.split('=')[0]
1311 return [ attr.split('=')[0]
1316 for attr in help.strip().splitlines()
1312 for attr in help.strip().splitlines()
1317 if attr.startswith(texts[1]) ]
1313 if attr.startswith(texts[1]) ]
1318 return []
1314 return []
1319
1315
1320 def magic_color_matches(self, text:str) -> List[str] :
1316 def magic_color_matches(self, text:str) -> List[str] :
1321 """ Match color schemes for %colors magic"""
1317 """ Match color schemes for %colors magic"""
1322 texts = text.split()
1318 texts = text.split()
1323 if text.endswith(' '):
1319 if text.endswith(' '):
1324 # .split() strips off the trailing whitespace. Add '' back
1320 # .split() strips off the trailing whitespace. Add '' back
1325 # so that: '%colors ' -> ['%colors', '']
1321 # so that: '%colors ' -> ['%colors', '']
1326 texts.append('')
1322 texts.append('')
1327
1323
1328 if len(texts) == 2 and (texts[0] == 'colors' or texts[0] == '%colors'):
1324 if len(texts) == 2 and (texts[0] == 'colors' or texts[0] == '%colors'):
1329 prefix = texts[1]
1325 prefix = texts[1]
1330 return [ color for color in InspectColors.keys()
1326 return [ color for color in InspectColors.keys()
1331 if color.startswith(prefix) ]
1327 if color.startswith(prefix) ]
1332 return []
1328 return []
1333
1329
1334 def _jedi_matches(self, cursor_column:int, cursor_line:int, text:str):
1330 def _jedi_matches(self, cursor_column:int, cursor_line:int, text:str):
1335 """
1331 """
1336
1332
1337 Return a list of :any:`jedi.api.Completions` object from a ``text`` and
1333 Return a list of :any:`jedi.api.Completions` object from a ``text`` and
1338 cursor position.
1334 cursor position.
1339
1335
1340 Parameters
1336 Parameters
1341 ----------
1337 ----------
1342 cursor_column : int
1338 cursor_column : int
1343 column position of the cursor in ``text``, 0-indexed.
1339 column position of the cursor in ``text``, 0-indexed.
1344 cursor_line : int
1340 cursor_line : int
1345 line position of the cursor in ``text``, 0-indexed
1341 line position of the cursor in ``text``, 0-indexed
1346 text : str
1342 text : str
1347 text to complete
1343 text to complete
1348
1344
1349 Debugging
1345 Debugging
1350 ---------
1346 ---------
1351
1347
1352 If ``IPCompleter.debug`` is ``True`` may return a :any:`_FakeJediCompletion`
1348 If ``IPCompleter.debug`` is ``True`` may return a :any:`_FakeJediCompletion`
1353 object containing a string with the Jedi debug information attached.
1349 object containing a string with the Jedi debug information attached.
1354 """
1350 """
1355 namespaces = [self.namespace]
1351 namespaces = [self.namespace]
1356 if self.global_namespace is not None:
1352 if self.global_namespace is not None:
1357 namespaces.append(self.global_namespace)
1353 namespaces.append(self.global_namespace)
1358
1354
1359 completion_filter = lambda x:x
1355 completion_filter = lambda x:x
1360 offset = cursor_to_position(text, cursor_line, cursor_column)
1356 offset = cursor_to_position(text, cursor_line, cursor_column)
1361 # filter output if we are completing for object members
1357 # filter output if we are completing for object members
1362 if offset:
1358 if offset:
1363 pre = text[offset-1]
1359 pre = text[offset-1]
1364 if pre == '.':
1360 if pre == '.':
1365 if self.omit__names == 2:
1361 if self.omit__names == 2:
1366 completion_filter = lambda c:not c.name.startswith('_')
1362 completion_filter = lambda c:not c.name.startswith('_')
1367 elif self.omit__names == 1:
1363 elif self.omit__names == 1:
1368 completion_filter = lambda c:not (c.name.startswith('__') and c.name.endswith('__'))
1364 completion_filter = lambda c:not (c.name.startswith('__') and c.name.endswith('__'))
1369 elif self.omit__names == 0:
1365 elif self.omit__names == 0:
1370 completion_filter = lambda x:x
1366 completion_filter = lambda x:x
1371 else:
1367 else:
1372 raise ValueError("Don't understand self.omit__names == {}".format(self.omit__names))
1368 raise ValueError("Don't understand self.omit__names == {}".format(self.omit__names))
1373
1369
1374 interpreter = jedi.Interpreter(
1370 interpreter = jedi.Interpreter(
1375 text[:offset], namespaces, column=cursor_column, line=cursor_line + 1)
1371 text[:offset], namespaces, column=cursor_column, line=cursor_line + 1)
1376 try_jedi = True
1372 try_jedi = True
1377
1373
1378 try:
1374 try:
1379 # should we check the type of the node is Error ?
1375 # should we check the type of the node is Error ?
1380 try:
1376 try:
1381 # jedi < 0.11
1377 # jedi < 0.11
1382 from jedi.parser.tree import ErrorLeaf
1378 from jedi.parser.tree import ErrorLeaf
1383 except ImportError:
1379 except ImportError:
1384 # jedi >= 0.11
1380 # jedi >= 0.11
1385 from parso.tree import ErrorLeaf
1381 from parso.tree import ErrorLeaf
1386
1382
1387 next_to_last_tree = interpreter._get_module().tree_node.children[-2]
1383 next_to_last_tree = interpreter._get_module().tree_node.children[-2]
1388 completing_string = False
1384 completing_string = False
1389 if isinstance(next_to_last_tree, ErrorLeaf):
1385 if isinstance(next_to_last_tree, ErrorLeaf):
1390 completing_string = next_to_last_tree.value.lstrip()[0] in {'"', "'"}
1386 completing_string = next_to_last_tree.value.lstrip()[0] in {'"', "'"}
1391 # if we are in a string jedi is likely not the right candidate for
1387 # if we are in a string jedi is likely not the right candidate for
1392 # now. Skip it.
1388 # now. Skip it.
1393 try_jedi = not completing_string
1389 try_jedi = not completing_string
1394 except Exception as e:
1390 except Exception as e:
1395 # many of things can go wrong, we are using private API just don't crash.
1391 # many of things can go wrong, we are using private API just don't crash.
1396 if self.debug:
1392 if self.debug:
1397 print("Error detecting if completing a non-finished string :", e, '|')
1393 print("Error detecting if completing a non-finished string :", e, '|')
1398
1394
1399 if not try_jedi:
1395 if not try_jedi:
1400 return []
1396 return []
1401 try:
1397 try:
1402 return filter(completion_filter, interpreter.completions())
1398 return filter(completion_filter, interpreter.completions())
1403 except Exception as e:
1399 except Exception as e:
1404 if self.debug:
1400 if self.debug:
1405 return [_FakeJediCompletion('Oops Jedi has crashed, please report a bug with the following:\n"""\n%s\ns"""' % (e))]
1401 return [_FakeJediCompletion('Oops Jedi has crashed, please report a bug with the following:\n"""\n%s\ns"""' % (e))]
1406 else:
1402 else:
1407 return []
1403 return []
1408
1404
1409 def python_matches(self, text):
1405 def python_matches(self, text):
1410 """Match attributes or global python names"""
1406 """Match attributes or global python names"""
1411 if "." in text:
1407 if "." in text:
1412 try:
1408 try:
1413 matches = self.attr_matches(text)
1409 matches = self.attr_matches(text)
1414 if text.endswith('.') and self.omit__names:
1410 if text.endswith('.') and self.omit__names:
1415 if self.omit__names == 1:
1411 if self.omit__names == 1:
1416 # true if txt is _not_ a __ name, false otherwise:
1412 # true if txt is _not_ a __ name, false otherwise:
1417 no__name = (lambda txt:
1413 no__name = (lambda txt:
1418 re.match(r'.*\.__.*?__',txt) is None)
1414 re.match(r'.*\.__.*?__',txt) is None)
1419 else:
1415 else:
1420 # true if txt is _not_ a _ name, false otherwise:
1416 # true if txt is _not_ a _ name, false otherwise:
1421 no__name = (lambda txt:
1417 no__name = (lambda txt:
1422 re.match(r'\._.*?',txt[txt.rindex('.'):]) is None)
1418 re.match(r'\._.*?',txt[txt.rindex('.'):]) is None)
1423 matches = filter(no__name, matches)
1419 matches = filter(no__name, matches)
1424 except NameError:
1420 except NameError:
1425 # catches <undefined attributes>.<tab>
1421 # catches <undefined attributes>.<tab>
1426 matches = []
1422 matches = []
1427 else:
1423 else:
1428 matches = self.global_matches(text)
1424 matches = self.global_matches(text)
1429 return matches
1425 return matches
1430
1426
1431 def _default_arguments_from_docstring(self, doc):
1427 def _default_arguments_from_docstring(self, doc):
1432 """Parse the first line of docstring for call signature.
1428 """Parse the first line of docstring for call signature.
1433
1429
1434 Docstring should be of the form 'min(iterable[, key=func])\n'.
1430 Docstring should be of the form 'min(iterable[, key=func])\n'.
1435 It can also parse cython docstring of the form
1431 It can also parse cython docstring of the form
1436 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
1432 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
1437 """
1433 """
1438 if doc is None:
1434 if doc is None:
1439 return []
1435 return []
1440
1436
1441 #care only the firstline
1437 #care only the firstline
1442 line = doc.lstrip().splitlines()[0]
1438 line = doc.lstrip().splitlines()[0]
1443
1439
1444 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
1440 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
1445 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
1441 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
1446 sig = self.docstring_sig_re.search(line)
1442 sig = self.docstring_sig_re.search(line)
1447 if sig is None:
1443 if sig is None:
1448 return []
1444 return []
1449 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
1445 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
1450 sig = sig.groups()[0].split(',')
1446 sig = sig.groups()[0].split(',')
1451 ret = []
1447 ret = []
1452 for s in sig:
1448 for s in sig:
1453 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
1449 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
1454 ret += self.docstring_kwd_re.findall(s)
1450 ret += self.docstring_kwd_re.findall(s)
1455 return ret
1451 return ret
1456
1452
1457 def _default_arguments(self, obj):
1453 def _default_arguments(self, obj):
1458 """Return the list of default arguments of obj if it is callable,
1454 """Return the list of default arguments of obj if it is callable,
1459 or empty list otherwise."""
1455 or empty list otherwise."""
1460 call_obj = obj
1456 call_obj = obj
1461 ret = []
1457 ret = []
1462 if inspect.isbuiltin(obj):
1458 if inspect.isbuiltin(obj):
1463 pass
1459 pass
1464 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
1460 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
1465 if inspect.isclass(obj):
1461 if inspect.isclass(obj):
1466 #for cython embedsignature=True the constructor docstring
1462 #for cython embedsignature=True the constructor docstring
1467 #belongs to the object itself not __init__
1463 #belongs to the object itself not __init__
1468 ret += self._default_arguments_from_docstring(
1464 ret += self._default_arguments_from_docstring(
1469 getattr(obj, '__doc__', ''))
1465 getattr(obj, '__doc__', ''))
1470 # for classes, check for __init__,__new__
1466 # for classes, check for __init__,__new__
1471 call_obj = (getattr(obj, '__init__', None) or
1467 call_obj = (getattr(obj, '__init__', None) or
1472 getattr(obj, '__new__', None))
1468 getattr(obj, '__new__', None))
1473 # for all others, check if they are __call__able
1469 # for all others, check if they are __call__able
1474 elif hasattr(obj, '__call__'):
1470 elif hasattr(obj, '__call__'):
1475 call_obj = obj.__call__
1471 call_obj = obj.__call__
1476 ret += self._default_arguments_from_docstring(
1472 ret += self._default_arguments_from_docstring(
1477 getattr(call_obj, '__doc__', ''))
1473 getattr(call_obj, '__doc__', ''))
1478
1474
1479 _keeps = (inspect.Parameter.KEYWORD_ONLY,
1475 _keeps = (inspect.Parameter.KEYWORD_ONLY,
1480 inspect.Parameter.POSITIONAL_OR_KEYWORD)
1476 inspect.Parameter.POSITIONAL_OR_KEYWORD)
1481
1477
1482 try:
1478 try:
1483 sig = inspect.signature(call_obj)
1479 sig = inspect.signature(call_obj)
1484 ret.extend(k for k, v in sig.parameters.items() if
1480 ret.extend(k for k, v in sig.parameters.items() if
1485 v.kind in _keeps)
1481 v.kind in _keeps)
1486 except ValueError:
1482 except ValueError:
1487 pass
1483 pass
1488
1484
1489 return list(set(ret))
1485 return list(set(ret))
1490
1486
1491 def python_func_kw_matches(self,text):
1487 def python_func_kw_matches(self,text):
1492 """Match named parameters (kwargs) of the last open function"""
1488 """Match named parameters (kwargs) of the last open function"""
1493
1489
1494 if "." in text: # a parameter cannot be dotted
1490 if "." in text: # a parameter cannot be dotted
1495 return []
1491 return []
1496 try: regexp = self.__funcParamsRegex
1492 try: regexp = self.__funcParamsRegex
1497 except AttributeError:
1493 except AttributeError:
1498 regexp = self.__funcParamsRegex = re.compile(r'''
1494 regexp = self.__funcParamsRegex = re.compile(r'''
1499 '.*?(?<!\\)' | # single quoted strings or
1495 '.*?(?<!\\)' | # single quoted strings or
1500 ".*?(?<!\\)" | # double quoted strings or
1496 ".*?(?<!\\)" | # double quoted strings or
1501 \w+ | # identifier
1497 \w+ | # identifier
1502 \S # other characters
1498 \S # other characters
1503 ''', re.VERBOSE | re.DOTALL)
1499 ''', re.VERBOSE | re.DOTALL)
1504 # 1. find the nearest identifier that comes before an unclosed
1500 # 1. find the nearest identifier that comes before an unclosed
1505 # parenthesis before the cursor
1501 # parenthesis before the cursor
1506 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
1502 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
1507 tokens = regexp.findall(self.text_until_cursor)
1503 tokens = regexp.findall(self.text_until_cursor)
1508 iterTokens = reversed(tokens); openPar = 0
1504 iterTokens = reversed(tokens); openPar = 0
1509
1505
1510 for token in iterTokens:
1506 for token in iterTokens:
1511 if token == ')':
1507 if token == ')':
1512 openPar -= 1
1508 openPar -= 1
1513 elif token == '(':
1509 elif token == '(':
1514 openPar += 1
1510 openPar += 1
1515 if openPar > 0:
1511 if openPar > 0:
1516 # found the last unclosed parenthesis
1512 # found the last unclosed parenthesis
1517 break
1513 break
1518 else:
1514 else:
1519 return []
1515 return []
1520 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
1516 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
1521 ids = []
1517 ids = []
1522 isId = re.compile(r'\w+$').match
1518 isId = re.compile(r'\w+$').match
1523
1519
1524 while True:
1520 while True:
1525 try:
1521 try:
1526 ids.append(next(iterTokens))
1522 ids.append(next(iterTokens))
1527 if not isId(ids[-1]):
1523 if not isId(ids[-1]):
1528 ids.pop(); break
1524 ids.pop(); break
1529 if not next(iterTokens) == '.':
1525 if not next(iterTokens) == '.':
1530 break
1526 break
1531 except StopIteration:
1527 except StopIteration:
1532 break
1528 break
1533
1529
1534 # Find all named arguments already assigned to, as to avoid suggesting
1530 # Find all named arguments already assigned to, as to avoid suggesting
1535 # them again
1531 # them again
1536 usedNamedArgs = set()
1532 usedNamedArgs = set()
1537 par_level = -1
1533 par_level = -1
1538 for token, next_token in zip(tokens, tokens[1:]):
1534 for token, next_token in zip(tokens, tokens[1:]):
1539 if token == '(':
1535 if token == '(':
1540 par_level += 1
1536 par_level += 1
1541 elif token == ')':
1537 elif token == ')':
1542 par_level -= 1
1538 par_level -= 1
1543
1539
1544 if par_level != 0:
1540 if par_level != 0:
1545 continue
1541 continue
1546
1542
1547 if next_token != '=':
1543 if next_token != '=':
1548 continue
1544 continue
1549
1545
1550 usedNamedArgs.add(token)
1546 usedNamedArgs.add(token)
1551
1547
1552 argMatches = []
1548 argMatches = []
1553 try:
1549 try:
1554 callableObj = '.'.join(ids[::-1])
1550 callableObj = '.'.join(ids[::-1])
1555 namedArgs = self._default_arguments(eval(callableObj,
1551 namedArgs = self._default_arguments(eval(callableObj,
1556 self.namespace))
1552 self.namespace))
1557
1553
1558 # Remove used named arguments from the list, no need to show twice
1554 # Remove used named arguments from the list, no need to show twice
1559 for namedArg in set(namedArgs) - usedNamedArgs:
1555 for namedArg in set(namedArgs) - usedNamedArgs:
1560 if namedArg.startswith(text):
1556 if namedArg.startswith(text):
1561 argMatches.append(u"%s=" %namedArg)
1557 argMatches.append(u"%s=" %namedArg)
1562 except:
1558 except:
1563 pass
1559 pass
1564
1560
1565 return argMatches
1561 return argMatches
1566
1562
1567 def dict_key_matches(self, text):
1563 def dict_key_matches(self, text):
1568 "Match string keys in a dictionary, after e.g. 'foo[' "
1564 "Match string keys in a dictionary, after e.g. 'foo[' "
1569 def get_keys(obj):
1565 def get_keys(obj):
1570 # Objects can define their own completions by defining an
1566 # Objects can define their own completions by defining an
1571 # _ipy_key_completions_() method.
1567 # _ipy_key_completions_() method.
1572 method = get_real_method(obj, '_ipython_key_completions_')
1568 method = get_real_method(obj, '_ipython_key_completions_')
1573 if method is not None:
1569 if method is not None:
1574 return method()
1570 return method()
1575
1571
1576 # Special case some common in-memory dict-like types
1572 # Special case some common in-memory dict-like types
1577 if isinstance(obj, dict) or\
1573 if isinstance(obj, dict) or\
1578 _safe_isinstance(obj, 'pandas', 'DataFrame'):
1574 _safe_isinstance(obj, 'pandas', 'DataFrame'):
1579 try:
1575 try:
1580 return list(obj.keys())
1576 return list(obj.keys())
1581 except Exception:
1577 except Exception:
1582 return []
1578 return []
1583 elif _safe_isinstance(obj, 'numpy', 'ndarray') or\
1579 elif _safe_isinstance(obj, 'numpy', 'ndarray') or\
1584 _safe_isinstance(obj, 'numpy', 'void'):
1580 _safe_isinstance(obj, 'numpy', 'void'):
1585 return obj.dtype.names or []
1581 return obj.dtype.names or []
1586 return []
1582 return []
1587
1583
1588 try:
1584 try:
1589 regexps = self.__dict_key_regexps
1585 regexps = self.__dict_key_regexps
1590 except AttributeError:
1586 except AttributeError:
1591 dict_key_re_fmt = r'''(?x)
1587 dict_key_re_fmt = r'''(?x)
1592 ( # match dict-referring expression wrt greedy setting
1588 ( # match dict-referring expression wrt greedy setting
1593 %s
1589 %s
1594 )
1590 )
1595 \[ # open bracket
1591 \[ # open bracket
1596 \s* # and optional whitespace
1592 \s* # and optional whitespace
1597 ([uUbB]? # string prefix (r not handled)
1593 ([uUbB]? # string prefix (r not handled)
1598 (?: # unclosed string
1594 (?: # unclosed string
1599 '(?:[^']|(?<!\\)\\')*
1595 '(?:[^']|(?<!\\)\\')*
1600 |
1596 |
1601 "(?:[^"]|(?<!\\)\\")*
1597 "(?:[^"]|(?<!\\)\\")*
1602 )
1598 )
1603 )?
1599 )?
1604 $
1600 $
1605 '''
1601 '''
1606 regexps = self.__dict_key_regexps = {
1602 regexps = self.__dict_key_regexps = {
1607 False: re.compile(dict_key_re_fmt % r'''
1603 False: re.compile(dict_key_re_fmt % r'''
1608 # identifiers separated by .
1604 # identifiers separated by .
1609 (?!\d)\w+
1605 (?!\d)\w+
1610 (?:\.(?!\d)\w+)*
1606 (?:\.(?!\d)\w+)*
1611 '''),
1607 '''),
1612 True: re.compile(dict_key_re_fmt % '''
1608 True: re.compile(dict_key_re_fmt % '''
1613 .+
1609 .+
1614 ''')
1610 ''')
1615 }
1611 }
1616
1612
1617 match = regexps[self.greedy].search(self.text_until_cursor)
1613 match = regexps[self.greedy].search(self.text_until_cursor)
1618 if match is None:
1614 if match is None:
1619 return []
1615 return []
1620
1616
1621 expr, prefix = match.groups()
1617 expr, prefix = match.groups()
1622 try:
1618 try:
1623 obj = eval(expr, self.namespace)
1619 obj = eval(expr, self.namespace)
1624 except Exception:
1620 except Exception:
1625 try:
1621 try:
1626 obj = eval(expr, self.global_namespace)
1622 obj = eval(expr, self.global_namespace)
1627 except Exception:
1623 except Exception:
1628 return []
1624 return []
1629
1625
1630 keys = get_keys(obj)
1626 keys = get_keys(obj)
1631 if not keys:
1627 if not keys:
1632 return keys
1628 return keys
1633 closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims)
1629 closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims)
1634 if not matches:
1630 if not matches:
1635 return matches
1631 return matches
1636
1632
1637 # get the cursor position of
1633 # get the cursor position of
1638 # - the text being completed
1634 # - the text being completed
1639 # - the start of the key text
1635 # - the start of the key text
1640 # - the start of the completion
1636 # - the start of the completion
1641 text_start = len(self.text_until_cursor) - len(text)
1637 text_start = len(self.text_until_cursor) - len(text)
1642 if prefix:
1638 if prefix:
1643 key_start = match.start(2)
1639 key_start = match.start(2)
1644 completion_start = key_start + token_offset
1640 completion_start = key_start + token_offset
1645 else:
1641 else:
1646 key_start = completion_start = match.end()
1642 key_start = completion_start = match.end()
1647
1643
1648 # grab the leading prefix, to make sure all completions start with `text`
1644 # grab the leading prefix, to make sure all completions start with `text`
1649 if text_start > key_start:
1645 if text_start > key_start:
1650 leading = ''
1646 leading = ''
1651 else:
1647 else:
1652 leading = text[text_start:completion_start]
1648 leading = text[text_start:completion_start]
1653
1649
1654 # the index of the `[` character
1650 # the index of the `[` character
1655 bracket_idx = match.end(1)
1651 bracket_idx = match.end(1)
1656
1652
1657 # append closing quote and bracket as appropriate
1653 # append closing quote and bracket as appropriate
1658 # this is *not* appropriate if the opening quote or bracket is outside
1654 # this is *not* appropriate if the opening quote or bracket is outside
1659 # the text given to this method
1655 # the text given to this method
1660 suf = ''
1656 suf = ''
1661 continuation = self.line_buffer[len(self.text_until_cursor):]
1657 continuation = self.line_buffer[len(self.text_until_cursor):]
1662 if key_start > text_start and closing_quote:
1658 if key_start > text_start and closing_quote:
1663 # quotes were opened inside text, maybe close them
1659 # quotes were opened inside text, maybe close them
1664 if continuation.startswith(closing_quote):
1660 if continuation.startswith(closing_quote):
1665 continuation = continuation[len(closing_quote):]
1661 continuation = continuation[len(closing_quote):]
1666 else:
1662 else:
1667 suf += closing_quote
1663 suf += closing_quote
1668 if bracket_idx > text_start:
1664 if bracket_idx > text_start:
1669 # brackets were opened inside text, maybe close them
1665 # brackets were opened inside text, maybe close them
1670 if not continuation.startswith(']'):
1666 if not continuation.startswith(']'):
1671 suf += ']'
1667 suf += ']'
1672
1668
1673 return [leading + k + suf for k in matches]
1669 return [leading + k + suf for k in matches]
1674
1670
1675 def unicode_name_matches(self, text):
1671 def unicode_name_matches(self, text):
1676 u"""Match Latex-like syntax for unicode characters base
1672 u"""Match Latex-like syntax for unicode characters base
1677 on the name of the character.
1673 on the name of the character.
1678
1674
1679 This does ``\\GREEK SMALL LETTER ETA`` -> ``Ξ·``
1675 This does ``\\GREEK SMALL LETTER ETA`` -> ``Ξ·``
1680
1676
1681 Works only on valid python 3 identifier, or on combining characters that
1677 Works only on valid python 3 identifier, or on combining characters that
1682 will combine to form a valid identifier.
1678 will combine to form a valid identifier.
1683
1679
1684 Used on Python 3 only.
1680 Used on Python 3 only.
1685 """
1681 """
1686 slashpos = text.rfind('\\')
1682 slashpos = text.rfind('\\')
1687 if slashpos > -1:
1683 if slashpos > -1:
1688 s = text[slashpos+1:]
1684 s = text[slashpos+1:]
1689 try :
1685 try :
1690 unic = unicodedata.lookup(s)
1686 unic = unicodedata.lookup(s)
1691 # allow combining chars
1687 # allow combining chars
1692 if ('a'+unic).isidentifier():
1688 if ('a'+unic).isidentifier():
1693 return '\\'+s,[unic]
1689 return '\\'+s,[unic]
1694 except KeyError:
1690 except KeyError:
1695 pass
1691 pass
1696 return u'', []
1692 return u'', []
1697
1693
1698
1694
1699 def latex_matches(self, text):
1695 def latex_matches(self, text):
1700 u"""Match Latex syntax for unicode characters.
1696 u"""Match Latex syntax for unicode characters.
1701
1697
1702 This does both ``\\alp`` -> ``\\alpha`` and ``\\alpha`` -> ``Ξ±``
1698 This does both ``\\alp`` -> ``\\alpha`` and ``\\alpha`` -> ``Ξ±``
1703
1699
1704 Used on Python 3 only.
1700 Used on Python 3 only.
1705 """
1701 """
1706 slashpos = text.rfind('\\')
1702 slashpos = text.rfind('\\')
1707 if slashpos > -1:
1703 if slashpos > -1:
1708 s = text[slashpos:]
1704 s = text[slashpos:]
1709 if s in latex_symbols:
1705 if s in latex_symbols:
1710 # Try to complete a full latex symbol to unicode
1706 # Try to complete a full latex symbol to unicode
1711 # \\alpha -> Ξ±
1707 # \\alpha -> Ξ±
1712 return s, [latex_symbols[s]]
1708 return s, [latex_symbols[s]]
1713 else:
1709 else:
1714 # If a user has partially typed a latex symbol, give them
1710 # If a user has partially typed a latex symbol, give them
1715 # a full list of options \al -> [\aleph, \alpha]
1711 # a full list of options \al -> [\aleph, \alpha]
1716 matches = [k for k in latex_symbols if k.startswith(s)]
1712 matches = [k for k in latex_symbols if k.startswith(s)]
1717 return s, matches
1713 return s, matches
1718 return u'', []
1714 return u'', []
1719
1715
1720 def dispatch_custom_completer(self, text):
1716 def dispatch_custom_completer(self, text):
1721 if not self.custom_completers:
1717 if not self.custom_completers:
1722 return
1718 return
1723
1719
1724 line = self.line_buffer
1720 line = self.line_buffer
1725 if not line.strip():
1721 if not line.strip():
1726 return None
1722 return None
1727
1723
1728 # Create a little structure to pass all the relevant information about
1724 # Create a little structure to pass all the relevant information about
1729 # the current completion to any custom completer.
1725 # the current completion to any custom completer.
1730 event = SimpleNamespace()
1726 event = SimpleNamespace()
1731 event.line = line
1727 event.line = line
1732 event.symbol = text
1728 event.symbol = text
1733 cmd = line.split(None,1)[0]
1729 cmd = line.split(None,1)[0]
1734 event.command = cmd
1730 event.command = cmd
1735 event.text_until_cursor = self.text_until_cursor
1731 event.text_until_cursor = self.text_until_cursor
1736
1732
1737 # for foo etc, try also to find completer for %foo
1733 # for foo etc, try also to find completer for %foo
1738 if not cmd.startswith(self.magic_escape):
1734 if not cmd.startswith(self.magic_escape):
1739 try_magic = self.custom_completers.s_matches(
1735 try_magic = self.custom_completers.s_matches(
1740 self.magic_escape + cmd)
1736 self.magic_escape + cmd)
1741 else:
1737 else:
1742 try_magic = []
1738 try_magic = []
1743
1739
1744 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1740 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1745 try_magic,
1741 try_magic,
1746 self.custom_completers.flat_matches(self.text_until_cursor)):
1742 self.custom_completers.flat_matches(self.text_until_cursor)):
1747 try:
1743 try:
1748 res = c(event)
1744 res = c(event)
1749 if res:
1745 if res:
1750 # first, try case sensitive match
1746 # first, try case sensitive match
1751 withcase = [r for r in res if r.startswith(text)]
1747 withcase = [r for r in res if r.startswith(text)]
1752 if withcase:
1748 if withcase:
1753 return withcase
1749 return withcase
1754 # if none, then case insensitive ones are ok too
1750 # if none, then case insensitive ones are ok too
1755 text_low = text.lower()
1751 text_low = text.lower()
1756 return [r for r in res if r.lower().startswith(text_low)]
1752 return [r for r in res if r.lower().startswith(text_low)]
1757 except TryNext:
1753 except TryNext:
1758 pass
1754 pass
1759 except KeyboardInterrupt:
1755 except KeyboardInterrupt:
1760 """
1756 """
1761 If custom completer take too long,
1757 If custom completer take too long,
1762 let keyboard interrupt abort and return nothing.
1758 let keyboard interrupt abort and return nothing.
1763 """
1759 """
1764 break
1760 break
1765
1761
1766 return None
1762 return None
1767
1763
1768 def completions(self, text: str, offset: int)->Iterator[Completion]:
1764 def completions(self, text: str, offset: int)->Iterator[Completion]:
1769 """
1765 """
1770 Returns an iterator over the possible completions
1766 Returns an iterator over the possible completions
1771
1767
1772 .. warning:: Unstable
1768 .. warning:: Unstable
1773
1769
1774 This function is unstable, API may change without warning.
1770 This function is unstable, API may change without warning.
1775 It will also raise unless use in proper context manager.
1771 It will also raise unless use in proper context manager.
1776
1772
1777 Parameters
1773 Parameters
1778 ----------
1774 ----------
1779
1775
1780 text:str
1776 text:str
1781 Full text of the current input, multi line string.
1777 Full text of the current input, multi line string.
1782 offset:int
1778 offset:int
1783 Integer representing the position of the cursor in ``text``. Offset
1779 Integer representing the position of the cursor in ``text``. Offset
1784 is 0-based indexed.
1780 is 0-based indexed.
1785
1781
1786 Yields
1782 Yields
1787 ------
1783 ------
1788 :any:`Completion` object
1784 :any:`Completion` object
1789
1785
1790
1786
1791 The cursor on a text can either be seen as being "in between"
1787 The cursor on a text can either be seen as being "in between"
1792 characters or "On" a character depending on the interface visible to
1788 characters or "On" a character depending on the interface visible to
1793 the user. For consistency the cursor being on "in between" characters X
1789 the user. For consistency the cursor being on "in between" characters X
1794 and Y is equivalent to the cursor being "on" character Y, that is to say
1790 and Y is equivalent to the cursor being "on" character Y, that is to say
1795 the character the cursor is on is considered as being after the cursor.
1791 the character the cursor is on is considered as being after the cursor.
1796
1792
1797 Combining characters may span more that one position in the
1793 Combining characters may span more that one position in the
1798 text.
1794 text.
1799
1795
1800
1796
1801 .. note::
1797 .. note::
1802
1798
1803 If ``IPCompleter.debug`` is :any:`True` will yield a ``--jedi/ipython--``
1799 If ``IPCompleter.debug`` is :any:`True` will yield a ``--jedi/ipython--``
1804 fake Completion token to distinguish completion returned by Jedi
1800 fake Completion token to distinguish completion returned by Jedi
1805 and usual IPython completion.
1801 and usual IPython completion.
1806
1802
1807 .. note::
1803 .. note::
1808
1804
1809 Completions are not completely deduplicated yet. If identical
1805 Completions are not completely deduplicated yet. If identical
1810 completions are coming from different sources this function does not
1806 completions are coming from different sources this function does not
1811 ensure that each completion object will only be present once.
1807 ensure that each completion object will only be present once.
1812 """
1808 """
1813 warnings.warn("_complete is a provisional API (as of IPython 6.0). "
1809 warnings.warn("_complete is a provisional API (as of IPython 6.0). "
1814 "It may change without warnings. "
1810 "It may change without warnings. "
1815 "Use in corresponding context manager.",
1811 "Use in corresponding context manager.",
1816 category=ProvisionalCompleterWarning, stacklevel=2)
1812 category=ProvisionalCompleterWarning, stacklevel=2)
1817
1813
1818 seen = set()
1814 seen = set()
1819 try:
1815 try:
1820 for c in self._completions(text, offset, _timeout=self.jedi_compute_type_timeout/1000):
1816 for c in self._completions(text, offset, _timeout=self.jedi_compute_type_timeout/1000):
1821 if c and (c in seen):
1817 if c and (c in seen):
1822 continue
1818 continue
1823 yield c
1819 yield c
1824 seen.add(c)
1820 seen.add(c)
1825 except KeyboardInterrupt:
1821 except KeyboardInterrupt:
1826 """if completions take too long and users send keyboard interrupt,
1822 """if completions take too long and users send keyboard interrupt,
1827 do not crash and return ASAP. """
1823 do not crash and return ASAP. """
1828 pass
1824 pass
1829
1825
1830 def _completions(self, full_text: str, offset: int, *, _timeout)->Iterator[Completion]:
1826 def _completions(self, full_text: str, offset: int, *, _timeout)->Iterator[Completion]:
1831 """
1827 """
1832 Core completion module.Same signature as :any:`completions`, with the
1828 Core completion module.Same signature as :any:`completions`, with the
1833 extra `timeout` parameter (in seconds).
1829 extra `timeout` parameter (in seconds).
1834
1830
1835
1831
1836 Computing jedi's completion ``.type`` can be quite expensive (it is a
1832 Computing jedi's completion ``.type`` can be quite expensive (it is a
1837 lazy property) and can require some warm-up, more warm up than just
1833 lazy property) and can require some warm-up, more warm up than just
1838 computing the ``name`` of a completion. The warm-up can be :
1834 computing the ``name`` of a completion. The warm-up can be :
1839
1835
1840 - Long warm-up the first time a module is encountered after
1836 - Long warm-up the first time a module is encountered after
1841 install/update: actually build parse/inference tree.
1837 install/update: actually build parse/inference tree.
1842
1838
1843 - first time the module is encountered in a session: load tree from
1839 - first time the module is encountered in a session: load tree from
1844 disk.
1840 disk.
1845
1841
1846 We don't want to block completions for tens of seconds so we give the
1842 We don't want to block completions for tens of seconds so we give the
1847 completer a "budget" of ``_timeout`` seconds per invocation to compute
1843 completer a "budget" of ``_timeout`` seconds per invocation to compute
1848 completions types, the completions that have not yet been computed will
1844 completions types, the completions that have not yet been computed will
1849 be marked as "unknown" an will have a chance to be computed next round
1845 be marked as "unknown" an will have a chance to be computed next round
1850 are things get cached.
1846 are things get cached.
1851
1847
1852 Keep in mind that Jedi is not the only thing treating the completion so
1848 Keep in mind that Jedi is not the only thing treating the completion so
1853 keep the timeout short-ish as if we take more than 0.3 second we still
1849 keep the timeout short-ish as if we take more than 0.3 second we still
1854 have lots of processing to do.
1850 have lots of processing to do.
1855
1851
1856 """
1852 """
1857 deadline = time.monotonic() + _timeout
1853 deadline = time.monotonic() + _timeout
1858
1854
1859
1855
1860 before = full_text[:offset]
1856 before = full_text[:offset]
1861 cursor_line, cursor_column = position_to_cursor(full_text, offset)
1857 cursor_line, cursor_column = position_to_cursor(full_text, offset)
1862
1858
1863 matched_text, matches, matches_origin, jedi_matches = self._complete(
1859 matched_text, matches, matches_origin, jedi_matches = self._complete(
1864 full_text=full_text, cursor_line=cursor_line, cursor_pos=cursor_column)
1860 full_text=full_text, cursor_line=cursor_line, cursor_pos=cursor_column)
1865
1861
1866 iter_jm = iter(jedi_matches)
1862 iter_jm = iter(jedi_matches)
1867 if _timeout:
1863 if _timeout:
1868 for jm in iter_jm:
1864 for jm in iter_jm:
1869 try:
1865 try:
1870 type_ = jm.type
1866 type_ = jm.type
1871 except Exception:
1867 except Exception:
1872 if self.debug:
1868 if self.debug:
1873 print("Error in Jedi getting type of ", jm)
1869 print("Error in Jedi getting type of ", jm)
1874 type_ = None
1870 type_ = None
1875 delta = len(jm.name_with_symbols) - len(jm.complete)
1871 delta = len(jm.name_with_symbols) - len(jm.complete)
1876 if type_ == 'function':
1872 if type_ == 'function':
1877 signature = _make_signature(jm)
1873 signature = _make_signature(jm)
1878 else:
1874 else:
1879 signature = ''
1875 signature = ''
1880 yield Completion(start=offset - delta,
1876 yield Completion(start=offset - delta,
1881 end=offset,
1877 end=offset,
1882 text=jm.name_with_symbols,
1878 text=jm.name_with_symbols,
1883 type=type_,
1879 type=type_,
1884 signature=signature,
1880 signature=signature,
1885 _origin='jedi')
1881 _origin='jedi')
1886
1882
1887 if time.monotonic() > deadline:
1883 if time.monotonic() > deadline:
1888 break
1884 break
1889
1885
1890 for jm in iter_jm:
1886 for jm in iter_jm:
1891 delta = len(jm.name_with_symbols) - len(jm.complete)
1887 delta = len(jm.name_with_symbols) - len(jm.complete)
1892 yield Completion(start=offset - delta,
1888 yield Completion(start=offset - delta,
1893 end=offset,
1889 end=offset,
1894 text=jm.name_with_symbols,
1890 text=jm.name_with_symbols,
1895 type='<unknown>', # don't compute type for speed
1891 type='<unknown>', # don't compute type for speed
1896 _origin='jedi',
1892 _origin='jedi',
1897 signature='')
1893 signature='')
1898
1894
1899
1895
1900 start_offset = before.rfind(matched_text)
1896 start_offset = before.rfind(matched_text)
1901
1897
1902 # TODO:
1898 # TODO:
1903 # Suppress this, right now just for debug.
1899 # Suppress this, right now just for debug.
1904 if jedi_matches and matches and self.debug:
1900 if jedi_matches and matches and self.debug:
1905 yield Completion(start=start_offset, end=offset, text='--jedi/ipython--',
1901 yield Completion(start=start_offset, end=offset, text='--jedi/ipython--',
1906 _origin='debug', type='none', signature='')
1902 _origin='debug', type='none', signature='')
1907
1903
1908 # I'm unsure if this is always true, so let's assert and see if it
1904 # I'm unsure if this is always true, so let's assert and see if it
1909 # crash
1905 # crash
1910 assert before.endswith(matched_text)
1906 assert before.endswith(matched_text)
1911 for m, t in zip(matches, matches_origin):
1907 for m, t in zip(matches, matches_origin):
1912 yield Completion(start=start_offset, end=offset, text=m, _origin=t, signature='', type='<unknown>')
1908 yield Completion(start=start_offset, end=offset, text=m, _origin=t, signature='', type='<unknown>')
1913
1909
1914
1910
1915 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1911 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1916 """Find completions for the given text and line context.
1912 """Find completions for the given text and line context.
1917
1913
1918 Note that both the text and the line_buffer are optional, but at least
1914 Note that both the text and the line_buffer are optional, but at least
1919 one of them must be given.
1915 one of them must be given.
1920
1916
1921 Parameters
1917 Parameters
1922 ----------
1918 ----------
1923 text : string, optional
1919 text : string, optional
1924 Text to perform the completion on. If not given, the line buffer
1920 Text to perform the completion on. If not given, the line buffer
1925 is split using the instance's CompletionSplitter object.
1921 is split using the instance's CompletionSplitter object.
1926
1922
1927 line_buffer : string, optional
1923 line_buffer : string, optional
1928 If not given, the completer attempts to obtain the current line
1924 If not given, the completer attempts to obtain the current line
1929 buffer via readline. This keyword allows clients which are
1925 buffer via readline. This keyword allows clients which are
1930 requesting for text completions in non-readline contexts to inform
1926 requesting for text completions in non-readline contexts to inform
1931 the completer of the entire text.
1927 the completer of the entire text.
1932
1928
1933 cursor_pos : int, optional
1929 cursor_pos : int, optional
1934 Index of the cursor in the full line buffer. Should be provided by
1930 Index of the cursor in the full line buffer. Should be provided by
1935 remote frontends where kernel has no access to frontend state.
1931 remote frontends where kernel has no access to frontend state.
1936
1932
1937 Returns
1933 Returns
1938 -------
1934 -------
1939 text : str
1935 text : str
1940 Text that was actually used in the completion.
1936 Text that was actually used in the completion.
1941
1937
1942 matches : list
1938 matches : list
1943 A list of completion matches.
1939 A list of completion matches.
1944
1940
1945
1941
1946 .. note::
1942 .. note::
1947
1943
1948 This API is likely to be deprecated and replaced by
1944 This API is likely to be deprecated and replaced by
1949 :any:`IPCompleter.completions` in the future.
1945 :any:`IPCompleter.completions` in the future.
1950
1946
1951
1947
1952 """
1948 """
1953 warnings.warn('`Completer.complete` is pending deprecation since '
1949 warnings.warn('`Completer.complete` is pending deprecation since '
1954 'IPython 6.0 and will be replaced by `Completer.completions`.',
1950 'IPython 6.0 and will be replaced by `Completer.completions`.',
1955 PendingDeprecationWarning)
1951 PendingDeprecationWarning)
1956 # potential todo, FOLD the 3rd throw away argument of _complete
1952 # potential todo, FOLD the 3rd throw away argument of _complete
1957 # into the first 2 one.
1953 # into the first 2 one.
1958 return self._complete(line_buffer=line_buffer, cursor_pos=cursor_pos, text=text, cursor_line=0)[:2]
1954 return self._complete(line_buffer=line_buffer, cursor_pos=cursor_pos, text=text, cursor_line=0)[:2]
1959
1955
1960 def _complete(self, *, cursor_line, cursor_pos, line_buffer=None, text=None,
1956 def _complete(self, *, cursor_line, cursor_pos, line_buffer=None, text=None,
1961 full_text=None) -> Tuple[str, List[str], List[str], Iterable[_FakeJediCompletion]]:
1957 full_text=None) -> Tuple[str, List[str], List[str], Iterable[_FakeJediCompletion]]:
1962 """
1958 """
1963
1959
1964 Like complete but can also returns raw jedi completions as well as the
1960 Like complete but can also returns raw jedi completions as well as the
1965 origin of the completion text. This could (and should) be made much
1961 origin of the completion text. This could (and should) be made much
1966 cleaner but that will be simpler once we drop the old (and stateful)
1962 cleaner but that will be simpler once we drop the old (and stateful)
1967 :any:`complete` API.
1963 :any:`complete` API.
1968
1964
1969
1965
1970 With current provisional API, cursor_pos act both (depending on the
1966 With current provisional API, cursor_pos act both (depending on the
1971 caller) as the offset in the ``text`` or ``line_buffer``, or as the
1967 caller) as the offset in the ``text`` or ``line_buffer``, or as the
1972 ``column`` when passing multiline strings this could/should be renamed
1968 ``column`` when passing multiline strings this could/should be renamed
1973 but would add extra noise.
1969 but would add extra noise.
1974 """
1970 """
1975
1971
1976 # if the cursor position isn't given, the only sane assumption we can
1972 # if the cursor position isn't given, the only sane assumption we can
1977 # make is that it's at the end of the line (the common case)
1973 # make is that it's at the end of the line (the common case)
1978 if cursor_pos is None:
1974 if cursor_pos is None:
1979 cursor_pos = len(line_buffer) if text is None else len(text)
1975 cursor_pos = len(line_buffer) if text is None else len(text)
1980
1976
1981 if self.use_main_ns:
1977 if self.use_main_ns:
1982 self.namespace = __main__.__dict__
1978 self.namespace = __main__.__dict__
1983
1979
1984 # if text is either None or an empty string, rely on the line buffer
1980 # if text is either None or an empty string, rely on the line buffer
1985 if (not line_buffer) and full_text:
1981 if (not line_buffer) and full_text:
1986 line_buffer = full_text.split('\n')[cursor_line]
1982 line_buffer = full_text.split('\n')[cursor_line]
1987 if not text:
1983 if not text:
1988 text = self.splitter.split_line(line_buffer, cursor_pos)
1984 text = self.splitter.split_line(line_buffer, cursor_pos)
1989
1985
1990 if self.backslash_combining_completions:
1986 if self.backslash_combining_completions:
1991 # allow deactivation of these on windows.
1987 # allow deactivation of these on windows.
1992 base_text = text if not line_buffer else line_buffer[:cursor_pos]
1988 base_text = text if not line_buffer else line_buffer[:cursor_pos]
1993 latex_text, latex_matches = self.latex_matches(base_text)
1989 latex_text, latex_matches = self.latex_matches(base_text)
1994 if latex_matches:
1990 if latex_matches:
1995 return latex_text, latex_matches, ['latex_matches']*len(latex_matches), ()
1991 return latex_text, latex_matches, ['latex_matches']*len(latex_matches), ()
1996 name_text = ''
1992 name_text = ''
1997 name_matches = []
1993 name_matches = []
1998 # need to add self.fwd_unicode_match() function here when done
1994 # need to add self.fwd_unicode_match() function here when done
1999 for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches, self.fwd_unicode_match):
1995 for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches, self.fwd_unicode_match):
2000 name_text, name_matches = meth(base_text)
1996 name_text, name_matches = meth(base_text)
2001 if name_text:
1997 if name_text:
2002 return name_text, name_matches[:MATCHES_LIMIT], \
1998 return name_text, name_matches[:MATCHES_LIMIT], \
2003 [meth.__qualname__]*min(len(name_matches), MATCHES_LIMIT), ()
1999 [meth.__qualname__]*min(len(name_matches), MATCHES_LIMIT), ()
2004
2000
2005
2001
2006 # If no line buffer is given, assume the input text is all there was
2002 # If no line buffer is given, assume the input text is all there was
2007 if line_buffer is None:
2003 if line_buffer is None:
2008 line_buffer = text
2004 line_buffer = text
2009
2005
2010 self.line_buffer = line_buffer
2006 self.line_buffer = line_buffer
2011 self.text_until_cursor = self.line_buffer[:cursor_pos]
2007 self.text_until_cursor = self.line_buffer[:cursor_pos]
2012
2008
2013 # Do magic arg matches
2009 # Do magic arg matches
2014 for matcher in self.magic_arg_matchers:
2010 for matcher in self.magic_arg_matchers:
2015 matches = list(matcher(line_buffer))[:MATCHES_LIMIT]
2011 matches = list(matcher(line_buffer))[:MATCHES_LIMIT]
2016 if matches:
2012 if matches:
2017 origins = [matcher.__qualname__] * len(matches)
2013 origins = [matcher.__qualname__] * len(matches)
2018 return text, matches, origins, ()
2014 return text, matches, origins, ()
2019
2015
2020 # Start with a clean slate of completions
2016 # Start with a clean slate of completions
2021 matches = []
2017 matches = []
2022 custom_res = self.dispatch_custom_completer(text)
2018 custom_res = self.dispatch_custom_completer(text)
2023 # FIXME: we should extend our api to return a dict with completions for
2019 # FIXME: we should extend our api to return a dict with completions for
2024 # different types of objects. The rlcomplete() method could then
2020 # different types of objects. The rlcomplete() method could then
2025 # simply collapse the dict into a list for readline, but we'd have
2021 # simply collapse the dict into a list for readline, but we'd have
2026 # richer completion semantics in other environments.
2022 # richer completion semantics in other environments.
2027 completions = ()
2023 completions = ()
2028 if self.use_jedi:
2024 if self.use_jedi:
2029 if not full_text:
2025 if not full_text:
2030 full_text = line_buffer
2026 full_text = line_buffer
2031 completions = self._jedi_matches(
2027 completions = self._jedi_matches(
2032 cursor_pos, cursor_line, full_text)
2028 cursor_pos, cursor_line, full_text)
2033 if custom_res is not None:
2029 if custom_res is not None:
2034 # did custom completers produce something?
2030 # did custom completers produce something?
2035 matches = [(m, 'custom') for m in custom_res]
2031 matches = [(m, 'custom') for m in custom_res]
2036 else:
2032 else:
2037 # Extend the list of completions with the results of each
2033 # Extend the list of completions with the results of each
2038 # matcher, so we return results to the user from all
2034 # matcher, so we return results to the user from all
2039 # namespaces.
2035 # namespaces.
2040 if self.merge_completions:
2036 if self.merge_completions:
2041 matches = []
2037 matches = []
2042 for matcher in self.matchers:
2038 for matcher in self.matchers:
2043 try:
2039 try:
2044 matches.extend([(m, matcher.__qualname__)
2040 matches.extend([(m, matcher.__qualname__)
2045 for m in matcher(text)])
2041 for m in matcher(text)])
2046 except:
2042 except:
2047 # Show the ugly traceback if the matcher causes an
2043 # Show the ugly traceback if the matcher causes an
2048 # exception, but do NOT crash the kernel!
2044 # exception, but do NOT crash the kernel!
2049 sys.excepthook(*sys.exc_info())
2045 sys.excepthook(*sys.exc_info())
2050 else:
2046 else:
2051 for matcher in self.matchers:
2047 for matcher in self.matchers:
2052 matches = [(m, matcher.__qualname__)
2048 matches = [(m, matcher.__qualname__)
2053 for m in matcher(text)]
2049 for m in matcher(text)]
2054 if matches:
2050 if matches:
2055 break
2051 break
2056 seen = set()
2052 seen = set()
2057 filtered_matches = set()
2053 filtered_matches = set()
2058 for m in matches:
2054 for m in matches:
2059 t, c = m
2055 t, c = m
2060 if t not in seen:
2056 if t not in seen:
2061 filtered_matches.add(m)
2057 filtered_matches.add(m)
2062 seen.add(t)
2058 seen.add(t)
2063
2059
2064 _filtered_matches = sorted(
2060 _filtered_matches = sorted(
2065 set(filtered_matches), key=lambda x: completions_sorting_key(x[0]))\
2061 set(filtered_matches), key=lambda x: completions_sorting_key(x[0]))\
2066 [:MATCHES_LIMIT]
2062 [:MATCHES_LIMIT]
2067
2063
2068 _matches = [m[0] for m in _filtered_matches]
2064 _matches = [m[0] for m in _filtered_matches]
2069 origins = [m[1] for m in _filtered_matches]
2065 origins = [m[1] for m in _filtered_matches]
2070
2066
2071 self.matches = _matches
2067 self.matches = _matches
2072
2068
2073 return text, _matches, origins, completions
2069 return text, _matches, origins, completions
2074
2070
2075 def fwd_unicode_match(self, text:str) -> Tuple[str, list]:
2071 def fwd_unicode_match(self, text:str) -> Tuple[str, list]:
2076 # initial code based on latex_matches() method
2072 if self._names is None:
2073 self._names = []
2074 for c in range(0,0x10FFFF + 1):
2075 try:
2076 self._names.append(unicodedata.name(chr(c)))
2077 except ValueError:
2078 pass
2079
2077 slashpos = text.rfind('\\')
2080 slashpos = text.rfind('\\')
2078 # if text starts with slash
2081 # if text starts with slash
2079 if slashpos > -1:
2082 if slashpos > -1:
2080 s = text[slashpos+1:]
2083 s = text[slashpos+1:]
2081 candidates = [x for x in names if x.startswith(s)]
2084 candidates = [x for x in self._names if x.startswith(s)]
2082 if candidates:
2085 if candidates:
2083 return s, [x for x in names if x.startswith(s)]
2086 return s, candidates
2084 else:
2087 else:
2085 return '', ()
2088 return '', ()
2086
2089
2087 # if text does not start with slash
2090 # if text does not start with slash
2088 else:
2091 else:
2089 return u'', ()
2092 return u'', ()
General Comments 0
You need to be logged in to leave comments. Login now