Skip to content

Commit 3e61be5

Browse files
committed
Support for jedi-vim's call signatures
Adds s:getline() and s:prevnonblank() wrappers to use b:_jedi_callsig_orig for the original line contents. Requires davidhalter/jedi-vim#652.
1 parent f196c5b commit 3e61be5

File tree

2 files changed

+62
-35
lines changed

2 files changed

+62
-35
lines changed

indent/python.vim

+50-33
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,11 @@ endif
5858
let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>'
5959

6060
" Skip strings and comments. Return 1 for chars to skip.
61-
" jedi* refers to syntax definitions from jedi-vim for call signatures, which
62-
" are inserted temporarily into the buffer.
6361
let s:skip_special_chars = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
64-
\ '=~? "\\vstring|comment|^pythonbytes%(contents)=$|jedi\\S"'
62+
\ '=~? "\\vstring|comment|^pythonbytes%(contents)=$"'
6563

6664
let s:skip_after_opening_paren = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
67-
\ '=~? "\\vcomment|jedi\\S"'
68-
69-
" Also ignore anything concealed.
70-
" Wrapper around synconcealed for older Vim (7.3.429, used on Travis CI).
71-
function! s:is_concealed(line, col)
72-
let concealed = synconcealed(a:line, a:col)
73-
return len(concealed) && concealed[0]
74-
endfunction
75-
if has('conceal')
76-
let s:skip_special_chars .= '|| s:is_concealed(line("."), col("."))'
77-
endif
65+
\ '=~? "\\vcomment"'
7866

7967

8068
let s:skip_search = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
@@ -122,8 +110,8 @@ endfunction
122110
function! s:find_start_of_multiline_statement(lnum)
123111
let lnum = a:lnum
124112
while lnum > 0
125-
if getline(lnum - 1) =~# '\\$'
126-
let lnum = prevnonblank(lnum - 1)
113+
if s:getline(lnum - 1) =~# '\\$'
114+
let lnum = s:prevnonblank(lnum - 1)
127115
else
128116
let [paren_lnum, _] = s:find_opening_paren(lnum)
129117
if paren_lnum < 1
@@ -144,7 +132,7 @@ function! s:find_start_of_block(lnum, types, multiple)
144132
while lnum > 0 && last_indent > 0
145133
let indent = indent(lnum)
146134
if indent < last_indent
147-
if getline(lnum) =~# re
135+
if s:getline(lnum) =~# re
148136
if !a:multiple
149137
return [indent]
150138
endif
@@ -154,15 +142,15 @@ function! s:find_start_of_block(lnum, types, multiple)
154142
let last_indent = indent
155143
endif
156144
endif
157-
let lnum = prevnonblank(lnum - 1)
145+
let lnum = s:prevnonblank(lnum - 1)
158146
endwhile
159147
return r
160148
endfunction
161149

162150
" Is "expr" true for every position in "lnum", beginning at "start"?
163151
" (optionally up to a:1 / 4th argument)
164152
function! s:match_expr_on_line(expr, lnum, start, ...)
165-
let text = getline(a:lnum)
153+
let text = s:getline(a:lnum)
166154
let end = a:0 ? a:1 : len(text)
167155
if a:start > end
168156
return 1
@@ -186,12 +174,12 @@ function! s:indent_like_opening_paren(lnum)
186174
if paren_lnum <= 0
187175
return -2
188176
endif
189-
let text = getline(paren_lnum)
177+
let text = s:getline(paren_lnum)
190178
let base = indent(paren_lnum)
191179

192180
let nothing_after_opening_paren = s:match_expr_on_line(
193181
\ s:skip_after_opening_paren, paren_lnum, paren_col+1)
194-
let starts_with_closing_paren = getline(a:lnum) =~# '^\s*[])}]'
182+
let starts_with_closing_paren = s:getline(a:lnum) =~# '^\s*[])}]'
195183

196184
let hang_closing = get(b:, 'python_pep8_indent_hang_closing',
197185
\ get(g:, 'python_pep8_indent_hang_closing', 0))
@@ -222,7 +210,7 @@ endfunction
222210

223211
" Match indent of first block of this type.
224212
function! s:indent_like_block(lnum)
225-
let text = getline(a:lnum)
213+
let text = s:getline(a:lnum)
226214
for [multiple, block_rules] in [
227215
\ [0, s:block_rules],
228216
\ [1, s:block_rules_multiple]]
@@ -252,15 +240,45 @@ function! s:indent_like_block(lnum)
252240
return -2
253241
endfunction
254242

243+
" Wrapper around getline that looks up jedi-vim's b:_jedi_callsig_orig to get
244+
" the original line.
245+
function! s:getline(lnum) abort
246+
let line = get(get(b:, '_jedi_callsig_orig', {}), a:lnum, 0)
247+
if line is 0
248+
return getline(a:lnum)
249+
endif
250+
return line
251+
endfunction
252+
253+
" Wrapper around prevnonblank that looks up jedi-vim's b:_jedi_callsig_orig to
254+
" check the original line's contents additionally.
255+
function! s:prevnonblank(lnum) abort
256+
let lnum = a:lnum
257+
while 1
258+
let lnum = prevnonblank(lnum)
259+
if lnum < 1
260+
return lnum
261+
endif
262+
let orig_line = get(get(b:, '_jedi_callsig_orig', {}), lnum, 0)
263+
if orig_line is 0
264+
return lnum
265+
endif
266+
if !empty(orig_line)
267+
return lnum
268+
endif
269+
let lnum -= 1
270+
endwhile
271+
endfunction
272+
255273
function! s:indent_like_previous_line(lnum)
256-
let lnum = prevnonblank(a:lnum - 1)
274+
let lnum = s:prevnonblank(a:lnum - 1)
257275

258276
" No previous line, keep current indent.
259277
if lnum < 1
260278
return -1
261279
endif
262280

263-
let text = getline(lnum)
281+
let text = s:getline(lnum)
264282
let start = s:find_start_of_multiline_statement(lnum)
265283
let base = indent(start)
266284
let current = indent(a:lnum)
@@ -286,25 +304,25 @@ function! s:indent_like_previous_line(lnum)
286304
" If this line is the continuation of a control statement
287305
" indent further to distinguish the continuation line
288306
" from the next logical line.
289-
if getline(start) =~# b:control_statement
307+
if s:getline(start) =~# b:control_statement
290308
return base + s:sw() * 2
291309
endif
292310

293311
" Nest (other) explicit continuations only one level deeper.
294312
return base + s:sw()
295313
endif
296314

297-
let empty = getline(a:lnum) =~# '^\s*$'
315+
let empty = s:getline(a:lnum) =~# '^\s*$'
298316

299317
" Current and prev line are empty, next is not -> indent like next.
300318
if empty && a:lnum > 1 &&
301-
\ (getline(a:lnum - 1) =~# '^\s*$') &&
302-
\ !(getline(a:lnum + 1) =~# '^\s*$')
319+
\ (s:getline(a:lnum - 1) =~# '^\s*$') &&
320+
\ !(s:getline(a:lnum + 1) =~# '^\s*$')
303321
return indent(a:lnum + 1)
304322
endif
305323

306324
" If the previous statement was a stop-execution statement or a pass
307-
if getline(start) =~# s:stop_statement
325+
if s:getline(start) =~# s:stop_statement
308326
" Remove one level of indentation if the user hasn't already dedented
309327
if empty || current > base - s:sw()
310328
return base - s:sw()
@@ -330,11 +348,10 @@ endfunction
330348

331349
" Is the syntax at lnum (and optionally cnum) a python string?
332350
function! s:is_python_string(lnum, ...)
333-
let line = getline(a:lnum)
334351
if a:0
335352
let cols = type(a:1) != type([]) ? [a:1] : a:1
336353
else
337-
let cols = range(1, max([1, len(line)]))
354+
let cols = range(1, max([1, len(s:getline(a:lnum))]))
338355
endif
339356
for cnum in cols
340357
if match(map(synstack(a:lnum, cnum),
@@ -351,8 +368,8 @@ function! GetPythonPEPIndent(lnum)
351368
return 0
352369
endif
353370

354-
let line = getline(a:lnum)
355-
let prevline = getline(a:lnum-1)
371+
let line = s:getline(a:lnum)
372+
let prevline = s:getline(a:lnum-1)
356373

357374
" Multilinestrings: continous, docstring or starting.
358375
if s:is_python_string(a:lnum-1, max([1, len(prevline)]))

spec/indent/indent_spec.rb

+12-2
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,25 @@
421421
end
422422

423423
describe "when jedi-vim call signatures are used" do
424-
before { vim.command 'syn match jediFunction "JEDI_CALL_SIGNATURE" keepend extend' }
424+
before {
425+
# jedi-vim uses a buffer variable that holds the original line contents
426+
# for concealed lines.
427+
vim.command 'let b:_jedi_callsig_orig = {3: "if True:", 4: "def f("}'
428+
}
429+
after {
430+
vim.command 'unlet b:_jedi_callsig_orig'
431+
}
425432

426433
it "ignores the call signature after a colon" do
434+
# Assert that we are in line 3.
435+
vim.echo("getcurpos()[1]").to_i.should == 3
427436
vim.feedkeys 'iif True: JEDI_CALL_SIGNATURE\<CR>'
428437
indent.should == shiftwidth
429438
end
430439

431440
it "ignores the call signature after a function" do
432-
vim.feedkeys 'idef f( JEDI_CALL_SIGNATURE\<CR>'
441+
vim.echo("getcurpos()[1]").to_i.should == 3
442+
vim.feedkeys 'odef f( JEDI_CALL_SIGNATURE\<CR>'
433443
indent.should == shiftwidth * 2
434444
end
435445
end

0 commit comments

Comments
 (0)