Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1 | " 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 Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 7 | " Latest Revision: 2021-09-29 |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 8 | |
| 9 | " Only load this indent file when no other was loaded. |
| 10 | if exists('b:did_indent') |
| 11 | finish |
| 12 | endif |
| 13 | let b:did_indent = 1 |
| 14 | |
| 15 | " Local defaults |
| 16 | setlocal expandtab |
| 17 | setlocal indentexpr=GetElmIndent() |
| 18 | setlocal indentkeys+=0=else,0=if,0=of,0=import,0=then,0=type,0\|,0},0\],0),=-},0=in |
| 19 | setlocal nolisp |
| 20 | setlocal nosmartindent |
| 21 | |
Bram Moolenaar | 6e64922 | 2021-10-04 21:32:54 +0100 | [diff] [blame] | 22 | let b:undo_indent = "setl et< inde< indk< lisp< si<" |
| 23 | |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 24 | " Only define the function once. |
| 25 | if exists('*GetElmIndent') |
| 26 | finish |
| 27 | endif |
| 28 | |
| 29 | " Indent pairs |
| 30 | function! 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"')) |
| 33 | endfunction |
| 34 | |
| 35 | function! 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 |
| 116 | endfunc |