Show More
@@ -1,1335 +1,1325 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """Word completion for IPython. |
|
2 | """Word completion for IPython. | |
3 |
|
3 | |||
4 | This module is a fork of the rlcompleter module in the Python standard |
|
4 | This module is a fork of the rlcompleter module in the Python standard | |
5 | library. The original enhancements made to rlcompleter have been sent |
|
5 | library. The original enhancements made to rlcompleter have been sent | |
6 | upstream and were accepted as of Python 2.3, but we need a lot more |
|
6 | upstream and were accepted as of Python 2.3, but we need a lot more | |
7 | functionality specific to IPython, so this module will continue to live as an |
|
7 | functionality specific to IPython, so this module will continue to live as an | |
8 | IPython-specific utility. |
|
8 | IPython-specific utility. | |
9 |
|
9 | |||
10 | Original rlcompleter documentation: |
|
10 | Original rlcompleter documentation: | |
11 |
|
11 | |||
12 | This requires the latest extension to the readline module (the |
|
12 | This requires the latest extension to the readline module (the | |
13 | completes keywords, built-ins and globals in __main__; when completing |
|
13 | completes keywords, built-ins and globals in __main__; when completing | |
14 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
|
14 | NAME.NAME..., it evaluates (!) the expression up to the last dot and | |
15 | completes its attributes. |
|
15 | completes its attributes. | |
16 |
|
16 | |||
17 | It's very cool to do "import string" type "string.", hit the |
|
17 | It's very cool to do "import string" type "string.", hit the | |
18 | completion key (twice), and see the list of names defined by the |
|
18 | completion key (twice), and see the list of names defined by the | |
19 | string module! |
|
19 | string module! | |
20 |
|
20 | |||
21 | Tip: to use the tab key as the completion key, call |
|
21 | Tip: to use the tab key as the completion key, call | |
22 |
|
22 | |||
23 | readline.parse_and_bind("tab: complete") |
|
23 | readline.parse_and_bind("tab: complete") | |
24 |
|
24 | |||
25 | Notes: |
|
25 | Notes: | |
26 |
|
26 | |||
27 | - Exceptions raised by the completer function are *ignored* (and |
|
27 | - Exceptions raised by the completer function are *ignored* (and | |
28 | generally cause the completion to fail). This is a feature -- since |
|
28 | generally cause the completion to fail). This is a feature -- since | |
29 | readline sets the tty device in raw (or cbreak) mode, printing a |
|
29 | readline sets the tty device in raw (or cbreak) mode, printing a | |
30 | traceback wouldn't work well without some complicated hoopla to save, |
|
30 | traceback wouldn't work well without some complicated hoopla to save, | |
31 | reset and restore the tty state. |
|
31 | reset and restore the tty state. | |
32 |
|
32 | |||
33 | - The evaluation of the NAME.NAME... form may cause arbitrary |
|
33 | - The evaluation of the NAME.NAME... form may cause arbitrary | |
34 | application defined code to be executed if an object with a |
|
34 | application defined code to be executed if an object with a | |
35 | ``__getattr__`` hook is found. Since it is the responsibility of the |
|
35 | ``__getattr__`` hook is found. Since it is the responsibility of the | |
36 | application (or the user) to enable this feature, I consider this an |
|
36 | application (or the user) to enable this feature, I consider this an | |
37 | acceptable risk. More complicated expressions (e.g. function calls or |
|
37 | acceptable risk. More complicated expressions (e.g. function calls or | |
38 | indexing operations) are *not* evaluated. |
|
38 | indexing operations) are *not* evaluated. | |
39 |
|
39 | |||
40 | - GNU readline is also used by the built-in functions input() and |
|
40 | - GNU readline is also used by the built-in functions input() and | |
41 | raw_input(), and thus these also benefit/suffer from the completer |
|
41 | raw_input(), and thus these also benefit/suffer from the completer | |
42 | features. Clearly an interactive application can benefit by |
|
42 | features. Clearly an interactive application can benefit by | |
43 | specifying its own completer function and using raw_input() for all |
|
43 | specifying its own completer function and using raw_input() for all | |
44 | its input. |
|
44 | its input. | |
45 |
|
45 | |||
46 | - When the original stdin is not a tty device, GNU readline is never |
|
46 | - When the original stdin is not a tty device, GNU readline is never | |
47 | used, and this module (and the readline module) are silently inactive. |
|
47 | used, and this module (and the readline module) are silently inactive. | |
48 | """ |
|
48 | """ | |
49 |
|
49 | |||
50 | # Copyright (c) IPython Development Team. |
|
50 | # Copyright (c) IPython Development Team. | |
51 | # Distributed under the terms of the Modified BSD License. |
|
51 | # Distributed under the terms of the Modified BSD License. | |
52 | # |
|
52 | # | |
53 | # Some of this code originated from rlcompleter in the Python standard library |
|
53 | # Some of this code originated from rlcompleter in the Python standard library | |
54 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
54 | # Copyright (C) 2001 Python Software Foundation, www.python.org | |
55 |
|
55 | |||
56 | from __future__ import print_function |
|
56 | from __future__ import print_function | |
57 |
|
57 | |||
58 | import __main__ |
|
58 | import __main__ | |
59 | import glob |
|
59 | import glob | |
60 | import inspect |
|
60 | import inspect | |
61 | import itertools |
|
61 | import itertools | |
62 | import keyword |
|
62 | import keyword | |
63 | import os |
|
63 | import os | |
64 | import re |
|
64 | import re | |
65 | import sys |
|
65 | import sys | |
66 | import unicodedata |
|
66 | import unicodedata | |
67 | import string |
|
67 | import string | |
68 |
|
68 | |||
69 | from traitlets.config.configurable import Configurable |
|
69 | from traitlets.config.configurable import Configurable | |
70 | from IPython.core.error import TryNext |
|
70 | from IPython.core.error import TryNext | |
71 | from IPython.core.inputsplitter import ESC_MAGIC |
|
71 | from IPython.core.inputsplitter import ESC_MAGIC | |
72 | from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol |
|
72 | from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol | |
73 | from IPython.utils import generics |
|
73 | from IPython.utils import generics | |
74 | from IPython.utils.decorators import undoc |
|
74 | from IPython.utils.decorators import undoc | |
75 | from IPython.utils.dir2 import dir2, get_real_method |
|
75 | from IPython.utils.dir2 import dir2, get_real_method | |
76 | from IPython.utils.process import arg_split |
|
76 | from IPython.utils.process import arg_split | |
77 | from IPython.utils.py3compat import builtin_mod, string_types, PY3, cast_unicode_py2 |
|
77 | from IPython.utils.py3compat import builtin_mod, string_types, PY3, cast_unicode_py2 | |
78 | from traitlets import CBool, Enum |
|
78 | from traitlets import CBool, Enum | |
79 |
|
79 | |||
80 | try: |
|
|||
81 |
|
|
80 | import jedi | |
82 |
|
|
81 | import jedi.api.helpers | |
83 |
|
|
82 | import jedi.parser.user_context | |
84 | JEDI_INSTALLED = True |
|
|||
85 | except ImportError: |
|
|||
86 | JEDI_INSTALLED = False |
|
|||
87 |
|
83 | |||
88 | #----------------------------------------------------------------------------- |
|
84 | #----------------------------------------------------------------------------- | |
89 | # Globals |
|
85 | # Globals | |
90 | #----------------------------------------------------------------------------- |
|
86 | #----------------------------------------------------------------------------- | |
91 |
|
87 | |||
92 | # Public API |
|
88 | # Public API | |
93 | __all__ = ['Completer','IPCompleter'] |
|
89 | __all__ = ['Completer','IPCompleter'] | |
94 |
|
90 | |||
95 | if sys.platform == 'win32': |
|
91 | if sys.platform == 'win32': | |
96 | PROTECTABLES = ' ' |
|
92 | PROTECTABLES = ' ' | |
97 | else: |
|
93 | else: | |
98 | PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&' |
|
94 | PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&' | |
99 |
|
95 | |||
100 |
|
96 | |||
101 | #----------------------------------------------------------------------------- |
|
97 | #----------------------------------------------------------------------------- | |
102 | # Main functions and classes |
|
98 | # Main functions and classes | |
103 | #----------------------------------------------------------------------------- |
|
99 | #----------------------------------------------------------------------------- | |
104 |
|
100 | |||
105 | def has_open_quotes(s): |
|
101 | def has_open_quotes(s): | |
106 | """Return whether a string has open quotes. |
|
102 | """Return whether a string has open quotes. | |
107 |
|
103 | |||
108 | This simply counts whether the number of quote characters of either type in |
|
104 | This simply counts whether the number of quote characters of either type in | |
109 | the string is odd. |
|
105 | the string is odd. | |
110 |
|
106 | |||
111 | Returns |
|
107 | Returns | |
112 | ------- |
|
108 | ------- | |
113 | If there is an open quote, the quote character is returned. Else, return |
|
109 | If there is an open quote, the quote character is returned. Else, return | |
114 | False. |
|
110 | False. | |
115 | """ |
|
111 | """ | |
116 | # We check " first, then ', so complex cases with nested quotes will get |
|
112 | # We check " first, then ', so complex cases with nested quotes will get | |
117 | # the " to take precedence. |
|
113 | # the " to take precedence. | |
118 | if s.count('"') % 2: |
|
114 | if s.count('"') % 2: | |
119 | return '"' |
|
115 | return '"' | |
120 | elif s.count("'") % 2: |
|
116 | elif s.count("'") % 2: | |
121 | return "'" |
|
117 | return "'" | |
122 | else: |
|
118 | else: | |
123 | return False |
|
119 | return False | |
124 |
|
120 | |||
125 |
|
121 | |||
126 | def protect_filename(s): |
|
122 | def protect_filename(s): | |
127 | """Escape a string to protect certain characters.""" |
|
123 | """Escape a string to protect certain characters.""" | |
128 |
|
124 | |||
129 | return "".join([(ch in PROTECTABLES and '\\' + ch or ch) |
|
125 | return "".join([(ch in PROTECTABLES and '\\' + ch or ch) | |
130 | for ch in s]) |
|
126 | for ch in s]) | |
131 |
|
127 | |||
132 | def expand_user(path): |
|
128 | def expand_user(path): | |
133 | """Expand '~'-style usernames in strings. |
|
129 | """Expand '~'-style usernames in strings. | |
134 |
|
130 | |||
135 | This is similar to :func:`os.path.expanduser`, but it computes and returns |
|
131 | This is similar to :func:`os.path.expanduser`, but it computes and returns | |
136 | extra information that will be useful if the input was being used in |
|
132 | extra information that will be useful if the input was being used in | |
137 | computing completions, and you wish to return the completions with the |
|
133 | computing completions, and you wish to return the completions with the | |
138 | original '~' instead of its expanded value. |
|
134 | original '~' instead of its expanded value. | |
139 |
|
135 | |||
140 | Parameters |
|
136 | Parameters | |
141 | ---------- |
|
137 | ---------- | |
142 | path : str |
|
138 | path : str | |
143 | String to be expanded. If no ~ is present, the output is the same as the |
|
139 | String to be expanded. If no ~ is present, the output is the same as the | |
144 | input. |
|
140 | input. | |
145 |
|
141 | |||
146 | Returns |
|
142 | Returns | |
147 | ------- |
|
143 | ------- | |
148 | newpath : str |
|
144 | newpath : str | |
149 | Result of ~ expansion in the input path. |
|
145 | Result of ~ expansion in the input path. | |
150 | tilde_expand : bool |
|
146 | tilde_expand : bool | |
151 | Whether any expansion was performed or not. |
|
147 | Whether any expansion was performed or not. | |
152 | tilde_val : str |
|
148 | tilde_val : str | |
153 | The value that ~ was replaced with. |
|
149 | The value that ~ was replaced with. | |
154 | """ |
|
150 | """ | |
155 | # Default values |
|
151 | # Default values | |
156 | tilde_expand = False |
|
152 | tilde_expand = False | |
157 | tilde_val = '' |
|
153 | tilde_val = '' | |
158 | newpath = path |
|
154 | newpath = path | |
159 |
|
155 | |||
160 | if path.startswith('~'): |
|
156 | if path.startswith('~'): | |
161 | tilde_expand = True |
|
157 | tilde_expand = True | |
162 | rest = len(path)-1 |
|
158 | rest = len(path)-1 | |
163 | newpath = os.path.expanduser(path) |
|
159 | newpath = os.path.expanduser(path) | |
164 | if rest: |
|
160 | if rest: | |
165 | tilde_val = newpath[:-rest] |
|
161 | tilde_val = newpath[:-rest] | |
166 | else: |
|
162 | else: | |
167 | tilde_val = newpath |
|
163 | tilde_val = newpath | |
168 |
|
164 | |||
169 | return newpath, tilde_expand, tilde_val |
|
165 | return newpath, tilde_expand, tilde_val | |
170 |
|
166 | |||
171 |
|
167 | |||
172 | def compress_user(path, tilde_expand, tilde_val): |
|
168 | def compress_user(path, tilde_expand, tilde_val): | |
173 | """Does the opposite of expand_user, with its outputs. |
|
169 | """Does the opposite of expand_user, with its outputs. | |
174 | """ |
|
170 | """ | |
175 | if tilde_expand: |
|
171 | if tilde_expand: | |
176 | return path.replace(tilde_val, '~') |
|
172 | return path.replace(tilde_val, '~') | |
177 | else: |
|
173 | else: | |
178 | return path |
|
174 | return path | |
179 |
|
175 | |||
180 |
|
176 | |||
181 |
|
177 | |||
182 | def completions_sorting_key(word): |
|
178 | def completions_sorting_key(word): | |
183 | """key for sorting completions |
|
179 | """key for sorting completions | |
184 |
|
180 | |||
185 | This does several things: |
|
181 | This does several things: | |
186 |
|
182 | |||
187 | - Lowercase all completions, so they are sorted alphabetically with |
|
183 | - Lowercase all completions, so they are sorted alphabetically with | |
188 | upper and lower case words mingled |
|
184 | upper and lower case words mingled | |
189 | - Demote any completions starting with underscores to the end |
|
185 | - Demote any completions starting with underscores to the end | |
190 | - Insert any %magic and %%cellmagic completions in the alphabetical order |
|
186 | - Insert any %magic and %%cellmagic completions in the alphabetical order | |
191 | by their name |
|
187 | by their name | |
192 | """ |
|
188 | """ | |
193 | # Case insensitive sort |
|
189 | # Case insensitive sort | |
194 | word = word.lower() |
|
190 | word = word.lower() | |
195 |
|
191 | |||
196 | prio1, prio2 = 0, 0 |
|
192 | prio1, prio2 = 0, 0 | |
197 |
|
193 | |||
198 | if word.startswith('__'): |
|
194 | if word.startswith('__'): | |
199 | prio1 = 2 |
|
195 | prio1 = 2 | |
200 | elif word.startswith('_'): |
|
196 | elif word.startswith('_'): | |
201 | prio1 = 1 |
|
197 | prio1 = 1 | |
202 |
|
198 | |||
203 | if word.endswith('='): |
|
199 | if word.endswith('='): | |
204 | prio1 = -1 |
|
200 | prio1 = -1 | |
205 |
|
201 | |||
206 | if word.startswith('%%'): |
|
202 | if word.startswith('%%'): | |
207 | # If there's another % in there, this is something else, so leave it alone |
|
203 | # If there's another % in there, this is something else, so leave it alone | |
208 | if not "%" in word[2:]: |
|
204 | if not "%" in word[2:]: | |
209 | word = word[2:] |
|
205 | word = word[2:] | |
210 | prio2 = 2 |
|
206 | prio2 = 2 | |
211 | elif word.startswith('%'): |
|
207 | elif word.startswith('%'): | |
212 | if not "%" in word[1:]: |
|
208 | if not "%" in word[1:]: | |
213 | word = word[1:] |
|
209 | word = word[1:] | |
214 | prio2 = 1 |
|
210 | prio2 = 1 | |
215 |
|
211 | |||
216 | return prio1, word, prio2 |
|
212 | return prio1, word, prio2 | |
217 |
|
213 | |||
218 |
|
214 | |||
219 | @undoc |
|
215 | @undoc | |
220 | class Bunch(object): pass |
|
216 | class Bunch(object): pass | |
221 |
|
217 | |||
222 |
|
218 | |||
223 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' |
|
219 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' | |
224 | GREEDY_DELIMS = ' =\r\n' |
|
220 | GREEDY_DELIMS = ' =\r\n' | |
225 |
|
221 | |||
226 |
|
222 | |||
227 | class CompletionSplitter(object): |
|
223 | class CompletionSplitter(object): | |
228 | """An object to split an input line in a manner similar to readline. |
|
224 | """An object to split an input line in a manner similar to readline. | |
229 |
|
225 | |||
230 | By having our own implementation, we can expose readline-like completion in |
|
226 | By having our own implementation, we can expose readline-like completion in | |
231 | a uniform manner to all frontends. This object only needs to be given the |
|
227 | a uniform manner to all frontends. This object only needs to be given the | |
232 | line of text to be split and the cursor position on said line, and it |
|
228 | line of text to be split and the cursor position on said line, and it | |
233 | returns the 'word' to be completed on at the cursor after splitting the |
|
229 | returns the 'word' to be completed on at the cursor after splitting the | |
234 | entire line. |
|
230 | entire line. | |
235 |
|
231 | |||
236 | What characters are used as splitting delimiters can be controlled by |
|
232 | What characters are used as splitting delimiters can be controlled by | |
237 | setting the `delims` attribute (this is a property that internally |
|
233 | setting the `delims` attribute (this is a property that internally | |
238 | automatically builds the necessary regular expression)""" |
|
234 | automatically builds the necessary regular expression)""" | |
239 |
|
235 | |||
240 | # Private interface |
|
236 | # Private interface | |
241 |
|
237 | |||
242 | # A string of delimiter characters. The default value makes sense for |
|
238 | # A string of delimiter characters. The default value makes sense for | |
243 | # IPython's most typical usage patterns. |
|
239 | # IPython's most typical usage patterns. | |
244 | _delims = DELIMS |
|
240 | _delims = DELIMS | |
245 |
|
241 | |||
246 | # The expression (a normal string) to be compiled into a regular expression |
|
242 | # The expression (a normal string) to be compiled into a regular expression | |
247 | # for actual splitting. We store it as an attribute mostly for ease of |
|
243 | # for actual splitting. We store it as an attribute mostly for ease of | |
248 | # debugging, since this type of code can be so tricky to debug. |
|
244 | # debugging, since this type of code can be so tricky to debug. | |
249 | _delim_expr = None |
|
245 | _delim_expr = None | |
250 |
|
246 | |||
251 | # The regular expression that does the actual splitting |
|
247 | # The regular expression that does the actual splitting | |
252 | _delim_re = None |
|
248 | _delim_re = None | |
253 |
|
249 | |||
254 | def __init__(self, delims=None): |
|
250 | def __init__(self, delims=None): | |
255 | delims = CompletionSplitter._delims if delims is None else delims |
|
251 | delims = CompletionSplitter._delims if delims is None else delims | |
256 | self.delims = delims |
|
252 | self.delims = delims | |
257 |
|
253 | |||
258 | @property |
|
254 | @property | |
259 | def delims(self): |
|
255 | def delims(self): | |
260 | """Return the string of delimiter characters.""" |
|
256 | """Return the string of delimiter characters.""" | |
261 | return self._delims |
|
257 | return self._delims | |
262 |
|
258 | |||
263 | @delims.setter |
|
259 | @delims.setter | |
264 | def delims(self, delims): |
|
260 | def delims(self, delims): | |
265 | """Set the delimiters for line splitting.""" |
|
261 | """Set the delimiters for line splitting.""" | |
266 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' |
|
262 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' | |
267 | self._delim_re = re.compile(expr) |
|
263 | self._delim_re = re.compile(expr) | |
268 | self._delims = delims |
|
264 | self._delims = delims | |
269 | self._delim_expr = expr |
|
265 | self._delim_expr = expr | |
270 |
|
266 | |||
271 | def split_line(self, line, cursor_pos=None): |
|
267 | def split_line(self, line, cursor_pos=None): | |
272 | """Split a line of text with a cursor at the given position. |
|
268 | """Split a line of text with a cursor at the given position. | |
273 | """ |
|
269 | """ | |
274 | l = line if cursor_pos is None else line[:cursor_pos] |
|
270 | l = line if cursor_pos is None else line[:cursor_pos] | |
275 | return self._delim_re.split(l)[-1] |
|
271 | return self._delim_re.split(l)[-1] | |
276 |
|
272 | |||
277 |
|
273 | |||
278 | class Completer(Configurable): |
|
274 | class Completer(Configurable): | |
279 |
|
275 | |||
280 | greedy = CBool(False, config=True, |
|
276 | greedy = CBool(False, config=True, | |
281 | help="""Activate greedy completion |
|
277 | help="""Activate greedy completion | |
282 | PENDING DEPRECTION. this is now mostly taken care of with Jedi. |
|
278 | PENDING DEPRECTION. this is now mostly taken care of with Jedi. | |
283 |
|
279 | |||
284 | This will enable completion on elements of lists, results of function calls, etc., |
|
280 | This will enable completion on elements of lists, results of function calls, etc., | |
285 | but can be unsafe because the code is actually evaluated on TAB. |
|
281 | but can be unsafe because the code is actually evaluated on TAB. | |
286 | """ |
|
282 | """ | |
287 | ) |
|
283 | ) | |
288 |
|
284 | |||
289 |
|
285 | |||
290 | def __init__(self, namespace=None, global_namespace=None, **kwargs): |
|
286 | def __init__(self, namespace=None, global_namespace=None, **kwargs): | |
291 | """Create a new completer for the command line. |
|
287 | """Create a new completer for the command line. | |
292 |
|
288 | |||
293 | Completer(namespace=ns, global_namespace=ns2) -> completer instance. |
|
289 | Completer(namespace=ns, global_namespace=ns2) -> completer instance. | |
294 |
|
290 | |||
295 | If unspecified, the default namespace where completions are performed |
|
291 | If unspecified, the default namespace where completions are performed | |
296 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
292 | is __main__ (technically, __main__.__dict__). Namespaces should be | |
297 | given as dictionaries. |
|
293 | given as dictionaries. | |
298 |
|
294 | |||
299 | An optional second namespace can be given. This allows the completer |
|
295 | An optional second namespace can be given. This allows the completer | |
300 | to handle cases where both the local and global scopes need to be |
|
296 | to handle cases where both the local and global scopes need to be | |
301 | distinguished. |
|
297 | distinguished. | |
302 |
|
298 | |||
303 | Completer instances should be used as the completion mechanism of |
|
299 | Completer instances should be used as the completion mechanism of | |
304 | readline via the set_completer() call: |
|
300 | readline via the set_completer() call: | |
305 |
|
301 | |||
306 | readline.set_completer(Completer(my_namespace).complete) |
|
302 | readline.set_completer(Completer(my_namespace).complete) | |
307 | """ |
|
303 | """ | |
308 |
|
304 | |||
309 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
305 | # Don't bind to namespace quite yet, but flag whether the user wants a | |
310 | # specific namespace or to use __main__.__dict__. This will allow us |
|
306 | # specific namespace or to use __main__.__dict__. This will allow us | |
311 | # to bind to __main__.__dict__ at completion time, not now. |
|
307 | # to bind to __main__.__dict__ at completion time, not now. | |
312 | if namespace is None: |
|
308 | if namespace is None: | |
313 | self.use_main_ns = 1 |
|
309 | self.use_main_ns = 1 | |
314 | else: |
|
310 | else: | |
315 | self.use_main_ns = 0 |
|
311 | self.use_main_ns = 0 | |
316 | self.namespace = namespace |
|
312 | self.namespace = namespace | |
317 |
|
313 | |||
318 | # The global namespace, if given, can be bound directly |
|
314 | # The global namespace, if given, can be bound directly | |
319 | if global_namespace is None: |
|
315 | if global_namespace is None: | |
320 | self.global_namespace = {} |
|
316 | self.global_namespace = {} | |
321 | else: |
|
317 | else: | |
322 | self.global_namespace = global_namespace |
|
318 | self.global_namespace = global_namespace | |
323 |
|
319 | |||
324 | super(Completer, self).__init__(**kwargs) |
|
320 | super(Completer, self).__init__(**kwargs) | |
325 |
|
321 | |||
326 | def complete(self, text, state): |
|
322 | def complete(self, text, state): | |
327 | """Return the next possible completion for 'text'. |
|
323 | """Return the next possible completion for 'text'. | |
328 |
|
324 | |||
329 | This is called successively with state == 0, 1, 2, ... until it |
|
325 | This is called successively with state == 0, 1, 2, ... until it | |
330 | returns None. The completion should begin with 'text'. |
|
326 | returns None. The completion should begin with 'text'. | |
331 |
|
327 | |||
332 | """ |
|
328 | """ | |
333 | if self.use_main_ns: |
|
329 | if self.use_main_ns: | |
334 | self.namespace = __main__.__dict__ |
|
330 | self.namespace = __main__.__dict__ | |
335 |
|
331 | |||
336 | if state == 0: |
|
332 | if state == 0: | |
337 | if "." in text: |
|
333 | if "." in text: | |
338 | self.matches = self.attr_matches(text) |
|
334 | self.matches = self.attr_matches(text) | |
339 | else: |
|
335 | else: | |
340 | self.matches = self.global_matches(text) |
|
336 | self.matches = self.global_matches(text) | |
341 | try: |
|
337 | try: | |
342 | return self.matches[state] |
|
338 | return self.matches[state] | |
343 | except IndexError: |
|
339 | except IndexError: | |
344 | return None |
|
340 | return None | |
345 |
|
341 | |||
346 | def global_matches(self, text): |
|
342 | def global_matches(self, text): | |
347 | """Compute matches when text is a simple name. |
|
343 | """Compute matches when text is a simple name. | |
348 |
|
344 | |||
349 | Return a list of all keywords, built-in functions and names currently |
|
345 | Return a list of all keywords, built-in functions and names currently | |
350 | defined in self.namespace or self.global_namespace that match. |
|
346 | defined in self.namespace or self.global_namespace that match. | |
351 |
|
347 | |||
352 | """ |
|
348 | """ | |
353 | matches = [] |
|
349 | matches = [] | |
354 | match_append = matches.append |
|
350 | match_append = matches.append | |
355 | n = len(text) |
|
351 | n = len(text) | |
356 | for lst in [keyword.kwlist, |
|
352 | for lst in [keyword.kwlist, | |
357 | builtin_mod.__dict__.keys(), |
|
353 | builtin_mod.__dict__.keys(), | |
358 | self.namespace.keys(), |
|
354 | self.namespace.keys(), | |
359 | self.global_namespace.keys()]: |
|
355 | self.global_namespace.keys()]: | |
360 | for word in lst: |
|
356 | for word in lst: | |
361 | if word[:n] == text and word != "__builtins__": |
|
357 | if word[:n] == text and word != "__builtins__": | |
362 | match_append(word) |
|
358 | match_append(word) | |
363 | return [cast_unicode_py2(m) for m in matches] |
|
359 | return [cast_unicode_py2(m) for m in matches] | |
364 |
|
360 | |||
365 | def attr_matches(self, text): |
|
361 | def attr_matches(self, text): | |
366 | """Compute matches when text contains a dot. |
|
362 | """Compute matches when text contains a dot. | |
367 |
|
363 | |||
368 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
364 | Assuming the text is of the form NAME.NAME....[NAME], and is | |
369 | evaluatable in self.namespace or self.global_namespace, it will be |
|
365 | evaluatable in self.namespace or self.global_namespace, it will be | |
370 | evaluated and its attributes (as revealed by dir()) are used as |
|
366 | evaluated and its attributes (as revealed by dir()) are used as | |
371 | possible completions. (For class instances, class members are are |
|
367 | possible completions. (For class instances, class members are are | |
372 | also considered.) |
|
368 | also considered.) | |
373 |
|
369 | |||
374 | WARNING: this can still invoke arbitrary C code, if an object |
|
370 | WARNING: this can still invoke arbitrary C code, if an object | |
375 | with a __getattr__ hook is evaluated. |
|
371 | with a __getattr__ hook is evaluated. | |
376 |
|
372 | |||
377 | """ |
|
373 | """ | |
378 |
|
374 | |||
379 | # Another option, seems to work great. Catches things like ''.<tab> |
|
375 | # Another option, seems to work great. Catches things like ''.<tab> | |
380 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
376 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) | |
381 |
|
377 | |||
382 | if m: |
|
378 | if m: | |
383 | expr, attr = m.group(1, 3) |
|
379 | expr, attr = m.group(1, 3) | |
384 | elif self.greedy: |
|
380 | elif self.greedy: | |
385 | m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer) |
|
381 | m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer) | |
386 | if not m2: |
|
382 | if not m2: | |
387 | return [] |
|
383 | return [] | |
388 | expr, attr = m2.group(1,2) |
|
384 | expr, attr = m2.group(1,2) | |
389 | else: |
|
385 | else: | |
390 | return [] |
|
386 | return [] | |
391 |
|
387 | |||
392 | try: |
|
388 | try: | |
393 | obj = eval(expr, self.namespace) |
|
389 | obj = eval(expr, self.namespace) | |
394 | except: |
|
390 | except: | |
395 | try: |
|
391 | try: | |
396 | obj = eval(expr, self.global_namespace) |
|
392 | obj = eval(expr, self.global_namespace) | |
397 | except: |
|
393 | except: | |
398 | return [] |
|
394 | return [] | |
399 |
|
395 | |||
400 | if self.limit_to__all__ and hasattr(obj, '__all__'): |
|
396 | if self.limit_to__all__ and hasattr(obj, '__all__'): | |
401 | words = get__all__entries(obj) |
|
397 | words = get__all__entries(obj) | |
402 | else: |
|
398 | else: | |
403 | words = dir2(obj) |
|
399 | words = dir2(obj) | |
404 |
|
400 | |||
405 | try: |
|
401 | try: | |
406 | words = generics.complete_object(obj, words) |
|
402 | words = generics.complete_object(obj, words) | |
407 | except TryNext: |
|
403 | except TryNext: | |
408 | pass |
|
404 | pass | |
409 | except Exception: |
|
405 | except Exception: | |
410 | # Silence errors from completion function |
|
406 | # Silence errors from completion function | |
411 | #raise # dbg |
|
407 | #raise # dbg | |
412 | pass |
|
408 | pass | |
413 | # Build match list to return |
|
409 | # Build match list to return | |
414 | n = len(attr) |
|
410 | n = len(attr) | |
415 | return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
411 | return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ] | |
416 |
|
412 | |||
417 |
|
413 | |||
418 | def get__all__entries(obj): |
|
414 | def get__all__entries(obj): | |
419 | """returns the strings in the __all__ attribute""" |
|
415 | """returns the strings in the __all__ attribute""" | |
420 | try: |
|
416 | try: | |
421 | words = getattr(obj, '__all__') |
|
417 | words = getattr(obj, '__all__') | |
422 | except: |
|
418 | except: | |
423 | return [] |
|
419 | return [] | |
424 |
|
420 | |||
425 | return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)] |
|
421 | return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)] | |
426 |
|
422 | |||
427 |
|
423 | |||
428 | def match_dict_keys(keys, prefix, delims): |
|
424 | def match_dict_keys(keys, prefix, delims): | |
429 | """Used by dict_key_matches, matching the prefix to a list of keys""" |
|
425 | """Used by dict_key_matches, matching the prefix to a list of keys""" | |
430 | if not prefix: |
|
426 | if not prefix: | |
431 | return None, 0, [repr(k) for k in keys |
|
427 | return None, 0, [repr(k) for k in keys | |
432 | if isinstance(k, (string_types, bytes))] |
|
428 | if isinstance(k, (string_types, bytes))] | |
433 | quote_match = re.search('["\']', prefix) |
|
429 | quote_match = re.search('["\']', prefix) | |
434 | quote = quote_match.group() |
|
430 | quote = quote_match.group() | |
435 | try: |
|
431 | try: | |
436 | prefix_str = eval(prefix + quote, {}) |
|
432 | prefix_str = eval(prefix + quote, {}) | |
437 | except Exception: |
|
433 | except Exception: | |
438 | return None, 0, [] |
|
434 | return None, 0, [] | |
439 |
|
435 | |||
440 | pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$' |
|
436 | pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$' | |
441 | token_match = re.search(pattern, prefix, re.UNICODE) |
|
437 | token_match = re.search(pattern, prefix, re.UNICODE) | |
442 | token_start = token_match.start() |
|
438 | token_start = token_match.start() | |
443 | token_prefix = token_match.group() |
|
439 | token_prefix = token_match.group() | |
444 |
|
440 | |||
445 | # TODO: support bytes in Py3k |
|
441 | # TODO: support bytes in Py3k | |
446 | matched = [] |
|
442 | matched = [] | |
447 | for key in keys: |
|
443 | for key in keys: | |
448 | try: |
|
444 | try: | |
449 | if not key.startswith(prefix_str): |
|
445 | if not key.startswith(prefix_str): | |
450 | continue |
|
446 | continue | |
451 | except (AttributeError, TypeError, UnicodeError): |
|
447 | except (AttributeError, TypeError, UnicodeError): | |
452 | # Python 3+ TypeError on b'a'.startswith('a') or vice-versa |
|
448 | # Python 3+ TypeError on b'a'.startswith('a') or vice-versa | |
453 | continue |
|
449 | continue | |
454 |
|
450 | |||
455 | # reformat remainder of key to begin with prefix |
|
451 | # reformat remainder of key to begin with prefix | |
456 | rem = key[len(prefix_str):] |
|
452 | rem = key[len(prefix_str):] | |
457 | # force repr wrapped in ' |
|
453 | # force repr wrapped in ' | |
458 | rem_repr = repr(rem + '"') |
|
454 | rem_repr = repr(rem + '"') | |
459 | if rem_repr.startswith('u') and prefix[0] not in 'uU': |
|
455 | if rem_repr.startswith('u') and prefix[0] not in 'uU': | |
460 | # Found key is unicode, but prefix is Py2 string. |
|
456 | # Found key is unicode, but prefix is Py2 string. | |
461 | # Therefore attempt to interpret key as string. |
|
457 | # Therefore attempt to interpret key as string. | |
462 | try: |
|
458 | try: | |
463 | rem_repr = repr(rem.encode('ascii') + '"') |
|
459 | rem_repr = repr(rem.encode('ascii') + '"') | |
464 | except UnicodeEncodeError: |
|
460 | except UnicodeEncodeError: | |
465 | continue |
|
461 | continue | |
466 |
|
462 | |||
467 | rem_repr = rem_repr[1 + rem_repr.index("'"):-2] |
|
463 | rem_repr = rem_repr[1 + rem_repr.index("'"):-2] | |
468 | if quote == '"': |
|
464 | if quote == '"': | |
469 | # The entered prefix is quoted with ", |
|
465 | # The entered prefix is quoted with ", | |
470 | # but the match is quoted with '. |
|
466 | # but the match is quoted with '. | |
471 | # A contained " hence needs escaping for comparison: |
|
467 | # A contained " hence needs escaping for comparison: | |
472 | rem_repr = rem_repr.replace('"', '\\"') |
|
468 | rem_repr = rem_repr.replace('"', '\\"') | |
473 |
|
469 | |||
474 | # then reinsert prefix from start of token |
|
470 | # then reinsert prefix from start of token | |
475 | matched.append('%s%s' % (token_prefix, rem_repr)) |
|
471 | matched.append('%s%s' % (token_prefix, rem_repr)) | |
476 | return quote, token_start, matched |
|
472 | return quote, token_start, matched | |
477 |
|
473 | |||
478 |
|
474 | |||
479 | def _safe_isinstance(obj, module, class_name): |
|
475 | def _safe_isinstance(obj, module, class_name): | |
480 | """Checks if obj is an instance of module.class_name if loaded |
|
476 | """Checks if obj is an instance of module.class_name if loaded | |
481 | """ |
|
477 | """ | |
482 | return (module in sys.modules and |
|
478 | return (module in sys.modules and | |
483 | isinstance(obj, getattr(__import__(module), class_name))) |
|
479 | isinstance(obj, getattr(__import__(module), class_name))) | |
484 |
|
480 | |||
485 |
|
481 | |||
486 | def back_unicode_name_matches(text): |
|
482 | def back_unicode_name_matches(text): | |
487 | u"""Match unicode characters back to unicode name |
|
483 | u"""Match unicode characters back to unicode name | |
488 |
|
484 | |||
489 | This does β -> \\snowman |
|
485 | This does β -> \\snowman | |
490 |
|
486 | |||
491 | Note that snowman is not a valid python3 combining character but will be expanded. |
|
487 | Note that snowman is not a valid python3 combining character but will be expanded. | |
492 | Though it will not recombine back to the snowman character by the completion machinery. |
|
488 | Though it will not recombine back to the snowman character by the completion machinery. | |
493 |
|
489 | |||
494 | This will not either back-complete standard sequences like \\n, \\b ... |
|
490 | This will not either back-complete standard sequences like \\n, \\b ... | |
495 |
|
491 | |||
496 | Used on Python 3 only. |
|
492 | Used on Python 3 only. | |
497 | """ |
|
493 | """ | |
498 | if len(text)<2: |
|
494 | if len(text)<2: | |
499 | return u'', () |
|
495 | return u'', () | |
500 | maybe_slash = text[-2] |
|
496 | maybe_slash = text[-2] | |
501 | if maybe_slash != '\\': |
|
497 | if maybe_slash != '\\': | |
502 | return u'', () |
|
498 | return u'', () | |
503 |
|
499 | |||
504 | char = text[-1] |
|
500 | char = text[-1] | |
505 | # no expand on quote for completion in strings. |
|
501 | # no expand on quote for completion in strings. | |
506 | # nor backcomplete standard ascii keys |
|
502 | # nor backcomplete standard ascii keys | |
507 | if char in string.ascii_letters or char in ['"',"'"]: |
|
503 | if char in string.ascii_letters or char in ['"',"'"]: | |
508 | return u'', () |
|
504 | return u'', () | |
509 | try : |
|
505 | try : | |
510 | unic = unicodedata.name(char) |
|
506 | unic = unicodedata.name(char) | |
511 | return '\\'+char,['\\'+unic] |
|
507 | return '\\'+char,['\\'+unic] | |
512 | except KeyError as e: |
|
508 | except KeyError as e: | |
513 | pass |
|
509 | pass | |
514 | return u'', () |
|
510 | return u'', () | |
515 |
|
511 | |||
516 | def back_latex_name_matches(text): |
|
512 | def back_latex_name_matches(text): | |
517 | u"""Match latex characters back to unicode name |
|
513 | u"""Match latex characters back to unicode name | |
518 |
|
514 | |||
519 | This does ->\\sqrt |
|
515 | This does ->\\sqrt | |
520 |
|
516 | |||
521 | Used on Python 3 only. |
|
517 | Used on Python 3 only. | |
522 | """ |
|
518 | """ | |
523 | if len(text)<2: |
|
519 | if len(text)<2: | |
524 | return u'', () |
|
520 | return u'', () | |
525 | maybe_slash = text[-2] |
|
521 | maybe_slash = text[-2] | |
526 | if maybe_slash != '\\': |
|
522 | if maybe_slash != '\\': | |
527 | return u'', () |
|
523 | return u'', () | |
528 |
|
524 | |||
529 |
|
525 | |||
530 | char = text[-1] |
|
526 | char = text[-1] | |
531 | # no expand on quote for completion in strings. |
|
527 | # no expand on quote for completion in strings. | |
532 | # nor backcomplete standard ascii keys |
|
528 | # nor backcomplete standard ascii keys | |
533 | if char in string.ascii_letters or char in ['"',"'"]: |
|
529 | if char in string.ascii_letters or char in ['"',"'"]: | |
534 | return u'', () |
|
530 | return u'', () | |
535 | try : |
|
531 | try : | |
536 | latex = reverse_latex_symbol[char] |
|
532 | latex = reverse_latex_symbol[char] | |
537 | # '\\' replace the \ as well |
|
533 | # '\\' replace the \ as well | |
538 | return '\\'+char,[latex] |
|
534 | return '\\'+char,[latex] | |
539 | except KeyError as e: |
|
535 | except KeyError as e: | |
540 | pass |
|
536 | pass | |
541 | return u'', () |
|
537 | return u'', () | |
542 |
|
538 | |||
543 |
|
539 | |||
544 | class IPCompleter(Completer): |
|
540 | class IPCompleter(Completer): | |
545 | """Extension of the completer class with IPython-specific features""" |
|
541 | """Extension of the completer class with IPython-specific features""" | |
546 |
|
542 | |||
547 | def _greedy_changed(self, name, old, new): |
|
543 | def _greedy_changed(self, name, old, new): | |
548 | """update the splitter and readline delims when greedy is changed""" |
|
544 | """update the splitter and readline delims when greedy is changed""" | |
549 | if new: |
|
545 | if new: | |
550 | self.splitter.delims = GREEDY_DELIMS |
|
546 | self.splitter.delims = GREEDY_DELIMS | |
551 | else: |
|
547 | else: | |
552 | self.splitter.delims = DELIMS |
|
548 | self.splitter.delims = DELIMS | |
553 |
|
549 | |||
554 | if self.readline: |
|
550 | if self.readline: | |
555 | self.readline.set_completer_delims(self.splitter.delims) |
|
551 | self.readline.set_completer_delims(self.splitter.delims) | |
556 |
|
552 | |||
557 | merge_completions = CBool(True, config=True, |
|
553 | merge_completions = CBool(True, config=True, | |
558 | help="""Whether to merge completion results into a single list |
|
554 | help="""Whether to merge completion results into a single list | |
559 |
|
555 | |||
560 | If False, only the completion results from the first non-empty |
|
556 | If False, only the completion results from the first non-empty | |
561 | completer will be returned. |
|
557 | completer will be returned. | |
562 | """ |
|
558 | """ | |
563 | ) |
|
559 | ) | |
564 | omit__names = Enum((0,1,2), default_value=2, config=True, |
|
560 | omit__names = Enum((0,1,2), default_value=2, config=True, | |
565 | help="""Instruct the completer to omit private method names |
|
561 | help="""Instruct the completer to omit private method names | |
566 |
|
562 | |||
567 | Specifically, when completing on ``object.<tab>``. |
|
563 | Specifically, when completing on ``object.<tab>``. | |
568 |
|
564 | |||
569 | When 2 [default]: all names that start with '_' will be excluded. |
|
565 | When 2 [default]: all names that start with '_' will be excluded. | |
570 |
|
566 | |||
571 | When 1: all 'magic' names (``__foo__``) will be excluded. |
|
567 | When 1: all 'magic' names (``__foo__``) will be excluded. | |
572 |
|
568 | |||
573 | When 0: nothing will be excluded. |
|
569 | When 0: nothing will be excluded. | |
574 | """ |
|
570 | """ | |
575 | ) |
|
571 | ) | |
576 | limit_to__all__ = CBool(default_value=False, config=True, |
|
572 | limit_to__all__ = CBool(default_value=False, config=True, | |
577 | help=""" |
|
573 | help=""" | |
578 | DEPRECATED as of version 5.0. |
|
574 | DEPRECATED as of version 5.0. | |
579 |
|
575 | |||
580 | Instruct the completer to use __all__ for the completion |
|
576 | Instruct the completer to use __all__ for the completion | |
581 |
|
577 | |||
582 | Specifically, when completing on ``object.<tab>``. |
|
578 | Specifically, when completing on ``object.<tab>``. | |
583 |
|
579 | |||
584 | When True: only those names in obj.__all__ will be included. |
|
580 | When True: only those names in obj.__all__ will be included. | |
585 |
|
581 | |||
586 | When False [default]: the __all__ attribute is ignored |
|
582 | When False [default]: the __all__ attribute is ignored | |
587 | """ |
|
583 | """ | |
588 | ) |
|
584 | ) | |
589 | use_jedi_completions = CBool(default_value=JEDI_INSTALLED, config=True, |
|
|||
590 | help="""Use Jedi to generate autocompletions. |
|
|||
591 | """) |
|
|||
592 |
|
585 | |||
593 | def __init__(self, shell=None, namespace=None, global_namespace=None, |
|
586 | def __init__(self, shell=None, namespace=None, global_namespace=None, | |
594 | use_readline=True, config=None, **kwargs): |
|
587 | use_readline=True, config=None, **kwargs): | |
595 | """IPCompleter() -> completer |
|
588 | """IPCompleter() -> completer | |
596 |
|
589 | |||
597 | Return a completer object suitable for use by the readline library |
|
590 | Return a completer object suitable for use by the readline library | |
598 | via readline.set_completer(). |
|
591 | via readline.set_completer(). | |
599 |
|
592 | |||
600 | Inputs: |
|
593 | Inputs: | |
601 |
|
594 | |||
602 | - shell: a pointer to the ipython shell itself. This is needed |
|
595 | - shell: a pointer to the ipython shell itself. This is needed | |
603 | because this completer knows about magic functions, and those can |
|
596 | because this completer knows about magic functions, and those can | |
604 | only be accessed via the ipython instance. |
|
597 | only be accessed via the ipython instance. | |
605 |
|
598 | |||
606 | - namespace: an optional dict where completions are performed. |
|
599 | - namespace: an optional dict where completions are performed. | |
607 |
|
600 | |||
608 | - global_namespace: secondary optional dict for completions, to |
|
601 | - global_namespace: secondary optional dict for completions, to | |
609 | handle cases (such as IPython embedded inside functions) where |
|
602 | handle cases (such as IPython embedded inside functions) where | |
610 | both Python scopes are visible. |
|
603 | both Python scopes are visible. | |
611 |
|
604 | |||
612 | use_readline : bool, optional |
|
605 | use_readline : bool, optional | |
613 | If true, use the readline library. This completer can still function |
|
606 | If true, use the readline library. This completer can still function | |
614 | without readline, though in that case callers must provide some extra |
|
607 | without readline, though in that case callers must provide some extra | |
615 | information on each call about the current line.""" |
|
608 | information on each call about the current line.""" | |
616 |
|
609 | |||
617 | self.magic_escape = ESC_MAGIC |
|
610 | self.magic_escape = ESC_MAGIC | |
618 | self.splitter = CompletionSplitter() |
|
611 | self.splitter = CompletionSplitter() | |
619 |
|
612 | |||
620 | # Readline configuration, only used by the rlcompleter method. |
|
613 | # Readline configuration, only used by the rlcompleter method. | |
621 | if use_readline: |
|
614 | if use_readline: | |
622 | # We store the right version of readline so that later code |
|
615 | # We store the right version of readline so that later code | |
623 | import IPython.utils.rlineimpl as readline |
|
616 | import IPython.utils.rlineimpl as readline | |
624 | self.readline = readline |
|
617 | self.readline = readline | |
625 | else: |
|
618 | else: | |
626 | self.readline = None |
|
619 | self.readline = None | |
627 |
|
620 | |||
628 | # _greedy_changed() depends on splitter and readline being defined: |
|
621 | # _greedy_changed() depends on splitter and readline being defined: | |
629 | Completer.__init__(self, namespace=namespace, global_namespace=global_namespace, |
|
622 | Completer.__init__(self, namespace=namespace, global_namespace=global_namespace, | |
630 | config=config, **kwargs) |
|
623 | config=config, **kwargs) | |
631 |
|
624 | |||
632 | # List where completion matches will be stored |
|
625 | # List where completion matches will be stored | |
633 | self.matches = [] |
|
626 | self.matches = [] | |
634 | self.shell = shell |
|
627 | self.shell = shell | |
635 | # Regexp to split filenames with spaces in them |
|
628 | # Regexp to split filenames with spaces in them | |
636 | self.space_name_re = re.compile(r'([^\\] )') |
|
629 | self.space_name_re = re.compile(r'([^\\] )') | |
637 | # Hold a local ref. to glob.glob for speed |
|
630 | # Hold a local ref. to glob.glob for speed | |
638 | self.glob = glob.glob |
|
631 | self.glob = glob.glob | |
639 |
|
632 | |||
640 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
633 | # Determine if we are running on 'dumb' terminals, like (X)Emacs | |
641 | # buffers, to avoid completion problems. |
|
634 | # buffers, to avoid completion problems. | |
642 | term = os.environ.get('TERM','xterm') |
|
635 | term = os.environ.get('TERM','xterm') | |
643 | self.dumb_terminal = term in ['dumb','emacs'] |
|
636 | self.dumb_terminal = term in ['dumb','emacs'] | |
644 |
|
637 | |||
645 | # Special handling of backslashes needed in win32 platforms |
|
638 | # Special handling of backslashes needed in win32 platforms | |
646 | if sys.platform == "win32": |
|
639 | if sys.platform == "win32": | |
647 | self.clean_glob = self._clean_glob_win32 |
|
640 | self.clean_glob = self._clean_glob_win32 | |
648 | else: |
|
641 | else: | |
649 | self.clean_glob = self._clean_glob |
|
642 | self.clean_glob = self._clean_glob | |
650 |
|
643 | |||
651 | #regexp to parse docstring for function signature |
|
644 | #regexp to parse docstring for function signature | |
652 | self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') |
|
645 | self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') | |
653 | self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') |
|
646 | self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') | |
654 | #use this if positional argument name is also needed |
|
647 | #use this if positional argument name is also needed | |
655 | #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)') |
|
648 | #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)') | |
656 |
|
649 | |||
657 | # All active matcher routines for completion |
|
650 | # All active matcher routines for completion | |
658 | self.matchers = [ |
|
651 | self.matchers = [ | |
659 | self.file_matches, |
|
652 | self.file_matches, | |
660 | self.magic_matches, |
|
653 | self.magic_matches, | |
661 | self.python_func_kw_matches, |
|
654 | self.python_func_kw_matches, | |
662 | self.dict_key_matches, |
|
655 | self.dict_key_matches, | |
663 | ] |
|
656 | ] | |
664 |
|
657 | |||
665 | def all_completions(self, text): |
|
658 | def all_completions(self, text): | |
666 | """ |
|
659 | """ | |
667 | Wrapper around the complete method for the benefit of emacs |
|
660 | Wrapper around the complete method for the benefit of emacs | |
668 | and pydb. |
|
661 | and pydb. | |
669 | """ |
|
662 | """ | |
670 | return self.complete(text)[1] |
|
663 | return self.complete(text)[1] | |
671 |
|
664 | |||
672 | def _clean_glob(self, text): |
|
665 | def _clean_glob(self, text): | |
673 | return self.glob("%s*" % text) |
|
666 | return self.glob("%s*" % text) | |
674 |
|
667 | |||
675 | def _clean_glob_win32(self,text): |
|
668 | def _clean_glob_win32(self,text): | |
676 | return [f.replace("\\","/") |
|
669 | return [f.replace("\\","/") | |
677 | for f in self.glob("%s*" % text)] |
|
670 | for f in self.glob("%s*" % text)] | |
678 |
|
671 | |||
679 | def file_matches(self, text): |
|
672 | def file_matches(self, text): | |
680 | """Match filenames, expanding ~USER type strings. |
|
673 | """Match filenames, expanding ~USER type strings. | |
681 |
|
674 | |||
682 | Most of the seemingly convoluted logic in this completer is an |
|
675 | Most of the seemingly convoluted logic in this completer is an | |
683 | attempt to handle filenames with spaces in them. And yet it's not |
|
676 | attempt to handle filenames with spaces in them. And yet it's not | |
684 | quite perfect, because Python's readline doesn't expose all of the |
|
677 | quite perfect, because Python's readline doesn't expose all of the | |
685 | GNU readline details needed for this to be done correctly. |
|
678 | GNU readline details needed for this to be done correctly. | |
686 |
|
679 | |||
687 | For a filename with a space in it, the printed completions will be |
|
680 | For a filename with a space in it, the printed completions will be | |
688 | only the parts after what's already been typed (instead of the |
|
681 | only the parts after what's already been typed (instead of the | |
689 | full completions, as is normally done). I don't think with the |
|
682 | full completions, as is normally done). I don't think with the | |
690 | current (as of Python 2.3) Python readline it's possible to do |
|
683 | current (as of Python 2.3) Python readline it's possible to do | |
691 | better.""" |
|
684 | better.""" | |
692 |
|
685 | |||
693 | # chars that require escaping with backslash - i.e. chars |
|
686 | # chars that require escaping with backslash - i.e. chars | |
694 | # that readline treats incorrectly as delimiters, but we |
|
687 | # that readline treats incorrectly as delimiters, but we | |
695 | # don't want to treat as delimiters in filename matching |
|
688 | # don't want to treat as delimiters in filename matching | |
696 | # when escaped with backslash |
|
689 | # when escaped with backslash | |
697 | if text.startswith('!'): |
|
690 | if text.startswith('!'): | |
698 | text = text[1:] |
|
691 | text = text[1:] | |
699 | text_prefix = u'!' |
|
692 | text_prefix = u'!' | |
700 | else: |
|
693 | else: | |
701 | text_prefix = u'' |
|
694 | text_prefix = u'' | |
702 |
|
695 | |||
703 | text_until_cursor = self.text_until_cursor |
|
696 | text_until_cursor = self.text_until_cursor | |
704 | # track strings with open quotes |
|
697 | # track strings with open quotes | |
705 | open_quotes = has_open_quotes(text_until_cursor) |
|
698 | open_quotes = has_open_quotes(text_until_cursor) | |
706 |
|
699 | |||
707 | if '(' in text_until_cursor or '[' in text_until_cursor: |
|
700 | if '(' in text_until_cursor or '[' in text_until_cursor: | |
708 | lsplit = text |
|
701 | lsplit = text | |
709 | else: |
|
702 | else: | |
710 | try: |
|
703 | try: | |
711 | # arg_split ~ shlex.split, but with unicode bugs fixed by us |
|
704 | # arg_split ~ shlex.split, but with unicode bugs fixed by us | |
712 | lsplit = arg_split(text_until_cursor)[-1] |
|
705 | lsplit = arg_split(text_until_cursor)[-1] | |
713 | except ValueError: |
|
706 | except ValueError: | |
714 | # typically an unmatched ", or backslash without escaped char. |
|
707 | # typically an unmatched ", or backslash without escaped char. | |
715 | if open_quotes: |
|
708 | if open_quotes: | |
716 | lsplit = text_until_cursor.split(open_quotes)[-1] |
|
709 | lsplit = text_until_cursor.split(open_quotes)[-1] | |
717 | else: |
|
710 | else: | |
718 | return [] |
|
711 | return [] | |
719 | except IndexError: |
|
712 | except IndexError: | |
720 | # tab pressed on empty line |
|
713 | # tab pressed on empty line | |
721 | lsplit = "" |
|
714 | lsplit = "" | |
722 |
|
715 | |||
723 | if not open_quotes and lsplit != protect_filename(lsplit): |
|
716 | if not open_quotes and lsplit != protect_filename(lsplit): | |
724 | # if protectables are found, do matching on the whole escaped name |
|
717 | # if protectables are found, do matching on the whole escaped name | |
725 | has_protectables = True |
|
718 | has_protectables = True | |
726 | text0,text = text,lsplit |
|
719 | text0,text = text,lsplit | |
727 | else: |
|
720 | else: | |
728 | has_protectables = False |
|
721 | has_protectables = False | |
729 | text = os.path.expanduser(text) |
|
722 | text = os.path.expanduser(text) | |
730 |
|
723 | |||
731 | if text == "": |
|
724 | if text == "": | |
732 | return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")] |
|
725 | return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")] | |
733 |
|
726 | |||
734 | # Compute the matches from the filesystem |
|
727 | # Compute the matches from the filesystem | |
735 | m0 = self.clean_glob(text.replace('\\','')) |
|
728 | m0 = self.clean_glob(text.replace('\\','')) | |
736 |
|
729 | |||
737 | if has_protectables: |
|
730 | if has_protectables: | |
738 | # If we had protectables, we need to revert our changes to the |
|
731 | # If we had protectables, we need to revert our changes to the | |
739 | # beginning of filename so that we don't double-write the part |
|
732 | # beginning of filename so that we don't double-write the part | |
740 | # of the filename we have so far |
|
733 | # of the filename we have so far | |
741 | len_lsplit = len(lsplit) |
|
734 | len_lsplit = len(lsplit) | |
742 | matches = [text_prefix + text0 + |
|
735 | matches = [text_prefix + text0 + | |
743 | protect_filename(f[len_lsplit:]) for f in m0] |
|
736 | protect_filename(f[len_lsplit:]) for f in m0] | |
744 | else: |
|
737 | else: | |
745 | if open_quotes: |
|
738 | if open_quotes: | |
746 | # if we have a string with an open quote, we don't need to |
|
739 | # if we have a string with an open quote, we don't need to | |
747 | # protect the names at all (and we _shouldn't_, as it |
|
740 | # protect the names at all (and we _shouldn't_, as it | |
748 | # would cause bugs when the filesystem call is made). |
|
741 | # would cause bugs when the filesystem call is made). | |
749 | matches = m0 |
|
742 | matches = m0 | |
750 | else: |
|
743 | else: | |
751 | matches = [text_prefix + |
|
744 | matches = [text_prefix + | |
752 | protect_filename(f) for f in m0] |
|
745 | protect_filename(f) for f in m0] | |
753 |
|
746 | |||
754 | # Mark directories in input list by appending '/' to their names. |
|
747 | # Mark directories in input list by appending '/' to their names. | |
755 | return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches] |
|
748 | return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches] | |
756 |
|
749 | |||
757 | def magic_matches(self, text): |
|
750 | def magic_matches(self, text): | |
758 | """Match magics""" |
|
751 | """Match magics""" | |
759 | # Get all shell magics now rather than statically, so magics loaded at |
|
752 | # Get all shell magics now rather than statically, so magics loaded at | |
760 | # runtime show up too. |
|
753 | # runtime show up too. | |
761 | lsm = self.shell.magics_manager.lsmagic() |
|
754 | lsm = self.shell.magics_manager.lsmagic() | |
762 | line_magics = lsm['line'] |
|
755 | line_magics = lsm['line'] | |
763 | cell_magics = lsm['cell'] |
|
756 | cell_magics = lsm['cell'] | |
764 | pre = self.magic_escape |
|
757 | pre = self.magic_escape | |
765 | pre2 = pre+pre |
|
758 | pre2 = pre+pre | |
766 |
|
759 | |||
767 | # Completion logic: |
|
760 | # Completion logic: | |
768 | # - user gives %%: only do cell magics |
|
761 | # - user gives %%: only do cell magics | |
769 | # - user gives %: do both line and cell magics |
|
762 | # - user gives %: do both line and cell magics | |
770 | # - no prefix: do both |
|
763 | # - no prefix: do both | |
771 | # In other words, line magics are skipped if the user gives %% explicitly |
|
764 | # In other words, line magics are skipped if the user gives %% explicitly | |
772 | bare_text = text.lstrip(pre) |
|
765 | bare_text = text.lstrip(pre) | |
773 | comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)] |
|
766 | comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)] | |
774 | if not text.startswith(pre2): |
|
767 | if not text.startswith(pre2): | |
775 | comp += [ pre+m for m in line_magics if m.startswith(bare_text)] |
|
768 | comp += [ pre+m for m in line_magics if m.startswith(bare_text)] | |
776 | return [cast_unicode_py2(c) for c in comp] |
|
769 | return [cast_unicode_py2(c) for c in comp] | |
777 |
|
770 | |||
778 | def python_jedi_matches(self, text, line_buffer, cursor_pos): |
|
771 | def python_jedi_matches(self, text, line_buffer, cursor_pos): | |
779 | """Match attributes or global Python names using Jedi.""" |
|
772 | """Match attributes or global Python names using Jedi.""" | |
780 | if line_buffer.startswith('aimport ') or line_buffer.startswith('%aimport '): |
|
773 | if line_buffer.startswith('aimport ') or line_buffer.startswith('%aimport '): | |
781 | return () |
|
774 | return () | |
782 | namespaces = [] |
|
775 | namespaces = [] | |
783 | if self.namespace is None: |
|
776 | if self.namespace is None: | |
784 | import __main__ |
|
777 | import __main__ | |
785 | namespaces.append(__main__.__dict__) |
|
778 | namespaces.append(__main__.__dict__) | |
786 | else: |
|
779 | else: | |
787 | namespaces.append(self.namespace) |
|
780 | namespaces.append(self.namespace) | |
788 | if self.global_namespace is not None: |
|
781 | if self.global_namespace is not None: | |
789 | namespaces.append(self.global_namespace) |
|
782 | namespaces.append(self.global_namespace) | |
790 |
|
783 | |||
791 | # cursor_pos is an it, jedi wants line and column |
|
784 | # cursor_pos is an it, jedi wants line and column | |
792 |
|
785 | |||
793 | interpreter = jedi.Interpreter(line_buffer, namespaces, column=cursor_pos) |
|
786 | interpreter = jedi.Interpreter(line_buffer, namespaces, column=cursor_pos) | |
794 | path = jedi.parser.user_context.UserContext(line_buffer, \ |
|
787 | path = jedi.parser.user_context.UserContext(line_buffer, \ | |
795 | (1, len(line_buffer))).get_path_until_cursor() |
|
788 | (1, len(line_buffer))).get_path_until_cursor() | |
796 | path, dot, like = jedi.api.helpers.completion_parts(path) |
|
789 | path, dot, like = jedi.api.helpers.completion_parts(path) | |
797 | if text.startswith('.'): |
|
790 | if text.startswith('.'): | |
798 | # text will be `.` on completions like `a[0].<tab>` |
|
791 | # text will be `.` on completions like `a[0].<tab>` | |
799 | before = dot |
|
792 | before = dot | |
800 | else: |
|
793 | else: | |
801 | before = line_buffer[:len(line_buffer) - len(like)] |
|
794 | before = line_buffer[:len(line_buffer) - len(like)] | |
802 |
|
795 | |||
803 |
|
796 | |||
804 | def trim_start(completion): |
|
797 | def trim_start(completion): | |
805 | """completions need to start with `text`, trim the beginning until it does""" |
|
798 | """completions need to start with `text`, trim the beginning until it does""" | |
806 | ltext = text.lower() |
|
799 | ltext = text.lower() | |
807 | lcomp = completion.lower() |
|
800 | lcomp = completion.lower() | |
808 | if ltext in lcomp and not (lcomp.startswith(ltext)): |
|
801 | if ltext in lcomp and not (lcomp.startswith(ltext)): | |
809 | start_index = lcomp.index(ltext) |
|
802 | start_index = lcomp.index(ltext) | |
810 | if cursor_pos: |
|
803 | if cursor_pos: | |
811 | if start_index >= cursor_pos: |
|
804 | if start_index >= cursor_pos: | |
812 | start_index = min(start_index, cursor_pos) |
|
805 | start_index = min(start_index, cursor_pos) | |
813 | return completion[start_index:] |
|
806 | return completion[start_index:] | |
814 | return completion |
|
807 | return completion | |
815 |
|
808 | |||
816 | completions = interpreter.completions() |
|
809 | completions = interpreter.completions() | |
817 |
|
810 | |||
818 | completion_text = [c.name_with_symbols for c in completions] |
|
811 | completion_text = [c.name_with_symbols for c in completions] | |
819 |
|
812 | |||
820 | if self.omit__names: |
|
813 | if self.omit__names: | |
821 | if self.omit__names == 1: |
|
814 | if self.omit__names == 1: | |
822 | # true if txt is _not_ a __ name, false otherwise: |
|
815 | # true if txt is _not_ a __ name, false otherwise: | |
823 | no__name = lambda txt: not txt.startswith('__') |
|
816 | no__name = lambda txt: not txt.startswith('__') | |
824 | else: |
|
817 | else: | |
825 | # true if txt is _not_ a _ name, false otherwise: |
|
818 | # true if txt is _not_ a _ name, false otherwise: | |
826 | no__name = lambda txt: not txt.startswith('_') |
|
819 | no__name = lambda txt: not txt.startswith('_') | |
827 | completion_text = filter(no__name, completion_text) |
|
820 | completion_text = filter(no__name, completion_text) | |
828 |
|
821 | |||
829 |
|
822 | |||
830 | return [trim_start(before + c_text) for c_text in completion_text] |
|
823 | return [trim_start(before + c_text) for c_text in completion_text] | |
831 |
|
824 | |||
832 |
|
825 | |||
833 | def python_matches(self, text): |
|
826 | def python_matches(self, text): | |
834 | """Match attributes or global python names""" |
|
827 | """Match attributes or global python names""" | |
835 | # Jedi completion |
|
|||
836 |
|
||||
837 | if "." in text: |
|
828 | if "." in text: | |
838 | try: |
|
829 | try: | |
839 | matches = self.attr_matches(text) |
|
830 | matches = self.attr_matches(text) | |
840 | if text.endswith('.') and self.omit__names: |
|
831 | if text.endswith('.') and self.omit__names: | |
841 | if self.omit__names == 1: |
|
832 | if self.omit__names == 1: | |
842 | # true if txt is _not_ a __ name, false otherwise: |
|
833 | # true if txt is _not_ a __ name, false otherwise: | |
843 | no__name = (lambda txt: |
|
834 | no__name = (lambda txt: | |
844 | re.match(r'.*\.__.*?__',txt) is None) |
|
835 | re.match(r'.*\.__.*?__',txt) is None) | |
845 | else: |
|
836 | else: | |
846 | # true if txt is _not_ a _ name, false otherwise: |
|
837 | # true if txt is _not_ a _ name, false otherwise: | |
847 | no__name = (lambda txt: |
|
838 | no__name = (lambda txt: | |
848 | re.match(r'\._.*?',txt[txt.rindex('.'):]) is None) |
|
839 | re.match(r'\._.*?',txt[txt.rindex('.'):]) is None) | |
849 | matches = filter(no__name, matches) |
|
840 | matches = filter(no__name, matches) | |
850 | except NameError: |
|
841 | except NameError: | |
851 | # catches <undefined attributes>.<tab> |
|
842 | # catches <undefined attributes>.<tab> | |
852 | matches = [] |
|
843 | matches = [] | |
853 | else: |
|
844 | else: | |
854 | matches = self.global_matches(text) |
|
845 | matches = self.global_matches(text) | |
855 | return matches |
|
846 | return matches | |
856 |
|
847 | |||
857 | def _default_arguments_from_docstring(self, doc): |
|
848 | def _default_arguments_from_docstring(self, doc): | |
858 | """Parse the first line of docstring for call signature. |
|
849 | """Parse the first line of docstring for call signature. | |
859 |
|
850 | |||
860 | Docstring should be of the form 'min(iterable[, key=func])\n'. |
|
851 | Docstring should be of the form 'min(iterable[, key=func])\n'. | |
861 | It can also parse cython docstring of the form |
|
852 | It can also parse cython docstring of the form | |
862 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'. |
|
853 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'. | |
863 | """ |
|
854 | """ | |
864 | if doc is None: |
|
855 | if doc is None: | |
865 | return [] |
|
856 | return [] | |
866 |
|
857 | |||
867 | #care only the firstline |
|
858 | #care only the firstline | |
868 | line = doc.lstrip().splitlines()[0] |
|
859 | line = doc.lstrip().splitlines()[0] | |
869 |
|
860 | |||
870 | #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') |
|
861 | #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') | |
871 | #'min(iterable[, key=func])\n' -> 'iterable[, key=func]' |
|
862 | #'min(iterable[, key=func])\n' -> 'iterable[, key=func]' | |
872 | sig = self.docstring_sig_re.search(line) |
|
863 | sig = self.docstring_sig_re.search(line) | |
873 | if sig is None: |
|
864 | if sig is None: | |
874 | return [] |
|
865 | return [] | |
875 | # iterable[, key=func]' -> ['iterable[' ,' key=func]'] |
|
866 | # iterable[, key=func]' -> ['iterable[' ,' key=func]'] | |
876 | sig = sig.groups()[0].split(',') |
|
867 | sig = sig.groups()[0].split(',') | |
877 | ret = [] |
|
868 | ret = [] | |
878 | for s in sig: |
|
869 | for s in sig: | |
879 | #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') |
|
870 | #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') | |
880 | ret += self.docstring_kwd_re.findall(s) |
|
871 | ret += self.docstring_kwd_re.findall(s) | |
881 | return ret |
|
872 | return ret | |
882 |
|
873 | |||
883 | def _default_arguments(self, obj): |
|
874 | def _default_arguments(self, obj): | |
884 | """Return the list of default arguments of obj if it is callable, |
|
875 | """Return the list of default arguments of obj if it is callable, | |
885 | or empty list otherwise.""" |
|
876 | or empty list otherwise.""" | |
886 | call_obj = obj |
|
877 | call_obj = obj | |
887 | ret = [] |
|
878 | ret = [] | |
888 | if inspect.isbuiltin(obj): |
|
879 | if inspect.isbuiltin(obj): | |
889 | pass |
|
880 | pass | |
890 | elif not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
881 | elif not (inspect.isfunction(obj) or inspect.ismethod(obj)): | |
891 | if inspect.isclass(obj): |
|
882 | if inspect.isclass(obj): | |
892 | #for cython embededsignature=True the constructor docstring |
|
883 | #for cython embededsignature=True the constructor docstring | |
893 | #belongs to the object itself not __init__ |
|
884 | #belongs to the object itself not __init__ | |
894 | ret += self._default_arguments_from_docstring( |
|
885 | ret += self._default_arguments_from_docstring( | |
895 | getattr(obj, '__doc__', '')) |
|
886 | getattr(obj, '__doc__', '')) | |
896 | # for classes, check for __init__,__new__ |
|
887 | # for classes, check for __init__,__new__ | |
897 | call_obj = (getattr(obj, '__init__', None) or |
|
888 | call_obj = (getattr(obj, '__init__', None) or | |
898 | getattr(obj, '__new__', None)) |
|
889 | getattr(obj, '__new__', None)) | |
899 | # for all others, check if they are __call__able |
|
890 | # for all others, check if they are __call__able | |
900 | elif hasattr(obj, '__call__'): |
|
891 | elif hasattr(obj, '__call__'): | |
901 | call_obj = obj.__call__ |
|
892 | call_obj = obj.__call__ | |
902 | ret += self._default_arguments_from_docstring( |
|
893 | ret += self._default_arguments_from_docstring( | |
903 | getattr(call_obj, '__doc__', '')) |
|
894 | getattr(call_obj, '__doc__', '')) | |
904 |
|
895 | |||
905 | if PY3: |
|
896 | if PY3: | |
906 | _keeps = (inspect.Parameter.KEYWORD_ONLY, |
|
897 | _keeps = (inspect.Parameter.KEYWORD_ONLY, | |
907 | inspect.Parameter.POSITIONAL_OR_KEYWORD) |
|
898 | inspect.Parameter.POSITIONAL_OR_KEYWORD) | |
908 | signature = inspect.signature |
|
899 | signature = inspect.signature | |
909 | else: |
|
900 | else: | |
910 | import IPython.utils.signatures |
|
901 | import IPython.utils.signatures | |
911 | _keeps = (IPython.utils.signatures.Parameter.KEYWORD_ONLY, |
|
902 | _keeps = (IPython.utils.signatures.Parameter.KEYWORD_ONLY, | |
912 | IPython.utils.signatures.Parameter.POSITIONAL_OR_KEYWORD) |
|
903 | IPython.utils.signatures.Parameter.POSITIONAL_OR_KEYWORD) | |
913 | signature = IPython.utils.signatures.signature |
|
904 | signature = IPython.utils.signatures.signature | |
914 |
|
905 | |||
915 | try: |
|
906 | try: | |
916 | sig = signature(call_obj) |
|
907 | sig = signature(call_obj) | |
917 | ret.extend(k for k, v in sig.parameters.items() if |
|
908 | ret.extend(k for k, v in sig.parameters.items() if | |
918 | v.kind in _keeps) |
|
909 | v.kind in _keeps) | |
919 | except ValueError: |
|
910 | except ValueError: | |
920 | pass |
|
911 | pass | |
921 |
|
912 | |||
922 | return list(set(ret)) |
|
913 | return list(set(ret)) | |
923 |
|
914 | |||
924 | def python_func_kw_matches(self,text): |
|
915 | def python_func_kw_matches(self,text): | |
925 | """Match named parameters (kwargs) of the last open function""" |
|
916 | """Match named parameters (kwargs) of the last open function""" | |
926 |
|
917 | |||
927 | if "." in text: # a parameter cannot be dotted |
|
918 | if "." in text: # a parameter cannot be dotted | |
928 | return [] |
|
919 | return [] | |
929 | try: regexp = self.__funcParamsRegex |
|
920 | try: regexp = self.__funcParamsRegex | |
930 | except AttributeError: |
|
921 | except AttributeError: | |
931 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
922 | regexp = self.__funcParamsRegex = re.compile(r''' | |
932 | '.*?(?<!\\)' | # single quoted strings or |
|
923 | '.*?(?<!\\)' | # single quoted strings or | |
933 | ".*?(?<!\\)" | # double quoted strings or |
|
924 | ".*?(?<!\\)" | # double quoted strings or | |
934 | \w+ | # identifier |
|
925 | \w+ | # identifier | |
935 | \S # other characters |
|
926 | \S # other characters | |
936 | ''', re.VERBOSE | re.DOTALL) |
|
927 | ''', re.VERBOSE | re.DOTALL) | |
937 | # 1. find the nearest identifier that comes before an unclosed |
|
928 | # 1. find the nearest identifier that comes before an unclosed | |
938 | # parenthesis before the cursor |
|
929 | # parenthesis before the cursor | |
939 | # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo" |
|
930 | # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo" | |
940 | tokens = regexp.findall(self.text_until_cursor) |
|
931 | tokens = regexp.findall(self.text_until_cursor) | |
941 | tokens.reverse() |
|
932 | tokens.reverse() | |
942 | iterTokens = iter(tokens); openPar = 0 |
|
933 | iterTokens = iter(tokens); openPar = 0 | |
943 |
|
934 | |||
944 | for token in iterTokens: |
|
935 | for token in iterTokens: | |
945 | if token == ')': |
|
936 | if token == ')': | |
946 | openPar -= 1 |
|
937 | openPar -= 1 | |
947 | elif token == '(': |
|
938 | elif token == '(': | |
948 | openPar += 1 |
|
939 | openPar += 1 | |
949 | if openPar > 0: |
|
940 | if openPar > 0: | |
950 | # found the last unclosed parenthesis |
|
941 | # found the last unclosed parenthesis | |
951 | break |
|
942 | break | |
952 | else: |
|
943 | else: | |
953 | return [] |
|
944 | return [] | |
954 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
945 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) | |
955 | ids = [] |
|
946 | ids = [] | |
956 | isId = re.compile(r'\w+$').match |
|
947 | isId = re.compile(r'\w+$').match | |
957 |
|
948 | |||
958 | while True: |
|
949 | while True: | |
959 | try: |
|
950 | try: | |
960 | ids.append(next(iterTokens)) |
|
951 | ids.append(next(iterTokens)) | |
961 | if not isId(ids[-1]): |
|
952 | if not isId(ids[-1]): | |
962 | ids.pop(); break |
|
953 | ids.pop(); break | |
963 | if not next(iterTokens) == '.': |
|
954 | if not next(iterTokens) == '.': | |
964 | break |
|
955 | break | |
965 | except StopIteration: |
|
956 | except StopIteration: | |
966 | break |
|
957 | break | |
967 | # lookup the candidate callable matches either using global_matches |
|
958 | # lookup the candidate callable matches either using global_matches | |
968 | # or attr_matches for dotted names |
|
959 | # or attr_matches for dotted names | |
969 | if len(ids) == 1: |
|
960 | if len(ids) == 1: | |
970 | callableMatches = self.global_matches(ids[0]) |
|
961 | callableMatches = self.global_matches(ids[0]) | |
971 | else: |
|
962 | else: | |
972 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
963 | callableMatches = self.attr_matches('.'.join(ids[::-1])) | |
973 | argMatches = [] |
|
964 | argMatches = [] | |
974 | for callableMatch in callableMatches: |
|
965 | for callableMatch in callableMatches: | |
975 | try: |
|
966 | try: | |
976 | namedArgs = self._default_arguments(eval(callableMatch, |
|
967 | namedArgs = self._default_arguments(eval(callableMatch, | |
977 | self.namespace)) |
|
968 | self.namespace)) | |
978 | except: |
|
969 | except: | |
979 | continue |
|
970 | continue | |
980 |
|
971 | |||
981 | for namedArg in namedArgs: |
|
972 | for namedArg in namedArgs: | |
982 | if namedArg.startswith(text): |
|
973 | if namedArg.startswith(text): | |
983 | argMatches.append(u"%s=" %namedArg) |
|
974 | argMatches.append(u"%s=" %namedArg) | |
984 | return argMatches |
|
975 | return argMatches | |
985 |
|
976 | |||
986 | def dict_key_matches(self, text): |
|
977 | def dict_key_matches(self, text): | |
987 | "Match string keys in a dictionary, after e.g. 'foo[' " |
|
978 | "Match string keys in a dictionary, after e.g. 'foo[' " | |
988 | def get_keys(obj): |
|
979 | def get_keys(obj): | |
989 | # Objects can define their own completions by defining an |
|
980 | # Objects can define their own completions by defining an | |
990 | # _ipy_key_completions_() method. |
|
981 | # _ipy_key_completions_() method. | |
991 | method = get_real_method(obj, '_ipython_key_completions_') |
|
982 | method = get_real_method(obj, '_ipython_key_completions_') | |
992 | if method is not None: |
|
983 | if method is not None: | |
993 | return method() |
|
984 | return method() | |
994 |
|
985 | |||
995 | # Special case some common in-memory dict-like types |
|
986 | # Special case some common in-memory dict-like types | |
996 | if isinstance(obj, dict) or\ |
|
987 | if isinstance(obj, dict) or\ | |
997 | _safe_isinstance(obj, 'pandas', 'DataFrame'): |
|
988 | _safe_isinstance(obj, 'pandas', 'DataFrame'): | |
998 | try: |
|
989 | try: | |
999 | return list(obj.keys()) |
|
990 | return list(obj.keys()) | |
1000 | except Exception: |
|
991 | except Exception: | |
1001 | return [] |
|
992 | return [] | |
1002 | elif _safe_isinstance(obj, 'numpy', 'ndarray') or\ |
|
993 | elif _safe_isinstance(obj, 'numpy', 'ndarray') or\ | |
1003 | _safe_isinstance(obj, 'numpy', 'void'): |
|
994 | _safe_isinstance(obj, 'numpy', 'void'): | |
1004 | return obj.dtype.names or [] |
|
995 | return obj.dtype.names or [] | |
1005 | return [] |
|
996 | return [] | |
1006 |
|
997 | |||
1007 | try: |
|
998 | try: | |
1008 | regexps = self.__dict_key_regexps |
|
999 | regexps = self.__dict_key_regexps | |
1009 | except AttributeError: |
|
1000 | except AttributeError: | |
1010 | dict_key_re_fmt = r'''(?x) |
|
1001 | dict_key_re_fmt = r'''(?x) | |
1011 | ( # match dict-referring expression wrt greedy setting |
|
1002 | ( # match dict-referring expression wrt greedy setting | |
1012 | %s |
|
1003 | %s | |
1013 | ) |
|
1004 | ) | |
1014 | \[ # open bracket |
|
1005 | \[ # open bracket | |
1015 | \s* # and optional whitespace |
|
1006 | \s* # and optional whitespace | |
1016 | ([uUbB]? # string prefix (r not handled) |
|
1007 | ([uUbB]? # string prefix (r not handled) | |
1017 | (?: # unclosed string |
|
1008 | (?: # unclosed string | |
1018 | '(?:[^']|(?<!\\)\\')* |
|
1009 | '(?:[^']|(?<!\\)\\')* | |
1019 | | |
|
1010 | | | |
1020 | "(?:[^"]|(?<!\\)\\")* |
|
1011 | "(?:[^"]|(?<!\\)\\")* | |
1021 | ) |
|
1012 | ) | |
1022 | )? |
|
1013 | )? | |
1023 | $ |
|
1014 | $ | |
1024 | ''' |
|
1015 | ''' | |
1025 | regexps = self.__dict_key_regexps = { |
|
1016 | regexps = self.__dict_key_regexps = { | |
1026 | False: re.compile(dict_key_re_fmt % ''' |
|
1017 | False: re.compile(dict_key_re_fmt % ''' | |
1027 | # identifiers separated by . |
|
1018 | # identifiers separated by . | |
1028 | (?!\d)\w+ |
|
1019 | (?!\d)\w+ | |
1029 | (?:\.(?!\d)\w+)* |
|
1020 | (?:\.(?!\d)\w+)* | |
1030 | '''), |
|
1021 | '''), | |
1031 | True: re.compile(dict_key_re_fmt % ''' |
|
1022 | True: re.compile(dict_key_re_fmt % ''' | |
1032 | .+ |
|
1023 | .+ | |
1033 | ''') |
|
1024 | ''') | |
1034 | } |
|
1025 | } | |
1035 |
|
1026 | |||
1036 | match = regexps[self.greedy].search(self.text_until_cursor) |
|
1027 | match = regexps[self.greedy].search(self.text_until_cursor) | |
1037 | if match is None: |
|
1028 | if match is None: | |
1038 | return [] |
|
1029 | return [] | |
1039 |
|
1030 | |||
1040 | expr, prefix = match.groups() |
|
1031 | expr, prefix = match.groups() | |
1041 | try: |
|
1032 | try: | |
1042 | obj = eval(expr, self.namespace) |
|
1033 | obj = eval(expr, self.namespace) | |
1043 | except Exception: |
|
1034 | except Exception: | |
1044 | try: |
|
1035 | try: | |
1045 | obj = eval(expr, self.global_namespace) |
|
1036 | obj = eval(expr, self.global_namespace) | |
1046 | except Exception: |
|
1037 | except Exception: | |
1047 | return [] |
|
1038 | return [] | |
1048 |
|
1039 | |||
1049 | keys = get_keys(obj) |
|
1040 | keys = get_keys(obj) | |
1050 | if not keys: |
|
1041 | if not keys: | |
1051 | return keys |
|
1042 | return keys | |
1052 | closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims) |
|
1043 | closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims) | |
1053 | if not matches: |
|
1044 | if not matches: | |
1054 | return matches |
|
1045 | return matches | |
1055 |
|
1046 | |||
1056 | # get the cursor position of |
|
1047 | # get the cursor position of | |
1057 | # - the text being completed |
|
1048 | # - the text being completed | |
1058 | # - the start of the key text |
|
1049 | # - the start of the key text | |
1059 | # - the start of the completion |
|
1050 | # - the start of the completion | |
1060 | text_start = len(self.text_until_cursor) - len(text) |
|
1051 | text_start = len(self.text_until_cursor) - len(text) | |
1061 | if prefix: |
|
1052 | if prefix: | |
1062 | key_start = match.start(2) |
|
1053 | key_start = match.start(2) | |
1063 | completion_start = key_start + token_offset |
|
1054 | completion_start = key_start + token_offset | |
1064 | else: |
|
1055 | else: | |
1065 | key_start = completion_start = match.end() |
|
1056 | key_start = completion_start = match.end() | |
1066 |
|
1057 | |||
1067 | # grab the leading prefix, to make sure all completions start with `text` |
|
1058 | # grab the leading prefix, to make sure all completions start with `text` | |
1068 | if text_start > key_start: |
|
1059 | if text_start > key_start: | |
1069 | leading = '' |
|
1060 | leading = '' | |
1070 | else: |
|
1061 | else: | |
1071 | leading = text[text_start:completion_start] |
|
1062 | leading = text[text_start:completion_start] | |
1072 |
|
1063 | |||
1073 | # the index of the `[` character |
|
1064 | # the index of the `[` character | |
1074 | bracket_idx = match.end(1) |
|
1065 | bracket_idx = match.end(1) | |
1075 |
|
1066 | |||
1076 | # append closing quote and bracket as appropriate |
|
1067 | # append closing quote and bracket as appropriate | |
1077 | # this is *not* appropriate if the opening quote or bracket is outside |
|
1068 | # this is *not* appropriate if the opening quote or bracket is outside | |
1078 | # the text given to this method |
|
1069 | # the text given to this method | |
1079 | suf = '' |
|
1070 | suf = '' | |
1080 | continuation = self.line_buffer[len(self.text_until_cursor):] |
|
1071 | continuation = self.line_buffer[len(self.text_until_cursor):] | |
1081 | if key_start > text_start and closing_quote: |
|
1072 | if key_start > text_start and closing_quote: | |
1082 | # quotes were opened inside text, maybe close them |
|
1073 | # quotes were opened inside text, maybe close them | |
1083 | if continuation.startswith(closing_quote): |
|
1074 | if continuation.startswith(closing_quote): | |
1084 | continuation = continuation[len(closing_quote):] |
|
1075 | continuation = continuation[len(closing_quote):] | |
1085 | else: |
|
1076 | else: | |
1086 | suf += closing_quote |
|
1077 | suf += closing_quote | |
1087 | if bracket_idx > text_start: |
|
1078 | if bracket_idx > text_start: | |
1088 | # brackets were opened inside text, maybe close them |
|
1079 | # brackets were opened inside text, maybe close them | |
1089 | if not continuation.startswith(']'): |
|
1080 | if not continuation.startswith(']'): | |
1090 | suf += ']' |
|
1081 | suf += ']' | |
1091 |
|
1082 | |||
1092 | return [leading + k + suf for k in matches] |
|
1083 | return [leading + k + suf for k in matches] | |
1093 |
|
1084 | |||
1094 | def unicode_name_matches(self, text): |
|
1085 | def unicode_name_matches(self, text): | |
1095 | u"""Match Latex-like syntax for unicode characters base |
|
1086 | u"""Match Latex-like syntax for unicode characters base | |
1096 | on the name of the character. |
|
1087 | on the name of the character. | |
1097 |
|
1088 | |||
1098 | This does \\GREEK SMALL LETTER ETA -> Ξ· |
|
1089 | This does \\GREEK SMALL LETTER ETA -> Ξ· | |
1099 |
|
1090 | |||
1100 | Works only on valid python 3 identifier, or on combining characters that |
|
1091 | Works only on valid python 3 identifier, or on combining characters that | |
1101 | will combine to form a valid identifier. |
|
1092 | will combine to form a valid identifier. | |
1102 |
|
1093 | |||
1103 | Used on Python 3 only. |
|
1094 | Used on Python 3 only. | |
1104 | """ |
|
1095 | """ | |
1105 | slashpos = text.rfind('\\') |
|
1096 | slashpos = text.rfind('\\') | |
1106 | if slashpos > -1: |
|
1097 | if slashpos > -1: | |
1107 | s = text[slashpos+1:] |
|
1098 | s = text[slashpos+1:] | |
1108 | try : |
|
1099 | try : | |
1109 | unic = unicodedata.lookup(s) |
|
1100 | unic = unicodedata.lookup(s) | |
1110 | # allow combining chars |
|
1101 | # allow combining chars | |
1111 | if ('a'+unic).isidentifier(): |
|
1102 | if ('a'+unic).isidentifier(): | |
1112 | return '\\'+s,[unic] |
|
1103 | return '\\'+s,[unic] | |
1113 | except KeyError as e: |
|
1104 | except KeyError as e: | |
1114 | pass |
|
1105 | pass | |
1115 | return u'', [] |
|
1106 | return u'', [] | |
1116 |
|
1107 | |||
1117 |
|
1108 | |||
1118 |
|
1109 | |||
1119 |
|
1110 | |||
1120 | def latex_matches(self, text): |
|
1111 | def latex_matches(self, text): | |
1121 | u"""Match Latex syntax for unicode characters. |
|
1112 | u"""Match Latex syntax for unicode characters. | |
1122 |
|
1113 | |||
1123 | This does both \\alp -> \\alpha and \\alpha -> Ξ± |
|
1114 | This does both \\alp -> \\alpha and \\alpha -> Ξ± | |
1124 |
|
1115 | |||
1125 | Used on Python 3 only. |
|
1116 | Used on Python 3 only. | |
1126 | """ |
|
1117 | """ | |
1127 | slashpos = text.rfind('\\') |
|
1118 | slashpos = text.rfind('\\') | |
1128 | if slashpos > -1: |
|
1119 | if slashpos > -1: | |
1129 | s = text[slashpos:] |
|
1120 | s = text[slashpos:] | |
1130 | if s in latex_symbols: |
|
1121 | if s in latex_symbols: | |
1131 | # Try to complete a full latex symbol to unicode |
|
1122 | # Try to complete a full latex symbol to unicode | |
1132 | # \\alpha -> Ξ± |
|
1123 | # \\alpha -> Ξ± | |
1133 | return s, [latex_symbols[s]] |
|
1124 | return s, [latex_symbols[s]] | |
1134 | else: |
|
1125 | else: | |
1135 | # If a user has partially typed a latex symbol, give them |
|
1126 | # If a user has partially typed a latex symbol, give them | |
1136 | # a full list of options \al -> [\aleph, \alpha] |
|
1127 | # a full list of options \al -> [\aleph, \alpha] | |
1137 | matches = [k for k in latex_symbols if k.startswith(s)] |
|
1128 | matches = [k for k in latex_symbols if k.startswith(s)] | |
1138 | return s, matches |
|
1129 | return s, matches | |
1139 | return u'', [] |
|
1130 | return u'', [] | |
1140 |
|
1131 | |||
1141 | def dispatch_custom_completer(self, text): |
|
1132 | def dispatch_custom_completer(self, text): | |
1142 | line = self.line_buffer |
|
1133 | line = self.line_buffer | |
1143 | if not line.strip(): |
|
1134 | if not line.strip(): | |
1144 | return None |
|
1135 | return None | |
1145 |
|
1136 | |||
1146 | # Create a little structure to pass all the relevant information about |
|
1137 | # Create a little structure to pass all the relevant information about | |
1147 | # the current completion to any custom completer. |
|
1138 | # the current completion to any custom completer. | |
1148 | event = Bunch() |
|
1139 | event = Bunch() | |
1149 | event.line = line |
|
1140 | event.line = line | |
1150 | event.symbol = text |
|
1141 | event.symbol = text | |
1151 | cmd = line.split(None,1)[0] |
|
1142 | cmd = line.split(None,1)[0] | |
1152 | event.command = cmd |
|
1143 | event.command = cmd | |
1153 | event.text_until_cursor = self.text_until_cursor |
|
1144 | event.text_until_cursor = self.text_until_cursor | |
1154 |
|
1145 | |||
1155 | # for foo etc, try also to find completer for %foo |
|
1146 | # for foo etc, try also to find completer for %foo | |
1156 | if not cmd.startswith(self.magic_escape): |
|
1147 | if not cmd.startswith(self.magic_escape): | |
1157 | try_magic = self.custom_completers.s_matches( |
|
1148 | try_magic = self.custom_completers.s_matches( | |
1158 | self.magic_escape + cmd) |
|
1149 | self.magic_escape + cmd) | |
1159 | else: |
|
1150 | else: | |
1160 | try_magic = [] |
|
1151 | try_magic = [] | |
1161 |
|
1152 | |||
1162 | for c in itertools.chain(self.custom_completers.s_matches(cmd), |
|
1153 | for c in itertools.chain(self.custom_completers.s_matches(cmd), | |
1163 | try_magic, |
|
1154 | try_magic, | |
1164 | self.custom_completers.flat_matches(self.text_until_cursor)): |
|
1155 | self.custom_completers.flat_matches(self.text_until_cursor)): | |
1165 | try: |
|
1156 | try: | |
1166 | res = c(event) |
|
1157 | res = c(event) | |
1167 | if res: |
|
1158 | if res: | |
1168 | # first, try case sensitive match |
|
1159 | # first, try case sensitive match | |
1169 | withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)] |
|
1160 | withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)] | |
1170 | if withcase: |
|
1161 | if withcase: | |
1171 | return withcase |
|
1162 | return withcase | |
1172 | # if none, then case insensitive ones are ok too |
|
1163 | # if none, then case insensitive ones are ok too | |
1173 | text_low = text.lower() |
|
1164 | text_low = text.lower() | |
1174 | return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)] |
|
1165 | return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)] | |
1175 | except TryNext: |
|
1166 | except TryNext: | |
1176 | pass |
|
1167 | pass | |
1177 |
|
1168 | |||
1178 | return None |
|
1169 | return None | |
1179 |
|
1170 | |||
1180 | def complete(self, text=None, line_buffer=None, cursor_pos=None): |
|
1171 | def complete(self, text=None, line_buffer=None, cursor_pos=None): | |
1181 | """Find completions for the given text and line context. |
|
1172 | """Find completions for the given text and line context. | |
1182 |
|
1173 | |||
1183 | Note that both the text and the line_buffer are optional, but at least |
|
1174 | Note that both the text and the line_buffer are optional, but at least | |
1184 | one of them must be given. |
|
1175 | one of them must be given. | |
1185 |
|
1176 | |||
1186 | Parameters |
|
1177 | Parameters | |
1187 | ---------- |
|
1178 | ---------- | |
1188 | text : string, optional |
|
1179 | text : string, optional | |
1189 | Text to perform the completion on. If not given, the line buffer |
|
1180 | Text to perform the completion on. If not given, the line buffer | |
1190 | is split using the instance's CompletionSplitter object. |
|
1181 | is split using the instance's CompletionSplitter object. | |
1191 |
|
1182 | |||
1192 | line_buffer : string, optional |
|
1183 | line_buffer : string, optional | |
1193 | If not given, the completer attempts to obtain the current line |
|
1184 | If not given, the completer attempts to obtain the current line | |
1194 | buffer via readline. This keyword allows clients which are |
|
1185 | buffer via readline. This keyword allows clients which are | |
1195 | requesting for text completions in non-readline contexts to inform |
|
1186 | requesting for text completions in non-readline contexts to inform | |
1196 | the completer of the entire text. |
|
1187 | the completer of the entire text. | |
1197 |
|
1188 | |||
1198 | cursor_pos : int, optional |
|
1189 | cursor_pos : int, optional | |
1199 | Index of the cursor in the full line buffer. Should be provided by |
|
1190 | Index of the cursor in the full line buffer. Should be provided by | |
1200 | remote frontends where kernel has no access to frontend state. |
|
1191 | remote frontends where kernel has no access to frontend state. | |
1201 |
|
1192 | |||
1202 | Returns |
|
1193 | Returns | |
1203 | ------- |
|
1194 | ------- | |
1204 | text : str |
|
1195 | text : str | |
1205 | Text that was actually used in the completion. |
|
1196 | Text that was actually used in the completion. | |
1206 |
|
1197 | |||
1207 | matches : list |
|
1198 | matches : list | |
1208 | A list of completion matches. |
|
1199 | A list of completion matches. | |
1209 | """ |
|
1200 | """ | |
1210 | # if the cursor position isn't given, the only sane assumption we can |
|
1201 | # if the cursor position isn't given, the only sane assumption we can | |
1211 | # make is that it's at the end of the line (the common case) |
|
1202 | # make is that it's at the end of the line (the common case) | |
1212 | if cursor_pos is None: |
|
1203 | if cursor_pos is None: | |
1213 | cursor_pos = len(line_buffer) if text is None else len(text) |
|
1204 | cursor_pos = len(line_buffer) if text is None else len(text) | |
1214 |
|
1205 | |||
1215 | if PY3: |
|
1206 | if PY3: | |
1216 |
|
1207 | |||
1217 | base_text = text if not line_buffer else line_buffer[:cursor_pos] |
|
1208 | base_text = text if not line_buffer else line_buffer[:cursor_pos] | |
1218 | latex_text, latex_matches = self.latex_matches(base_text) |
|
1209 | latex_text, latex_matches = self.latex_matches(base_text) | |
1219 | if latex_matches: |
|
1210 | if latex_matches: | |
1220 | return latex_text, latex_matches |
|
1211 | return latex_text, latex_matches | |
1221 | name_text = '' |
|
1212 | name_text = '' | |
1222 | name_matches = [] |
|
1213 | name_matches = [] | |
1223 | for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches): |
|
1214 | for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches): | |
1224 | name_text, name_matches = meth(base_text) |
|
1215 | name_text, name_matches = meth(base_text) | |
1225 | if name_text: |
|
1216 | if name_text: | |
1226 | return name_text, name_matches |
|
1217 | return name_text, name_matches | |
1227 |
|
1218 | |||
1228 | # if text is either None or an empty string, rely on the line buffer |
|
1219 | # if text is either None or an empty string, rely on the line buffer | |
1229 | if not text: |
|
1220 | if not text: | |
1230 | text = self.splitter.split_line(line_buffer, cursor_pos) |
|
1221 | text = self.splitter.split_line(line_buffer, cursor_pos) | |
1231 |
|
1222 | |||
1232 | # If no line buffer is given, assume the input text is all there was |
|
1223 | # If no line buffer is given, assume the input text is all there was | |
1233 | if line_buffer is None: |
|
1224 | if line_buffer is None: | |
1234 | line_buffer = text |
|
1225 | line_buffer = text | |
1235 |
|
1226 | |||
1236 | self.line_buffer = line_buffer |
|
1227 | self.line_buffer = line_buffer | |
1237 | self.text_until_cursor = self.line_buffer[:cursor_pos] |
|
1228 | self.text_until_cursor = self.line_buffer[:cursor_pos] | |
1238 |
|
1229 | |||
1239 | # Start with a clean slate of completions |
|
1230 | # Start with a clean slate of completions | |
1240 | self.matches[:] = [] |
|
1231 | self.matches[:] = [] | |
1241 | custom_res = self.dispatch_custom_completer(text) |
|
1232 | custom_res = self.dispatch_custom_completer(text) | |
1242 | if custom_res is not None: |
|
1233 | if custom_res is not None: | |
1243 | # did custom completers produce something? |
|
1234 | # did custom completers produce something? | |
1244 | self.matches = custom_res |
|
1235 | self.matches = custom_res | |
1245 | else: |
|
1236 | else: | |
1246 | # Extend the list of completions with the results of each |
|
1237 | # Extend the list of completions with the results of each | |
1247 | # matcher, so we return results to the user from all |
|
1238 | # matcher, so we return results to the user from all | |
1248 | # namespaces. |
|
1239 | # namespaces. | |
1249 | if self.merge_completions: |
|
1240 | if self.merge_completions: | |
1250 | self.matches = [] |
|
1241 | self.matches = [] | |
1251 | for matcher in self.matchers: |
|
1242 | for matcher in self.matchers: | |
1252 | try: |
|
1243 | try: | |
1253 | self.matches.extend(matcher(text)) |
|
1244 | self.matches.extend(matcher(text)) | |
1254 | except: |
|
1245 | except: | |
1255 | # Show the ugly traceback if the matcher causes an |
|
1246 | # Show the ugly traceback if the matcher causes an | |
1256 | # exception, but do NOT crash the kernel! |
|
1247 | # exception, but do NOT crash the kernel! | |
1257 | sys.excepthook(*sys.exc_info()) |
|
1248 | sys.excepthook(*sys.exc_info()) | |
1258 | else: |
|
1249 | else: | |
1259 | for matcher in self.matchers: |
|
1250 | for matcher in self.matchers: | |
1260 | self.matches = matcher(text) |
|
1251 | self.matches = matcher(text) | |
1261 | if self.matches: |
|
1252 | if self.matches: | |
1262 | break |
|
1253 | break | |
1263 | # FIXME: we should extend our api to return a dict with completions for |
|
1254 | # FIXME: we should extend our api to return a dict with completions for | |
1264 | # different types of objects. The rlcomplete() method could then |
|
1255 | # different types of objects. The rlcomplete() method could then | |
1265 | # simply collapse the dict into a list for readline, but we'd have |
|
1256 | # simply collapse the dict into a list for readline, but we'd have | |
1266 | # richer completion semantics in other evironments. |
|
1257 | # richer completion semantics in other evironments. | |
1267 | if self.use_jedi_completions: |
|
|||
1268 |
|
|
1258 | self.matches.extend(self.python_jedi_matches(text, line_buffer, cursor_pos)) | |
1269 |
|
1259 | |||
1270 | self.matches = sorted(set(self.matches), key=completions_sorting_key) |
|
1260 | self.matches = sorted(set(self.matches), key=completions_sorting_key) | |
1271 |
|
1261 | |||
1272 | return text, self.matches |
|
1262 | return text, self.matches | |
1273 |
|
1263 | |||
1274 | def rlcomplete(self, text, state): |
|
1264 | def rlcomplete(self, text, state): | |
1275 | """Return the state-th possible completion for 'text'. |
|
1265 | """Return the state-th possible completion for 'text'. | |
1276 |
|
1266 | |||
1277 | This is called successively with state == 0, 1, 2, ... until it |
|
1267 | This is called successively with state == 0, 1, 2, ... until it | |
1278 | returns None. The completion should begin with 'text'. |
|
1268 | returns None. The completion should begin with 'text'. | |
1279 |
|
1269 | |||
1280 | Parameters |
|
1270 | Parameters | |
1281 | ---------- |
|
1271 | ---------- | |
1282 | text : string |
|
1272 | text : string | |
1283 | Text to perform the completion on. |
|
1273 | Text to perform the completion on. | |
1284 |
|
1274 | |||
1285 | state : int |
|
1275 | state : int | |
1286 | Counter used by readline. |
|
1276 | Counter used by readline. | |
1287 | """ |
|
1277 | """ | |
1288 | if state==0: |
|
1278 | if state==0: | |
1289 |
|
1279 | |||
1290 | self.line_buffer = line_buffer = self.readline.get_line_buffer() |
|
1280 | self.line_buffer = line_buffer = self.readline.get_line_buffer() | |
1291 | cursor_pos = self.readline.get_endidx() |
|
1281 | cursor_pos = self.readline.get_endidx() | |
1292 |
|
1282 | |||
1293 | #io.rprint("\nRLCOMPLETE: %r %r %r" % |
|
1283 | #io.rprint("\nRLCOMPLETE: %r %r %r" % | |
1294 | # (text, line_buffer, cursor_pos) ) # dbg |
|
1284 | # (text, line_buffer, cursor_pos) ) # dbg | |
1295 |
|
1285 | |||
1296 | # if there is only a tab on a line with only whitespace, instead of |
|
1286 | # if there is only a tab on a line with only whitespace, instead of | |
1297 | # the mostly useless 'do you want to see all million completions' |
|
1287 | # the mostly useless 'do you want to see all million completions' | |
1298 | # message, just do the right thing and give the user his tab! |
|
1288 | # message, just do the right thing and give the user his tab! | |
1299 | # Incidentally, this enables pasting of tabbed text from an editor |
|
1289 | # Incidentally, this enables pasting of tabbed text from an editor | |
1300 | # (as long as autoindent is off). |
|
1290 | # (as long as autoindent is off). | |
1301 |
|
1291 | |||
1302 | # It should be noted that at least pyreadline still shows file |
|
1292 | # It should be noted that at least pyreadline still shows file | |
1303 | # completions - is there a way around it? |
|
1293 | # completions - is there a way around it? | |
1304 |
|
1294 | |||
1305 | # don't apply this on 'dumb' terminals, such as emacs buffers, so |
|
1295 | # don't apply this on 'dumb' terminals, such as emacs buffers, so | |
1306 | # we don't interfere with their own tab-completion mechanism. |
|
1296 | # we don't interfere with their own tab-completion mechanism. | |
1307 | if not (self.dumb_terminal or line_buffer.strip()): |
|
1297 | if not (self.dumb_terminal or line_buffer.strip()): | |
1308 | self.readline.insert_text('\t') |
|
1298 | self.readline.insert_text('\t') | |
1309 | sys.stdout.flush() |
|
1299 | sys.stdout.flush() | |
1310 | return None |
|
1300 | return None | |
1311 |
|
1301 | |||
1312 | # Note: debugging exceptions that may occur in completion is very |
|
1302 | # Note: debugging exceptions that may occur in completion is very | |
1313 | # tricky, because readline unconditionally silences them. So if |
|
1303 | # tricky, because readline unconditionally silences them. So if | |
1314 | # during development you suspect a bug in the completion code, turn |
|
1304 | # during development you suspect a bug in the completion code, turn | |
1315 | # this flag on temporarily by uncommenting the second form (don't |
|
1305 | # this flag on temporarily by uncommenting the second form (don't | |
1316 | # flip the value in the first line, as the '# dbg' marker can be |
|
1306 | # flip the value in the first line, as the '# dbg' marker can be | |
1317 | # automatically detected and is used elsewhere). |
|
1307 | # automatically detected and is used elsewhere). | |
1318 | DEBUG = False |
|
1308 | DEBUG = False | |
1319 | #DEBUG = True # dbg |
|
1309 | #DEBUG = True # dbg | |
1320 | if DEBUG: |
|
1310 | if DEBUG: | |
1321 | try: |
|
1311 | try: | |
1322 | self.complete(text, line_buffer, cursor_pos) |
|
1312 | self.complete(text, line_buffer, cursor_pos) | |
1323 | except: |
|
1313 | except: | |
1324 | import traceback; traceback.print_exc() |
|
1314 | import traceback; traceback.print_exc() | |
1325 | else: |
|
1315 | else: | |
1326 | # The normal production version is here |
|
1316 | # The normal production version is here | |
1327 |
|
1317 | |||
1328 | # This method computes the self.matches array |
|
1318 | # This method computes the self.matches array | |
1329 | self.complete(text, line_buffer, cursor_pos) |
|
1319 | self.complete(text, line_buffer, cursor_pos) | |
1330 |
|
1320 | |||
1331 | try: |
|
1321 | try: | |
1332 | return self.matches[state] |
|
1322 | return self.matches[state] | |
1333 | except IndexError: |
|
1323 | except IndexError: | |
1334 | return None |
|
1324 | return None | |
1335 |
|
1325 |
General Comments 0
You need to be logged in to leave comments.
Login now