Show More
@@ -1,933 +1,980 b'' | |||||
1 | """Word completion for IPython. |
|
1 | """Word completion for IPython. | |
2 |
|
2 | |||
3 | This module is a fork of the rlcompleter module in the Python standard |
|
3 | This module is a fork of the rlcompleter module in the Python standard | |
4 | library. The original enhancements made to rlcompleter have been sent |
|
4 | library. The original enhancements made to rlcompleter have been sent | |
5 | upstream and were accepted as of Python 2.3, but we need a lot more |
|
5 | upstream and were accepted as of Python 2.3, but we need a lot more | |
6 | functionality specific to IPython, so this module will continue to live as an |
|
6 | functionality specific to IPython, so this module will continue to live as an | |
7 | IPython-specific utility. |
|
7 | IPython-specific utility. | |
8 |
|
8 | |||
9 | Original rlcompleter documentation: |
|
9 | Original rlcompleter documentation: | |
10 |
|
10 | |||
11 | This requires the latest extension to the readline module (the |
|
11 | This requires the latest extension to the readline module (the | |
12 | completes keywords, built-ins and globals in __main__; when completing |
|
12 | completes keywords, built-ins and globals in __main__; when completing | |
13 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
|
13 | NAME.NAME..., it evaluates (!) the expression up to the last dot and | |
14 | completes its attributes. |
|
14 | completes its attributes. | |
15 |
|
15 | |||
16 | It's very cool to do "import string" type "string.", hit the |
|
16 | It's very cool to do "import string" type "string.", hit the | |
17 | completion key (twice), and see the list of names defined by the |
|
17 | completion key (twice), and see the list of names defined by the | |
18 | string module! |
|
18 | string module! | |
19 |
|
19 | |||
20 | Tip: to use the tab key as the completion key, call |
|
20 | Tip: to use the tab key as the completion key, call | |
21 |
|
21 | |||
22 | readline.parse_and_bind("tab: complete") |
|
22 | readline.parse_and_bind("tab: complete") | |
23 |
|
23 | |||
24 | Notes: |
|
24 | Notes: | |
25 |
|
25 | |||
26 | - Exceptions raised by the completer function are *ignored* (and |
|
26 | - Exceptions raised by the completer function are *ignored* (and | |
27 | generally cause the completion to fail). This is a feature -- since |
|
27 | generally cause the completion to fail). This is a feature -- since | |
28 | readline sets the tty device in raw (or cbreak) mode, printing a |
|
28 | readline sets the tty device in raw (or cbreak) mode, printing a | |
29 | traceback wouldn't work well without some complicated hoopla to save, |
|
29 | traceback wouldn't work well without some complicated hoopla to save, | |
30 | reset and restore the tty state. |
|
30 | reset and restore the tty state. | |
31 |
|
31 | |||
32 | - The evaluation of the NAME.NAME... form may cause arbitrary |
|
32 | - The evaluation of the NAME.NAME... form may cause arbitrary | |
33 | application defined code to be executed if an object with a |
|
33 | application defined code to be executed if an object with a | |
34 | __getattr__ hook is found. Since it is the responsibility of the |
|
34 | __getattr__ hook is found. Since it is the responsibility of the | |
35 | application (or the user) to enable this feature, I consider this an |
|
35 | application (or the user) to enable this feature, I consider this an | |
36 | acceptable risk. More complicated expressions (e.g. function calls or |
|
36 | acceptable risk. More complicated expressions (e.g. function calls or | |
37 | indexing operations) are *not* evaluated. |
|
37 | indexing operations) are *not* evaluated. | |
38 |
|
38 | |||
39 | - GNU readline is also used by the built-in functions input() and |
|
39 | - GNU readline is also used by the built-in functions input() and | |
40 | raw_input(), and thus these also benefit/suffer from the completer |
|
40 | raw_input(), and thus these also benefit/suffer from the completer | |
41 | features. Clearly an interactive application can benefit by |
|
41 | features. Clearly an interactive application can benefit by | |
42 | specifying its own completer function and using raw_input() for all |
|
42 | specifying its own completer function and using raw_input() for all | |
43 | its input. |
|
43 | its input. | |
44 |
|
44 | |||
45 | - When the original stdin is not a tty device, GNU readline is never |
|
45 | - When the original stdin is not a tty device, GNU readline is never | |
46 | used, and this module (and the readline module) are silently inactive. |
|
46 | used, and this module (and the readline module) are silently inactive. | |
47 | """ |
|
47 | """ | |
48 |
|
48 | |||
49 | #***************************************************************************** |
|
49 | #***************************************************************************** | |
50 | # |
|
50 | # | |
51 | # Since this file is essentially a minimally modified copy of the rlcompleter |
|
51 | # Since this file is essentially a minimally modified copy of the rlcompleter | |
52 | # module which is part of the standard Python distribution, I assume that the |
|
52 | # module which is part of the standard Python distribution, I assume that the | |
53 | # proper procedure is to maintain its copyright as belonging to the Python |
|
53 | # proper procedure is to maintain its copyright as belonging to the Python | |
54 | # Software Foundation (in addition to my own, for all new code). |
|
54 | # Software Foundation (in addition to my own, for all new code). | |
55 | # |
|
55 | # | |
56 | # Copyright (C) 2008 IPython Development Team |
|
56 | # Copyright (C) 2008 IPython Development Team | |
57 | # Copyright (C) 2001 Fernando Perez. <fperez@colorado.edu> |
|
57 | # Copyright (C) 2001 Fernando Perez. <fperez@colorado.edu> | |
58 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
58 | # Copyright (C) 2001 Python Software Foundation, www.python.org | |
59 | # |
|
59 | # | |
60 | # Distributed under the terms of the BSD License. The full license is in |
|
60 | # Distributed under the terms of the BSD License. The full license is in | |
61 | # the file COPYING, distributed as part of this software. |
|
61 | # the file COPYING, distributed as part of this software. | |
62 | # |
|
62 | # | |
63 | #***************************************************************************** |
|
63 | #***************************************************************************** | |
64 |
|
64 | |||
65 | #----------------------------------------------------------------------------- |
|
65 | #----------------------------------------------------------------------------- | |
66 | # Imports |
|
66 | # Imports | |
67 | #----------------------------------------------------------------------------- |
|
67 | #----------------------------------------------------------------------------- | |
68 |
|
68 | |||
69 | import __builtin__ |
|
69 | import __builtin__ | |
70 | import __main__ |
|
70 | import __main__ | |
71 | import glob |
|
71 | import glob | |
72 | import inspect |
|
72 | import inspect | |
73 | import itertools |
|
73 | import itertools | |
74 | import keyword |
|
74 | import keyword | |
75 | import os |
|
75 | import os | |
76 | import re |
|
76 | import re | |
77 | import shlex |
|
77 | import shlex | |
78 | import sys |
|
78 | import sys | |
79 |
|
79 | |||
80 | from IPython.config.configurable import Configurable |
|
80 | from IPython.config.configurable import Configurable | |
81 | from IPython.core.error import TryNext |
|
81 | from IPython.core.error import TryNext | |
82 | from IPython.core.inputsplitter import ESC_MAGIC |
|
82 | from IPython.core.inputsplitter import ESC_MAGIC | |
83 | from IPython.utils import generics |
|
83 | from IPython.utils import generics | |
84 | from IPython.utils import io |
|
84 | from IPython.utils import io | |
85 | from IPython.utils.dir2 import dir2 |
|
85 | from IPython.utils.dir2 import dir2 | |
86 | from IPython.utils.process import arg_split |
|
86 | from IPython.utils.process import arg_split | |
87 | from IPython.utils.traitlets import CBool, Enum |
|
87 | from IPython.utils.traitlets import CBool, Enum | |
88 |
|
88 | |||
89 | #----------------------------------------------------------------------------- |
|
89 | #----------------------------------------------------------------------------- | |
90 | # Globals |
|
90 | # Globals | |
91 | #----------------------------------------------------------------------------- |
|
91 | #----------------------------------------------------------------------------- | |
92 |
|
92 | |||
93 | # Public API |
|
93 | # Public API | |
94 | __all__ = ['Completer','IPCompleter'] |
|
94 | __all__ = ['Completer','IPCompleter'] | |
95 |
|
95 | |||
96 | if sys.platform == 'win32': |
|
96 | if sys.platform == 'win32': | |
97 | PROTECTABLES = ' ' |
|
97 | PROTECTABLES = ' ' | |
98 | else: |
|
98 | else: | |
99 | PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&' |
|
99 | PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&' | |
100 |
|
100 | |||
101 | #----------------------------------------------------------------------------- |
|
101 | #----------------------------------------------------------------------------- | |
102 | # Main functions and classes |
|
102 | # Main functions and classes | |
103 | #----------------------------------------------------------------------------- |
|
103 | #----------------------------------------------------------------------------- | |
104 |
|
104 | |||
105 | def has_open_quotes(s): |
|
105 | def has_open_quotes(s): | |
106 | """Return whether a string has open quotes. |
|
106 | """Return whether a string has open quotes. | |
107 |
|
107 | |||
108 | This simply counts whether the number of quote characters of either type in |
|
108 | This simply counts whether the number of quote characters of either type in | |
109 | the string is odd. |
|
109 | the string is odd. | |
110 |
|
110 | |||
111 | Returns |
|
111 | Returns | |
112 | ------- |
|
112 | ------- | |
113 | If there is an open quote, the quote character is returned. Else, return |
|
113 | If there is an open quote, the quote character is returned. Else, return | |
114 | False. |
|
114 | False. | |
115 | """ |
|
115 | """ | |
116 | # We check " first, then ', so complex cases with nested quotes will get |
|
116 | # We check " first, then ', so complex cases with nested quotes will get | |
117 | # the " to take precedence. |
|
117 | # the " to take precedence. | |
118 | if s.count('"') % 2: |
|
118 | if s.count('"') % 2: | |
119 | return '"' |
|
119 | return '"' | |
120 | elif s.count("'") % 2: |
|
120 | elif s.count("'") % 2: | |
121 | return "'" |
|
121 | return "'" | |
122 | else: |
|
122 | else: | |
123 | return False |
|
123 | return False | |
124 |
|
124 | |||
125 |
|
125 | |||
126 | def protect_filename(s): |
|
126 | def protect_filename(s): | |
127 | """Escape a string to protect certain characters.""" |
|
127 | """Escape a string to protect certain characters.""" | |
128 |
|
128 | |||
129 | return "".join([(ch in PROTECTABLES and '\\' + ch or ch) |
|
129 | return "".join([(ch in PROTECTABLES and '\\' + ch or ch) | |
130 | for ch in s]) |
|
130 | for ch in s]) | |
131 |
|
131 | |||
132 | def expand_user(path): |
|
132 | def expand_user(path): | |
133 | """Expand '~'-style usernames in strings. |
|
133 | """Expand '~'-style usernames in strings. | |
134 |
|
134 | |||
135 | This is similar to :func:`os.path.expanduser`, but it computes and returns |
|
135 | 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 |
|
136 | 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 |
|
137 | computing completions, and you wish to return the completions with the | |
138 | original '~' instead of its expanded value. |
|
138 | original '~' instead of its expanded value. | |
139 |
|
139 | |||
140 | Parameters |
|
140 | Parameters | |
141 | ---------- |
|
141 | ---------- | |
142 | path : str |
|
142 | path : str | |
143 | String to be expanded. If no ~ is present, the output is the same as the |
|
143 | String to be expanded. If no ~ is present, the output is the same as the | |
144 | input. |
|
144 | input. | |
145 |
|
145 | |||
146 | Returns |
|
146 | Returns | |
147 | ------- |
|
147 | ------- | |
148 | newpath : str |
|
148 | newpath : str | |
149 | Result of ~ expansion in the input path. |
|
149 | Result of ~ expansion in the input path. | |
150 | tilde_expand : bool |
|
150 | tilde_expand : bool | |
151 | Whether any expansion was performed or not. |
|
151 | Whether any expansion was performed or not. | |
152 | tilde_val : str |
|
152 | tilde_val : str | |
153 | The value that ~ was replaced with. |
|
153 | The value that ~ was replaced with. | |
154 | """ |
|
154 | """ | |
155 | # Default values |
|
155 | # Default values | |
156 | tilde_expand = False |
|
156 | tilde_expand = False | |
157 | tilde_val = '' |
|
157 | tilde_val = '' | |
158 | newpath = path |
|
158 | newpath = path | |
159 |
|
159 | |||
160 | if path.startswith('~'): |
|
160 | if path.startswith('~'): | |
161 | tilde_expand = True |
|
161 | tilde_expand = True | |
162 | rest = len(path)-1 |
|
162 | rest = len(path)-1 | |
163 | newpath = os.path.expanduser(path) |
|
163 | newpath = os.path.expanduser(path) | |
164 | if rest: |
|
164 | if rest: | |
165 | tilde_val = newpath[:-rest] |
|
165 | tilde_val = newpath[:-rest] | |
166 | else: |
|
166 | else: | |
167 | tilde_val = newpath |
|
167 | tilde_val = newpath | |
168 |
|
168 | |||
169 | return newpath, tilde_expand, tilde_val |
|
169 | return newpath, tilde_expand, tilde_val | |
170 |
|
170 | |||
171 |
|
171 | |||
172 | def compress_user(path, tilde_expand, tilde_val): |
|
172 | def compress_user(path, tilde_expand, tilde_val): | |
173 | """Does the opposite of expand_user, with its outputs. |
|
173 | """Does the opposite of expand_user, with its outputs. | |
174 | """ |
|
174 | """ | |
175 | if tilde_expand: |
|
175 | if tilde_expand: | |
176 | return path.replace(tilde_val, '~') |
|
176 | return path.replace(tilde_val, '~') | |
177 | else: |
|
177 | else: | |
178 | return path |
|
178 | return path | |
179 |
|
179 | |||
180 |
|
180 | |||
181 | class Bunch(object): pass |
|
181 | class Bunch(object): pass | |
182 |
|
182 | |||
183 |
|
183 | |||
184 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' |
|
184 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' | |
185 | GREEDY_DELIMS = ' =\r\n' |
|
185 | GREEDY_DELIMS = ' =\r\n' | |
186 |
|
186 | |||
187 |
|
187 | |||
188 | class CompletionSplitter(object): |
|
188 | class CompletionSplitter(object): | |
189 | """An object to split an input line in a manner similar to readline. |
|
189 | """An object to split an input line in a manner similar to readline. | |
190 |
|
190 | |||
191 | By having our own implementation, we can expose readline-like completion in |
|
191 | By having our own implementation, we can expose readline-like completion in | |
192 | a uniform manner to all frontends. This object only needs to be given the |
|
192 | a uniform manner to all frontends. This object only needs to be given the | |
193 | line of text to be split and the cursor position on said line, and it |
|
193 | line of text to be split and the cursor position on said line, and it | |
194 | returns the 'word' to be completed on at the cursor after splitting the |
|
194 | returns the 'word' to be completed on at the cursor after splitting the | |
195 | entire line. |
|
195 | entire line. | |
196 |
|
196 | |||
197 | What characters are used as splitting delimiters can be controlled by |
|
197 | What characters are used as splitting delimiters can be controlled by | |
198 | setting the `delims` attribute (this is a property that internally |
|
198 | setting the `delims` attribute (this is a property that internally | |
199 | automatically builds the necessary regular expression)""" |
|
199 | automatically builds the necessary regular expression)""" | |
200 |
|
200 | |||
201 | # Private interface |
|
201 | # Private interface | |
202 |
|
202 | |||
203 | # A string of delimiter characters. The default value makes sense for |
|
203 | # A string of delimiter characters. The default value makes sense for | |
204 | # IPython's most typical usage patterns. |
|
204 | # IPython's most typical usage patterns. | |
205 | _delims = DELIMS |
|
205 | _delims = DELIMS | |
206 |
|
206 | |||
207 | # The expression (a normal string) to be compiled into a regular expression |
|
207 | # The expression (a normal string) to be compiled into a regular expression | |
208 | # for actual splitting. We store it as an attribute mostly for ease of |
|
208 | # for actual splitting. We store it as an attribute mostly for ease of | |
209 | # debugging, since this type of code can be so tricky to debug. |
|
209 | # debugging, since this type of code can be so tricky to debug. | |
210 | _delim_expr = None |
|
210 | _delim_expr = None | |
211 |
|
211 | |||
212 | # The regular expression that does the actual splitting |
|
212 | # The regular expression that does the actual splitting | |
213 | _delim_re = None |
|
213 | _delim_re = None | |
214 |
|
214 | |||
215 | def __init__(self, delims=None): |
|
215 | def __init__(self, delims=None): | |
216 | delims = CompletionSplitter._delims if delims is None else delims |
|
216 | delims = CompletionSplitter._delims if delims is None else delims | |
217 | self.delims = delims |
|
217 | self.delims = delims | |
218 |
|
218 | |||
219 | @property |
|
219 | @property | |
220 | def delims(self): |
|
220 | def delims(self): | |
221 | """Return the string of delimiter characters.""" |
|
221 | """Return the string of delimiter characters.""" | |
222 | return self._delims |
|
222 | return self._delims | |
223 |
|
223 | |||
224 | @delims.setter |
|
224 | @delims.setter | |
225 | def delims(self, delims): |
|
225 | def delims(self, delims): | |
226 | """Set the delimiters for line splitting.""" |
|
226 | """Set the delimiters for line splitting.""" | |
227 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' |
|
227 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' | |
228 | self._delim_re = re.compile(expr) |
|
228 | self._delim_re = re.compile(expr) | |
229 | self._delims = delims |
|
229 | self._delims = delims | |
230 | self._delim_expr = expr |
|
230 | self._delim_expr = expr | |
231 |
|
231 | |||
232 | def split_line(self, line, cursor_pos=None): |
|
232 | def split_line(self, line, cursor_pos=None): | |
233 | """Split a line of text with a cursor at the given position. |
|
233 | """Split a line of text with a cursor at the given position. | |
234 | """ |
|
234 | """ | |
235 | l = line if cursor_pos is None else line[:cursor_pos] |
|
235 | l = line if cursor_pos is None else line[:cursor_pos] | |
236 | return self._delim_re.split(l)[-1] |
|
236 | return self._delim_re.split(l)[-1] | |
237 |
|
237 | |||
238 |
|
238 | |||
239 | class Completer(Configurable): |
|
239 | class Completer(Configurable): | |
240 |
|
240 | |||
241 | greedy = CBool(False, config=True, |
|
241 | greedy = CBool(False, config=True, | |
242 | help="""Activate greedy completion |
|
242 | help="""Activate greedy completion | |
243 |
|
243 | |||
244 | This will enable completion on elements of lists, results of function calls, etc., |
|
244 | This will enable completion on elements of lists, results of function calls, etc., | |
245 | but can be unsafe because the code is actually evaluated on TAB. |
|
245 | but can be unsafe because the code is actually evaluated on TAB. | |
246 | """ |
|
246 | """ | |
247 | ) |
|
247 | ) | |
248 |
|
248 | |||
249 |
|
249 | |||
250 | def __init__(self, namespace=None, global_namespace=None, config=None, **kwargs): |
|
250 | def __init__(self, namespace=None, global_namespace=None, config=None, **kwargs): | |
251 | """Create a new completer for the command line. |
|
251 | """Create a new completer for the command line. | |
252 |
|
252 | |||
253 | Completer(namespace=ns,global_namespace=ns2) -> completer instance. |
|
253 | Completer(namespace=ns,global_namespace=ns2) -> completer instance. | |
254 |
|
254 | |||
255 | If unspecified, the default namespace where completions are performed |
|
255 | If unspecified, the default namespace where completions are performed | |
256 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
256 | is __main__ (technically, __main__.__dict__). Namespaces should be | |
257 | given as dictionaries. |
|
257 | given as dictionaries. | |
258 |
|
258 | |||
259 | An optional second namespace can be given. This allows the completer |
|
259 | An optional second namespace can be given. This allows the completer | |
260 | to handle cases where both the local and global scopes need to be |
|
260 | to handle cases where both the local and global scopes need to be | |
261 | distinguished. |
|
261 | distinguished. | |
262 |
|
262 | |||
263 | Completer instances should be used as the completion mechanism of |
|
263 | Completer instances should be used as the completion mechanism of | |
264 | readline via the set_completer() call: |
|
264 | readline via the set_completer() call: | |
265 |
|
265 | |||
266 | readline.set_completer(Completer(my_namespace).complete) |
|
266 | readline.set_completer(Completer(my_namespace).complete) | |
267 | """ |
|
267 | """ | |
268 |
|
268 | |||
269 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
269 | # Don't bind to namespace quite yet, but flag whether the user wants a | |
270 | # specific namespace or to use __main__.__dict__. This will allow us |
|
270 | # specific namespace or to use __main__.__dict__. This will allow us | |
271 | # to bind to __main__.__dict__ at completion time, not now. |
|
271 | # to bind to __main__.__dict__ at completion time, not now. | |
272 | if namespace is None: |
|
272 | if namespace is None: | |
273 | self.use_main_ns = 1 |
|
273 | self.use_main_ns = 1 | |
274 | else: |
|
274 | else: | |
275 | self.use_main_ns = 0 |
|
275 | self.use_main_ns = 0 | |
276 | self.namespace = namespace |
|
276 | self.namespace = namespace | |
277 |
|
277 | |||
278 | # The global namespace, if given, can be bound directly |
|
278 | # The global namespace, if given, can be bound directly | |
279 | if global_namespace is None: |
|
279 | if global_namespace is None: | |
280 | self.global_namespace = {} |
|
280 | self.global_namespace = {} | |
281 | else: |
|
281 | else: | |
282 | self.global_namespace = global_namespace |
|
282 | self.global_namespace = global_namespace | |
283 |
|
283 | |||
284 | super(Completer, self).__init__(config=config, **kwargs) |
|
284 | super(Completer, self).__init__(config=config, **kwargs) | |
285 |
|
285 | |||
286 | def complete(self, text, state): |
|
286 | def complete(self, text, state): | |
287 | """Return the next possible completion for 'text'. |
|
287 | """Return the next possible completion for 'text'. | |
288 |
|
288 | |||
289 | This is called successively with state == 0, 1, 2, ... until it |
|
289 | This is called successively with state == 0, 1, 2, ... until it | |
290 | returns None. The completion should begin with 'text'. |
|
290 | returns None. The completion should begin with 'text'. | |
291 |
|
291 | |||
292 | """ |
|
292 | """ | |
293 | if self.use_main_ns: |
|
293 | if self.use_main_ns: | |
294 | self.namespace = __main__.__dict__ |
|
294 | self.namespace = __main__.__dict__ | |
295 |
|
295 | |||
296 | if state == 0: |
|
296 | if state == 0: | |
297 | if "." in text: |
|
297 | if "." in text: | |
298 | self.matches = self.attr_matches(text) |
|
298 | self.matches = self.attr_matches(text) | |
299 | else: |
|
299 | else: | |
300 | self.matches = self.global_matches(text) |
|
300 | self.matches = self.global_matches(text) | |
301 | try: |
|
301 | try: | |
302 | return self.matches[state] |
|
302 | return self.matches[state] | |
303 | except IndexError: |
|
303 | except IndexError: | |
304 | return None |
|
304 | return None | |
305 |
|
305 | |||
306 | def global_matches(self, text): |
|
306 | def global_matches(self, text): | |
307 | """Compute matches when text is a simple name. |
|
307 | """Compute matches when text is a simple name. | |
308 |
|
308 | |||
309 | Return a list of all keywords, built-in functions and names currently |
|
309 | Return a list of all keywords, built-in functions and names currently | |
310 | defined in self.namespace or self.global_namespace that match. |
|
310 | defined in self.namespace or self.global_namespace that match. | |
311 |
|
311 | |||
312 | """ |
|
312 | """ | |
313 | #print 'Completer->global_matches, txt=%r' % text # dbg |
|
313 | #print 'Completer->global_matches, txt=%r' % text # dbg | |
314 | matches = [] |
|
314 | matches = [] | |
315 | match_append = matches.append |
|
315 | match_append = matches.append | |
316 | n = len(text) |
|
316 | n = len(text) | |
317 | for lst in [keyword.kwlist, |
|
317 | for lst in [keyword.kwlist, | |
318 | __builtin__.__dict__.keys(), |
|
318 | __builtin__.__dict__.keys(), | |
319 | self.namespace.keys(), |
|
319 | self.namespace.keys(), | |
320 | self.global_namespace.keys()]: |
|
320 | self.global_namespace.keys()]: | |
321 | for word in lst: |
|
321 | for word in lst: | |
322 | if word[:n] == text and word != "__builtins__": |
|
322 | if word[:n] == text and word != "__builtins__": | |
323 | match_append(word) |
|
323 | match_append(word) | |
324 | return matches |
|
324 | return matches | |
325 |
|
325 | |||
326 | def attr_matches(self, text): |
|
326 | def attr_matches(self, text): | |
327 | """Compute matches when text contains a dot. |
|
327 | """Compute matches when text contains a dot. | |
328 |
|
328 | |||
329 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
329 | Assuming the text is of the form NAME.NAME....[NAME], and is | |
330 | evaluatable in self.namespace or self.global_namespace, it will be |
|
330 | evaluatable in self.namespace or self.global_namespace, it will be | |
331 | evaluated and its attributes (as revealed by dir()) are used as |
|
331 | evaluated and its attributes (as revealed by dir()) are used as | |
332 | possible completions. (For class instances, class members are are |
|
332 | possible completions. (For class instances, class members are are | |
333 | also considered.) |
|
333 | also considered.) | |
334 |
|
334 | |||
335 | WARNING: this can still invoke arbitrary C code, if an object |
|
335 | WARNING: this can still invoke arbitrary C code, if an object | |
336 | with a __getattr__ hook is evaluated. |
|
336 | with a __getattr__ hook is evaluated. | |
337 |
|
337 | |||
338 | """ |
|
338 | """ | |
339 |
|
339 | |||
340 | #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg |
|
340 | #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg | |
341 | # Another option, seems to work great. Catches things like ''.<tab> |
|
341 | # Another option, seems to work great. Catches things like ''.<tab> | |
342 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
342 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) | |
343 |
|
343 | |||
344 | if m: |
|
344 | if m: | |
345 | expr, attr = m.group(1, 3) |
|
345 | expr, attr = m.group(1, 3) | |
346 | elif self.greedy: |
|
346 | elif self.greedy: | |
347 | m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer) |
|
347 | m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer) | |
348 | if not m2: |
|
348 | if not m2: | |
349 | return [] |
|
349 | return [] | |
350 | expr, attr = m2.group(1,2) |
|
350 | expr, attr = m2.group(1,2) | |
351 | else: |
|
351 | else: | |
352 | return [] |
|
352 | return [] | |
353 |
|
353 | |||
354 | try: |
|
354 | try: | |
355 | obj = eval(expr, self.namespace) |
|
355 | obj = eval(expr, self.namespace) | |
356 | except: |
|
356 | except: | |
357 | try: |
|
357 | try: | |
358 | obj = eval(expr, self.global_namespace) |
|
358 | obj = eval(expr, self.global_namespace) | |
359 | except: |
|
359 | except: | |
360 | return [] |
|
360 | return [] | |
361 |
|
361 | |||
362 | if self.limit_to__all__ and hasattr(obj, '__all__'): |
|
362 | if self.limit_to__all__ and hasattr(obj, '__all__'): | |
363 | words = get__all__entries(obj) |
|
363 | words = get__all__entries(obj) | |
364 | else: |
|
364 | else: | |
365 | words = dir2(obj) |
|
365 | words = dir2(obj) | |
366 |
|
366 | |||
367 | try: |
|
367 | try: | |
368 | words = generics.complete_object(obj, words) |
|
368 | words = generics.complete_object(obj, words) | |
369 | except TryNext: |
|
369 | except TryNext: | |
370 | pass |
|
370 | pass | |
371 | except Exception: |
|
371 | except Exception: | |
372 | # Silence errors from completion function |
|
372 | # Silence errors from completion function | |
373 | #raise # dbg |
|
373 | #raise # dbg | |
374 | pass |
|
374 | pass | |
375 | # Build match list to return |
|
375 | # Build match list to return | |
376 | n = len(attr) |
|
376 | n = len(attr) | |
377 | res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
377 | res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ] | |
378 | return res |
|
378 | return res | |
379 |
|
379 | |||
380 |
|
380 | |||
381 | def get__all__entries(obj): |
|
381 | def get__all__entries(obj): | |
382 | """returns the strings in the __all__ attribute""" |
|
382 | """returns the strings in the __all__ attribute""" | |
383 | try: |
|
383 | try: | |
384 | words = getattr(obj, '__all__') |
|
384 | words = getattr(obj, '__all__') | |
385 | except: |
|
385 | except: | |
386 | return [] |
|
386 | return [] | |
387 |
|
387 | |||
388 | return [w for w in words if isinstance(w, basestring)] |
|
388 | return [w for w in words if isinstance(w, basestring)] | |
389 |
|
389 | |||
390 |
|
390 | |||
391 | class IPCompleter(Completer): |
|
391 | class IPCompleter(Completer): | |
392 | """Extension of the completer class with IPython-specific features""" |
|
392 | """Extension of the completer class with IPython-specific features""" | |
393 |
|
393 | |||
394 | def _greedy_changed(self, name, old, new): |
|
394 | def _greedy_changed(self, name, old, new): | |
395 | """update the splitter and readline delims when greedy is changed""" |
|
395 | """update the splitter and readline delims when greedy is changed""" | |
396 | if new: |
|
396 | if new: | |
397 | self.splitter.delims = GREEDY_DELIMS |
|
397 | self.splitter.delims = GREEDY_DELIMS | |
398 | else: |
|
398 | else: | |
399 | self.splitter.delims = DELIMS |
|
399 | self.splitter.delims = DELIMS | |
400 |
|
400 | |||
401 | if self.readline: |
|
401 | if self.readline: | |
402 | self.readline.set_completer_delims(self.splitter.delims) |
|
402 | self.readline.set_completer_delims(self.splitter.delims) | |
403 |
|
403 | |||
404 | merge_completions = CBool(True, config=True, |
|
404 | merge_completions = CBool(True, config=True, | |
405 | help="""Whether to merge completion results into a single list |
|
405 | help="""Whether to merge completion results into a single list | |
406 |
|
406 | |||
407 | If False, only the completion results from the first non-empty |
|
407 | If False, only the completion results from the first non-empty | |
408 | completer will be returned. |
|
408 | completer will be returned. | |
409 | """ |
|
409 | """ | |
410 | ) |
|
410 | ) | |
411 | omit__names = Enum((0,1,2), default_value=2, config=True, |
|
411 | omit__names = Enum((0,1,2), default_value=2, config=True, | |
412 | help="""Instruct the completer to omit private method names |
|
412 | help="""Instruct the completer to omit private method names | |
413 |
|
413 | |||
414 | Specifically, when completing on ``object.<tab>``. |
|
414 | Specifically, when completing on ``object.<tab>``. | |
415 |
|
415 | |||
416 | When 2 [default]: all names that start with '_' will be excluded. |
|
416 | When 2 [default]: all names that start with '_' will be excluded. | |
417 |
|
417 | |||
418 | When 1: all 'magic' names (``__foo__``) will be excluded. |
|
418 | When 1: all 'magic' names (``__foo__``) will be excluded. | |
419 |
|
419 | |||
420 | When 0: nothing will be excluded. |
|
420 | When 0: nothing will be excluded. | |
421 | """ |
|
421 | """ | |
422 | ) |
|
422 | ) | |
423 | limit_to__all__ = CBool(default_value=False, config=True, |
|
423 | limit_to__all__ = CBool(default_value=False, config=True, | |
424 | help="""Instruct the completer to use __all__ for the completion |
|
424 | help="""Instruct the completer to use __all__ for the completion | |
425 |
|
425 | |||
426 | Specifically, when completing on ``object.<tab>``. |
|
426 | Specifically, when completing on ``object.<tab>``. | |
427 |
|
427 | |||
428 | When True: only those names in obj.__all__ will be included. |
|
428 | When True: only those names in obj.__all__ will be included. | |
429 |
|
429 | |||
430 | When False [default]: the __all__ attribute is ignored |
|
430 | When False [default]: the __all__ attribute is ignored | |
431 | """ |
|
431 | """ | |
432 | ) |
|
432 | ) | |
433 |
|
433 | |||
434 | def __init__(self, shell=None, namespace=None, global_namespace=None, |
|
434 | def __init__(self, shell=None, namespace=None, global_namespace=None, | |
435 | alias_table=None, use_readline=True, |
|
435 | alias_table=None, use_readline=True, | |
436 | config=None, **kwargs): |
|
436 | config=None, **kwargs): | |
437 | """IPCompleter() -> completer |
|
437 | """IPCompleter() -> completer | |
438 |
|
438 | |||
439 | Return a completer object suitable for use by the readline library |
|
439 | Return a completer object suitable for use by the readline library | |
440 | via readline.set_completer(). |
|
440 | via readline.set_completer(). | |
441 |
|
441 | |||
442 | Inputs: |
|
442 | Inputs: | |
443 |
|
443 | |||
444 | - shell: a pointer to the ipython shell itself. This is needed |
|
444 | - shell: a pointer to the ipython shell itself. This is needed | |
445 | because this completer knows about magic functions, and those can |
|
445 | because this completer knows about magic functions, and those can | |
446 | only be accessed via the ipython instance. |
|
446 | only be accessed via the ipython instance. | |
447 |
|
447 | |||
448 | - namespace: an optional dict where completions are performed. |
|
448 | - namespace: an optional dict where completions are performed. | |
449 |
|
449 | |||
450 | - global_namespace: secondary optional dict for completions, to |
|
450 | - global_namespace: secondary optional dict for completions, to | |
451 | handle cases (such as IPython embedded inside functions) where |
|
451 | handle cases (such as IPython embedded inside functions) where | |
452 | both Python scopes are visible. |
|
452 | both Python scopes are visible. | |
453 |
|
453 | |||
454 | - If alias_table is supplied, it should be a dictionary of aliases |
|
454 | - If alias_table is supplied, it should be a dictionary of aliases | |
455 | to complete. |
|
455 | to complete. | |
456 |
|
456 | |||
457 | use_readline : bool, optional |
|
457 | use_readline : bool, optional | |
458 | If true, use the readline library. This completer can still function |
|
458 | If true, use the readline library. This completer can still function | |
459 | without readline, though in that case callers must provide some extra |
|
459 | without readline, though in that case callers must provide some extra | |
460 | information on each call about the current line.""" |
|
460 | information on each call about the current line.""" | |
461 |
|
461 | |||
462 | self.magic_escape = ESC_MAGIC |
|
462 | self.magic_escape = ESC_MAGIC | |
463 | self.splitter = CompletionSplitter() |
|
463 | self.splitter = CompletionSplitter() | |
464 |
|
464 | |||
465 | # Readline configuration, only used by the rlcompleter method. |
|
465 | # Readline configuration, only used by the rlcompleter method. | |
466 | if use_readline: |
|
466 | if use_readline: | |
467 | # We store the right version of readline so that later code |
|
467 | # We store the right version of readline so that later code | |
468 | import IPython.utils.rlineimpl as readline |
|
468 | import IPython.utils.rlineimpl as readline | |
469 | self.readline = readline |
|
469 | self.readline = readline | |
470 | else: |
|
470 | else: | |
471 | self.readline = None |
|
471 | self.readline = None | |
472 |
|
472 | |||
473 | # _greedy_changed() depends on splitter and readline being defined: |
|
473 | # _greedy_changed() depends on splitter and readline being defined: | |
474 | Completer.__init__(self, namespace=namespace, global_namespace=global_namespace, |
|
474 | Completer.__init__(self, namespace=namespace, global_namespace=global_namespace, | |
475 | config=config, **kwargs) |
|
475 | config=config, **kwargs) | |
476 |
|
476 | |||
477 | # List where completion matches will be stored |
|
477 | # List where completion matches will be stored | |
478 | self.matches = [] |
|
478 | self.matches = [] | |
479 | self.shell = shell |
|
479 | self.shell = shell | |
480 | if alias_table is None: |
|
480 | if alias_table is None: | |
481 | alias_table = {} |
|
481 | alias_table = {} | |
482 | self.alias_table = alias_table |
|
482 | self.alias_table = alias_table | |
483 | # Regexp to split filenames with spaces in them |
|
483 | # Regexp to split filenames with spaces in them | |
484 | self.space_name_re = re.compile(r'([^\\] )') |
|
484 | self.space_name_re = re.compile(r'([^\\] )') | |
485 | # Hold a local ref. to glob.glob for speed |
|
485 | # Hold a local ref. to glob.glob for speed | |
486 | self.glob = glob.glob |
|
486 | self.glob = glob.glob | |
487 |
|
487 | |||
488 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
488 | # Determine if we are running on 'dumb' terminals, like (X)Emacs | |
489 | # buffers, to avoid completion problems. |
|
489 | # buffers, to avoid completion problems. | |
490 | term = os.environ.get('TERM','xterm') |
|
490 | term = os.environ.get('TERM','xterm') | |
491 | self.dumb_terminal = term in ['dumb','emacs'] |
|
491 | self.dumb_terminal = term in ['dumb','emacs'] | |
492 |
|
492 | |||
493 | # Special handling of backslashes needed in win32 platforms |
|
493 | # Special handling of backslashes needed in win32 platforms | |
494 | if sys.platform == "win32": |
|
494 | if sys.platform == "win32": | |
495 | self.clean_glob = self._clean_glob_win32 |
|
495 | self.clean_glob = self._clean_glob_win32 | |
496 | else: |
|
496 | else: | |
497 | self.clean_glob = self._clean_glob |
|
497 | self.clean_glob = self._clean_glob | |
498 |
|
498 | |||
|
499 | #regexp to parse docstring for function signature | |||
|
500 | self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') | |||
|
501 | self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') | |||
|
502 | #use this if positional argument name is also needed | |||
|
503 | #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)') | |||
|
504 | ||||
499 | # All active matcher routines for completion |
|
505 | # All active matcher routines for completion | |
500 | self.matchers = [self.python_matches, |
|
506 | self.matchers = [self.python_matches, | |
501 | self.file_matches, |
|
507 | self.file_matches, | |
502 | self.magic_matches, |
|
508 | self.magic_matches, | |
503 | self.alias_matches, |
|
509 | self.alias_matches, | |
504 | self.python_func_kw_matches, |
|
510 | self.python_func_kw_matches, | |
505 | ] |
|
511 | ] | |
506 |
|
512 | |||
507 | def all_completions(self, text): |
|
513 | def all_completions(self, text): | |
508 | """ |
|
514 | """ | |
509 | Wrapper around the complete method for the benefit of emacs |
|
515 | Wrapper around the complete method for the benefit of emacs | |
510 | and pydb. |
|
516 | and pydb. | |
511 | """ |
|
517 | """ | |
512 | return self.complete(text)[1] |
|
518 | return self.complete(text)[1] | |
513 |
|
519 | |||
514 | def _clean_glob(self,text): |
|
520 | def _clean_glob(self,text): | |
515 | return self.glob("%s*" % text) |
|
521 | return self.glob("%s*" % text) | |
516 |
|
522 | |||
517 | def _clean_glob_win32(self,text): |
|
523 | def _clean_glob_win32(self,text): | |
518 | return [f.replace("\\","/") |
|
524 | return [f.replace("\\","/") | |
519 | for f in self.glob("%s*" % text)] |
|
525 | for f in self.glob("%s*" % text)] | |
520 |
|
526 | |||
521 | def file_matches(self, text): |
|
527 | def file_matches(self, text): | |
522 | """Match filenames, expanding ~USER type strings. |
|
528 | """Match filenames, expanding ~USER type strings. | |
523 |
|
529 | |||
524 | Most of the seemingly convoluted logic in this completer is an |
|
530 | Most of the seemingly convoluted logic in this completer is an | |
525 | attempt to handle filenames with spaces in them. And yet it's not |
|
531 | attempt to handle filenames with spaces in them. And yet it's not | |
526 | quite perfect, because Python's readline doesn't expose all of the |
|
532 | quite perfect, because Python's readline doesn't expose all of the | |
527 | GNU readline details needed for this to be done correctly. |
|
533 | GNU readline details needed for this to be done correctly. | |
528 |
|
534 | |||
529 | For a filename with a space in it, the printed completions will be |
|
535 | For a filename with a space in it, the printed completions will be | |
530 | only the parts after what's already been typed (instead of the |
|
536 | only the parts after what's already been typed (instead of the | |
531 | full completions, as is normally done). I don't think with the |
|
537 | full completions, as is normally done). I don't think with the | |
532 | current (as of Python 2.3) Python readline it's possible to do |
|
538 | current (as of Python 2.3) Python readline it's possible to do | |
533 | better.""" |
|
539 | better.""" | |
534 |
|
540 | |||
535 | #io.rprint('Completer->file_matches: <%r>' % text) # dbg |
|
541 | #io.rprint('Completer->file_matches: <%r>' % text) # dbg | |
536 |
|
542 | |||
537 | # chars that require escaping with backslash - i.e. chars |
|
543 | # chars that require escaping with backslash - i.e. chars | |
538 | # that readline treats incorrectly as delimiters, but we |
|
544 | # that readline treats incorrectly as delimiters, but we | |
539 | # don't want to treat as delimiters in filename matching |
|
545 | # don't want to treat as delimiters in filename matching | |
540 | # when escaped with backslash |
|
546 | # when escaped with backslash | |
541 | if text.startswith('!'): |
|
547 | if text.startswith('!'): | |
542 | text = text[1:] |
|
548 | text = text[1:] | |
543 | text_prefix = '!' |
|
549 | text_prefix = '!' | |
544 | else: |
|
550 | else: | |
545 | text_prefix = '' |
|
551 | text_prefix = '' | |
546 |
|
552 | |||
547 | text_until_cursor = self.text_until_cursor |
|
553 | text_until_cursor = self.text_until_cursor | |
548 | # track strings with open quotes |
|
554 | # track strings with open quotes | |
549 | open_quotes = has_open_quotes(text_until_cursor) |
|
555 | open_quotes = has_open_quotes(text_until_cursor) | |
550 |
|
556 | |||
551 | if '(' in text_until_cursor or '[' in text_until_cursor: |
|
557 | if '(' in text_until_cursor or '[' in text_until_cursor: | |
552 | lsplit = text |
|
558 | lsplit = text | |
553 | else: |
|
559 | else: | |
554 | try: |
|
560 | try: | |
555 | # arg_split ~ shlex.split, but with unicode bugs fixed by us |
|
561 | # arg_split ~ shlex.split, but with unicode bugs fixed by us | |
556 | lsplit = arg_split(text_until_cursor)[-1] |
|
562 | lsplit = arg_split(text_until_cursor)[-1] | |
557 | except ValueError: |
|
563 | except ValueError: | |
558 | # typically an unmatched ", or backslash without escaped char. |
|
564 | # typically an unmatched ", or backslash without escaped char. | |
559 | if open_quotes: |
|
565 | if open_quotes: | |
560 | lsplit = text_until_cursor.split(open_quotes)[-1] |
|
566 | lsplit = text_until_cursor.split(open_quotes)[-1] | |
561 | else: |
|
567 | else: | |
562 | return [] |
|
568 | return [] | |
563 | except IndexError: |
|
569 | except IndexError: | |
564 | # tab pressed on empty line |
|
570 | # tab pressed on empty line | |
565 | lsplit = "" |
|
571 | lsplit = "" | |
566 |
|
572 | |||
567 | if not open_quotes and lsplit != protect_filename(lsplit): |
|
573 | if not open_quotes and lsplit != protect_filename(lsplit): | |
568 | # if protectables are found, do matching on the whole escaped name |
|
574 | # if protectables are found, do matching on the whole escaped name | |
569 | has_protectables = True |
|
575 | has_protectables = True | |
570 | text0,text = text,lsplit |
|
576 | text0,text = text,lsplit | |
571 | else: |
|
577 | else: | |
572 | has_protectables = False |
|
578 | has_protectables = False | |
573 | text = os.path.expanduser(text) |
|
579 | text = os.path.expanduser(text) | |
574 |
|
580 | |||
575 | if text == "": |
|
581 | if text == "": | |
576 | return [text_prefix + protect_filename(f) for f in self.glob("*")] |
|
582 | return [text_prefix + protect_filename(f) for f in self.glob("*")] | |
577 |
|
583 | |||
578 | # Compute the matches from the filesystem |
|
584 | # Compute the matches from the filesystem | |
579 | m0 = self.clean_glob(text.replace('\\','')) |
|
585 | m0 = self.clean_glob(text.replace('\\','')) | |
580 |
|
586 | |||
581 | if has_protectables: |
|
587 | if has_protectables: | |
582 | # If we had protectables, we need to revert our changes to the |
|
588 | # If we had protectables, we need to revert our changes to the | |
583 | # beginning of filename so that we don't double-write the part |
|
589 | # beginning of filename so that we don't double-write the part | |
584 | # of the filename we have so far |
|
590 | # of the filename we have so far | |
585 | len_lsplit = len(lsplit) |
|
591 | len_lsplit = len(lsplit) | |
586 | matches = [text_prefix + text0 + |
|
592 | matches = [text_prefix + text0 + | |
587 | protect_filename(f[len_lsplit:]) for f in m0] |
|
593 | protect_filename(f[len_lsplit:]) for f in m0] | |
588 | else: |
|
594 | else: | |
589 | if open_quotes: |
|
595 | if open_quotes: | |
590 | # if we have a string with an open quote, we don't need to |
|
596 | # if we have a string with an open quote, we don't need to | |
591 | # protect the names at all (and we _shouldn't_, as it |
|
597 | # protect the names at all (and we _shouldn't_, as it | |
592 | # would cause bugs when the filesystem call is made). |
|
598 | # would cause bugs when the filesystem call is made). | |
593 | matches = m0 |
|
599 | matches = m0 | |
594 | else: |
|
600 | else: | |
595 | matches = [text_prefix + |
|
601 | matches = [text_prefix + | |
596 | protect_filename(f) for f in m0] |
|
602 | protect_filename(f) for f in m0] | |
597 |
|
603 | |||
598 | #io.rprint('mm', matches) # dbg |
|
604 | #io.rprint('mm', matches) # dbg | |
599 |
|
605 | |||
600 | # Mark directories in input list by appending '/' to their names. |
|
606 | # Mark directories in input list by appending '/' to their names. | |
601 | matches = [x+'/' if os.path.isdir(x) else x for x in matches] |
|
607 | matches = [x+'/' if os.path.isdir(x) else x for x in matches] | |
602 | return matches |
|
608 | return matches | |
603 |
|
609 | |||
604 | def magic_matches(self, text): |
|
610 | def magic_matches(self, text): | |
605 | """Match magics""" |
|
611 | """Match magics""" | |
606 | #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg |
|
612 | #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg | |
607 | # Get all shell magics now rather than statically, so magics loaded at |
|
613 | # Get all shell magics now rather than statically, so magics loaded at | |
608 | # runtime show up too. |
|
614 | # runtime show up too. | |
609 | lsm = self.shell.magics_manager.lsmagic() |
|
615 | lsm = self.shell.magics_manager.lsmagic() | |
610 | line_magics = lsm['line'] |
|
616 | line_magics = lsm['line'] | |
611 | cell_magics = lsm['cell'] |
|
617 | cell_magics = lsm['cell'] | |
612 | pre = self.magic_escape |
|
618 | pre = self.magic_escape | |
613 | pre2 = pre+pre |
|
619 | pre2 = pre+pre | |
614 |
|
620 | |||
615 | # Completion logic: |
|
621 | # Completion logic: | |
616 | # - user gives %%: only do cell magics |
|
622 | # - user gives %%: only do cell magics | |
617 | # - user gives %: do both line and cell magics |
|
623 | # - user gives %: do both line and cell magics | |
618 | # - no prefix: do both |
|
624 | # - no prefix: do both | |
619 | # In other words, line magics are skipped if the user gives %% explicitly |
|
625 | # In other words, line magics are skipped if the user gives %% explicitly | |
620 | bare_text = text.lstrip(pre) |
|
626 | bare_text = text.lstrip(pre) | |
621 | comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)] |
|
627 | comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)] | |
622 | if not text.startswith(pre2): |
|
628 | if not text.startswith(pre2): | |
623 | comp += [ pre+m for m in line_magics if m.startswith(bare_text)] |
|
629 | comp += [ pre+m for m in line_magics if m.startswith(bare_text)] | |
624 | return comp |
|
630 | return comp | |
625 |
|
631 | |||
626 | def alias_matches(self, text): |
|
632 | def alias_matches(self, text): | |
627 | """Match internal system aliases""" |
|
633 | """Match internal system aliases""" | |
628 | #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg |
|
634 | #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg | |
629 |
|
635 | |||
630 | # if we are not in the first 'item', alias matching |
|
636 | # if we are not in the first 'item', alias matching | |
631 | # doesn't make sense - unless we are starting with 'sudo' command. |
|
637 | # doesn't make sense - unless we are starting with 'sudo' command. | |
632 | main_text = self.text_until_cursor.lstrip() |
|
638 | main_text = self.text_until_cursor.lstrip() | |
633 | if ' ' in main_text and not main_text.startswith('sudo'): |
|
639 | if ' ' in main_text and not main_text.startswith('sudo'): | |
634 | return [] |
|
640 | return [] | |
635 | text = os.path.expanduser(text) |
|
641 | text = os.path.expanduser(text) | |
636 | aliases = self.alias_table.keys() |
|
642 | aliases = self.alias_table.keys() | |
637 | if text == '': |
|
643 | if text == '': | |
638 | return aliases |
|
644 | return aliases | |
639 | else: |
|
645 | else: | |
640 | return [a for a in aliases if a.startswith(text)] |
|
646 | return [a for a in aliases if a.startswith(text)] | |
641 |
|
647 | |||
642 | def python_matches(self,text): |
|
648 | def python_matches(self,text): | |
643 | """Match attributes or global python names""" |
|
649 | """Match attributes or global python names""" | |
644 |
|
650 | |||
645 | #io.rprint('Completer->python_matches, txt=%r' % text) # dbg |
|
651 | #io.rprint('Completer->python_matches, txt=%r' % text) # dbg | |
646 | if "." in text: |
|
652 | if "." in text: | |
647 | try: |
|
653 | try: | |
648 | matches = self.attr_matches(text) |
|
654 | matches = self.attr_matches(text) | |
649 | if text.endswith('.') and self.omit__names: |
|
655 | if text.endswith('.') and self.omit__names: | |
650 | if self.omit__names == 1: |
|
656 | if self.omit__names == 1: | |
651 | # true if txt is _not_ a __ name, false otherwise: |
|
657 | # true if txt is _not_ a __ name, false otherwise: | |
652 | no__name = (lambda txt: |
|
658 | no__name = (lambda txt: | |
653 | re.match(r'.*\.__.*?__',txt) is None) |
|
659 | re.match(r'.*\.__.*?__',txt) is None) | |
654 | else: |
|
660 | else: | |
655 | # true if txt is _not_ a _ name, false otherwise: |
|
661 | # true if txt is _not_ a _ name, false otherwise: | |
656 | no__name = (lambda txt: |
|
662 | no__name = (lambda txt: | |
657 | re.match(r'.*\._.*?',txt) is None) |
|
663 | re.match(r'.*\._.*?',txt) is None) | |
658 | matches = filter(no__name, matches) |
|
664 | matches = filter(no__name, matches) | |
659 | except NameError: |
|
665 | except NameError: | |
660 | # catches <undefined attributes>.<tab> |
|
666 | # catches <undefined attributes>.<tab> | |
661 | matches = [] |
|
667 | matches = [] | |
662 | else: |
|
668 | else: | |
663 | matches = self.global_matches(text) |
|
669 | matches = self.global_matches(text) | |
664 |
|
670 | |||
665 | return matches |
|
671 | return matches | |
666 |
|
672 | |||
|
673 | def _default_arguments_from_docstring(self, doc): | |||
|
674 | """Parse the first line of docstring for call signature. | |||
|
675 | ||||
|
676 | Docstring should be of the form 'min(iterable[, key=func])\n'. | |||
|
677 | It can also parse cython docstring of the form | |||
|
678 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'. | |||
|
679 | """ | |||
|
680 | if doc is None: | |||
|
681 | return [] | |||
|
682 | ||||
|
683 | #care only the firstline | |||
|
684 | line = doc.lstrip().splitlines()[0] | |||
|
685 | ||||
|
686 | #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') | |||
|
687 | #'min(iterable[, key=func])\n' -> 'iterable[, key=func]' | |||
|
688 | sig = self.docstring_sig_re.search(line) | |||
|
689 | if sig is None: | |||
|
690 | return [] | |||
|
691 | # iterable[, key=func]' -> ['iterable[' ,' key=func]'] | |||
|
692 | sig = sig.groups()[0].split(',') | |||
|
693 | ret = [] | |||
|
694 | for s in sig: | |||
|
695 | #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') | |||
|
696 | ret += self.docstring_kwd_re.findall(s) | |||
|
697 | return ret | |||
|
698 | ||||
667 | def _default_arguments(self, obj): |
|
699 | def _default_arguments(self, obj): | |
668 | """Return the list of default arguments of obj if it is callable, |
|
700 | """Return the list of default arguments of obj if it is callable, | |
669 | or empty list otherwise.""" |
|
701 | or empty list otherwise.""" | |
670 |
|
702 | call_obj = obj | ||
671 | if not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
703 | ret = [] | |
672 | # for classes, check for __init__,__new__ |
|
704 | if inspect.isbuiltin(obj): | |
|
705 | pass | |||
|
706 | elif not (inspect.isfunction(obj) or inspect.ismethod(obj)): | |||
673 | if inspect.isclass(obj): |
|
707 | if inspect.isclass(obj): | |
674 | obj = (getattr(obj,'__init__',None) or |
|
708 | #for cython embededsignature=True the constructor docstring | |
675 | getattr(obj,'__new__',None)) |
|
709 | #belongs to the object itself not __init__ | |
|
710 | ret += self._default_arguments_from_docstring( | |||
|
711 | getattr(obj, '__doc__', '')) | |||
|
712 | # for classes, check for __init__,__new__ | |||
|
713 | call_obj = (getattr(obj, '__init__', None) or | |||
|
714 | getattr(obj, '__new__', None)) | |||
676 | # for all others, check if they are __call__able |
|
715 | # for all others, check if they are __call__able | |
677 | elif hasattr(obj, '__call__'): |
|
716 | elif hasattr(obj, '__call__'): | |
678 | obj = obj.__call__ |
|
717 | call_obj = obj.__call__ | |
679 | # XXX: is there a way to handle the builtins ? |
|
718 | ||
|
719 | ret += self._default_arguments_from_docstring( | |||
|
720 | getattr(call_obj, '__doc__', '')) | |||
|
721 | ||||
680 | try: |
|
722 | try: | |
681 | args,_,_1,defaults = inspect.getargspec(obj) |
|
723 | args,_,_1,defaults = inspect.getargspec(call_obj) | |
682 | if defaults: |
|
724 | if defaults: | |
683 |
ret |
|
725 | ret+=args[-len(defaults):] | |
684 |
except TypeError: |
|
726 | except TypeError: | |
685 | return [] |
|
727 | pass | |
|
728 | ||||
|
729 | return list(set(ret)) | |||
686 |
|
730 | |||
687 | def python_func_kw_matches(self,text): |
|
731 | def python_func_kw_matches(self,text): | |
688 | """Match named parameters (kwargs) of the last open function""" |
|
732 | """Match named parameters (kwargs) of the last open function""" | |
689 |
|
733 | |||
690 | if "." in text: # a parameter cannot be dotted |
|
734 | if "." in text: # a parameter cannot be dotted | |
691 | return [] |
|
735 | return [] | |
692 | try: regexp = self.__funcParamsRegex |
|
736 | try: regexp = self.__funcParamsRegex | |
693 | except AttributeError: |
|
737 | except AttributeError: | |
694 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
738 | regexp = self.__funcParamsRegex = re.compile(r''' | |
695 | '.*?(?<!\\)' | # single quoted strings or |
|
739 | '.*?(?<!\\)' | # single quoted strings or | |
696 | ".*?(?<!\\)" | # double quoted strings or |
|
740 | ".*?(?<!\\)" | # double quoted strings or | |
697 | \w+ | # identifier |
|
741 | \w+ | # identifier | |
698 | \S # other characters |
|
742 | \S # other characters | |
699 | ''', re.VERBOSE | re.DOTALL) |
|
743 | ''', re.VERBOSE | re.DOTALL) | |
700 | # 1. find the nearest identifier that comes before an unclosed |
|
744 | # 1. find the nearest identifier that comes before an unclosed | |
701 | # parenthesis before the cursor |
|
745 | # parenthesis before the cursor | |
702 | # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo" |
|
746 | # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo" | |
703 | tokens = regexp.findall(self.text_until_cursor) |
|
747 | tokens = regexp.findall(self.text_until_cursor) | |
704 | tokens.reverse() |
|
748 | tokens.reverse() | |
705 | iterTokens = iter(tokens); openPar = 0 |
|
749 | iterTokens = iter(tokens); openPar = 0 | |
|
750 | ||||
706 | for token in iterTokens: |
|
751 | for token in iterTokens: | |
707 | if token == ')': |
|
752 | if token == ')': | |
708 | openPar -= 1 |
|
753 | openPar -= 1 | |
709 | elif token == '(': |
|
754 | elif token == '(': | |
710 | openPar += 1 |
|
755 | openPar += 1 | |
711 | if openPar > 0: |
|
756 | if openPar > 0: | |
712 | # found the last unclosed parenthesis |
|
757 | # found the last unclosed parenthesis | |
713 | break |
|
758 | break | |
714 | else: |
|
759 | else: | |
715 | return [] |
|
760 | return [] | |
716 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
761 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) | |
717 | ids = [] |
|
762 | ids = [] | |
718 | isId = re.compile(r'\w+$').match |
|
763 | isId = re.compile(r'\w+$').match | |
|
764 | ||||
719 | while True: |
|
765 | while True: | |
720 | try: |
|
766 | try: | |
721 | ids.append(next(iterTokens)) |
|
767 | ids.append(next(iterTokens)) | |
722 | if not isId(ids[-1]): |
|
768 | if not isId(ids[-1]): | |
723 | ids.pop(); break |
|
769 | ids.pop(); break | |
724 | if not next(iterTokens) == '.': |
|
770 | if not next(iterTokens) == '.': | |
725 | break |
|
771 | break | |
726 | except StopIteration: |
|
772 | except StopIteration: | |
727 | break |
|
773 | break | |
728 | # lookup the candidate callable matches either using global_matches |
|
774 | # lookup the candidate callable matches either using global_matches | |
729 | # or attr_matches for dotted names |
|
775 | # or attr_matches for dotted names | |
730 | if len(ids) == 1: |
|
776 | if len(ids) == 1: | |
731 | callableMatches = self.global_matches(ids[0]) |
|
777 | callableMatches = self.global_matches(ids[0]) | |
732 | else: |
|
778 | else: | |
733 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
779 | callableMatches = self.attr_matches('.'.join(ids[::-1])) | |
734 | argMatches = [] |
|
780 | argMatches = [] | |
735 | for callableMatch in callableMatches: |
|
781 | for callableMatch in callableMatches: | |
736 | try: |
|
782 | try: | |
737 | namedArgs = self._default_arguments(eval(callableMatch, |
|
783 | namedArgs = self._default_arguments(eval(callableMatch, | |
738 |
|
|
784 | self.namespace)) | |
739 | except: |
|
785 | except: | |
740 | continue |
|
786 | continue | |
|
787 | ||||
741 | for namedArg in namedArgs: |
|
788 | for namedArg in namedArgs: | |
742 | if namedArg.startswith(text): |
|
789 | if namedArg.startswith(text): | |
743 | argMatches.append("%s=" %namedArg) |
|
790 | argMatches.append("%s=" %namedArg) | |
744 | return argMatches |
|
791 | return argMatches | |
745 |
|
792 | |||
746 | def dispatch_custom_completer(self, text): |
|
793 | def dispatch_custom_completer(self, text): | |
747 | #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg |
|
794 | #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg | |
748 | line = self.line_buffer |
|
795 | line = self.line_buffer | |
749 | if not line.strip(): |
|
796 | if not line.strip(): | |
750 | return None |
|
797 | return None | |
751 |
|
798 | |||
752 | # Create a little structure to pass all the relevant information about |
|
799 | # Create a little structure to pass all the relevant information about | |
753 | # the current completion to any custom completer. |
|
800 | # the current completion to any custom completer. | |
754 | event = Bunch() |
|
801 | event = Bunch() | |
755 | event.line = line |
|
802 | event.line = line | |
756 | event.symbol = text |
|
803 | event.symbol = text | |
757 | cmd = line.split(None,1)[0] |
|
804 | cmd = line.split(None,1)[0] | |
758 | event.command = cmd |
|
805 | event.command = cmd | |
759 | event.text_until_cursor = self.text_until_cursor |
|
806 | event.text_until_cursor = self.text_until_cursor | |
760 |
|
807 | |||
761 | #print "\ncustom:{%s]\n" % event # dbg |
|
808 | #print "\ncustom:{%s]\n" % event # dbg | |
762 |
|
809 | |||
763 | # for foo etc, try also to find completer for %foo |
|
810 | # for foo etc, try also to find completer for %foo | |
764 | if not cmd.startswith(self.magic_escape): |
|
811 | if not cmd.startswith(self.magic_escape): | |
765 | try_magic = self.custom_completers.s_matches( |
|
812 | try_magic = self.custom_completers.s_matches( | |
766 | self.magic_escape + cmd) |
|
813 | self.magic_escape + cmd) | |
767 | else: |
|
814 | else: | |
768 | try_magic = [] |
|
815 | try_magic = [] | |
769 |
|
816 | |||
770 | for c in itertools.chain(self.custom_completers.s_matches(cmd), |
|
817 | for c in itertools.chain(self.custom_completers.s_matches(cmd), | |
771 | try_magic, |
|
818 | try_magic, | |
772 | self.custom_completers.flat_matches(self.text_until_cursor)): |
|
819 | self.custom_completers.flat_matches(self.text_until_cursor)): | |
773 | #print "try",c # dbg |
|
820 | #print "try",c # dbg | |
774 | try: |
|
821 | try: | |
775 | res = c(event) |
|
822 | res = c(event) | |
776 | if res: |
|
823 | if res: | |
777 | # first, try case sensitive match |
|
824 | # first, try case sensitive match | |
778 | withcase = [r for r in res if r.startswith(text)] |
|
825 | withcase = [r for r in res if r.startswith(text)] | |
779 | if withcase: |
|
826 | if withcase: | |
780 | return withcase |
|
827 | return withcase | |
781 | # if none, then case insensitive ones are ok too |
|
828 | # if none, then case insensitive ones are ok too | |
782 | text_low = text.lower() |
|
829 | text_low = text.lower() | |
783 | return [r for r in res if r.lower().startswith(text_low)] |
|
830 | return [r for r in res if r.lower().startswith(text_low)] | |
784 | except TryNext: |
|
831 | except TryNext: | |
785 | pass |
|
832 | pass | |
786 |
|
833 | |||
787 | return None |
|
834 | return None | |
788 |
|
835 | |||
789 | def complete(self, text=None, line_buffer=None, cursor_pos=None): |
|
836 | def complete(self, text=None, line_buffer=None, cursor_pos=None): | |
790 | """Find completions for the given text and line context. |
|
837 | """Find completions for the given text and line context. | |
791 |
|
838 | |||
792 | This is called successively with state == 0, 1, 2, ... until it |
|
839 | This is called successively with state == 0, 1, 2, ... until it | |
793 | returns None. The completion should begin with 'text'. |
|
840 | returns None. The completion should begin with 'text'. | |
794 |
|
841 | |||
795 | Note that both the text and the line_buffer are optional, but at least |
|
842 | Note that both the text and the line_buffer are optional, but at least | |
796 | one of them must be given. |
|
843 | one of them must be given. | |
797 |
|
844 | |||
798 | Parameters |
|
845 | Parameters | |
799 | ---------- |
|
846 | ---------- | |
800 | text : string, optional |
|
847 | text : string, optional | |
801 | Text to perform the completion on. If not given, the line buffer |
|
848 | Text to perform the completion on. If not given, the line buffer | |
802 | is split using the instance's CompletionSplitter object. |
|
849 | is split using the instance's CompletionSplitter object. | |
803 |
|
850 | |||
804 | line_buffer : string, optional |
|
851 | line_buffer : string, optional | |
805 | If not given, the completer attempts to obtain the current line |
|
852 | If not given, the completer attempts to obtain the current line | |
806 | buffer via readline. This keyword allows clients which are |
|
853 | buffer via readline. This keyword allows clients which are | |
807 | requesting for text completions in non-readline contexts to inform |
|
854 | requesting for text completions in non-readline contexts to inform | |
808 | the completer of the entire text. |
|
855 | the completer of the entire text. | |
809 |
|
856 | |||
810 | cursor_pos : int, optional |
|
857 | cursor_pos : int, optional | |
811 | Index of the cursor in the full line buffer. Should be provided by |
|
858 | Index of the cursor in the full line buffer. Should be provided by | |
812 | remote frontends where kernel has no access to frontend state. |
|
859 | remote frontends where kernel has no access to frontend state. | |
813 |
|
860 | |||
814 | Returns |
|
861 | Returns | |
815 | ------- |
|
862 | ------- | |
816 | text : str |
|
863 | text : str | |
817 | Text that was actually used in the completion. |
|
864 | Text that was actually used in the completion. | |
818 |
|
865 | |||
819 | matches : list |
|
866 | matches : list | |
820 | A list of completion matches. |
|
867 | A list of completion matches. | |
821 | """ |
|
868 | """ | |
822 | #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg |
|
869 | #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg | |
823 |
|
870 | |||
824 | # if the cursor position isn't given, the only sane assumption we can |
|
871 | # if the cursor position isn't given, the only sane assumption we can | |
825 | # make is that it's at the end of the line (the common case) |
|
872 | # make is that it's at the end of the line (the common case) | |
826 | if cursor_pos is None: |
|
873 | if cursor_pos is None: | |
827 | cursor_pos = len(line_buffer) if text is None else len(text) |
|
874 | cursor_pos = len(line_buffer) if text is None else len(text) | |
828 |
|
875 | |||
829 | # if text is either None or an empty string, rely on the line buffer |
|
876 | # if text is either None or an empty string, rely on the line buffer | |
830 | if not text: |
|
877 | if not text: | |
831 | text = self.splitter.split_line(line_buffer, cursor_pos) |
|
878 | text = self.splitter.split_line(line_buffer, cursor_pos) | |
832 |
|
879 | |||
833 | # If no line buffer is given, assume the input text is all there was |
|
880 | # If no line buffer is given, assume the input text is all there was | |
834 | if line_buffer is None: |
|
881 | if line_buffer is None: | |
835 | line_buffer = text |
|
882 | line_buffer = text | |
836 |
|
883 | |||
837 | self.line_buffer = line_buffer |
|
884 | self.line_buffer = line_buffer | |
838 | self.text_until_cursor = self.line_buffer[:cursor_pos] |
|
885 | self.text_until_cursor = self.line_buffer[:cursor_pos] | |
839 | #io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg |
|
886 | #io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg | |
840 |
|
887 | |||
841 | # Start with a clean slate of completions |
|
888 | # Start with a clean slate of completions | |
842 | self.matches[:] = [] |
|
889 | self.matches[:] = [] | |
843 | custom_res = self.dispatch_custom_completer(text) |
|
890 | custom_res = self.dispatch_custom_completer(text) | |
844 | if custom_res is not None: |
|
891 | if custom_res is not None: | |
845 | # did custom completers produce something? |
|
892 | # did custom completers produce something? | |
846 | self.matches = custom_res |
|
893 | self.matches = custom_res | |
847 | else: |
|
894 | else: | |
848 | # Extend the list of completions with the results of each |
|
895 | # Extend the list of completions with the results of each | |
849 | # matcher, so we return results to the user from all |
|
896 | # matcher, so we return results to the user from all | |
850 | # namespaces. |
|
897 | # namespaces. | |
851 | if self.merge_completions: |
|
898 | if self.merge_completions: | |
852 | self.matches = [] |
|
899 | self.matches = [] | |
853 | for matcher in self.matchers: |
|
900 | for matcher in self.matchers: | |
854 | try: |
|
901 | try: | |
855 | self.matches.extend(matcher(text)) |
|
902 | self.matches.extend(matcher(text)) | |
856 | except: |
|
903 | except: | |
857 | # Show the ugly traceback if the matcher causes an |
|
904 | # Show the ugly traceback if the matcher causes an | |
858 | # exception, but do NOT crash the kernel! |
|
905 | # exception, but do NOT crash the kernel! | |
859 | sys.excepthook(*sys.exc_info()) |
|
906 | sys.excepthook(*sys.exc_info()) | |
860 | else: |
|
907 | else: | |
861 | for matcher in self.matchers: |
|
908 | for matcher in self.matchers: | |
862 | self.matches = matcher(text) |
|
909 | self.matches = matcher(text) | |
863 | if self.matches: |
|
910 | if self.matches: | |
864 | break |
|
911 | break | |
865 | # FIXME: we should extend our api to return a dict with completions for |
|
912 | # FIXME: we should extend our api to return a dict with completions for | |
866 | # different types of objects. The rlcomplete() method could then |
|
913 | # different types of objects. The rlcomplete() method could then | |
867 | # simply collapse the dict into a list for readline, but we'd have |
|
914 | # simply collapse the dict into a list for readline, but we'd have | |
868 | # richer completion semantics in other evironments. |
|
915 | # richer completion semantics in other evironments. | |
869 | self.matches = sorted(set(self.matches)) |
|
916 | self.matches = sorted(set(self.matches)) | |
870 | #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg |
|
917 | #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg | |
871 | return text, self.matches |
|
918 | return text, self.matches | |
872 |
|
919 | |||
873 | def rlcomplete(self, text, state): |
|
920 | def rlcomplete(self, text, state): | |
874 | """Return the state-th possible completion for 'text'. |
|
921 | """Return the state-th possible completion for 'text'. | |
875 |
|
922 | |||
876 | This is called successively with state == 0, 1, 2, ... until it |
|
923 | This is called successively with state == 0, 1, 2, ... until it | |
877 | returns None. The completion should begin with 'text'. |
|
924 | returns None. The completion should begin with 'text'. | |
878 |
|
925 | |||
879 | Parameters |
|
926 | Parameters | |
880 | ---------- |
|
927 | ---------- | |
881 | text : string |
|
928 | text : string | |
882 | Text to perform the completion on. |
|
929 | Text to perform the completion on. | |
883 |
|
930 | |||
884 | state : int |
|
931 | state : int | |
885 | Counter used by readline. |
|
932 | Counter used by readline. | |
886 | """ |
|
933 | """ | |
887 | if state==0: |
|
934 | if state==0: | |
888 |
|
935 | |||
889 | self.line_buffer = line_buffer = self.readline.get_line_buffer() |
|
936 | self.line_buffer = line_buffer = self.readline.get_line_buffer() | |
890 | cursor_pos = self.readline.get_endidx() |
|
937 | cursor_pos = self.readline.get_endidx() | |
891 |
|
938 | |||
892 | #io.rprint("\nRLCOMPLETE: %r %r %r" % |
|
939 | #io.rprint("\nRLCOMPLETE: %r %r %r" % | |
893 | # (text, line_buffer, cursor_pos) ) # dbg |
|
940 | # (text, line_buffer, cursor_pos) ) # dbg | |
894 |
|
941 | |||
895 | # if there is only a tab on a line with only whitespace, instead of |
|
942 | # if there is only a tab on a line with only whitespace, instead of | |
896 | # the mostly useless 'do you want to see all million completions' |
|
943 | # the mostly useless 'do you want to see all million completions' | |
897 | # message, just do the right thing and give the user his tab! |
|
944 | # message, just do the right thing and give the user his tab! | |
898 | # Incidentally, this enables pasting of tabbed text from an editor |
|
945 | # Incidentally, this enables pasting of tabbed text from an editor | |
899 | # (as long as autoindent is off). |
|
946 | # (as long as autoindent is off). | |
900 |
|
947 | |||
901 | # It should be noted that at least pyreadline still shows file |
|
948 | # It should be noted that at least pyreadline still shows file | |
902 | # completions - is there a way around it? |
|
949 | # completions - is there a way around it? | |
903 |
|
950 | |||
904 | # don't apply this on 'dumb' terminals, such as emacs buffers, so |
|
951 | # don't apply this on 'dumb' terminals, such as emacs buffers, so | |
905 | # we don't interfere with their own tab-completion mechanism. |
|
952 | # we don't interfere with their own tab-completion mechanism. | |
906 | if not (self.dumb_terminal or line_buffer.strip()): |
|
953 | if not (self.dumb_terminal or line_buffer.strip()): | |
907 | self.readline.insert_text('\t') |
|
954 | self.readline.insert_text('\t') | |
908 | sys.stdout.flush() |
|
955 | sys.stdout.flush() | |
909 | return None |
|
956 | return None | |
910 |
|
957 | |||
911 | # Note: debugging exceptions that may occur in completion is very |
|
958 | # Note: debugging exceptions that may occur in completion is very | |
912 | # tricky, because readline unconditionally silences them. So if |
|
959 | # tricky, because readline unconditionally silences them. So if | |
913 | # during development you suspect a bug in the completion code, turn |
|
960 | # during development you suspect a bug in the completion code, turn | |
914 | # this flag on temporarily by uncommenting the second form (don't |
|
961 | # this flag on temporarily by uncommenting the second form (don't | |
915 | # flip the value in the first line, as the '# dbg' marker can be |
|
962 | # flip the value in the first line, as the '# dbg' marker can be | |
916 | # automatically detected and is used elsewhere). |
|
963 | # automatically detected and is used elsewhere). | |
917 | DEBUG = False |
|
964 | DEBUG = False | |
918 | #DEBUG = True # dbg |
|
965 | #DEBUG = True # dbg | |
919 | if DEBUG: |
|
966 | if DEBUG: | |
920 | try: |
|
967 | try: | |
921 | self.complete(text, line_buffer, cursor_pos) |
|
968 | self.complete(text, line_buffer, cursor_pos) | |
922 | except: |
|
969 | except: | |
923 | import traceback; traceback.print_exc() |
|
970 | import traceback; traceback.print_exc() | |
924 | else: |
|
971 | else: | |
925 | # The normal production version is here |
|
972 | # The normal production version is here | |
926 |
|
973 | |||
927 | # This method computes the self.matches array |
|
974 | # This method computes the self.matches array | |
928 | self.complete(text, line_buffer, cursor_pos) |
|
975 | self.complete(text, line_buffer, cursor_pos) | |
929 |
|
976 | |||
930 | try: |
|
977 | try: | |
931 | return self.matches[state] |
|
978 | return self.matches[state] | |
932 | except IndexError: |
|
979 | except IndexError: | |
933 | return None |
|
980 | return None |
@@ -1,345 +1,364 b'' | |||||
1 | """Tests for the IPython tab-completion machinery. |
|
1 | """Tests for the IPython tab-completion machinery. | |
2 | """ |
|
2 | """ | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Module imports |
|
4 | # Module imports | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 |
|
6 | |||
7 | # stdlib |
|
7 | # stdlib | |
8 | import os |
|
8 | import os | |
9 | import sys |
|
9 | import sys | |
10 | import unittest |
|
10 | import unittest | |
11 |
|
11 | |||
12 | # third party |
|
12 | # third party | |
13 | import nose.tools as nt |
|
13 | import nose.tools as nt | |
14 |
|
14 | |||
15 | # our own packages |
|
15 | # our own packages | |
16 | from IPython.config.loader import Config |
|
16 | from IPython.config.loader import Config | |
17 | from IPython.core import completer |
|
17 | from IPython.core import completer | |
18 | from IPython.external.decorators import knownfailureif |
|
18 | from IPython.external.decorators import knownfailureif | |
19 | from IPython.utils.tempdir import TemporaryDirectory |
|
19 | from IPython.utils.tempdir import TemporaryDirectory | |
20 | from IPython.utils.generics import complete_object |
|
20 | from IPython.utils.generics import complete_object | |
21 |
|
21 | |||
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 | # Test functions |
|
23 | # Test functions | |
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | def test_protect_filename(): |
|
25 | def test_protect_filename(): | |
26 | pairs = [ ('abc','abc'), |
|
26 | pairs = [ ('abc','abc'), | |
27 | (' abc',r'\ abc'), |
|
27 | (' abc',r'\ abc'), | |
28 | ('a bc',r'a\ bc'), |
|
28 | ('a bc',r'a\ bc'), | |
29 | ('a bc',r'a\ \ bc'), |
|
29 | ('a bc',r'a\ \ bc'), | |
30 | (' bc',r'\ \ bc'), |
|
30 | (' bc',r'\ \ bc'), | |
31 | ] |
|
31 | ] | |
32 | # On posix, we also protect parens and other special characters |
|
32 | # On posix, we also protect parens and other special characters | |
33 | if sys.platform != 'win32': |
|
33 | if sys.platform != 'win32': | |
34 | pairs.extend( [('a(bc',r'a\(bc'), |
|
34 | pairs.extend( [('a(bc',r'a\(bc'), | |
35 | ('a)bc',r'a\)bc'), |
|
35 | ('a)bc',r'a\)bc'), | |
36 | ('a( )bc',r'a\(\ \)bc'), |
|
36 | ('a( )bc',r'a\(\ \)bc'), | |
37 | ('a[1]bc', r'a\[1\]bc'), |
|
37 | ('a[1]bc', r'a\[1\]bc'), | |
38 | ('a{1}bc', r'a\{1\}bc'), |
|
38 | ('a{1}bc', r'a\{1\}bc'), | |
39 | ('a#bc', r'a\#bc'), |
|
39 | ('a#bc', r'a\#bc'), | |
40 | ('a?bc', r'a\?bc'), |
|
40 | ('a?bc', r'a\?bc'), | |
41 | ('a=bc', r'a\=bc'), |
|
41 | ('a=bc', r'a\=bc'), | |
42 | ('a\\bc', r'a\\bc'), |
|
42 | ('a\\bc', r'a\\bc'), | |
43 | ('a|bc', r'a\|bc'), |
|
43 | ('a|bc', r'a\|bc'), | |
44 | ('a;bc', r'a\;bc'), |
|
44 | ('a;bc', r'a\;bc'), | |
45 | ('a:bc', r'a\:bc'), |
|
45 | ('a:bc', r'a\:bc'), | |
46 | ("a'bc", r"a\'bc"), |
|
46 | ("a'bc", r"a\'bc"), | |
47 | ('a*bc', r'a\*bc'), |
|
47 | ('a*bc', r'a\*bc'), | |
48 | ('a"bc', r'a\"bc'), |
|
48 | ('a"bc', r'a\"bc'), | |
49 | ('a^bc', r'a\^bc'), |
|
49 | ('a^bc', r'a\^bc'), | |
50 | ('a&bc', r'a\&bc'), |
|
50 | ('a&bc', r'a\&bc'), | |
51 | ] ) |
|
51 | ] ) | |
52 | # run the actual tests |
|
52 | # run the actual tests | |
53 | for s1, s2 in pairs: |
|
53 | for s1, s2 in pairs: | |
54 | s1p = completer.protect_filename(s1) |
|
54 | s1p = completer.protect_filename(s1) | |
55 | nt.assert_equal(s1p, s2) |
|
55 | nt.assert_equal(s1p, s2) | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | def check_line_split(splitter, test_specs): |
|
58 | def check_line_split(splitter, test_specs): | |
59 | for part1, part2, split in test_specs: |
|
59 | for part1, part2, split in test_specs: | |
60 | cursor_pos = len(part1) |
|
60 | cursor_pos = len(part1) | |
61 | line = part1+part2 |
|
61 | line = part1+part2 | |
62 | out = splitter.split_line(line, cursor_pos) |
|
62 | out = splitter.split_line(line, cursor_pos) | |
63 | nt.assert_equal(out, split) |
|
63 | nt.assert_equal(out, split) | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | def test_line_split(): |
|
66 | def test_line_split(): | |
67 | """Basice line splitter test with default specs.""" |
|
67 | """Basice line splitter test with default specs.""" | |
68 | sp = completer.CompletionSplitter() |
|
68 | sp = completer.CompletionSplitter() | |
69 | # The format of the test specs is: part1, part2, expected answer. Parts 1 |
|
69 | # The format of the test specs is: part1, part2, expected answer. Parts 1 | |
70 | # and 2 are joined into the 'line' sent to the splitter, as if the cursor |
|
70 | # and 2 are joined into the 'line' sent to the splitter, as if the cursor | |
71 | # was at the end of part1. So an empty part2 represents someone hitting |
|
71 | # was at the end of part1. So an empty part2 represents someone hitting | |
72 | # tab at the end of the line, the most common case. |
|
72 | # tab at the end of the line, the most common case. | |
73 | t = [('run some/scrip', '', 'some/scrip'), |
|
73 | t = [('run some/scrip', '', 'some/scrip'), | |
74 | ('run scripts/er', 'ror.py foo', 'scripts/er'), |
|
74 | ('run scripts/er', 'ror.py foo', 'scripts/er'), | |
75 | ('echo $HOM', '', 'HOM'), |
|
75 | ('echo $HOM', '', 'HOM'), | |
76 | ('print sys.pa', '', 'sys.pa'), |
|
76 | ('print sys.pa', '', 'sys.pa'), | |
77 | ('print(sys.pa', '', 'sys.pa'), |
|
77 | ('print(sys.pa', '', 'sys.pa'), | |
78 | ("execfile('scripts/er", '', 'scripts/er'), |
|
78 | ("execfile('scripts/er", '', 'scripts/er'), | |
79 | ('a[x.', '', 'x.'), |
|
79 | ('a[x.', '', 'x.'), | |
80 | ('a[x.', 'y', 'x.'), |
|
80 | ('a[x.', 'y', 'x.'), | |
81 | ('cd "some_file/', '', 'some_file/'), |
|
81 | ('cd "some_file/', '', 'some_file/'), | |
82 | ] |
|
82 | ] | |
83 | check_line_split(sp, t) |
|
83 | check_line_split(sp, t) | |
84 | # Ensure splitting works OK with unicode by re-running the tests with |
|
84 | # Ensure splitting works OK with unicode by re-running the tests with | |
85 | # all inputs turned into unicode |
|
85 | # all inputs turned into unicode | |
86 | check_line_split(sp, [ map(unicode, p) for p in t] ) |
|
86 | check_line_split(sp, [ map(unicode, p) for p in t] ) | |
87 |
|
87 | |||
88 |
|
88 | |||
89 | def test_custom_completion_error(): |
|
89 | def test_custom_completion_error(): | |
90 | """Test that errors from custom attribute completers are silenced.""" |
|
90 | """Test that errors from custom attribute completers are silenced.""" | |
91 | ip = get_ipython() |
|
91 | ip = get_ipython() | |
92 | class A(object): pass |
|
92 | class A(object): pass | |
93 | ip.user_ns['a'] = A() |
|
93 | ip.user_ns['a'] = A() | |
94 |
|
94 | |||
95 | @complete_object.when_type(A) |
|
95 | @complete_object.when_type(A) | |
96 | def complete_A(a, existing_completions): |
|
96 | def complete_A(a, existing_completions): | |
97 | raise TypeError("this should be silenced") |
|
97 | raise TypeError("this should be silenced") | |
98 |
|
98 | |||
99 | ip.complete("a.") |
|
99 | ip.complete("a.") | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | def test_unicode_completions(): |
|
102 | def test_unicode_completions(): | |
103 | ip = get_ipython() |
|
103 | ip = get_ipython() | |
104 | # Some strings that trigger different types of completion. Check them both |
|
104 | # Some strings that trigger different types of completion. Check them both | |
105 | # in str and unicode forms |
|
105 | # in str and unicode forms | |
106 | s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/'] |
|
106 | s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/'] | |
107 | for t in s + map(unicode, s): |
|
107 | for t in s + map(unicode, s): | |
108 | # We don't need to check exact completion values (they may change |
|
108 | # We don't need to check exact completion values (they may change | |
109 | # depending on the state of the namespace, but at least no exceptions |
|
109 | # depending on the state of the namespace, but at least no exceptions | |
110 | # should be thrown and the return value should be a pair of text, list |
|
110 | # should be thrown and the return value should be a pair of text, list | |
111 | # values. |
|
111 | # values. | |
112 | text, matches = ip.complete(t) |
|
112 | text, matches = ip.complete(t) | |
113 | nt.assert_true(isinstance(text, basestring)) |
|
113 | nt.assert_true(isinstance(text, basestring)) | |
114 | nt.assert_true(isinstance(matches, list)) |
|
114 | nt.assert_true(isinstance(matches, list)) | |
115 |
|
115 | |||
116 |
|
116 | |||
117 | class CompletionSplitterTestCase(unittest.TestCase): |
|
117 | class CompletionSplitterTestCase(unittest.TestCase): | |
118 | def setUp(self): |
|
118 | def setUp(self): | |
119 | self.sp = completer.CompletionSplitter() |
|
119 | self.sp = completer.CompletionSplitter() | |
120 |
|
120 | |||
121 | def test_delim_setting(self): |
|
121 | def test_delim_setting(self): | |
122 | self.sp.delims = ' ' |
|
122 | self.sp.delims = ' ' | |
123 | nt.assert_equal(self.sp.delims, ' ') |
|
123 | nt.assert_equal(self.sp.delims, ' ') | |
124 | nt.assert_equal(self.sp._delim_expr, '[\ ]') |
|
124 | nt.assert_equal(self.sp._delim_expr, '[\ ]') | |
125 |
|
125 | |||
126 | def test_spaces(self): |
|
126 | def test_spaces(self): | |
127 | """Test with only spaces as split chars.""" |
|
127 | """Test with only spaces as split chars.""" | |
128 | self.sp.delims = ' ' |
|
128 | self.sp.delims = ' ' | |
129 | t = [('foo', '', 'foo'), |
|
129 | t = [('foo', '', 'foo'), | |
130 | ('run foo', '', 'foo'), |
|
130 | ('run foo', '', 'foo'), | |
131 | ('run foo', 'bar', 'foo'), |
|
131 | ('run foo', 'bar', 'foo'), | |
132 | ] |
|
132 | ] | |
133 | check_line_split(self.sp, t) |
|
133 | check_line_split(self.sp, t) | |
134 |
|
134 | |||
135 |
|
135 | |||
136 | def test_has_open_quotes1(): |
|
136 | def test_has_open_quotes1(): | |
137 | for s in ["'", "'''", "'hi' '"]: |
|
137 | for s in ["'", "'''", "'hi' '"]: | |
138 | nt.assert_equal(completer.has_open_quotes(s), "'") |
|
138 | nt.assert_equal(completer.has_open_quotes(s), "'") | |
139 |
|
139 | |||
140 |
|
140 | |||
141 | def test_has_open_quotes2(): |
|
141 | def test_has_open_quotes2(): | |
142 | for s in ['"', '"""', '"hi" "']: |
|
142 | for s in ['"', '"""', '"hi" "']: | |
143 | nt.assert_equal(completer.has_open_quotes(s), '"') |
|
143 | nt.assert_equal(completer.has_open_quotes(s), '"') | |
144 |
|
144 | |||
145 |
|
145 | |||
146 | def test_has_open_quotes3(): |
|
146 | def test_has_open_quotes3(): | |
147 | for s in ["''", "''' '''", "'hi' 'ipython'"]: |
|
147 | for s in ["''", "''' '''", "'hi' 'ipython'"]: | |
148 | nt.assert_false(completer.has_open_quotes(s)) |
|
148 | nt.assert_false(completer.has_open_quotes(s)) | |
149 |
|
149 | |||
150 |
|
150 | |||
151 | def test_has_open_quotes4(): |
|
151 | def test_has_open_quotes4(): | |
152 | for s in ['""', '""" """', '"hi" "ipython"']: |
|
152 | for s in ['""', '""" """', '"hi" "ipython"']: | |
153 | nt.assert_false(completer.has_open_quotes(s)) |
|
153 | nt.assert_false(completer.has_open_quotes(s)) | |
154 |
|
154 | |||
155 |
|
155 | |||
156 | @knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows") |
|
156 | @knownfailureif(sys.platform == 'win32', "abspath completions fail on Windows") | |
157 | def test_abspath_file_completions(): |
|
157 | def test_abspath_file_completions(): | |
158 | ip = get_ipython() |
|
158 | ip = get_ipython() | |
159 | with TemporaryDirectory() as tmpdir: |
|
159 | with TemporaryDirectory() as tmpdir: | |
160 | prefix = os.path.join(tmpdir, 'foo') |
|
160 | prefix = os.path.join(tmpdir, 'foo') | |
161 | suffixes = map(str, [1,2]) |
|
161 | suffixes = map(str, [1,2]) | |
162 | names = [prefix+s for s in suffixes] |
|
162 | names = [prefix+s for s in suffixes] | |
163 | for n in names: |
|
163 | for n in names: | |
164 | open(n, 'w').close() |
|
164 | open(n, 'w').close() | |
165 |
|
165 | |||
166 | # Check simple completion |
|
166 | # Check simple completion | |
167 | c = ip.complete(prefix)[1] |
|
167 | c = ip.complete(prefix)[1] | |
168 | nt.assert_equal(c, names) |
|
168 | nt.assert_equal(c, names) | |
169 |
|
169 | |||
170 | # Now check with a function call |
|
170 | # Now check with a function call | |
171 | cmd = 'a = f("%s' % prefix |
|
171 | cmd = 'a = f("%s' % prefix | |
172 | c = ip.complete(prefix, cmd)[1] |
|
172 | c = ip.complete(prefix, cmd)[1] | |
173 | comp = [prefix+s for s in suffixes] |
|
173 | comp = [prefix+s for s in suffixes] | |
174 | nt.assert_equal(c, comp) |
|
174 | nt.assert_equal(c, comp) | |
175 |
|
175 | |||
176 |
|
176 | |||
177 | def test_local_file_completions(): |
|
177 | def test_local_file_completions(): | |
178 | ip = get_ipython() |
|
178 | ip = get_ipython() | |
179 | cwd = os.getcwdu() |
|
179 | cwd = os.getcwdu() | |
180 | try: |
|
180 | try: | |
181 | with TemporaryDirectory() as tmpdir: |
|
181 | with TemporaryDirectory() as tmpdir: | |
182 | os.chdir(tmpdir) |
|
182 | os.chdir(tmpdir) | |
183 | prefix = './foo' |
|
183 | prefix = './foo' | |
184 | suffixes = map(str, [1,2]) |
|
184 | suffixes = map(str, [1,2]) | |
185 | names = [prefix+s for s in suffixes] |
|
185 | names = [prefix+s for s in suffixes] | |
186 | for n in names: |
|
186 | for n in names: | |
187 | open(n, 'w').close() |
|
187 | open(n, 'w').close() | |
188 |
|
188 | |||
189 | # Check simple completion |
|
189 | # Check simple completion | |
190 | c = ip.complete(prefix)[1] |
|
190 | c = ip.complete(prefix)[1] | |
191 | nt.assert_equal(c, names) |
|
191 | nt.assert_equal(c, names) | |
192 |
|
192 | |||
193 | # Now check with a function call |
|
193 | # Now check with a function call | |
194 | cmd = 'a = f("%s' % prefix |
|
194 | cmd = 'a = f("%s' % prefix | |
195 | c = ip.complete(prefix, cmd)[1] |
|
195 | c = ip.complete(prefix, cmd)[1] | |
196 | comp = [prefix+s for s in suffixes] |
|
196 | comp = [prefix+s for s in suffixes] | |
197 | nt.assert_equal(c, comp) |
|
197 | nt.assert_equal(c, comp) | |
198 | finally: |
|
198 | finally: | |
199 | # prevent failures from making chdir stick |
|
199 | # prevent failures from making chdir stick | |
200 | os.chdir(cwd) |
|
200 | os.chdir(cwd) | |
201 |
|
201 | |||
202 |
|
202 | |||
203 | def test_greedy_completions(): |
|
203 | def test_greedy_completions(): | |
204 | ip = get_ipython() |
|
204 | ip = get_ipython() | |
205 | greedy_original = ip.Completer.greedy |
|
205 | greedy_original = ip.Completer.greedy | |
206 | try: |
|
206 | try: | |
207 | ip.Completer.greedy = False |
|
207 | ip.Completer.greedy = False | |
208 | ip.ex('a=range(5)') |
|
208 | ip.ex('a=range(5)') | |
209 | _,c = ip.complete('.',line='a[0].') |
|
209 | _,c = ip.complete('.',line='a[0].') | |
210 | nt.assert_false('a[0].real' in c, |
|
210 | nt.assert_false('a[0].real' in c, | |
211 | "Shouldn't have completed on a[0]: %s"%c) |
|
211 | "Shouldn't have completed on a[0]: %s"%c) | |
212 | ip.Completer.greedy = True |
|
212 | ip.Completer.greedy = True | |
213 | _,c = ip.complete('.',line='a[0].') |
|
213 | _,c = ip.complete('.',line='a[0].') | |
214 | nt.assert_true('a[0].real' in c, "Should have completed on a[0]: %s"%c) |
|
214 | nt.assert_true('a[0].real' in c, "Should have completed on a[0]: %s"%c) | |
215 | finally: |
|
215 | finally: | |
216 | ip.Completer.greedy = greedy_original |
|
216 | ip.Completer.greedy = greedy_original | |
217 |
|
217 | |||
218 |
|
218 | |||
219 | def test_omit__names(): |
|
219 | def test_omit__names(): | |
220 | # also happens to test IPCompleter as a configurable |
|
220 | # also happens to test IPCompleter as a configurable | |
221 | ip = get_ipython() |
|
221 | ip = get_ipython() | |
222 | ip._hidden_attr = 1 |
|
222 | ip._hidden_attr = 1 | |
223 | c = ip.Completer |
|
223 | c = ip.Completer | |
224 | ip.ex('ip=get_ipython()') |
|
224 | ip.ex('ip=get_ipython()') | |
225 | cfg = Config() |
|
225 | cfg = Config() | |
226 | cfg.IPCompleter.omit__names = 0 |
|
226 | cfg.IPCompleter.omit__names = 0 | |
227 | c.update_config(cfg) |
|
227 | c.update_config(cfg) | |
228 | s,matches = c.complete('ip.') |
|
228 | s,matches = c.complete('ip.') | |
229 | nt.assert_true('ip.__str__' in matches) |
|
229 | nt.assert_true('ip.__str__' in matches) | |
230 | nt.assert_true('ip._hidden_attr' in matches) |
|
230 | nt.assert_true('ip._hidden_attr' in matches) | |
231 | cfg.IPCompleter.omit__names = 1 |
|
231 | cfg.IPCompleter.omit__names = 1 | |
232 | c.update_config(cfg) |
|
232 | c.update_config(cfg) | |
233 | s,matches = c.complete('ip.') |
|
233 | s,matches = c.complete('ip.') | |
234 | nt.assert_false('ip.__str__' in matches) |
|
234 | nt.assert_false('ip.__str__' in matches) | |
235 | nt.assert_true('ip._hidden_attr' in matches) |
|
235 | nt.assert_true('ip._hidden_attr' in matches) | |
236 | cfg.IPCompleter.omit__names = 2 |
|
236 | cfg.IPCompleter.omit__names = 2 | |
237 | c.update_config(cfg) |
|
237 | c.update_config(cfg) | |
238 | s,matches = c.complete('ip.') |
|
238 | s,matches = c.complete('ip.') | |
239 | nt.assert_false('ip.__str__' in matches) |
|
239 | nt.assert_false('ip.__str__' in matches) | |
240 | nt.assert_false('ip._hidden_attr' in matches) |
|
240 | nt.assert_false('ip._hidden_attr' in matches) | |
241 | del ip._hidden_attr |
|
241 | del ip._hidden_attr | |
242 |
|
242 | |||
243 |
|
243 | |||
244 | def test_limit_to__all__False_ok(): |
|
244 | def test_limit_to__all__False_ok(): | |
245 | ip = get_ipython() |
|
245 | ip = get_ipython() | |
246 | c = ip.Completer |
|
246 | c = ip.Completer | |
247 | ip.ex('class D: x=24') |
|
247 | ip.ex('class D: x=24') | |
248 | ip.ex('d=D()') |
|
248 | ip.ex('d=D()') | |
249 | cfg = Config() |
|
249 | cfg = Config() | |
250 | cfg.IPCompleter.limit_to__all__ = False |
|
250 | cfg.IPCompleter.limit_to__all__ = False | |
251 | c.update_config(cfg) |
|
251 | c.update_config(cfg) | |
252 | s, matches = c.complete('d.') |
|
252 | s, matches = c.complete('d.') | |
253 | nt.assert_true('d.x' in matches) |
|
253 | nt.assert_true('d.x' in matches) | |
254 |
|
254 | |||
255 |
|
255 | |||
256 | def test_limit_to__all__True_ok(): |
|
256 | def test_limit_to__all__True_ok(): | |
257 | ip = get_ipython() |
|
257 | ip = get_ipython() | |
258 | c = ip.Completer |
|
258 | c = ip.Completer | |
259 | ip.ex('class D: x=24') |
|
259 | ip.ex('class D: x=24') | |
260 | ip.ex('d=D()') |
|
260 | ip.ex('d=D()') | |
261 | ip.ex("d.__all__=['z']") |
|
261 | ip.ex("d.__all__=['z']") | |
262 | cfg = Config() |
|
262 | cfg = Config() | |
263 | cfg.IPCompleter.limit_to__all__ = True |
|
263 | cfg.IPCompleter.limit_to__all__ = True | |
264 | c.update_config(cfg) |
|
264 | c.update_config(cfg) | |
265 | s, matches = c.complete('d.') |
|
265 | s, matches = c.complete('d.') | |
266 | nt.assert_true('d.z' in matches) |
|
266 | nt.assert_true('d.z' in matches) | |
267 | nt.assert_false('d.x' in matches) |
|
267 | nt.assert_false('d.x' in matches) | |
268 |
|
268 | |||
269 |
|
269 | |||
270 | def test_get__all__entries_ok(): |
|
270 | def test_get__all__entries_ok(): | |
271 | class A(object): |
|
271 | class A(object): | |
272 | __all__ = ['x', 1] |
|
272 | __all__ = ['x', 1] | |
273 | words = completer.get__all__entries(A()) |
|
273 | words = completer.get__all__entries(A()) | |
274 | nt.assert_equal(words, ['x']) |
|
274 | nt.assert_equal(words, ['x']) | |
275 |
|
275 | |||
276 |
|
276 | |||
277 | def test_get__all__entries_no__all__ok(): |
|
277 | def test_get__all__entries_no__all__ok(): | |
278 | class A(object): |
|
278 | class A(object): | |
279 | pass |
|
279 | pass | |
280 | words = completer.get__all__entries(A()) |
|
280 | words = completer.get__all__entries(A()) | |
281 | nt.assert_equal(words, []) |
|
281 | nt.assert_equal(words, []) | |
282 |
|
282 | |||
283 |
|
283 | |||
284 | def test_func_kw_completions(): |
|
284 | def test_func_kw_completions(): | |
285 | ip = get_ipython() |
|
285 | ip = get_ipython() | |
286 | c = ip.Completer |
|
286 | c = ip.Completer | |
287 | ip.ex('def myfunc(a=1,b=2): return a+b') |
|
287 | ip.ex('def myfunc(a=1,b=2): return a+b') | |
288 | s, matches = c.complete(None, 'myfunc(1,b') |
|
288 | s, matches = c.complete(None, 'myfunc(1,b') | |
289 | nt.assert_in('b=', matches) |
|
289 | nt.assert_in('b=', matches) | |
290 | # Simulate completing with cursor right after b (pos==10): |
|
290 | # Simulate completing with cursor right after b (pos==10): | |
291 | s, matches = c.complete(None,'myfunc(1,b)', 10) |
|
291 | s, matches = c.complete(None, 'myfunc(1,b)', 10) | |
292 | nt.assert_in('b=', matches) |
|
292 | nt.assert_in('b=', matches) | |
293 | s, matches = c.complete(None,'myfunc(a="escaped\\")string",b') |
|
293 | s, matches = c.complete(None, 'myfunc(a="escaped\\")string",b') | |
294 | nt.assert_in('b=', matches) |
|
294 | nt.assert_in('b=', matches) | |
|
295 | #builtin function | |||
|
296 | s, matches = c.complete(None, 'min(k, k') | |||
|
297 | nt.assert_in('key=', matches) | |||
295 |
|
298 | |||
296 |
|
299 | |||
|
300 | def test_default_arguments_from_docstring(): | |||
|
301 | doc = min.__doc__ | |||
|
302 | ip = get_ipython() | |||
|
303 | c = ip.Completer | |||
|
304 | kwd = c._default_arguments_from_docstring( | |||
|
305 | 'min(iterable[, key=func]) -> value') | |||
|
306 | nt.assert_equal(kwd, ['key']) | |||
|
307 | #with cython type etc | |||
|
308 | kwd = c._default_arguments_from_docstring( | |||
|
309 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n') | |||
|
310 | nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit']) | |||
|
311 | #white spaces | |||
|
312 | kwd = c._default_arguments_from_docstring( | |||
|
313 | '\n Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)\n') | |||
|
314 | nt.assert_equal(kwd, ['ncall', 'resume', 'nsplit']) | |||
|
315 | ||||
297 | def test_line_magics(): |
|
316 | def test_line_magics(): | |
298 | ip = get_ipython() |
|
317 | ip = get_ipython() | |
299 | c = ip.Completer |
|
318 | c = ip.Completer | |
300 | s, matches = c.complete(None, 'lsmag') |
|
319 | s, matches = c.complete(None, 'lsmag') | |
301 | nt.assert_in('%lsmagic', matches) |
|
320 | nt.assert_in('%lsmagic', matches) | |
302 | s, matches = c.complete(None, '%lsmag') |
|
321 | s, matches = c.complete(None, '%lsmag') | |
303 | nt.assert_in('%lsmagic', matches) |
|
322 | nt.assert_in('%lsmagic', matches) | |
304 |
|
323 | |||
305 |
|
324 | |||
306 | def test_cell_magics(): |
|
325 | def test_cell_magics(): | |
307 | from IPython.core.magic import register_cell_magic |
|
326 | from IPython.core.magic import register_cell_magic | |
308 |
|
327 | |||
309 | @register_cell_magic |
|
328 | @register_cell_magic | |
310 | def _foo_cellm(line, cell): |
|
329 | def _foo_cellm(line, cell): | |
311 | pass |
|
330 | pass | |
312 |
|
331 | |||
313 | ip = get_ipython() |
|
332 | ip = get_ipython() | |
314 | c = ip.Completer |
|
333 | c = ip.Completer | |
315 |
|
334 | |||
316 | s, matches = c.complete(None, '_foo_ce') |
|
335 | s, matches = c.complete(None, '_foo_ce') | |
317 | nt.assert_in('%%_foo_cellm', matches) |
|
336 | nt.assert_in('%%_foo_cellm', matches) | |
318 | s, matches = c.complete(None, '%%_foo_ce') |
|
337 | s, matches = c.complete(None, '%%_foo_ce') | |
319 | nt.assert_in('%%_foo_cellm', matches) |
|
338 | nt.assert_in('%%_foo_cellm', matches) | |
320 |
|
339 | |||
321 |
|
340 | |||
322 | def test_line_cell_magics(): |
|
341 | def test_line_cell_magics(): | |
323 | from IPython.core.magic import register_line_cell_magic |
|
342 | from IPython.core.magic import register_line_cell_magic | |
324 |
|
343 | |||
325 | @register_line_cell_magic |
|
344 | @register_line_cell_magic | |
326 | def _bar_cellm(line, cell): |
|
345 | def _bar_cellm(line, cell): | |
327 | pass |
|
346 | pass | |
328 |
|
347 | |||
329 | ip = get_ipython() |
|
348 | ip = get_ipython() | |
330 | c = ip.Completer |
|
349 | c = ip.Completer | |
331 |
|
350 | |||
332 | # The policy here is trickier, see comments in completion code. The |
|
351 | # The policy here is trickier, see comments in completion code. The | |
333 | # returned values depend on whether the user passes %% or not explicitly, |
|
352 | # returned values depend on whether the user passes %% or not explicitly, | |
334 | # and this will show a difference if the same name is both a line and cell |
|
353 | # and this will show a difference if the same name is both a line and cell | |
335 | # magic. |
|
354 | # magic. | |
336 | s, matches = c.complete(None, '_bar_ce') |
|
355 | s, matches = c.complete(None, '_bar_ce') | |
337 | nt.assert_in('%_bar_cellm', matches) |
|
356 | nt.assert_in('%_bar_cellm', matches) | |
338 | nt.assert_in('%%_bar_cellm', matches) |
|
357 | nt.assert_in('%%_bar_cellm', matches) | |
339 | s, matches = c.complete(None, '%_bar_ce') |
|
358 | s, matches = c.complete(None, '%_bar_ce') | |
340 | nt.assert_in('%_bar_cellm', matches) |
|
359 | nt.assert_in('%_bar_cellm', matches) | |
341 | nt.assert_in('%%_bar_cellm', matches) |
|
360 | nt.assert_in('%%_bar_cellm', matches) | |
342 | s, matches = c.complete(None, '%%_bar_ce') |
|
361 | s, matches = c.complete(None, '%%_bar_ce') | |
343 | nt.assert_not_in('%_bar_cellm', matches) |
|
362 | nt.assert_not_in('%_bar_cellm', matches) | |
344 | nt.assert_in('%%_bar_cellm', matches) |
|
363 | nt.assert_in('%%_bar_cellm', matches) | |
345 |
|
364 |
General Comments 0
You need to be logged in to leave comments.
Login now