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