Skip to content

Commit 479ecb5

Browse files
committed
Improve indent for async for statement
1 parent 44c3435 commit 479ecb5

2 files changed

Lines changed: 73 additions & 4 deletions

File tree

indent/python.vim

Lines changed: 4 additions & 4 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-07-19
4+
" Last Change: 2023-07-20
55
" License: MIT License
66

77
if exists('b:did_indent')
@@ -28,7 +28,7 @@ set cpo&vim
2828
let s:maxoff = 50
2929
let s:compound_stmts = {
3030
\ '\v^\s*<elif>': '\v^\s*<%(if|elif)>',
31-
\ '\v^\s*<else>': '\v^\s*<%(if|elif|while|for|try|except)>',
31+
\ '\v^\s*<else>': '\v^\s*<%(if|elif|while|%(async\s+)=for|try|except)>',
3232
\ '\v^\s*<except>': '\v^\s*<%(try|except)>',
3333
\ '\v^\s*<finally>': '\v^\s*<%(try|except|else)>',
3434
\ '\v^\s*<case>': '\v^\s*<case>',
@@ -179,11 +179,11 @@ function! s:synmatch(lnum, col, pat) abort
179179
endfunction
180180

181181
function! s:is_compound_stmt(str, ...) abort
182-
return (a:0 && a:1 && a:str =~# '\v^\s*<%(%(async\s+)=def|class)>') || a:str =~# '\v^\s*<%(if|elif|while|for|except|with|match|case)>'
182+
return (a:0 && a:1 && a:str =~# '\v^\s*<%(%(async\s+)=def|class)>') || a:str =~# '\v^\s*<%(if|elif|while|%(async\s+)=for|except|with|match|case)>'
183183
endfunction
184184

185185
function! s:matchkw(str) abort
186-
return matchstr(a:str, '\v^\s*\zs<%(%(await|assert|del|return|yield%(\s+from)=|raise|import|from|global|nonlocal|if|elif|while|for|with|match|case|%(async\s+)=def|class|async)>|except>%(\s*\*)=)')
186+
return matchstr(a:str, '\v^\s*\zs<%(%(await|assert|del|return|yield%(\s+from)=|raise|import|from|global|nonlocal|if|elif|while|%(async\s+)=%(for|def)|with|match|case|class|async)>|except>%(\s*\*)=)')
187187
endfunction
188188

189189
function! s:cont() abort

test/indent.vimspec

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,75 @@ Describe filetype indent
11391139
Assert Equals(Insert(in), Buffer(out))
11401140
End
11411141
End
1142+
1143+
Describe async for statement
1144+
It aligns with left brackets
1145+
let in = "async for _ in (True,\<CR>False):"
1146+
let out = [
1147+
\ 'async for _ in (True,',
1148+
\ ' False):',
1149+
\]
1150+
Assert Equals(Insert(in), Buffer(out))
1151+
End
1152+
1153+
It increases the indent level by 2
1154+
let b:python_indent_multiline_statement = 1
1155+
let in = "async for _ in (True,\<CR>False):"
1156+
let out = [
1157+
\ 'async for _ in (True,',
1158+
\ ' False):',
1159+
\]
1160+
Assert Equals(Insert(in), Buffer(out))
1161+
End
1162+
1163+
It increases the indent level (hanging indent)
1164+
let in = "async for _ in (\<CR>True,\<CR>False,\<CR>):"
1165+
let out = [
1166+
\ 'async for _ in (',
1167+
\ ' True,',
1168+
\ ' False,',
1169+
\ '):',
1170+
\]
1171+
Assert Equals(Insert(in), Buffer(out))
1172+
End
1173+
1174+
It increases the indent level by 2 (hanging indent)
1175+
let b:python_indent_multiline_statement = 1
1176+
let in = "async for _ in (\<CR>True,\<CR>False,\<CR>):"
1177+
let out = [
1178+
\ 'async for _ in (',
1179+
\ ' True,',
1180+
\ ' False,',
1181+
\ '):',
1182+
\]
1183+
Assert Equals(Insert(in), Buffer(out))
1184+
End
1185+
1186+
It increases the indent level (line continueation)
1187+
let in = "async for _ in True, \\\<CR>False:"
1188+
let out = [
1189+
\ 'async for _ in True, \',
1190+
\ ' False:',
1191+
\]
1192+
Assert Equals(Insert(in), Buffer(out))
1193+
End
1194+
1195+
It indents the else clause
1196+
let in = "async for _ in range(1):\<CR>"
1197+
let in .= "async for _ in range(2):\<CR>continue\<CR>"
1198+
let in .= "else:\<CR>pass\<CR>"
1199+
let in .= "else:"
1200+
let out = [
1201+
\ 'async for _ in range(1):',
1202+
\ ' async for _ in range(2):',
1203+
\ ' continue',
1204+
\ ' else:',
1205+
\ ' pass',
1206+
\ 'else:',
1207+
\]
1208+
Assert Equals(Insert(in), Buffer(out))
1209+
End
1210+
End
11421211
End
11431212
End
11441213
End

0 commit comments

Comments
 (0)