Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 1 | " Vim indent file |
Bram Moolenaar | 7a32991 | 2010-05-21 12:05:36 +0200 | [diff] [blame] | 2 | " Language: Haml |
| 3 | " Maintainer: Tim Pope <vimNOSPAM@tpope.org> |
Bram Moolenaar | c08ee74 | 2019-12-05 22:47:25 +0100 | [diff] [blame] | 4 | " Last Change: 2019 Dec 05 |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 5 | |
| 6 | if exists("b:did_indent") |
| 7 | finish |
| 8 | endif |
| 9 | runtime! indent/ruby.vim |
| 10 | unlet! b:did_indent |
| 11 | let b:did_indent = 1 |
| 12 | |
Bram Moolenaar | c08ee74 | 2019-12-05 22:47:25 +0100 | [diff] [blame] | 13 | setlocal autoindent |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 14 | setlocal indentexpr=GetHamlIndent() |
| 15 | setlocal indentkeys=o,O,*<Return>,},],0),!^F,=end,=else,=elsif,=rescue,=ensure,=when |
| 16 | |
| 17 | " Only define the function once. |
| 18 | if exists("*GetHamlIndent") |
| 19 | finish |
| 20 | endif |
| 21 | |
| 22 | let s:attributes = '\%({.\{-\}}\|\[.\{-\}\]\)' |
| 23 | let s:tag = '\%([%.#][[:alnum:]_-]\+\|'.s:attributes.'\)*[<>]*' |
| 24 | |
| 25 | if !exists('g:haml_self_closing_tags') |
Bram Moolenaar | 543b7ef | 2013-06-01 14:50:56 +0200 | [diff] [blame] | 26 | let g:haml_self_closing_tags = 'base|link|meta|br|hr|img|input' |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 27 | endif |
| 28 | |
| 29 | function! GetHamlIndent() |
| 30 | let lnum = prevnonblank(v:lnum-1) |
| 31 | if lnum == 0 |
| 32 | return 0 |
| 33 | endif |
| 34 | let line = substitute(getline(lnum),'\s\+$','','') |
| 35 | let cline = substitute(substitute(getline(v:lnum),'\s\+$','',''),'^\s\+','','') |
| 36 | let lastcol = strlen(line) |
| 37 | let line = substitute(line,'^\s\+','','') |
| 38 | let indent = indent(lnum) |
| 39 | let cindent = indent(v:lnum) |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 40 | let sw = shiftwidth() |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 41 | if cline =~# '\v^-\s*%(elsif|else|when)>' |
Bram Moolenaar | 89bcfda | 2016-08-30 23:26:57 +0200 | [diff] [blame] | 42 | let indent = cindent < indent ? cindent : indent - sw |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 43 | endif |
Bram Moolenaar | 89bcfda | 2016-08-30 23:26:57 +0200 | [diff] [blame] | 44 | let increase = indent + sw |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 45 | if indent == indent(lnum) |
| 46 | let indent = cindent <= indent ? -1 : increase |
| 47 | endif |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 48 | |
| 49 | let group = synIDattr(synID(lnum,lastcol,1),'name') |
| 50 | |
| 51 | if line =~ '^!!!' |
| 52 | return indent |
| 53 | elseif line =~ '^/\%(\[[^]]*\]\)\=$' |
| 54 | return increase |
Bram Moolenaar | 7a32991 | 2010-05-21 12:05:36 +0200 | [diff] [blame] | 55 | elseif group == 'hamlFilter' |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 56 | return increase |
Bram Moolenaar | 7a32991 | 2010-05-21 12:05:36 +0200 | [diff] [blame] | 57 | elseif line =~ '^'.s:tag.'[&!]\=[=~-]\s*\%(\%(if\|else\|elsif\|unless\|case\|when\|while\|until\|for\|begin\|module\|class\|def\)\>\%(.*\<end\>\)\@!\|.*do\%(\s*|[^|]*|\)\=\s*$\)' |
| 58 | return increase |
| 59 | elseif line =~ '^'.s:tag.'[&!]\=[=~-].*,\s*$' |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 60 | return increase |
| 61 | elseif line == '-#' |
| 62 | return increase |
| 63 | elseif group =~? '\v^(hamlSelfCloser)$' || line =~? '^%\v%('.g:haml_self_closing_tags.')>' |
| 64 | return indent |
| 65 | elseif group =~? '\v^%(hamlTag|hamlAttributesDelimiter|hamlObjectDelimiter|hamlClass|hamlId|htmlTagName|htmlSpecialTagName)$' |
| 66 | return increase |
| 67 | elseif synIDattr(synID(v:lnum,1,1),'name') ==? 'hamlRubyFilter' |
| 68 | return GetRubyIndent() |
| 69 | else |
| 70 | return indent |
| 71 | endif |
| 72 | endfunction |
| 73 | |
| 74 | " vim:set sw=2: |