Skip to content

Commit 3228b6f

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 62189f5 commit 3228b6f

File tree

2 files changed

+62
-23
lines changed

2 files changed

+62
-23
lines changed

indent/python.vim

+50-21
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ endif
6767
let s:stop_statement = '^\s*\(break\|continue\|raise\|return\|pass\)\>'
6868

6969
let s:skip_after_opening_paren = 'synIDattr(synID(line("."), col("."), 0), "name") ' .
70-
\ '=~? "\\vcomment|jedi\\S"'
70+
\ '=~? "\\vcomment"'
7171

7272
if !get(g:, 'python_pep8_indent_skip_concealed', 0) || !has('conceal')
7373
" Skip strings and comments. Return 1 for chars to skip.
7474
" jedi* refers to syntax definitions from jedi-vim for call signatures, which
7575
" are inserted temporarily into the buffer.
7676
function! s:_skip_special_chars(line, col)
7777
return synIDattr(synID(a:line, a:col, 0), 'name')
78-
\ =~? "\\vstring|comment|^pythonbytes%(contents)=$|jedi\\S"
78+
\ =~? "\\vstring|comment|^pythonbytes%(contents)=$"
7979
endfunction
8080
else
8181
" Also ignore anything concealed.
@@ -134,8 +134,8 @@ endfunction
134134
function! s:find_start_of_multiline_statement(lnum)
135135
let lnum = a:lnum
136136
while lnum > 0
137-
if getline(lnum - 1) =~# '\\$'
138-
let lnum = prevnonblank(lnum - 1)
137+
if s:getline(lnum - 1) =~# '\\$'
138+
let lnum = s:prevnonblank(lnum - 1)
139139
else
140140
let [paren_lnum, _] = s:find_opening_paren(lnum, 1)
141141
if paren_lnum < 1
@@ -156,7 +156,7 @@ function! s:find_start_of_block(lnum, types, multiple)
156156
while lnum > 0 && last_indent > 0
157157
let indent = indent(lnum)
158158
if indent < last_indent
159-
if getline(lnum) =~# re
159+
if s:getline(lnum) =~# re
160160
if !a:multiple
161161
return [indent]
162162
endif
@@ -166,15 +166,15 @@ function! s:find_start_of_block(lnum, types, multiple)
166166
let last_indent = indent
167167
endif
168168
endif
169-
let lnum = prevnonblank(lnum - 1)
169+
let lnum = s:prevnonblank(lnum - 1)
170170
endwhile
171171
return r
172172
endfunction
173173

174174
" Is "expr" true for every position in "lnum", beginning at "start"?
175175
" (optionally up to a:1 / 4th argument)
176176
function! s:match_expr_on_line(expr, lnum, start, ...)
177-
let text = getline(a:lnum)
177+
let text = s:getline(a:lnum)
178178
let end = a:0 ? a:1 : len(text)
179179
if a:start > end
180180
return 1
@@ -198,12 +198,12 @@ function! s:indent_like_opening_paren(lnum)
198198
if paren_lnum <= 0
199199
return -2
200200
endif
201-
let text = getline(paren_lnum)
201+
let text = s:getline(paren_lnum)
202202
let base = indent(paren_lnum)
203203

204204
let nothing_after_opening_paren = s:match_expr_on_line(
205205
\ s:skip_after_opening_paren, paren_lnum, paren_col+1)
206-
let starts_with_closing_paren = getline(a:lnum) =~# '^\s*[])}]'
206+
let starts_with_closing_paren = s:getline(a:lnum) =~# '^\s*[])}]'
207207

208208
let hang_closing = get(b:, 'python_pep8_indent_hang_closing',
209209
\ get(g:, 'python_pep8_indent_hang_closing', 0))
@@ -234,7 +234,7 @@ endfunction
234234

235235
" Match indent of first block of this type.
236236
function! s:indent_like_block(lnum)
237-
let text = getline(a:lnum)
237+
let text = s:getline(a:lnum)
238238
for [multiple, block_rules] in [
239239
\ [0, s:block_rules],
240240
\ [1, s:block_rules_multiple]]
@@ -264,15 +264,45 @@ function! s:indent_like_block(lnum)
264264
return -2
265265
endfunction
266266

267+
" Wrapper around getline that looks up jedi-vim's b:_jedi_callsig_orig to get
268+
" the original line.
269+
function! s:getline(lnum) abort
270+
let line = get(get(b:, '_jedi_callsig_orig', {}), a:lnum, 0)
271+
if line is 0
272+
return getline(a:lnum)
273+
endif
274+
return line
275+
endfunction
276+
277+
" Wrapper around prevnonblank that looks up jedi-vim's b:_jedi_callsig_orig to
278+
" check the original line's contents additionally.
279+
function! s:prevnonblank(lnum) abort
280+
let lnum = a:lnum
281+
while 1
282+
let lnum = prevnonblank(lnum)
283+
if lnum < 1
284+
return lnum
285+
endif
286+
let orig_line = get(get(b:, '_jedi_callsig_orig', {}), lnum, 0)
287+
if orig_line is 0
288+
return lnum
289+
endif
290+
if !empty(orig_line)
291+
return lnum
292+
endif
293+
let lnum -= 1
294+
endwhile
295+
endfunction
296+
267297
function! s:indent_like_previous_line(lnum)
268-
let lnum = prevnonblank(a:lnum - 1)
298+
let lnum = s:prevnonblank(a:lnum - 1)
269299

270300
" No previous line, keep current indent.
271301
if lnum < 1
272302
return -1
273303
endif
274304

275-
let text = getline(lnum)
305+
let text = s:getline(lnum)
276306
let start = s:find_start_of_multiline_statement(lnum)
277307
let base = indent(start)
278308
let current = indent(a:lnum)
@@ -297,25 +327,25 @@ function! s:indent_like_previous_line(lnum)
297327
" If this line is the continuation of a control statement
298328
" indent further to distinguish the continuation line
299329
" from the next logical line.
300-
if getline(start) =~# b:control_statement
330+
if s:getline(start) =~# b:control_statement
301331
return base + s:sw() * 2
302332
endif
303333

304334
" Nest (other) explicit continuations only one level deeper.
305335
return base + s:sw()
306336
endif
307337

308-
let empty = getline(a:lnum) =~# '^\s*$'
338+
let empty = s:getline(a:lnum) =~# '^\s*$'
309339

310340
" Current and prev line are empty, next is not -> indent like next.
311341
if empty && a:lnum > 1 &&
312-
\ (getline(a:lnum - 1) =~# '^\s*$') &&
313-
\ !(getline(a:lnum + 1) =~# '^\s*$')
342+
\ (s:getline(a:lnum - 1) =~# '^\s*$') &&
343+
\ !(s:getline(a:lnum + 1) =~# '^\s*$')
314344
return indent(a:lnum + 1)
315345
endif
316346

317347
" If the previous statement was a stop-execution statement or a pass
318-
if getline(start) =~# s:stop_statement
348+
if s:getline(start) =~# s:stop_statement
319349
" Remove one level of indentation if the user hasn't already dedented
320350
if empty || current > base - s:sw()
321351
return base - s:sw()
@@ -341,11 +371,10 @@ endfunction
341371

342372
" Is the syntax at lnum (and optionally cnum) a python string?
343373
function! s:is_python_string(lnum, ...)
344-
let line = getline(a:lnum)
345374
if a:0
346375
let cols = type(a:1) != type([]) ? [a:1] : a:1
347376
else
348-
let cols = range(1, max([1, len(line)]))
377+
let cols = range(1, max([1, len(s:getline(a:lnum))]))
349378
endif
350379
for cnum in cols
351380
if match(map(synstack(a:lnum, cnum),
@@ -362,8 +391,8 @@ function! GetPythonPEPIndent(lnum)
362391
return 0
363392
endif
364393

365-
let line = getline(a:lnum)
366-
let prevline = getline(a:lnum-1)
394+
let line = s:getline(a:lnum)
395+
let prevline = s:getline(a:lnum-1)
367396

368397
" Multilinestrings: continous, docstring or starting.
369398
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)