Commit dacfd49e authored by HiPhish's avatar HiPhish
Browse files

Merge branch 'syntax-on'

parents 7e8399c4 56feeed8
Loading
Loading
Loading
Loading
+29 −2
Original line number Diff line number Diff line
@@ -21,10 +21,21 @@
"   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" }}}

autocmd! BufRead,BufNewFile *.jinja,*jinja2,*.j2 call <SID>extension(expand('<afile>'))

" How it works: Jinja files may have a compound file name like
" 'foo.html.jinja' or 'foo.tex.jinja'.  We trim off the '.jinja' (or '.jinja2'
" or '.j2') part of the file name and try detecting the file type type.  If
" one is found we append '.jinja' to the file type.  Otherwise we use just
" `jinja` for the file type.
"
" There are separate implementations for Vim and Neovim because I could not
" make one solution work for both.


autocmd! BufRead,BufNewFile *.jinja,*.jinja2,*.j2 if has('nvim') | call <SID>ft_nvim(expand('<afile>')) | else | call <SID>ft_vim(expand('<afile>')) | endif

" Detect a normal or compound file extension (like 'foo.html.jinja')
function! s:extension(fname)
function! s:ft_vim(fname)
	let l:previous_ft = &ft

	" Try to detect the file type without the Jinja extension first. This will
@@ -52,3 +63,19 @@ function! s:extension(fname)
		set filetype+=.jinja
	endif
endfunction

" Detect compound file type using Neovim's Lua API
function! s:ft_nvim(fname) abort
	let l:clipped_fname = fnamemodify(fnameescape(a:fname), ':r')
	execute 'file' l:clipped_fname
	let l:secondary_ft = luaeval('vim.filetype.match(_A)', {'buf': 0})
	if empty(l:secondary_ft)
		set filetype=jinja
	else
		let &filetype = printf('%s.jinja', l:secondary_ft)
	endif
	" Using ':file' has dissociated the buffer from its file, but executing
	" ':edit' fixes this
	execute 'file' a:fname
	noautocmd silent edit
endfunction
+0 −1
Original line number Diff line number Diff line
@@ -40,7 +40,6 @@ describe('Syntax highlighting', function()

	it('highlights filters', function()
		assert.nvim(nvim).at_position(5, 15).has_hlgroup('jinjaFilter')
		assert.nvim(nvim).at_position(7, 21).has_hlgroup('jinjaFilter')
	end)

	it('highlights statement blocks', function()
+3 −0
Original line number Diff line number Diff line
" HTML is a special case because Vim tries to be clever and attempts to detect
" Django.
autocmd! BufRead,BufNewFile *.html  set ft=html | call jinja#AdjustFiletype()