Commit 51b8a2a5 authored by HiPhish's avatar HiPhish
Browse files

Improve file type detection by file extension

parent d20b0cd6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@
                    |__/       |__/                         ~


Version: 1.0.0
Version: 1.1.0
Author: Alejandro "HiPhish" Sanchez
License: MIT License

+10 −22
Original line number Diff line number Diff line
@@ -21,35 +21,23 @@
"   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" }}}




" This is flawed: if the file name is 'foo.jinja' it will work fine, but if
" the file name is 'foo.html.jinja' we would want the file type to be
" 'html.jinja' instead of just 'jinja'. However, we cannot simply take
" everything after the first dot as the file type because something like
" `main.macros.html.jinja` would get the wrong file type as well.
autocmd! BufRead,BufNewFile *.jinja,*jinja2,*.j2 call <SID>DetectFileExtension(expand('<afile>'))
augroup filetypedetect
autocmd! BufRead,BufNewFile *.jinja,*jinja2,*.j2 call <SID>extension(expand('<afile>'))
augroup END

" Detect a normal or compound file extension (like 'foo.html.jinja')
function! s:DetectFileExtension(fname)
	" Clear the file because if the next command fails to set it the old file
	" type will persist.
	" Bug: The below well cause 'did_filetype()' to return true, which will
	" prevent the next command from setting the file type at all. there needs
	" to be a way of supressing the event.
	" noautocmd set filetype=

function! s:extension(fname)
	" This will fail setting the file type of unknown file extension like
	" 'foo.nonsense.jinja', which is what we want.
	execute 'doautocmd BufReadPost '.fnamemodify(a:fname, ':r')
	noautocmd exe 'file ' .. fnamemodify(a:fname, ':r')
	filetype detect

	" Now that we have detected the parent file type we can append Jinja to it
	if empty(&filetype)
		set filetype=jinja
		" execute 'setfiletype jinja'
	elseif &filetype =~? 'jinja'
		return
	else
	elseif &filetype !~? 'jinja'
		set filetype+=.jinja
	endif

	exe 'noautocmd file ' .. a:fname
endfunction
+51 −8
Original line number Diff line number Diff line
@@ -2,7 +2,10 @@
# Detection of Jinja files based of file extension #
####################################################

Execute (A simple file extension):
Given (A simple file extension):
  Hello world

Execute:
  silent file foo.jinja
  filetype detect

@@ -10,7 +13,32 @@ Then:
  AssertEqual 'jinja', &filetype


Execute (Two file extensions):
Given (File extension j2):
  Hello world

Execute:
  silent file foo.j2
  filetype detect

Then:
  AssertEqual 'jinja', &filetype


Given (File extension jinja2):
  Hello world

Execute:
  silent file foo.jinja2
  filetype detect

Then:
  AssertEqual 'jinja', &filetype


Given (Two file extensions):
  Hello world

Execute:
  silent file foo.html.jinja
  filetype detect

@@ -18,7 +46,10 @@ Then:
  AssertEqual 'html.jinja', &filetype


Execute (Three file extensions):
Given (Three file extensions):
  Hello world

Execute:
  silent file foo.tex.html.jinja
  filetype detect

@@ -26,8 +57,11 @@ Then:
  AssertEqual 'html.jinja', &filetype


Given (Nonsense file extensions):
  Hello world

# I really hope there is no 'nonsense' file format out there
Execute (Nonsense file extensions):
Execute:
  silent file foo.nonsense.jinja
  filetype detect

@@ -35,7 +69,7 @@ Then:
  AssertEqual 'jinja', &filetype


Given html (Do not double-detect jinja):
Given (Do not double-detect jinja):
  {# This is a Jinja comment #}

Execute:
@@ -46,8 +80,11 @@ Then:
  AssertEqual 'html.jinja', &filetype


Given (Do not append Jinja multiple times):
  Hello world

# This will fail because the filetype remains 'html.jinja'.
Execute (Do not append Jinja multiple times):
Execute:
  silent file foo.html.jinja
  filetype detect
  silent file foo.nonsense.jinja
@@ -57,7 +94,10 @@ Then:
  AssertEqual 'jinja', &filetype


Execute (Add new file type where necessary):
Given (Add new file type where necessary):
  Hello world

Execute:
  silent file foo.nonsense.jinja
  filetype detect
  silent file foo.html.jinja
@@ -67,7 +107,10 @@ Then:
  AssertEqual 'html.jinja', &filetype


Execute (change file type when necessary):
Given (change file type when necessary):
  Hello world

Execute:
  silent file foo.html.jinja
  filetype detect
  silent file foo.xml.jinja