Skip to content

Commit 7dfebca

Browse files
authored
fix multiline strings when breaking a string after opening parenthesis (#58)
1 parent 2a7f8fd commit 7dfebca

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

indent/python.vim

+37-10
Original file line numberDiff line numberDiff line change
@@ -349,34 +349,61 @@ function! GetPythonPEPIndent(lnum)
349349
return 0
350350
endif
351351

352+
let line = getline(a:lnum)
353+
let prevline = getline(a:lnum-1)
354+
352355
" Multilinestrings: continous, docstring or starting.
353-
if s:is_python_string(a:lnum, 1)
354-
\ && s:is_python_string(a:lnum-1, len(getline(a:lnum-1)))
355-
" Keep existing indent.
356-
if match(getline(a:lnum), '\v^\s*\S') != -1
357-
return -1
356+
if s:is_python_string(a:lnum-1, len(prevline))
357+
\ && (s:is_python_string(a:lnum, 1)
358+
\ || match(line, '^\%("""\|''''''\)') != -1)
359+
360+
" Indent closing quotes as the line with the opening ones.
361+
let match_quotes = match(line, '^\s*\zs\%("""\|''''''\)')
362+
if match_quotes != -1
363+
" closing multiline string
364+
let quotes = line[match_quotes:match_quotes+2]
365+
let pairpos = searchpairpos(quotes, '', quotes, 'b')
366+
if pairpos[0] != 0
367+
return indent(pairpos[0])
368+
else
369+
" TODO: test to cover this!
370+
endif
358371
endif
359372

360373
if s:is_python_string(a:lnum-1)
361374
" Previous line is (completely) a string.
362375
return indent(a:lnum-1)
363376
endif
364377

365-
if match(getline(a:lnum-1), '^\s*\%("""\|''''''\)') != -1
378+
if match(prevline, '^\s*\%("""\|''''''\)') != -1
366379
" docstring.
367380
return indent(a:lnum-1)
368381
endif
369382

370383
let indent_multi = get(b:, 'python_pep8_indent_multiline_string',
371384
\ get(g:, 'python_pep8_indent_multiline_string', 0))
385+
if match(prevline, '\v%("""|'''''')$') != -1
386+
" Opening multiline string, started in previous line.
387+
if (&autoindent && indent(a:lnum) == indent(a:lnum-1))
388+
\ || match(line, '\v^\s+$') != -1
389+
" <CR> with empty line or to split up 'foo("""bar' into
390+
" 'foo("""' and 'bar'.
391+
if indent_multi == -2
392+
return indent(a:lnum-1) + s:sw()
393+
endif
394+
return indent_multi
395+
endif
396+
endif
397+
398+
" Keep existing indent.
399+
if match(line, '\v^\s*\S') != -1
400+
return -1
401+
endif
402+
372403
if indent_multi != -2
373404
return indent_multi
374405
endif
375406

376-
if match(getline(a:lnum-1), '\v%("""|'''''')$') != -1
377-
" Opening multiline string, started in previous line.
378-
return indent(a:lnum-1) + s:sw()
379-
endif
380407
return s:indent_like_opening_paren(a:lnum)
381408
endif
382409

spec/indent/indent_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,16 @@
456456
proposed_indent.should == 4
457457
end
458458
end
459+
460+
describe "when breaking a string after opening parenthesis" do
461+
before { vim.feedkeys 'i foo("""bar<Left><Left><Left>' }
462+
it "it does indent the next line as after an opening multistring" do
463+
vim.feedkeys '\<CR>'
464+
expected_proposed, expected_indent = multiline_indent(4, 4 + shiftwidth)
465+
indent.should == expected_indent
466+
proposed_indent.should == expected_proposed
467+
end
468+
end
459469
end
460470

461471
describe "vim when using width of 4" do

0 commit comments

Comments
 (0)