##// END OF EJS Templates
Fix bugs in completer.py with incompletely-implemented property....
Fernando Perez -
Show More
@@ -178,11 +178,14 b' def compress_user(path, tilde_expand, tilde_val):'
178 else:
178 else:
179 return path
179 return path
180
180
181
181 class Bunch(object): pass
182 class Bunch(object): pass
182
183
184
183 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
185 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
184 GREEDY_DELIMS = ' \r\n'
186 GREEDY_DELIMS = ' \r\n'
185
187
188
186 class CompletionSplitter(object):
189 class CompletionSplitter(object):
187 """An object to split an input line in a manner similar to readline.
190 """An object to split an input line in a manner similar to readline.
188
191
@@ -194,7 +197,7 b' class CompletionSplitter(object):'
194
197
195 What characters are used as splitting delimiters can be controlled by
198 What characters are used as splitting delimiters can be controlled by
196 setting the `delims` attribute (this is a property that internally
199 setting the `delims` attribute (this is a property that internally
197 automatically builds the necessary """
200 automatically builds the necessary regular expression)"""
198
201
199 # Private interface
202 # Private interface
200
203
@@ -212,19 +215,21 b' class CompletionSplitter(object):'
212
215
213 def __init__(self, delims=None):
216 def __init__(self, delims=None):
214 delims = CompletionSplitter._delims if delims is None else delims
217 delims = CompletionSplitter._delims if delims is None else delims
215 self.set_delims(delims)
218 self.delims = delims
216
219
217 def set_delims(self, delims):
220 @property
221 def delims(self):
222 """Return the string of delimiter characters."""
223 return self._delims
224
225 @delims.setter
226 def delims(self, delims):
218 """Set the delimiters for line splitting."""
227 """Set the delimiters for line splitting."""
219 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
228 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
220 self._delim_re = re.compile(expr)
229 self._delim_re = re.compile(expr)
221 self._delims = delims
230 self._delims = delims
222 self._delim_expr = expr
231 self._delim_expr = expr
223
232
224 def get_delims(self):
225 """Return the string of delimiter characters."""
226 return self._delims
227
228 def split_line(self, line, cursor_pos=None):
233 def split_line(self, line, cursor_pos=None):
229 """Split a line of text with a cursor at the given position.
234 """Split a line of text with a cursor at the given position.
230 """
235 """
@@ -377,7 +382,7 b' class Completer(Configurable):'
377 def get__all__entries(obj):
382 def get__all__entries(obj):
378 """returns the strings in the __all__ attribute"""
383 """returns the strings in the __all__ attribute"""
379 try:
384 try:
380 words = getattr(obj,'__all__')
385 words = getattr(obj, '__all__')
381 except:
386 except:
382 return []
387 return []
383
388
@@ -390,12 +395,12 b' class IPCompleter(Completer):'
390 def _greedy_changed(self, name, old, new):
395 def _greedy_changed(self, name, old, new):
391 """update the splitter and readline delims when greedy is changed"""
396 """update the splitter and readline delims when greedy is changed"""
392 if new:
397 if new:
393 self.splitter.set_delims(GREEDY_DELIMS)
398 self.splitter.delims = GREEDY_DELIMS
394 else:
399 else:
395 self.splitter.set_delims(DELIMS)
400 self.splitter.delims = DELIMS
396
401
397 if self.readline:
402 if self.readline:
398 self.readline.set_completer_delims(self.splitter.get_delims())
403 self.readline.set_completer_delims(self.splitter.delims)
399
404
400 merge_completions = CBool(True, config=True,
405 merge_completions = CBool(True, config=True,
401 help="""Whether to merge completion results into a single list
406 help="""Whether to merge completion results into a single list
@@ -821,7 +826,7 b' class IPCompleter(Completer):'
821
826
822 self.line_buffer = line_buffer
827 self.line_buffer = line_buffer
823 self.text_until_cursor = self.line_buffer[:cursor_pos]
828 self.text_until_cursor = self.line_buffer[:cursor_pos]
824 #io.rprint('\nCOMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
829 #io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
825
830
826 # Start with a clean slate of completions
831 # Start with a clean slate of completions
827 self.matches[:] = []
832 self.matches[:] = []
@@ -119,8 +119,8 b' class CompletionSplitterTestCase(unittest.TestCase):'
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.set_delims(' ')
122 self.sp.delims = ' '
123 nt.assert_equal(self.sp.get_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):
@@ -202,13 +202,18 b' def test_local_file_completions():'
202
202
203 def test_greedy_completions():
203 def test_greedy_completions():
204 ip = get_ipython()
204 ip = get_ipython()
205 ip.Completer.greedy = False
205 greedy_original = ip.Completer.greedy
206 ip.ex('a=range(5)')
206 try:
207 _,c = ip.complete('.',line='a[0].')
207 ip.Completer.greedy = False
208 nt.assert_false('a[0].real' in c, "Shouldn't have completed on a[0]: %s"%c)
208 ip.ex('a=range(5)')
209 ip.Completer.greedy = True
209 _,c = ip.complete('.',line='a[0].')
210 _,c = ip.complete('.',line='a[0].')
210 nt.assert_false('a[0].real' in c,
211 nt.assert_true('a[0].real' in c, "Should have completed on a[0]: %s"%c)
211 "Shouldn't have completed on a[0]: %s"%c)
212 ip.Completer.greedy = True
213 _,c = ip.complete('.',line='a[0].')
214 nt.assert_true('a[0].real' in c, "Should have completed on a[0]: %s"%c)
215 finally:
216 ip.Completer.greedy = greedy_original
212
217
213
218
214 def test_omit__names():
219 def test_omit__names():
@@ -280,7 +285,8 b' def test_func_kw_completions():'
280 ip = get_ipython()
285 ip = get_ipython()
281 c = ip.Completer
286 c = ip.Completer
282 ip.ex('def myfunc(a=1,b=2): return a+b')
287 ip.ex('def myfunc(a=1,b=2): return a+b')
283 s, matches = c.complete(None,'myfunc(1,b')
288 s, matches = c.complete(None, 'myfunc(1,b')
284 nt.assert_true('b=' in matches)
289 nt.assert_in('b=', matches)
285 s, matches = c.complete(None,'myfunc(1,b)',10)#cursor is right after b
290 # Simulate completing with cursor right after b (pos==10):
286 nt.assert_true('b=' in matches)
291 s, matches = c.complete(None,'myfunc(1,b)', 10)
292 nt.assert_in('b=', matches)
General Comments 0
You need to be logged in to leave comments. Login now