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