4141
4242"""
4343
44- breakpoint_regex = r"^[\t ]*_breakpoint\(\) # ([a-f0-9]{8})"
45- breakpoint_re = re .compile (breakpoint_regex , re .DOTALL )
44+ bp_regex = r"^[\t ]*_breakpoint\(\) # ([a-f0-9]{8})"
45+ bp_re = re .compile (bp_regex , re .DOTALL )
4646
4747EXPR_PRE = ['class' , 'def' , 'if' , 'for' , 'try' , 'while' , 'with' ]
4848EXPR_PST = ['elif' , 'else' , 'except' , 'finally' ]
@@ -59,7 +59,7 @@ class Breakpoint(object):
5959 def __init__ (self , from_text = None ):
6060 self .uid = None
6161 if from_text is not None :
62- m = breakpoint_re .match (from_text )
62+ m = bp_re .match (from_text )
6363 if m :
6464 self .uid = m .groups ()[0 ]
6565 else :
@@ -84,7 +84,7 @@ def highlight(self, view, rg):
8484# Helper routines #
8585###################
8686
87- def is_python (view ): # only run if syntax is set to Python
87+ def is_python (view ):
8888 return view .match_selector (0 , 'source.python' )
8989
9090
@@ -153,6 +153,8 @@ def calc_indent(view, rg):
153153 line = view .substr (l ).strip ()
154154 if not line or line .startswith ('#' ):
155155 lines .remove (l )
156+ elif ln is not None :
157+ break # reached current and next line
156158
157159 # a couple of hacks to handle corner cases
158160 if not ln :
@@ -180,15 +182,14 @@ def calc_indent(view, rg):
180182 next_indent = _indent (next_line )
181183 debug ('indent p' , prev_indent , 'c' , curr_indent , 'n' , next_indent )
182184
183- # abort if previous or current line already contains a breakpoint
184- # at the same level
185- if ((breakpoint_re .match (prev_line ) and prev_indent == curr_indent )
186- or (breakpoint_re .match (curr_line ) and curr_indent == next_indent )):
187- return
188-
189185 def _result (msg , indent ):
190186 debug (msg )
191- return indent
187+ # check if previous or next line already contains a breakpoint
188+ # at the same indent level
189+ c1 = indent == prev_indent and bp_re .match (prev_line )
190+ c2 = indent == next_indent and bp_re .match (next_line ) and not curr_line
191+ if not (c1 or c2 ):
192+ return indent
192193
193194 # order of checks is critical!
194195 if expr_re1 .match (prev_line ):
@@ -218,7 +219,7 @@ def _result(msg, indent):
218219 elif prev_dist <= next_dist :
219220 return _result ('he1-1' , prev_indent )
220221 else :
221- return _result ('he1-2' , curr_indent )
222+ return _result ('he1-2' , next_indent )
222223
223224
224225def find_pdb_block (view ):
@@ -230,7 +231,7 @@ def find_pdb_block(view):
230231
231232def find_breakpoint (view ):
232233 """return position of the 1st breakpoint, or None"""
233- rg = view .find (breakpoint_regex , 0 )
234+ rg = view .find (bp_regex , 0 )
234235 if rg :
235236 return rg .end ()
236237
@@ -250,11 +251,10 @@ def remove_breakpoint(edit, view, rg):
250251 lines = view .lines (sublime .Region (0 , rg .end ()))
251252 ln = min (ln_from_region (view , rg ), len (lines ) - 1 )
252253
253- for i in (0 , 1 ): # search current and previous lines
254- bp = Breakpoint (view .substr (lines [ln - i ]))
255- debug ('lines[ln-%d]' % i , view .substr (lines [ln - i ]))
254+ for line in (lines [ln ], lines [ln - 1 ]): # search current and prev lines
255+ bp = Breakpoint (view .substr (line ))
256256 if bp .uid :
257- view .erase (edit , view .full_line (lines [ ln - i ] ))
257+ view .erase (edit , view .full_line (line ))
258258 view .erase_regions (bp .rg_key )
259259 return True
260260 return False
@@ -322,7 +322,7 @@ def run(self, edit):
322322 if not is_python (view ):
323323 return
324324
325- bp_regions = view .find_all (breakpoint_regex , 0 )
325+ bp_regions = view .find_all (bp_regex , 0 )
326326 items = [[] for __ in bp_regions ]
327327 lines = view .lines (sublime .Region (0 , view .size ()))
328328
@@ -379,6 +379,6 @@ def on_load(self, view):
379379 on file load, scan it for breakpoints and highlight them
380380 """
381381 if is_python (view ) and find_pdb_block (view ):
382- for rg in view .find_all (breakpoint_regex , 0 ):
382+ for rg in view .find_all (bp_regex , 0 ):
383383 bp = Breakpoint (view .substr (rg ))
384384 bp .highlight (view , rg )
0 commit comments