Commit a6d2cd50 authored by HiPhish's avatar HiPhish
Browse files

Improve filetype detection

More test cases and better handling of compound file types. Still have
one bug, but I cannot fix it within a plugin.
parent ade1c6fb
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -73,6 +73,13 @@ authors' attention; fixing the issue once in that plugin will forever benefit
everyone while applying a  hack to my plugin is just  shoving the problem under
the rug for the time being.

Jinja.vim will recognise the file  `foo.html.jinja` as of `html.jinja` type and
`foo.deprecated.jinja`  or `foo.jinja`  as just  `jinja`, but  if you  were the
change the file  name from the former to  one of the latter the  plugin will be
unable to pick up  that change. This only happens when the  target file type is
plain `jinja`. Changing `foo.html.jinja` to  `foo.xml.jinja` will work fine. To
my knowledge there is no way of fixing this without changing Vim.


License
#######
+17 −6
Original line number Diff line number Diff line
@@ -31,11 +31,22 @@ autocmd! BufRead,BufNewFile *.jinja call <SID>DetectFileExtension(expand('<afil

" 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=

	" 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')
	execute 'doautocmd BufReadPost '.fnamemodify(a:fname, ':r')

	if empty(&filetype)
		set filetype=jinja
		" execute 'setfiletype jinja'
	elseif &ft =~? 'jinja'
		return
	else
		set filetype+=.jinja
	endif
+40 −0
Original line number Diff line number Diff line
@@ -35,3 +35,43 @@ Then:
  AssertEqual 'jinja', &filetype


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

Execute:
  silent file foo.html.jinja
  filetype detect

Then:
  AssertEqual 'html.jinja', &filetype


# This will fail because the filetype remains 'html.jinja'.
Execute (Do not append Jinja multiple times):
  silent file foo.html.jinja
  filetype detect
  silent file foo.nonsense.jinja
  filetype detect

Then:
  AssertEqual 'jinja', &filetype


Execute (Add new file type where necessary):
  silent file foo.nonsense.jinja
  filetype detect
  silent file foo.html.jinja
  filetype detect

Then:
  AssertEqual 'html.jinja', &filetype


Execute (change file type when necessary):
  silent file foo.html.jinja
  filetype detect
  silent file foo.xml.jinja
  filetype detect

Then:
  AssertEqual 'xml.jinja', &filetype