Skip to content

Commit 06c33bc

Browse files
committed
Improve indent for match statement
1 parent ff4d7c1 commit 06c33bc

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

indent/python.vim

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

77
if exists('b:did_indent')
@@ -31,6 +31,7 @@ let s:compound_stmts = {
3131
\ '\v^\s*<else>': '\v^\s*<%(if|elif|while|for|try|except)>',
3232
\ '\v^\s*<except>': '\v^\s*<%(try|except)>',
3333
\ '\v^\s*<finally>': '\v^\s*<%(try|except|else)>',
34+
\ '\v^\s*<case>': '\v^\s*<case>',
3435
\}
3536
let s:dedent = '\v^\s*<%(pass|return|raise|break|continue)>'
3637
let s:ellipsis = '\v^\s*\.{3}\.@!'
@@ -176,11 +177,11 @@ function! s:synmatch(lnum, col, pat) abort
176177
endfunction
177178

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

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

186187
function! s:cont() abort

test/indent.vimspec

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,70 @@ Describe filetype indent
871871
End
872872
End
873873

874+
Describe match statement
875+
It aligns with left brackets
876+
let in = "match expr:\<CR>"
877+
let in .= "case (spam,\<CR>eggs):"
878+
let out = [
879+
\ 'match expr:',
880+
\ ' case (spam,',
881+
\ ' eggs):',
882+
\]
883+
Assert Equals(Insert(in), Buffer(out))
884+
End
885+
886+
It increases the indent level by 2
887+
let b:python_indent_multiline_statement = 1
888+
let in = "match expr:\<CR>"
889+
let in .= "case (spam,\<CR>eggs):"
890+
let out = [
891+
\ 'match expr:',
892+
\ ' case (spam,',
893+
\ ' eggs):',
894+
\]
895+
Assert Equals(Insert(in), Buffer(out))
896+
End
897+
898+
It increases the indent level (hanging indent)
899+
let in = "match expr:\<CR>"
900+
let in .= "case (\<CR>spam,\<CR>eggs,\<CR>):"
901+
let out = [
902+
\ 'match expr:',
903+
\ ' case (',
904+
\ ' spam,',
905+
\ ' eggs,',
906+
\ ' ):',
907+
\]
908+
Assert Equals(Insert(in), Buffer(out))
909+
End
910+
911+
It increases the indent level by 2 (hanging indent)
912+
let b:python_indent_multiline_statement = 1
913+
let in = "match expr:\<CR>"
914+
let in .= "case (\<CR>spam,\<CR>eggs,\<CR>):"
915+
let out = [
916+
\ 'match expr:',
917+
\ ' case (',
918+
\ ' spam,',
919+
\ ' eggs,',
920+
\ ' ):',
921+
\]
922+
Assert Equals(Insert(in), Buffer(out))
923+
End
924+
925+
It increases the indent level (line continueation)
926+
let in = "match obj \\\<CR>.meth():\<CR>"
927+
let in .= "case spam, \\\<CR>eggs:"
928+
let out = [
929+
\ 'match obj \',
930+
\ ' .meth():',
931+
\ ' case spam, \',
932+
\ ' eggs:',
933+
\]
934+
Assert Equals(Insert(in), Buffer(out))
935+
End
936+
End
937+
874938
Describe function definition
875939
It aligns with left brackets
876940
let in = "def spam(*args,\<CR>**kwargs):"

0 commit comments

Comments
 (0)