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