vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

handlebars.vim (3964B)


      1 " Vim indent file
      2 " Language:     Handlebars
      3 " Maintainer:   Devin Weaver
      4 " Last Change:  2026 Feb 20
      5 " Origin:       https://github.com/joukevandermaas/vim-ember-hbs
      6 " Credits:      Jouke van der Maas
      7 " Acknowledgement: Based on eruby.vim indentation by TPope
      8 " License:      MIT
      9 " The MIT License (MIT)
     10 "
     11 " Copyright (c) 2026 Devin Weaver
     12 " Copyright (c) 2015 Jouke van der Maas
     13 "
     14 " Permission is hereby granted, free of charge, to any person obtaining a copy
     15 " of this software and associated documentation files (the "Software"), to deal
     16 " in the Software without restriction, including without limitation the rights
     17 " to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     18 " copies of the Software, and to permit persons to whom the Software is
     19 " furnished to do so, subject to the following conditions:
     20 "
     21 " The above copyright notice and this permission notice shall be included in all
     22 " copies or substantial portions of the Software.
     23 "
     24 " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     25 " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     26 " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     27 " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     28 " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     29 " OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     30 " SOFTWARE.
     31 
     32 if exists("b:did_indent")
     33   finish
     34 endif
     35 
     36 runtime! indent/html.vim
     37 unlet! b:did_indent
     38 
     39 " Force HTML indent to not keep state.
     40 let b:html_indent_usestate = 0
     41 let b:handlebars_current_indent = 0
     42 
     43 if &l:indentexpr == ''
     44   if &l:cindent
     45     let &l:indentexpr = 'cindent(v:lnum)'
     46   else
     47     let &l:indentexpr = 'indent(prevnonblank(v:lnum-1))'
     48   endif
     49 endif
     50 let b:handlebars_subtype_indentexpr = &l:indentexpr
     51 
     52 let b:did_indent = 1
     53 
     54 setlocal indentexpr=GetHandlebarsIndent()
     55 setlocal indentkeys=o,O,*<Return>,<>>,{,},0),0],o,O,!^F,=else,={{#,={{/
     56 
     57 " Only define the function once.
     58 if exists("*GetHandlebarsIndent")
     59   finish
     60 endif
     61 
     62 function! GetHandlebarsIndent(...)
     63   " The value of a single shift-width
     64   let sw = shiftwidth()
     65 
     66   if a:0 && a:1 == '.'
     67     let v:lnum = line('.')
     68   elseif a:0 && a:1 =~ '^\d'
     69     let v:lnum = a:1
     70   endif
     71   let vcol = col('.')
     72   call cursor(v:lnum,1)
     73   call cursor(v:lnum,vcol)
     74   exe "let ind = ".b:handlebars_subtype_indentexpr
     75 
     76   " Workaround for Andy Wokula's HTML indent. This should be removed after
     77   " some time, since the newest version is fixed in a different way. Credit
     78   " to eruby.vim indent by tpope
     79   if b:handlebars_subtype_indentexpr =~# '^HtmlIndent('
     80   \ && exists('b:indent')
     81   \ && type(b:indent) == type({})
     82   \ && has_key(b:indent, 'lnum')
     83     " Force HTML indent to not keep state
     84     let b:indent.lnum = -1
     85   endif
     86 
     87   let lnum = prevnonblank(v:lnum-1)
     88   let prevLine = getline(lnum)
     89   let currentLine = getline(v:lnum)
     90 
     91   " all indent rules only apply if the block opening/closing
     92   " tag is on a separate line
     93 
     94   " indent after block {{#block
     95   if prevLine =~# '\v\s*\{\{\#'
     96     let ind = ind + sw
     97   endif
     98   " but not if the block ends on the same line
     99   if prevLine =~# '\v\s*\{\{\#(.+)(\s+|\}\}).+\{\{\/\1'
    100     let ind = ind - sw
    101   endif
    102   " unindent after block close {{/block}}
    103   if currentLine =~# '\v^\s*\{\{\/'
    104     let ind = ind - sw
    105   endif
    106   " indent after component block {{a-component
    107   if prevLine =~# '\v\s*\{\{\w'
    108      let ind = ind + sw
    109   endif
    110   " but not if the component block ends on the same line
    111   if prevLine =~# '\v\s*\{\{\w(.+)\}\}'
    112     let ind = ind - sw
    113   endif
    114   " unindent }} lines
    115   if currentLine =~# '\v^\s*\}\}\s*$' || (currentLine !~# '\v^\s*\{\{\/' && prevLine =~# '\v^\s*[^\{\}]+\}\}\s*$')
    116     let ind = ind - sw
    117   endif
    118   " unindent {{else}}
    119   if currentLine =~# '\v^\s*\{\{else'
    120     let ind = ind - sw
    121   endif
    122   " indent again after {{else}}
    123   if prevLine =~# '\v^\s*\{\{else'
    124     let ind = ind + sw
    125   endif
    126 
    127   return ind
    128 endfunction