Skip to content

Commit d55fef9

Browse files
committed
Use s:_skip_special_chars function directly/only, optimize
Does not use `skip` with `searchpairpos` directly, but manually (for performance reasons; vim/vim#3613).
1 parent efa7e6b commit d55fef9

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

indent/python.vim

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,33 @@ else
6666
endif
6767
let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>'
6868

69-
" Skip strings and comments. Return 1 for chars to skip.
70-
" jedi* refers to syntax definitions from jedi-vim for call signatures, which
71-
" are inserted temporarily into the buffer.
72-
" let s:skip_special_chars = '(execute("sleep 100m") && 0) || synIDattr(synID(line("."), col("."), 0), "name") ' .
73-
function! s:_skip_special_chars()
74-
return synIDattr(synID(line('.'), col('.'), 0), 'name')
75-
\ =~? "\\vstring|comment|^pythonbytes%(contents)=$|jedi\\S"
76-
endfunction
77-
let s:skip_special_chars = 's:_skip_special_chars()'
78-
7969
let s:skip_after_opening_paren = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
8070
\ '=~? "\\vcomment|jedi\\S"'
8171

82-
" Also ignore anything concealed.
83-
" TODO: doc; likely only necessary with jedi-vim, where a better version is
84-
" planned (https://github.com/Vimjas/vim-python-pep8-indent/pull/98).
85-
if get(g:, 'python_pep8_indent_skip_concealed', 0)
72+
if !get(g:, 'python_pep8_indent_skip_concealed', 0) || !has('conceal')
73+
" Skip strings and comments. Return 1 for chars to skip.
74+
" jedi* refers to syntax definitions from jedi-vim for call signatures, which
75+
" are inserted temporarily into the buffer.
76+
function! s:_skip_special_chars(line, col)
77+
return synIDattr(synID(a:line, a:col, 0), 'name')
78+
\ =~? "\\vstring|comment|^pythonbytes%(contents)=$|jedi\\S"
79+
endfunction
80+
else
81+
" Also ignore anything concealed.
82+
" TODO: doc; likely only necessary with jedi-vim, where a better version is
83+
" planned (https://github.com/Vimjas/vim-python-pep8-indent/pull/98).
84+
8685
" Wrapper around synconcealed for older Vim (7.3.429, used on Travis CI).
8786
function! s:is_concealed(line, col)
8887
let concealed = synconcealed(a:line, a:col)
8988
return len(concealed) && concealed[0]
9089
endfunction
91-
if has('conceal')
92-
let s:skip_special_chars .= '|| s:is_concealed(line("."), col("."))'
93-
endif
90+
91+
function! s:_skip_special_chars(line, col)
92+
return synIDattr(synID(a:line, a:col, 0), 'name')
93+
\ =~? "\\vstring|comment|^pythonbytes%(contents)=$|jedi\\S"
94+
\ || s:is_concealed(a:line, a:col)
95+
endfunction
9496
endif
9597

9698

@@ -125,8 +127,19 @@ function! s:find_opening_paren(...)
125127
let nearest = [0, 0]
126128
for [p, maxoff] in items(s:paren_pairs)
127129
let stopline = max([0, line('.') - maxoff, nearest[0]])
128-
let next = searchpairpos(
129-
\ '\V'.p[0], '', '\V'.p[1], 'bnW', s:skip_special_chars, stopline, g:python_pep8_indent_searchpair_timeout)
130+
let found = 0
131+
while 1
132+
let next = searchpairpos(
133+
\ '\V'.p[0], '', '\V'.p[1], 'bnW', '', stopline, g:python_pep8_indent_searchpair_timeout)
134+
135+
if !next[0]
136+
break
137+
endif
138+
if !s:_skip_special_chars(next[0], next[1])
139+
break
140+
endif
141+
call cursor(next[0], next[1])
142+
endwhile
130143
if next[0] && (next[0] > nearest[0] || (next[0] == nearest[0] && next[1] > nearest[1]))
131144
let nearest = next
132145
endif
@@ -281,24 +294,23 @@ function! s:indent_like_previous_line(lnum)
281294
let base = indent(start)
282295
let current = indent(a:lnum)
283296

284-
" Jump to last character in previous line.
285-
call cursor(lnum, len(text))
286-
let ignore_last_char = eval(s:skip_special_chars)
297+
" Ignore last character in previous line?
298+
let lastcol = len(text)
299+
let col = lastcol
287300

288301
" Search for final colon that is not inside something to be ignored.
289302
while 1
290-
let curpos = getpos('.')[2]
291-
if curpos == 1 | break | endif
292-
if text[curpos-1] =~# '\s' || eval(s:skip_special_chars)
293-
normal! h
303+
if col == 1 | break | endif
304+
if text[col-1] =~# '\s' || s:_skip_special_chars(lnum, col)
305+
let col = col - 1
294306
continue
295-
elseif text[curpos-1] ==# ':'
307+
elseif text[col-1] ==# ':'
296308
return base + s:sw()
297309
endif
298310
break
299311
endwhile
300312

301-
if text =~# '\\$' && !ignore_last_char
313+
if text =~# '\\$' && !s:_skip_special_chars(lnum, lastcol)
302314
" If this line is the continuation of a control statement
303315
" indent further to distinguish the continuation line
304316
" from the next logical line.

0 commit comments

Comments
 (0)