3939from bpython import inspection
4040from bpython import importcompletion
4141from bpython import line as lineparts
42+ from bpython .line import LinePart
4243from bpython ._py3compat import py3 , try_decode
4344from bpython .lazyre import LazyReCompile
4445
@@ -126,9 +127,10 @@ def format(self, word):
126127
127128 def substitute (self , cursor_offset , line , match ):
128129 """Returns a cursor offset and line with match swapped in"""
129- start , end , word = self .locate (cursor_offset , line )
130- result = start + len (match ), line [:start ] + match + line [end :]
131- return result
130+ lpart = self .locate (cursor_offset , line )
131+ offset = lpart .start + len (match )
132+ changed_line = line [:lpart .start ] + match + line [lpart .end :]
133+ return offset , changed_line
132134
133135 @property
134136 def shown_before_tab (self ):
@@ -200,14 +202,13 @@ def matches(self, cursor_offset, line, **kwargs):
200202 cs = lineparts .current_string (cursor_offset , line )
201203 if cs is None :
202204 return None
203- start , end , text = cs
204205 matches = set ()
205- username = text .split (os .path .sep , 1 )[0 ]
206+ username = cs . word .split (os .path .sep , 1 )[0 ]
206207 user_dir = os .path .expanduser (username )
207- for filename in self .safe_glob (os .path .expanduser (text )):
208+ for filename in self .safe_glob (os .path .expanduser (cs . word )):
208209 if os .path .isdir (filename ):
209210 filename += os .path .sep
210- if text .startswith ('~' ):
211+ if cs . word .startswith ('~' ):
211212 filename = username + filename [len (user_dir ):]
212213 matches .add (filename )
213214 return matches
@@ -235,25 +236,24 @@ def matches(self, cursor_offset, line, **kwargs):
235236 r = self .locate (cursor_offset , line )
236237 if r is None :
237238 return None
238- text = r [2 ]
239239
240240 if locals_ is None :
241241 locals_ = __main__ .__dict__
242242
243- assert '.' in text
243+ assert '.' in r . word
244244
245- for i in range (1 , len (text ) + 1 ):
246- if text [- i ] == '[' :
245+ for i in range (1 , len (r . word ) + 1 ):
246+ if r . word [- i ] == '[' :
247247 i -= 1
248248 break
249- methodtext = text [- i :]
250- matches = set ('' .join ([text [:- i ], m ])
249+ methodtext = r . word [- i :]
250+ matches = set ('' .join ([r . word [:- i ], m ])
251251 for m in self .attr_matches (methodtext , locals_ ))
252252
253253 # TODO add open paren for methods via _callable_prefix (or decide not
254254 # to) unless the first character is a _ filter out all attributes
255255 # starting with a _
256- if not text .split ('.' )[- 1 ].startswith ('_' ):
256+ if not r . word .split ('.' )[- 1 ].startswith ('_' ):
257257 matches = set (match for match in matches
258258 if not match .split ('.' )[- 1 ].startswith ('_' ))
259259 return matches
@@ -340,15 +340,14 @@ def matches(self, cursor_offset, line, **kwargs):
340340 r = self .locate (cursor_offset , line )
341341 if r is None :
342342 return None
343- start , end , orig = r
344343 _ , _ , dexpr = lineparts .current_dict (cursor_offset , line )
345344 try :
346345 obj = safe_eval (dexpr , locals_ )
347346 except EvaluationError :
348347 return set ()
349348 if isinstance (obj , dict ) and obj .keys ():
350349 return set ("{0!r}]" .format (k ) for k in obj .keys ()
351- if repr (k ).startswith (orig ))
350+ if repr (k ).startswith (r . word ))
352351 else :
353352 return set ()
354353
@@ -371,8 +370,7 @@ def matches(self, cursor_offset, line, **kwargs):
371370 return None
372371 if 'class' not in current_block :
373372 return None
374- start , end , word = r
375- return set (name for name in MAGIC_METHODS if name .startswith (word ))
373+ return set (name for name in MAGIC_METHODS if name .startswith (r .word ))
376374
377375 def locate (self , current_offset , line ):
378376 return lineparts .current_method_definition_name (current_offset , line )
@@ -392,20 +390,20 @@ def matches(self, cursor_offset, line, **kwargs):
392390 r = self .locate (cursor_offset , line )
393391 if r is None :
394392 return None
395- start , end , text = r
396393
397394 matches = set ()
398- n = len (text )
395+ n = len (r . word )
399396 for word in KEYWORDS :
400- if self .method_match (word , n , text ):
397+ if self .method_match (word , n , r . word ):
401398 matches .add (word )
402399 for nspace in (builtins .__dict__ , locals_ ):
403400 for word , val in iteritems (nspace ):
404401 word = try_decode (word , 'ascii' )
405402 # if identifier isn't ascii, don't complete (syntax error)
406403 if word is None :
407404 continue
408- if self .method_match (word , n , text ) and word != "__builtins__" :
405+ if (self .method_match (word , n , r .word ) and
406+ word != "__builtins__" ):
409407 matches .add (_callable_postfix (val , word ))
410408 return matches
411409
@@ -425,14 +423,13 @@ def matches(self, cursor_offset, line, **kwargs):
425423 r = self .locate (cursor_offset , line )
426424 if r is None :
427425 return None
428- start , end , word = r
429426 if argspec :
430427 matches = set (name + '=' for name in argspec [1 ][0 ]
431428 if isinstance (name , string_types ) and
432- name .startswith (word ))
429+ name .startswith (r . word ))
433430 if py3 :
434431 matches .update (name + '=' for name in argspec [1 ][4 ]
435- if name .startswith (word ))
432+ if name .startswith (r . word ))
436433 return matches
437434
438435 def locate (self , current_offset , line ):
@@ -446,14 +443,13 @@ def matches(self, cursor_offset, line, **kwargs):
446443 if r is None :
447444 return None
448445
449- start , end , word = r
450446 attrs = dir ('' )
451447 if not py3 :
452448 # decode attributes
453449 attrs = (att .decode ('ascii' ) for att in attrs )
454450
455- matches = set (att for att in attrs if att .startswith (word ))
456- if not word .startswith ('_' ):
451+ matches = set (att for att in attrs if att .startswith (r . word ))
452+ if not r . word .startswith ('_' ):
457453 return set (match for match in matches if not match .startswith ('_' ))
458454 return matches
459455
@@ -513,7 +509,7 @@ def matches(self, cursor_offset, line, **kwargs):
513509 def locate (self , cursor_offset , line ):
514510 start = self ._orig_start
515511 end = cursor_offset
516- return start , end , line [start :end ]
512+ return LinePart ( start , end , line [start :end ])
517513
518514 class MultilineJediCompletion (JediCompletion ):
519515 def matches (self , cursor_offset , line , ** kwargs ):
0 commit comments