Skip to content

Commit

Permalink
Improve indent for match statement
Browse files Browse the repository at this point in the history
  • Loading branch information
hattya committed Jul 13, 2023
1 parent ff4d7c1 commit 06c33bc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
7 changes: 4 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-09
" Last Change: 2023-07-13
" License: MIT License

if exists('b:did_indent')
Expand Down Expand Up @@ -31,6 +31,7 @@ let s:compound_stmts = {
\ '\v^\s*<else>': '\v^\s*<%(if|elif|while|for|try|except)>',
\ '\v^\s*<except>': '\v^\s*<%(try|except)>',
\ '\v^\s*<finally>': '\v^\s*<%(try|except|else)>',
\ '\v^\s*<case>': '\v^\s*<case>',
\}
let s:dedent = '\v^\s*<%(pass|return|raise|break|continue)>'
let s:ellipsis = '\v^\s*\.{3}\.@!'
Expand Down Expand Up @@ -176,11 +177,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)>'
return (a:0 && a:1 && a:str =~# '\v^\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|except|with|def|class)>')
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)>')
endfunction

function! s:cont() abort
Expand Down
64 changes: 64 additions & 0 deletions test/indent.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,70 @@ Describe filetype indent
End
End

Describe match statement
It aligns with left brackets
let in = "match expr:\<CR>"
let in .= "case (spam,\<CR>eggs):"
let out = [
\ 'match expr:',
\ ' case (spam,',
\ ' eggs):',
\]
Assert Equals(Insert(in), Buffer(out))
End

It increases the indent level by 2
let b:python_indent_multiline_statement = 1
let in = "match expr:\<CR>"
let in .= "case (spam,\<CR>eggs):"
let out = [
\ 'match expr:',
\ ' case (spam,',
\ ' eggs):',
\]
Assert Equals(Insert(in), Buffer(out))
End

It increases the indent level (hanging indent)
let in = "match expr:\<CR>"
let in .= "case (\<CR>spam,\<CR>eggs,\<CR>):"
let out = [
\ 'match expr:',
\ ' case (',
\ ' spam,',
\ ' eggs,',
\ ' ):',
\]
Assert Equals(Insert(in), Buffer(out))
End

It increases the indent level by 2 (hanging indent)
let b:python_indent_multiline_statement = 1
let in = "match expr:\<CR>"
let in .= "case (\<CR>spam,\<CR>eggs,\<CR>):"
let out = [
\ 'match expr:',
\ ' case (',
\ ' spam,',
\ ' eggs,',
\ ' ):',
\]
Assert Equals(Insert(in), Buffer(out))
End

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

Describe function definition
It aligns with left brackets
let in = "def spam(*args,\<CR>**kwargs):"
Expand Down

0 comments on commit 06c33bc

Please sign in to comment.