Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix indentation inconsistencies #129

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions indent/elm.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let b:did_indent = 1
" Local defaults
setlocal expandtab
setlocal indentexpr=GetElmIndent()
setlocal indentkeys+=0=else,0=if,0=of,0=import,0=then,0=type,0\|,0},0\],0),=-},0=in
setlocal indentkeys+=0=else,0=if,0=of,0=import,0=then,0=type,0\|,0},0\],0),=-},0=in,0\=
setlocal nolisp
setlocal nosmartindent

Expand Down Expand Up @@ -43,17 +43,9 @@ function! GetElmIndent()
if l:line =~? '^\s*}'
return s:FindPair('{', '', '}')

" Indent if current line begins with 'else':
" Align 'else' with the parent if.
elseif l:line =~# '^\s*else\>'
if l:lline !~# '^\s*\(if\|then\)\>'
return s:FindPair('\<if\>', '', '\<else\>')
endif

" Indent if current line begins with 'then':
elseif l:line =~# '^\s*then\>'
if l:lline !~# '^\s*\(if\|else\)\>'
return s:FindPair('\<if\>', '', '\<then\>')
endif
return indent(search('^\s*if', 'bWn'))

" HACK: Indent lines in case with nearest case clause:
elseif l:line =~# '->' && l:line !~# ':' && l:line !~# '\\'
Expand Down Expand Up @@ -89,17 +81,14 @@ function! GetElmIndent()
if l:lline =~# '\(|\|=\|->\|<-\|(\|\[\|{\|\<\(of\|else\|if\|then\)\)\s*$'
let l:ind = l:ind + &shiftwidth

" Add a 'shiftwidth' after lines starting with type ending with '=':
elseif l:lline =~# '^\s*type' && l:line =~# '^\s*='
let l:ind = l:ind + &shiftwidth

" Back to normal indent after comments:
elseif l:lline =~# '-}\s*$'
call search('-}', 'bW')
let l:ind = indent(searchpair('{-', '', '-}', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))

" Ident some operators if there aren't any starting the last line.
elseif l:line =~# '^\s*\(!\|&\|(\|`\|+\||\|{\|[\|,\)=' && l:lline !~# '^\s*\(!\|&\|(\|`\|+\||\|{\|[\|,\)=' && l:lline !~# '^\s*$'
" Including '=' for Union types indentation
elseif l:line =~# '^\s*\(!\|&\|(\|`\|+\||\|{\|[\|,\|=\)' && l:lline !~# '^\s*\(!\|&\|(\|`\|+\||\|{\|[\|,\|=\)' && l:lline !~# '^\s*$'
let l:ind = l:ind + &shiftwidth

elseif l:lline ==# '' && getline(l:lnum - 1) !=# ''
Expand Down
19 changes: 11 additions & 8 deletions syntax/elm.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ endif

" Keywords
syn keyword elmConditional case else if of then
syn keyword elmAlias alias
syn keyword elmTypedef type port let in
syn match elmTypealias "type alias" contains=elmTypedef
syn keyword elmTypedef type port let in infixr infixl infix
syn keyword elmImport exposing as import module where

" Operators
syn match elmOperator "\([-!#$%`&\*\+./<=>\?@\\^|~:]\|\<_\>\)"

" Types
syn match elmType "\<[A-Z][0-9A-Za-z_'-]*"
syn keyword elmNumberType number
syn keyword elmTypeClass number appendable comparable

" Delimiters
syn match elmDelimiter "[,;]"
Expand All @@ -37,13 +37,16 @@ syn region elmTripleString start="\"\"\"" skip="\\\"" end="\"\"\"" contains=elmS
syn match elmChar "'[^'\\]'\|'\\.'\|'\\u[0-9a-fA-F]\{4}'"

" Numbers
syn match elmInt "-\?\<\d\+\>\|0[xX][0-9a-fA-F]\+\>"
syn match elmFloat "\(\<\d\+\.\d\+\>\)"
syn match elmInt "-\?\<0[xX][0-9a-fA-F]\+\>"
syn match elmFloat "-\?\<[0-9]\+\(\.[0-9]\+\)\?\([eE][+-]\?[0-9]\+\)\?\>"

" Identifiers
syn match elmTopLevelDecl "^\s*[a-zA-Z][a-zA-z0-9_]*\('\)*\s\+:\s\+" contains=elmOperator
syn region elmRecord matchgroup=elmRecord start="{[^-]" end="[^-]}" contains=ALL contained
syn match elmTopLevelDecl "^\s*[a-zA-Z][a-zA-z0-9_]*\('\)*\_s\+:\(\s\+\|$\)" contains=elmOperator
syn match elmTopLevelBinding "^\s*\(let \)\?\zs[a-zA-Z][a-zA-z0-9_]*\('\)*\ze\([^{]*\|.*{[^}]*}.*\)[^-!#$%&\*\+./<=>\?@\\^|~:]=\([^-!#$%&\*\+./<=>\?@\\^|~:]\|$\)" contains=elmRecord

hi def link elmTopLevelDecl Function
hi def link elmTopLevelBinding Function
hi def link elmTupleFunction Normal
hi def link elmTodo Todo
hi def link elmComment Comment
Expand All @@ -56,13 +59,13 @@ hi def link elmInt Number
hi def link elmFloat Float
hi def link elmDelimiter Delimiter
hi def link elmBraces Delimiter
hi def link elmTypealias TypeDef
hi def link elmTypedef TypeDef
hi def link elmImport Include
hi def link elmConditional Conditional
hi def link elmAlias Delimiter
hi def link elmOperator Operator
hi def link elmType Identifier
hi def link elmNumberType Identifier
hi def link elmTypeClass Identifier

syn sync minlines=500

Expand Down