@@ -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
0 commit comments