From e2bbed8acc1f1cf498a0085cf771cf9bf40fb709 Mon Sep 17 00:00:00 2001 From: Adam Stankiewicz Date: Thu, 24 Sep 2020 10:50:19 +0200 Subject: Fix django highlighting, fixes #553 --- ftplugin/handlebars.vim | 127 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 ftplugin/handlebars.vim (limited to 'ftplugin/handlebars.vim') diff --git a/ftplugin/handlebars.vim b/ftplugin/handlebars.vim new file mode 100644 index 00000000..649984aa --- /dev/null +++ b/ftplugin/handlebars.vim @@ -0,0 +1,127 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'handlebars') == -1 + +if exists('b:loaded_mustache_handlebars') + finish +endif +let b:loaded_mustache_handlebars = 1 + +let s:cpo_save = &cpo +set cpo&vim + +" Matchit support for Mustache & Handlebars +" extending HTML matchit groups +if exists("loaded_matchit") && exists("b:match_words") + let b:match_words = b:match_words + \ . ',{:},[:],(:),' + \ . '\%({{\)\@<=#\s*\%(if\|unless\)\s*.\{-}}}' + \ . ':' + \ . '\%({{\)\@<=\s*else\s*}}' + \ . ':' + \ . '\%({{\)\@<=/\s*\%(if\|unless\)\s*}},' + \ . '\%({{\)\@<=[#^]\s*\([-0-9a-zA-Z_?!/.]\+\).\{-}}}' + \ . ':' + \ . '\%({{\)\@<=/\s*\1\s*}}' +endif + +" Set template for comment +setlocal commentstring={{!--\ %s\ --}} + +if exists("g:mustache_abbreviations") + inoremap {{{ {{{}}} + inoremap {{ {{}} + inoremap {{! {{!}} + inoremap {{< {{<}} + inoremap {{> {{>}} + inoremap {{# {{#}}{{/}} + inoremap {{if {{#if }}{{/if}} + inoremap {{ife {{#if }}{{else}}{{/if}} +endif + + +" Section movement +" Adapted from vim-ruby - many thanks to the maintainers of that plugin + +function! s:sectionmovement(pattern,flags,mode,count) + norm! m' + if a:mode ==# 'v' + norm! gv + endif + let i = 0 + while i < a:count + let i = i + 1 + " saving current position + let line = line('.') + let col = col('.') + let pos = search(a:pattern,'W'.a:flags) + " if there's no more matches, return to last position + if pos == 0 + call cursor(line,col) + return + endif + endwhile +endfunction + +nnoremap [[ :call sectionmovement('{{','b','n',v:count1) +nnoremap ]] :call sectionmovement('{{','' ,'n',v:count1) +xnoremap [[ :call sectionmovement('{{','b','v',v:count1) +xnoremap ]] :call sectionmovement('{{','' ,'v',v:count1) + + +" Operator pending mappings + +" Operators are available by default. Set `let g:mustache_operators = 0` in +" your .vimrc to disable them. +if ! exists("g:mustache_operators") + let g:mustache_operators = 1 +endif + +if exists("g:mustache_operators") && g:mustache_operators + onoremap ie :call wrap_inside() + onoremap ae :call wrap_around() + xnoremap ie :call wrap_inside() + xnoremap ae :call wrap_around() +endif + +function! s:wrap_around() + " If the cursor is at the end of the tag element, move back + " so that the end tag can be detected. + while getline('.')[col('.')-1] ==# '}' + execute 'norm h' + endwhile + + " Moves to the end of the closing tag + let pos = search('}}','We') + if pos != 0 + if getline('.')[col('.')] ==# '}' + " Ending tag contains 3 closing brackets '}}}', + " move to the last bracket. + execute 'norm l' + endif + + " select the whole tag + execute 'norm v%' + endif +endfunction + +function! s:wrap_inside() + " If the cursor is at the end of the tag element, move back + " so that the end tag can be detected. + while getline('.')[col('.')-1] ==# '}' + execute 'norm h' + endwhile + + " Moves to the end of the closing tag + let pos = search('}}','W') + if pos != 0 + " select only inside the tag + execute 'norm v%loho' + endif +endfunction + + +let &cpo = s:cpo_save +unlet s:cpo_save + +" vim: nofoldenable + +endif -- cgit v1.2.3