Skip to content

Commit

Permalink
Improve indent for coroutine function definition
Browse files Browse the repository at this point in the history
  • Loading branch information
hattya committed Jul 19, 2023
1 parent f0fedf6 commit 44c3435
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
6 changes: 3 additions & 3 deletions indent/python.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
" Vim indent file
" Language: Python
" Author: Akinori Hattori <hattya@gmail.com>
" Last Change: 2023-07-17
" Last Change: 2023-07-19
" License: MIT License

if exists('b:did_indent')
Expand Down Expand Up @@ -179,11 +179,11 @@ function! s:synmatch(lnum, col, pat) abort
endfunction

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

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

function! s:cont() abort
Expand Down
51 changes: 51 additions & 0 deletions test/indent.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,15 @@ Describe filetype indent
Assert Equals(Insert(in), Buffer(out))
End
End

It indents the await expression
let in = "await spam \\\<CR>.eggs()"
let out = [
\ 'await spam \',
\ ' .eggs()',
\]
Assert Equals(Insert(in), Buffer(out))
End
End

Describe simple statement
Expand Down Expand Up @@ -1089,5 +1098,47 @@ Describe filetype indent
Assert Equals(Insert(in), Buffer(out))
End
End

Describe coroutine
Describe coroutine function definition
It aligns with left brackets
let in = "async def spam(*args,\<CR>**kwargs):"
let out = [
\ 'async def spam(*args,',
\ ' **kwargs):',
\]
Assert Equals(Insert(in), Buffer(out))
End

It increases the indent level (hanging indent)
let in = "async def spam(\<CR>*args,\<CR>**kwargs\<CR>):"
let out = [
\ 'async def spam(',
\ ' *args,',
\ ' **kwargs',
\ '):',
\]
Assert Equals(Insert(in), Buffer(out))
End

It increases the indent level (line continueation)
let in = "async def \\\<CR>spam():\<CR>...\<CR>"
let in .= "async \\\<CR>def eggs():\<CR>...\<CR>"
let in .= "async \\\<CR>def \\\<CR>ham():"
let out = [
\ 'async def \',
\ ' spam():',
\ ' ...',
\ 'async \',
\ ' def eggs():',
\ ' ...',
\ 'async \',
\ ' def \',
\ ' ham():',
\]
Assert Equals(Insert(in), Buffer(out))
End
End
End
End
End

0 comments on commit 44c3435

Please sign in to comment.