blob: 7b08de71267adcc698bf3baf54e5ad490dc7f437 [file] [log] [blame]
Bram Moolenaaracc22402020-06-07 21:07:18 +02001" Elm indent plugin file
2" Language: Elm
3" Maintainer: Andreas Scharf <as@99n.de>
4" Original Author: Joseph Hager <ajhager@gmail.com>
5" Copyright: Joseph Hager <ajhager@gmail.com>
6" License: BSD3
Bram Moolenaar6e649222021-10-04 21:32:54 +01007" Latest Revision: 2021-09-29
Bram Moolenaaracc22402020-06-07 21:07:18 +02008
9" Only load this indent file when no other was loaded.
10if exists('b:did_indent')
11 finish
12endif
13let b:did_indent = 1
14
15" Local defaults
16setlocal expandtab
17setlocal indentexpr=GetElmIndent()
18setlocal indentkeys+=0=else,0=if,0=of,0=import,0=then,0=type,0\|,0},0\],0),=-},0=in
19setlocal nolisp
20setlocal nosmartindent
21
Bram Moolenaar6e649222021-10-04 21:32:54 +010022let b:undo_indent = "setl et< inde< indk< lisp< si<"
23
Bram Moolenaaracc22402020-06-07 21:07:18 +020024" Only define the function once.
25if exists('*GetElmIndent')
26 finish
27endif
28
29" Indent pairs
30function! s:FindPair(pstart, pmid, pend)
31 "call search(a:pend, 'bW')
32 return indent(searchpair(a:pstart, a:pmid, a:pend, 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"'))
33endfunction
34
35function! GetElmIndent()
36 let l:lnum = v:lnum - 1
37
38 " Ident 0 if the first line of the file:
39 if l:lnum == 0
40 return 0
41 endif
42
43 let l:ind = indent(l:lnum)
44 let l:lline = getline(l:lnum)
45 let l:line = getline(v:lnum)
46
47 " Indent if current line begins with '}':
48 if l:line =~? '^\s*}'
49 return s:FindPair('{', '', '}')
50
51 " Indent if current line begins with 'else':
52 elseif l:line =~# '^\s*else\>'
53 if l:lline !~# '^\s*\(if\|then\)\>'
54 return s:FindPair('\<if\>', '', '\<else\>')
55 endif
56
57 " Indent if current line begins with 'then':
58 elseif l:line =~# '^\s*then\>'
59 if l:lline !~# '^\s*\(if\|else\)\>'
60 return s:FindPair('\<if\>', '', '\<then\>')
61 endif
62
63 " HACK: Indent lines in case with nearest case clause:
64 elseif l:line =~# '->' && l:line !~# ':' && l:line !~# '\\'
65 return indent(search('^\s*case', 'bWn')) + &shiftwidth
66
67 " HACK: Don't change the indentation if the last line is a comment.
68 elseif l:lline =~# '^\s*--'
69 return l:ind
70
71 " Align the end of block comments with the start
72 elseif l:line =~# '^\s*-}'
73 return indent(search('{-', 'bWn'))
74
75 " Indent double shift after let with an empty rhs
76 elseif l:lline =~# '\<let\>.*\s=$'
77 return l:ind + 4 + &shiftwidth
78
79 " Align 'in' with the parent let.
80 elseif l:line =~# '^\s*in\>'
81 return indent(search('^\s*let', 'bWn'))
82
83 " Align bindings with the parent let.
84 elseif l:lline =~# '\<let\>'
85 return l:ind + 4
86
87 " Align bindings with the parent in.
88 elseif l:lline =~# '^\s*in\>'
89 return l:ind
90
91 endif
92
93 " Add a 'shiftwidth' after lines ending with:
94 if l:lline =~# '\(|\|=\|->\|<-\|(\|\[\|{\|\<\(of\|else\|if\|then\)\)\s*$'
95 let l:ind = l:ind + &shiftwidth
96
97 " Add a 'shiftwidth' after lines starting with type ending with '=':
98 elseif l:lline =~# '^\s*type' && l:line =~# '^\s*='
99 let l:ind = l:ind + &shiftwidth
100
101 " Back to normal indent after comments:
102 elseif l:lline =~# '-}\s*$'
103 call search('-}', 'bW')
104 let l:ind = indent(searchpair('{-', '', '-}', 'bWn', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
105
106 " Ident some operators if there aren't any starting the last line.
107 elseif l:line =~# '^\s*\(!\|&\|(\|`\|+\||\|{\|[\|,\)=' && l:lline !~# '^\s*\(!\|&\|(\|`\|+\||\|{\|[\|,\)=' && l:lline !~# '^\s*$'
108 let l:ind = l:ind + &shiftwidth
109
110 elseif l:lline ==# '' && getline(l:lnum - 1) !=# ''
111 let l:ind = indent(search('^\s*\S+', 'bWn'))
112
113 endif
114
115 return l:ind
116endfunc