@@ -34,7 +34,6 @@ if !exists('g:python_pep8_indent_multiline_string')
3434 let g: python_pep8_indent_multiline_string = 0
3535endif
3636
37- let s: maxoff = 50
3837let s: block_rules = {
3938 \ ' ^\s*elif\>' : [' if' , ' elif' ],
4039 \ ' ^\s*except\>' : [' try' , ' except' ],
@@ -43,7 +42,13 @@ let s:block_rules = {
4342let s: block_rules_multiple = {
4443 \ ' ^\s*else\>' : [' if' , ' elif' , ' for' , ' try' , ' except' ],
4544 \ }
46- let s: paren_pairs = [' ()' , ' {}' , ' []' ]
45+ " Pairs to look for when searching for opening parenthesis.
46+ " The value is the maximum offset in lines.
47+ let s: paren_pairs = {' ()' : 50 , ' []' : 100 , ' {}' : 1000 }
48+
49+ " Maximum offset when looking for multiline statements (in round parenthesis).
50+ let s: maxoff_multiline_statement = 50
51+
4752if &filetype == # ' pyrex' || &filetype == # ' cython'
4853 let b: control_statement = ' \v^\s*(class|def|if|while|with|for|except|cdef|cpdef)>'
4954else
@@ -105,37 +110,36 @@ function! s:find_opening_paren(...)
105110 return ret
106111 endif
107112
108- let stopline = max ([0 , line (' .' ) - s: maxoff ])
109-
110113 " Return if cursor is in a comment.
111114 exe ' if' s: skip_search ' | return [0, 0] | endif'
112115
113- let positions = []
114- for p in s: paren_pairs
115- call add (positions, searchpairpos (
116- \ ' \V' .p [0 ], ' ' , ' \V' .p [1 ], ' bnW' , s: skip_special_chars , stopline))
116+ let nearest = [0 , 0 ]
117+ for [p , maxoff] in items (s: paren_pairs )
118+ let stopline = max ([0 , line (' .' ) - maxoff, nearest[0 ]])
119+ let next = searchpairpos (
120+ \ ' \V' .p [0 ], ' ' , ' \V' .p [1 ], ' bnW' , s: skip_special_chars , stopline)
121+ if next [0 ] && (next [0 ] > nearest[0 ] || (next [0 ] == nearest[0 ] && next [1 ] > nearest[1 ]))
122+ let nearest = next
123+ endif
117124 endfor
118-
119- " Remove empty matches and return the type with the closest match
120- call filter (positions, ' v:val[0]' )
121- call sort (positions, ' s:pair_sort' )
122-
123- return get (positions, -1 , [0 , 0 ])
125+ return nearest
124126endfunction
125127
126- " Find the start of a multi-line statement
128+ " Find the start of a multi-line statement (based on surrounding parens).
127129function ! s: find_start_of_multiline_statement (lnum)
128130 let lnum = a: lnum
129131 while lnum > 0
132+ " XXX: not tested?!
130133 if getline (lnum - 1 ) = ~# ' \\$'
131134 let lnum = prevnonblank (lnum - 1 )
132135 else
133- let [paren_lnum, _] = s: find_opening_paren (lnum)
134- if paren_lnum < 1
135- return lnum
136- else
137- let lnum = paren_lnum
136+ call cursor (lnum, 1 )
137+ let stopline = max ([ 1 , lnum - s: maxoff_multiline_statement ])
138+ let pos = searchpairpos ( ' \V( ' , ' ' , ' \V) ' , ' bnW ' , s: skip_special_chars , stopline)
139+ if pos[ 0 ]
140+ return pos[ 0 ]
138141 endif
142+ return lnum
139143 endif
140144 endwhile
141145endfunction
0 commit comments