From 9a3aa7b20a67c1b7991da1da9508ad5f78f76352 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Sat, 28 Mar 2020 00:57:27 +0300 Subject: proper jinja indentation and file detection --- dotfiles/.vim/ftdetect/jinja.vim | 14 +++++++ dotfiles/.vim/indent/jinja.vim | 80 +++++++++++++++++++++++++++++++++++ dotfiles/.vim/syntax/html.vim | 90 ++++++++++++++++++++++++++++++++++++++++ dotfiles/.vim/syntax/jinja.vim | 86 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 270 insertions(+) create mode 100644 dotfiles/.vim/ftdetect/jinja.vim create mode 100644 dotfiles/.vim/indent/jinja.vim create mode 100644 dotfiles/.vim/syntax/html.vim create mode 100644 dotfiles/.vim/syntax/jinja.vim (limited to 'dotfiles/.vim') diff --git a/dotfiles/.vim/ftdetect/jinja.vim b/dotfiles/.vim/ftdetect/jinja.vim new file mode 100644 index 0000000..d700140 --- /dev/null +++ b/dotfiles/.vim/ftdetect/jinja.vim @@ -0,0 +1,14 @@ +" Figure out which type of hilighting to use for html. +fun! s:SelectHTML() + let n = 1 + while n < 50 && n <= line("$") + " check for jinja + if getline(n) =~ '{{.*}}\|{%-\?\s*\(end.*\|extends\|block\|macro\|set\|if\|for\|include\|trans\)\>' + set ft=jinja.html + return + endif + let n = n + 1 + endwhile +endfun +autocmd BufNewFile,BufRead *.html,*.htm call s:SelectHTML() +autocmd BufNewFile,BufRead *.jinja2,*.j2,*.jinja,*.nunjucks,*.nunjs,*.njk set ft=jinja diff --git a/dotfiles/.vim/indent/jinja.vim b/dotfiles/.vim/indent/jinja.vim new file mode 100644 index 0000000..25e8f66 --- /dev/null +++ b/dotfiles/.vim/indent/jinja.vim @@ -0,0 +1,80 @@ +" Vim indent file +" Language: Jinja HTML template +" Maintainer: Evan Hammer +" Last Change: 2013 Jan 26 + +" Only load this indent file when no other was loaded. +if exists("b:did_indent") + finish +endif + +" Use HTML formatting rules. +setl indentkeys=o,O,,<>>,!^F +runtime! indent/html.vim +setl nosmartindent +let b:did_indent = 1 + +" Indent within the jinja tags +" Made by Steve Losh +if &l:indentexpr == '' + if &l:cindent + let &l:indentexpr = 'cindent(v:lnum)' + else + let &l:indentexpr = 'indent(prevnonblank(v:lnum-1))' + endif +endif +let b:html_indentexpr = &l:indentexpr + +let b:did_indent = 1 + +setlocal indentexpr=GetDjangoIndent() +setlocal indentkeys=o,O,*,{,},o,O,!^F,<>> + +" Only define the function once. +if exists("*GetDjangoIndent") + finish +endif + +function! GetDjangoIndent(...) + if a:0 && a:1 == '.' + let v:lnum = line('.') + elseif a:0 && a:1 =~ '^\d' + let v:lnum = a:1 + endif + let vcol = col('.') + + call cursor(v:lnum,vcol) + + exe "let ind = ".b:html_indentexpr + + let lnum = prevnonblank(v:lnum-1) + let pnb = getline(lnum) + let cur = getline(v:lnum) + + let tagstart = '.*' . '{%\s*' + let tagend = '.*%}' . '.*' + + let blocktags = '\(block\|for\|if\|with\|autoescape\|comment\|filter\|spaceless\)' + let midtags = '\(empty\|else\|elif\)' + + let pnb_blockstart = pnb =~# tagstart . blocktags . tagend + let pnb_blockend = pnb =~# tagstart . 'end' . blocktags . tagend + let pnb_blockmid = pnb =~# tagstart . midtags . tagend + + let cur_blockstart = cur =~# tagstart . blocktags . tagend + let cur_blockend = cur =~# tagstart . 'end' . blocktags . tagend + let cur_blockmid = cur =~# tagstart . midtags . tagend + + if pnb_blockstart && !pnb_blockend + let ind = ind + &sw + elseif pnb_blockmid && !pnb_blockend + let ind = ind + &sw + endif + + if cur_blockend && !cur_blockstart + let ind = ind - &sw + elseif cur_blockmid + let ind = ind - &sw + endif + + return ind +endfunction diff --git a/dotfiles/.vim/syntax/html.vim b/dotfiles/.vim/syntax/html.vim new file mode 100644 index 0000000..9e1111b --- /dev/null +++ b/dotfiles/.vim/syntax/html.vim @@ -0,0 +1,90 @@ +" Vim syntax file +" Language: HTML (version 5) +" Maintainer: Rodrigo Machado +" URL: http://rm.blog.br/vim/syntax/html.vim +" Last Change: 2009 Aug 19 +" License: Public domain +" (but let me know if you like :) ) +" +" Note: This file just adds the new tags from HTML 5 +" and don't replace default html.vim syntax file +" +" Modified: othree +" Changes: update to Draft 28 August 2010 +" add complete new attributes +" add wai-aria attributes +" add microdata attributes +" add rdfa attributes + + +syn keyword htmlTagName contained script +" HTML 5 tags +syn keyword htmlTagName contained article aside audio canvas command +syn keyword htmlTagName contained datalist details dialog embed figcaption figure footer +syn keyword htmlTagName contained header hgroup keygen mark meter menu nav output +syn keyword htmlTagName contained progress time ruby rt rp section source summary time track video wbr + +" HTML 5 arguments +" Core Attributes +syn keyword htmlArg contained accesskey class contenteditable contextmenu dir +syn keyword htmlArg contained draggable hidden id lang spellcheck style tabindex title +" Event-handler Attributes +syn keyword htmlArg contained onabort onblur oncanplay oncanplaythrough onchange +syn keyword htmlArg contained onclick oncontextmenu ondblclick ondrag ondragend ondragenter ondragleave ondragover +syn keyword htmlArg contained ondragstart ondrop ondurationchange onemptied onended onerror onfocus onformchange +syn keyword htmlArg contained onforminput oninput oninvalid onkeydown onkeypress onkeyup onload onloadeddata +syn keyword htmlArg contained onloadedmetadata onloadstart onmousedown onmousemove onmouseout onmouseover onmouseup +syn keyword htmlArg contained onmousewheel onpause onplay onplaying onprogress onratechange onreadystatechange +syn keyword htmlArg contained onscroll onseeked onseeking onselect onshow onstalled onsubmit onsuspend ontimeupdate +syn keyword htmlArg contained onvolumechange onwaiting +" XML Attributes +syn keyword htmlArg contained xml:lang xml:space xml:base +" new features +" +syn keyword htmlArg contained onafterprint onbeforeprint onbeforeunload onblur onerror onfocus onhashchange onload +syn keyword htmlArg contained onmessage onoffline ononline onpopstate onredo onresize onstorage onundo onunload +"