22
33# The MIT License
44#
5- # Copyright (c) 2009-2015 the bpython authors.
65#
76# Permission is hereby granted, free of charge, to any person obtaining a copy
87# of this software and associated documentation files (the "Software"), to deal
3938from bpython import inspection
4039from bpython import importcompletion
4140from bpython import line as lineparts
41+ from bpython .line import LinePart
4242from bpython ._py3compat import py3 , try_decode
4343from bpython .lazyre import LazyReCompile
4444
@@ -126,8 +126,8 @@ def format(self, word):
126126
127127 def substitute (self , cursor_offset , line , match ):
128128 """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 :]
129+ lpart = self .locate (cursor_offset , line )
130+ result = lpart . start + len (match ), line [:lpart . start ] + match + line [lpart . end :]
131131 return result
132132
133133 @property
@@ -200,14 +200,13 @@ def matches(self, cursor_offset, line, **kwargs):
200200 cs = lineparts .current_string (cursor_offset , line )
201201 if cs is None :
202202 return None
203- start , end , text = cs
204203 matches = set ()
205- username = text .split (os .path .sep , 1 )[0 ]
204+ username = cs . word .split (os .path .sep , 1 )[0 ]
206205 user_dir = os .path .expanduser (username )
207- for filename in self .safe_glob (os .path .expanduser (text )):
206+ for filename in self .safe_glob (os .path .expanduser (cs . word )):
208207 if os .path .isdir (filename ):
209208 filename += os .path .sep
210- if text .startswith ('~' ):
209+ if cs . word .startswith ('~' ):
211210 filename = username + filename [len (user_dir ):]
212211 matches .add (filename )
213212 return matches
@@ -235,25 +234,24 @@ def matches(self, cursor_offset, line, **kwargs):
235234 r = self .locate (cursor_offset , line )
236235 if r is None :
237236 return None
238- text = r [2 ]
239237
240238 if locals_ is None :
241239 locals_ = __main__ .__dict__
242240
243- assert '.' in text
241+ assert '.' in r . word
244242
245- for i in range (1 , len (text ) + 1 ):
246- if text [- i ] == '[' :
243+ for i in range (1 , len (r . word ) + 1 ):
244+ if r . word [- i ] == '[' :
247245 i -= 1
248246 break
249- methodtext = text [- i :]
250- matches = set ('' .join ([text [:- i ], m ])
247+ methodtext = r . word [- i :]
248+ matches = set ('' .join ([r . word [:- i ], m ])
251249 for m in self .attr_matches (methodtext , locals_ ))
252250
253251 # TODO add open paren for methods via _callable_prefix (or decide not
254252 # to) unless the first character is a _ filter out all attributes
255253 # starting with a _
256- if not text .split ('.' )[- 1 ].startswith ('_' ):
254+ if not r . word .split ('.' )[- 1 ].startswith ('_' ):
257255 matches = set (match for match in matches
258256 if not match .split ('.' )[- 1 ].startswith ('_' ))
259257 return matches
@@ -340,15 +338,14 @@ def matches(self, cursor_offset, line, **kwargs):
340338 r = self .locate (cursor_offset , line )
341339 if r is None :
342340 return None
343- start , end , orig = r
344341 _ , _ , dexpr = lineparts .current_dict (cursor_offset , line )
345342 try :
346343 obj = safe_eval (dexpr , locals_ )
347344 except EvaluationError :
348345 return set ()
349346 if isinstance (obj , dict ) and obj .keys ():
350347 return set ("{0!r}]" .format (k ) for k in obj .keys ()
351- if repr (k ).startswith (orig ))
348+ if repr (k ).startswith (r . word ))
352349 else :
353350 return set ()
354351
@@ -371,8 +368,7 @@ def matches(self, cursor_offset, line, **kwargs):
371368 return None
372369 if 'class' not in current_block :
373370 return None
374- start , end , word = r
375- return set (name for name in MAGIC_METHODS if name .startswith (word ))
371+ return set (name for name in MAGIC_METHODS if name .startswith (r .word ))
376372
377373 def locate (self , current_offset , line ):
378374 return lineparts .current_method_definition_name (current_offset , line )
@@ -392,20 +388,19 @@ def matches(self, cursor_offset, line, **kwargs):
392388 r = self .locate (cursor_offset , line )
393389 if r is None :
394390 return None
395- start , end , text = r
396391
397392 matches = set ()
398- n = len (text )
393+ n = len (r . word )
399394 for word in KEYWORDS :
400- if self .method_match (word , n , text ):
395+ if self .method_match (word , n , r . word ):
401396 matches .add (word )
402397 for nspace in (builtins .__dict__ , locals_ ):
403398 for word , val in iteritems (nspace ):
404399 word = try_decode (word , 'ascii' )
405400 # if identifier isn't ascii, don't complete (syntax error)
406401 if word is None :
407402 continue
408- if self .method_match (word , n , text ) and word != "__builtins__" :
403+ if self .method_match (word , n , r . word ) and word != "__builtins__" :
409404 matches .add (_callable_postfix (val , word ))
410405 return matches
411406
@@ -425,14 +420,13 @@ def matches(self, cursor_offset, line, **kwargs):
425420 r = self .locate (cursor_offset , line )
426421 if r is None :
427422 return None
428- start , end , word = r
429423 if argspec :
430424 matches = set (name + '=' for name in argspec [1 ][0 ]
431425 if isinstance (name , string_types ) and
432- name .startswith (word ))
426+ name .startswith (r . word ))
433427 if py3 :
434428 matches .update (name + '=' for name in argspec [1 ][4 ]
435- if name .startswith (word ))
429+ if name .startswith (r . word ))
436430 return matches
437431
438432 def locate (self , current_offset , line ):
@@ -446,14 +440,13 @@ def matches(self, cursor_offset, line, **kwargs):
446440 if r is None :
447441 return None
448442
449- start , end , word = r
450443 attrs = dir ('' )
451444 if not py3 :
452445 # decode attributes
453446 attrs = (att .decode ('ascii' ) for att in attrs )
454447
455- matches = set (att for att in attrs if att .startswith (word ))
456- if not word .startswith ('_' ):
448+ matches = set (att for att in attrs if att .startswith (r . word ))
449+ if not r . word .startswith ('_' ):
457450 return set (match for match in matches if not match .startswith ('_' ))
458451 return matches
459452
@@ -513,7 +506,7 @@ def matches(self, cursor_offset, line, **kwargs):
513506 def locate (self , cursor_offset , line ):
514507 start = self ._orig_start
515508 end = cursor_offset
516- return start , end , line [start :end ]
509+ return LinePart ( start , end , line [start :end ])
517510
518511 class MultilineJediCompletion (JediCompletion ):
519512 def matches (self , cursor_offset , line , ** kwargs ):
0 commit comments