From ba9a3dbc4ed4dc2b75e2678955879492175a4223 Mon Sep 17 00:00:00 2001 From: Ian Emnace Date: Tue, 19 Mar 2019 13:44:46 +0800 Subject: [PATCH 1/3] Allow for configurable g:vue_pre_processors Prior to this commit, there was a single Boolean option, g:vue_disable_pre_processors, which would only activate either *every* pre-processor syntax file or none of them at all. This was a known pain point when it comes to performance. On some machines, loading all the pre-processor syntax files could slow down Vim noticeably, hence the need for such an option in the first place. However, turning all of them off means having to live with no syntax highlighting at all if one uses a pre-processor language. This commit introduces another option: g:vue_pre_processors. This is a List of names of pre-processor syntaxes, e.g. ['pug','scss']. If a user provides this option, only the named pre-processor syntax files will be loaded. This change still allows for g:vue_disable_pre_processors: If g:vue_disable_pre_processors is truthy, pre-processor syntax files aren't loaded regardless of the value of g:vue_pre_processors. --- syntax/vue.vim | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/syntax/vue.vim b/syntax/vue.vim index 3d3d514..4946b9d 100644 --- a/syntax/vue.vim +++ b/syntax/vue.vim @@ -42,17 +42,32 @@ 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'], + \ } + + if !exists("g:vue_disable_pre_processors") || !g:vue_disable_pre_processors - call s:register_language('less', 'style') - call s:register_language('pug', 'template', s:attr('lang', '\%(pug\|jade\)')) - call s:register_language('slm', 'template') - call s:register_language('handlebars', 'template') - call s:register_language('haml', 'template') - call s:register_language('typescript', 'script', '\%(lang=\("\|''\)[^\1]*\(ts\|typescript\)[^\1]*\1\|ts\)') - call s:register_language('coffee', 'script') - call s:register_language('stylus', 'style') - call s:register_language('sass', 'style') - call s:register_language('scss', 'style') + if exists("g:vue_pre_processors") + let pre_processors = g:vue_pre_processors + else + let pre_processors = keys(s:language_config) + endif + + for language in pre_processors + if has_key(s:language_config, language) + call call("s:register_language", get(s:language_config, language)) + endif + endfor endif syn region vueSurroundingTag contained start=+<\(script\|style\|template\)+ end=+>+ fold contains=htmlTagN,htmlString,htmlArg,htmlValue,htmlTagError,htmlEvent From 6f476f3b90c8fcdce15eb4591d324075eb853ccc Mon Sep 17 00:00:00 2001 From: Ian Emnace Date: Tue, 19 Mar 2019 13:52:19 +0800 Subject: [PATCH 2/3] Update readme for g:vue_pre_processors The change in commit ba9a3dbc4ed4dc2b75e2678955879492175a4223 is documented in the README, to augment the information regarding pre-processor languages and g:vue_disable_pre_processors. --- readme.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 1100754..9079ab9 100644 --- a/readme.md +++ b/readme.md @@ -128,5 +128,14 @@ endfunction ### _Vim slows down when using this plugin_ How can I fix that? +When checking for pre-processor languages, multiple syntax highlighting checks are done, which can slow down vim. You can trim down which pre-processors to use by setting `g:vue_pre_processors` to a whitelist of languages to support: -Add `let g:vue_disable_pre_processors=1` in your .vimrc to disable checking for prepocessors. When checking for preprocessor languages, multiple syntax highlighting checks are done, which can slow down vim. This variable prevents vim-vue from supporting **every** pre-processor language highlighting. +```vim +let g:vue_pre_processors = ['pug', 'scss'] +``` + +Or alternatively, disable pre-processor languages altogether: + +```vim +let g:vue_disable_pre_processors = 1 +``` From 033df1b169d9b48ba9a1d760b309bbaab79fd635 Mon Sep 17 00:00:00 2001 From: Ian Emnace Date: Tue, 19 Mar 2019 14:50:15 +0800 Subject: [PATCH 3/3] 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). --- syntax/vue.vim | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/syntax/vue.vim b/syntax/vue.vim index 4946b9d..ab43aed 100644 --- a/syntax/vue.vim +++ b/syntax/vue.vim @@ -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