Skip to content

Commit ff4d7c1

Browse files
committed
Improve indent for line continuation
1 parent 053ab31 commit ff4d7c1

2 files changed

Lines changed: 103 additions & 64 deletions

File tree

indent/python.vim

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" Vim indent file
22
" Language: Python
33
" Author: Akinori Hattori <hattya@gmail.com>
4-
" Last Change: 2023-06-24
4+
" Last Change: 2023-07-09
55
" License: MIT License
66

77
if exists('b:did_indent')
@@ -129,7 +129,15 @@ function! GetPEP8PythonIndent() abort
129129

130130
let ind = indent(lnum)
131131
let ll = join(buf, ' ')
132-
if ll =~# '\v:\s*%(#.*)=$'
132+
if getline(v:lnum - 1) =~# s:lcont
133+
" line continuation
134+
let kw = s:matchkw(ll)
135+
if kw ==# ''
136+
let ind += s:cont()
137+
else
138+
let ind += len(substitute(kw, '\s\+', ' ', '')) + 1
139+
endif
140+
elseif ll =~# '\v:\s*%(#.*)=$'
133141
" compound statement
134142
let ind += shiftwidth()
135143
elseif ll =~# s:dedent
@@ -138,17 +146,6 @@ function! GetPEP8PythonIndent() abort
138146
elseif ll =~# s:ellipsis
139147
" ellipsis
140148
let ind -= shiftwidth()
141-
elseif getline(v:lnum - 1) =~# s:lcont
142-
" line continuation
143-
if s:is_compound_stmt(ll)
144-
if ll =~# '\v^\s*<with>'
145-
let ind += len('with') + 1
146-
else
147-
let ind += s:ml_stmt() ? shiftwidth() * 2 : shiftwidth()
148-
endif
149-
else
150-
let ind += s:cont()
151-
endif
152149
elseif colon
153150
" : is at EOL
154151
return -1
@@ -182,6 +179,10 @@ function! s:is_compound_stmt(str, ...) abort
182179
return (a:0 && a:1 && a:str =~# '\v^\s*<%(def|class)>') || a:str =~# '\v^\s*<%(if|elif|while|for|except|with)>'
183180
endfunction
184181

182+
function! s:matchkw(str) abort
183+
return matchstr(a:str, '\v^\s*\zs<%(assert|del|return|yield%(\s+from)=|raise|import|from|global|nonlocal|if|elif|while|for|except|with|def|class)>')
184+
endfunction
185+
185186
function! s:cont() abort
186187
return eval(s:getvar('python_indent_continue', shiftwidth()))
187188
endfunction

test/indent.vimspec

Lines changed: 89 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,91 @@ Describe filetype indent
352352
End
353353
End
354354

355+
Describe simple statement
356+
It indents the assert statement
357+
let in = "assert True \\\<CR>or False"
358+
let out = [
359+
\ 'assert True \',
360+
\ ' or False',
361+
\]
362+
Assert Equals(Insert(in), Buffer(out))
363+
End
364+
365+
It indents the del statement
366+
let in = "del spam, \\\<CR>eggs"
367+
let out = [
368+
\ 'del spam, \',
369+
\ ' eggs',
370+
\]
371+
Assert Equals(Insert(in), Buffer(out))
372+
End
373+
374+
It indents the return statement
375+
let in = "return True \\\<CR>or False"
376+
let out = [
377+
\ 'return True \',
378+
\ ' or False',
379+
\]
380+
Assert Equals(Insert(in), Buffer(out))
381+
End
382+
383+
It indents the yield statement
384+
let in = "yield spam \\\<CR>.eggs()\<CR>"
385+
let in .= "yield from ham \\\<CR>.toast()\<CR>"
386+
let in .= "yield \\\<CR>from \\\<CR>beans \\\<CR>.bacon()"
387+
let out = [
388+
\ 'yield spam \',
389+
\ ' .eggs()',
390+
\ 'yield from ham \',
391+
\ ' .toast()',
392+
\ 'yield \',
393+
\ ' from \',
394+
\ ' beans \',
395+
\ ' .bacon()',
396+
\]
397+
Assert Equals(Insert(in), Buffer(out))
398+
End
399+
400+
It indents the raise statement
401+
let in = "raise Exception() \\\<CR>.with_traceback(tb)"
402+
let out = [
403+
\ 'raise Exception() \',
404+
\ ' .with_traceback(tb)',
405+
\]
406+
Assert Equals(Insert(in), Buffer(out))
407+
End
408+
409+
It indents the import statement
410+
let in = "import spam \\\<CR>.eggs\<CR>"
411+
let in .= "from ham \\\<CR>.toast"
412+
let out = [
413+
\ 'import spam \',
414+
\ ' .eggs',
415+
\ 'from ham \',
416+
\ ' .toast',
417+
\]
418+
Assert Equals(Insert(in), Buffer(out))
419+
End
420+
421+
It indents the global statement
422+
let in = "global spam, \\\<CR>eggs"
423+
let out = [
424+
\ 'global spam, \',
425+
\ ' eggs',
426+
\]
427+
Assert Equals(Insert(in), Buffer(out))
428+
End
429+
430+
It indents the nonlocal statement
431+
let in = "nonlocal spam, \\\<CR>eggs"
432+
let out = [
433+
\ 'nonlocal spam, \',
434+
\ ' eggs',
435+
\]
436+
Assert Equals(Insert(in), Buffer(out))
437+
End
438+
End
439+
355440
Describe compound statement
356441
Describe if statement
357442
It aligns with left brackets
@@ -421,24 +506,10 @@ Describe filetype indent
421506
let in .= "elif False and \\\<CR>True:"
422507
let out = [
423508
\ 'if True and \',
424-
\ ' False:',
425-
\ ' pass',
426-
\ 'elif False and \',
427-
\ ' True:',
428-
\]
429-
Assert Equals(Insert(in), Buffer(out))
430-
End
431-
432-
It increases the indent level by 2 (line continuation)
433-
let b:python_indent_multiline_statement = 1
434-
let in = "if True and \\\<CR>False:\<CR>pass\<CR>"
435-
let in .= "elif False and \\\<CR>True:"
436-
let out = [
437-
\ 'if True and \',
438-
\ ' False:',
509+
\ ' False:',
439510
\ ' pass',
440511
\ 'elif False and \',
441-
\ ' True:',
512+
\ ' True:',
442513
\]
443514
Assert Equals(Insert(in), Buffer(out))
444515
End
@@ -526,17 +597,7 @@ Describe filetype indent
526597
let in = "while True and \\\<CR>False:"
527598
let out = [
528599
\ 'while True and \',
529-
\ ' False:',
530-
\]
531-
Assert Equals(Insert(in), Buffer(out))
532-
End
533-
534-
It increases the indent level by 2 (line continuation)
535-
let b:python_indent_multiline_statement = 1
536-
let in = "while True and \\\<CR>False:"
537-
let out = [
538-
\ 'while True and \',
539-
\ ' False:',
600+
\ ' False:',
540601
\]
541602
Assert Equals(Insert(in), Buffer(out))
542603
End
@@ -610,16 +671,6 @@ Describe filetype indent
610671
Assert Equals(Insert(in), Buffer(out))
611672
End
612673

613-
It increases the indent level by 2 (line continuation)
614-
let b:python_indent_multiline_statement = 1
615-
let in = "for _ in True, \\\<CR>False:"
616-
let out = [
617-
\ 'for _ in True, \',
618-
\ ' False:',
619-
\]
620-
Assert Equals(Insert(in), Buffer(out))
621-
End
622-
623674
It indents the else clause
624675
let in = "for _ in range(1):\<CR>"
625676
let in .= "for _ in range(2):\<CR>continue\<CR>"
@@ -699,20 +750,7 @@ Describe filetype indent
699750
\ 'try:',
700751
\ ' ...',
701752
\ 'except TypeError, \',
702-
\ ' ValueError:',
703-
\]
704-
Assert Equals(Insert(in), Buffer(out))
705-
End
706-
707-
It increases the indent level by 2 (line continuation)
708-
let b:python_indent_multiline_statement = 1
709-
let in = "try:\<CR>...\<CR>"
710-
let in .= "except TypeError, \\\<CR>ValueError:"
711-
let out = [
712-
\ 'try:',
713-
\ ' ...',
714-
\ 'except TypeError, \',
715-
\ ' ValueError:',
753+
\ ' ValueError:',
716754
\]
717755
Assert Equals(Insert(in), Buffer(out))
718756
End

0 commit comments

Comments
 (0)