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