Enforce order of pre-processor syntax files

The plugin's current behavior is reliant on the order of loading
pre-processor syntax files. In particular, Vader tests are failing for
SCSS snippets; SASS syntax identifiers are applied instead.

Unfortunately, no ordering information can be embedded in a Dictionary,
so s:language_config must be turned into a List.

In the unconfigured case (no g:vue_disable_pre_processors, no
g:vue_pre_processors), pre_processors can be initialized to a
well-ordered List of languages (ordered as they appear in
s:language_config), instead of the unstable keys() on a dict.

Random access of language config is faster from a dict, though, so a
dict is rebuilt in s:language_dict. Going through the s:language_config
List for every pre-processor in pre_processors is O(m * n), as opposed
to building s:language_dict once then looping through pre-processors
once for O(m + n) (since dict access is constant time).
This commit is contained in:
Ian Emnace
2019-03-19 14:50:15 +08:00
parent 6f476f3b90
commit 033df1b169

View File

@@ -42,30 +42,33 @@ function! s:register_language(language, tag, ...)
endif
endfunction
let s:language_config = {
\ 'less': ['less', 'style'],
\ 'pug': ['pug', 'template', s:attr('lang', '\%(pug\|jade\)')],
\ 'slm': ['slm', 'template'],
\ 'handlebars': ['handlebars', 'template'],
\ 'haml': ['haml', 'template'],
\ 'typescript': ['typescript', 'script', '\%(lang=\("\|''\)[^\1]*\(ts\|typescript\)[^\1]*\1\|ts\)'],
\ 'coffee': ['coffee', 'script'],
\ 'stylus': ['stylus', 'style'],
\ 'sass': ['sass', 'style'],
\ 'scss': ['scss', 'style'],
\ }
let s:language_config = [
\ {'lang': 'less', 'args': ['less', 'style']},
\ {'lang': 'pug', 'args': ['pug', 'template', s:attr('lang', '\%(pug\|jade\)')]},
\ {'lang': 'slm', 'args': ['slm', 'template']},
\ {'lang': 'handlebars', 'args': ['handlebars', 'template']},
\ {'lang': 'haml', 'args': ['haml', 'template']},
\ {'lang': 'typescript', 'args': ['typescript', 'script', '\%(lang=\("\|''\)[^\1]*\(ts\|typescript\)[^\1]*\1\|ts\)']},
\ {'lang': 'coffee', 'args': ['coffee', 'script']},
\ {'lang': 'stylus', 'args': ['stylus', 'style']},
\ {'lang': 'sass', 'args': ['sass', 'style']},
\ {'lang': 'scss', 'args': ['scss', 'style']},
\ ]
let s:language_dict = {}
for item in s:language_config
let s:language_dict[item.lang] = item.args
endfor
if !exists("g:vue_disable_pre_processors") || !g:vue_disable_pre_processors
if exists("g:vue_pre_processors")
let pre_processors = g:vue_pre_processors
else
let pre_processors = keys(s:language_config)
let pre_processors = map(copy(s:language_config), {k, v -> v.lang})
endif
for language in pre_processors
if has_key(s:language_config, language)
call call("s:register_language", get(s:language_config, language))
if has_key(s:language_dict, language)
call call("s:register_language", get(s:language_dict, language))
endif
endfor
endif