From 8d464e950664e944fdf6861d2c9b34e05c521c00 Mon Sep 17 00:00:00 2001 From: leafOfTree Date: Fri, 13 Aug 2021 16:29:57 +0800 Subject: [PATCH] Support vue3 API keywords --- syntax/patch/javascript.vim | 89 ++---------------------------------- syntax/patch/typescript.vim | 5 ++ syntax/patch/vue-keyword.vim | 83 +++++++++++++++++++++++++++++++++ syntax/vue.vim | 37 ++++++++++----- 4 files changed, 118 insertions(+), 96 deletions(-) create mode 100644 syntax/patch/typescript.vim create mode 100644 syntax/patch/vue-keyword.vim diff --git a/syntax/patch/javascript.vim b/syntax/patch/javascript.vim index e034f76..eecde5e 100644 --- a/syntax/patch/javascript.vim +++ b/syntax/patch/javascript.vim @@ -1,89 +1,10 @@ -""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" -" Config {{{ -" -""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -let s:config = vue#GetConfig('config', {}) -let s:keyword = s:config.keyword -let s:initial_indent = s:config.initial_indent - -let s:enable_initial_indent = count(s:initial_indent, 'script') - \ || count(s:initial_indent, 'javascript') - \ || count(s:initial_indent, 'script.javascript') -"}}} - -""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -" -" Syntax highlight {{{ -" -""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " Number with minus syntax match javaScriptNumber '\v<-?\d+L?>|0[xX][0-9a-fA-F]+>' \ containedin=@javascript display highlight link javaScriptNumber Constant -" Check if Vue keywords syntax are enabled -if !s:keyword | finish | endif - -let s:vue_keywords = 'name parent functional delimiters comments components directives filters extends mixins inheritAttrs model props propsData data methods template render renderError inject provide beforeCreate created beforeMount mounted beforeUpdate updated activated deactivated beforeDestroy destroyed beforeUnmount unmounted errorCaptured renderTracked renderTriggered' - -let s:indent = &sw * (1 + s:enable_initial_indent) -let s:keywords_regexp = '\v^\s{'.s:indent.'}(async )?<(' - \.join(split(s:vue_keywords, ' '), '|') - \.')\ze' - -" Currently support https://github.com/pangloss/vim-javascript -let s:useJavaScriptPlugin = hlexists('jsAsyncKeyword') -let s:containedin = s:useJavaScriptPlugin - \? 'jsObject,jsFuncBlock,@jsExpression' - \: 'javascriptScriptBlock' -let s:contains = s:useJavaScriptPlugin - \? 'jsAsyncKeyword' - \: 'javaScriptReserved' -let s:match_option = - \' containedin='.s:containedin - \.' contains='.s:contains - \.' skipwhite skipempty' - -execute 'syntax match vueObjectKey display /' - \.s:keywords_regexp - \.'\s*:/' - \.s:match_option - \.' nextgroup=jsObjectValue' - -execute 'syntax match vueObjectFuncName display /' - \.s:keywords_regexp - \.'\_s*\(/' - \.s:match_option - \.' nextgroup=jsFuncArgs' - -execute 'syntax match vueObjectFuncKey display /' - \.s:keywords_regexp - \.'\s*:\s*function>/' - \.s:match_option - \.' nextgroup=jsFuncArgs' - -" https://v3.vuejs.org/api/ -let s:basic_reactive = 'reactive readonly isProxy isReactive isReadonly toRaw markRaw shallowReactive shallowReadonly' -let s:refs = 'ref unref toRef toRefs isRef customRef shallowRef triggerRef' -let s:computed_and_watch = 'computed watchEffect watchPostEffect watchSyncEffect watch' -let s:composition = 'setup onBeforeMount onMounted onBeforeUpdate onUpdated onBeforeUnmount onUnmounted onErrorCaptured onRenderTracked onRenderTriggered onActivated onDeactivated getCurrentInstance InjectionKey provide inject' -let s:global = 'createApp h defineComponent defineAsyncComponent defineCustomElement resolveComponent resolveDynamicComponent resolveDirective withDirectives createRenderer nextTick mergeProps useCssModule' -let s:vue3_keywords = join([s:basic_reactive, s:refs, s:computed_and_watch, s:composition, s:global], ' ') - -let s:vue3_keywords_regexp = '\v<(' - \.join(split(s:vue3_keywords, ' '), '|') - \.')\ze' - -execute 'syntax match vue3Keyword display /' - \.s:vue3_keywords_regexp - \.'\_s*\(/' - \.s:match_option - -highlight default link vueObjectKey vueObjectKeyword -highlight default link vueObjectFuncName vueObjectKeyword -highlight default link vue3Keyword vueObjectKeyword -highlight default link vueObjectFuncKey vueObjectKeyword -highlight default link vueObjectKeyword Type -"}}} -" vim: fdm=marker +let s:config = vue#GetConfig('config', {}) +let s:keyword = s:config.keyword +if s:keyword + source :h/vue-keyword.vim +endif diff --git a/syntax/patch/typescript.vim b/syntax/patch/typescript.vim new file mode 100644 index 0000000..99c0970 --- /dev/null +++ b/syntax/patch/typescript.vim @@ -0,0 +1,5 @@ +let s:config = vue#GetConfig('config', {}) +let s:keyword = s:config.keyword +if s:keyword + source :h/vue-keyword.vim +endif diff --git a/syntax/patch/vue-keyword.vim b/syntax/patch/vue-keyword.vim new file mode 100644 index 0000000..e5922eb --- /dev/null +++ b/syntax/patch/vue-keyword.vim @@ -0,0 +1,83 @@ +function! s:GetIndent() + let config = vue#GetConfig('config', {}) + + let enable_initial_indent = 0 + for val in config.initial_indent + if val =~ 'script' + let enable_initial_indent = 1 + endif + endfor + + let indent = &sw * (1 + enable_initial_indent) + return indent +endfunction + +function! s:GetMatchOption() + " Currently support https://github.com/pangloss/vim-javascript + let useJavaScriptPlugin = hlexists('jsAsyncKeyword') + if useJavaScriptPlugin + let containedin = 'jsObject,jsFuncBlock,@jsExpression' + else + let containedin = '.*ScriptBlock' + endif + let containedin .= ',typescriptIdentifierName' + + let contains = useJavaScriptPlugin + \? 'jsAsyncKeyword' + \: 'javaScriptReserved' + let match_option = + \' containedin='.containedin + \.' contains='.contains + \.' skipwhite skipempty' + return match_option +endfunction + +" Vue keywords as option key +let s:vue_keywords = 'name parent functional delimiters comments components directives filters extends mixins inheritAttrs model props propsData data computed watch methods template render renderError inject provide beforeCreate created beforeMount mounted beforeUpdate updated activated deactivated beforeDestroy destroyed setup beforeUnmount unmounted errorCaptured renderTracked renderTriggered' + +let s:indent = s:GetIndent() +let s:keywords_regexp = '\v^\s{'.s:indent.'}(async )?<(' + \.join(split(s:vue_keywords, ' '), '|') + \.')\ze' +let s:match_option = s:GetMatchOption() + +execute 'syntax match vueObjectKey display /' + \.s:keywords_regexp + \.'\s*:/' + \.s:match_option + \.' nextgroup=jsObjectValue' + +execute 'syntax match vueObjectFuncName display /' + \.s:keywords_regexp + \.'\_s*\(/' + \.s:match_option + \.' nextgroup=jsFuncArgs' + +execute 'syntax match vueObjectFuncKey display /' + \.s:keywords_regexp + \.'\s*:\s*function>/' + \.s:match_option + \.' nextgroup=jsFuncArgs' + +" Vue3 keywords as API, https://v3.vuejs.org/api/ +let s:basic_reactive = 'reactive readonly isProxy isReactive isReadonly toRaw markRaw shallowReactive shallowReadonly' +let s:refs = 'ref unref toRef toRefs isRef customRef shallowRef triggerRef' +let s:computed_and_watch = 'computed watchEffect watchPostEffect watchSyncEffect watch' +let s:composition = 'setup onBeforeMount onMounted onBeforeUpdate onUpdated onBeforeUnmount onUnmounted onErrorCaptured onRenderTracked onRenderTriggered onActivated onDeactivated getCurrentInstance InjectionKey provide inject' +let s:global = 'createApp h defineComponent defineAsyncComponent defineCustomElement resolveComponent resolveDynamicComponent resolveDirective withDirectives createRenderer nextTick mergeProps useCssModule' +let s:vue3_keywords = join([s:basic_reactive, s:refs, s:computed_and_watch, s:composition, s:global], ' ') + +let s:vue3_keywords_regexp = '\v<(' + \.join(split(s:vue3_keywords, ' '), '|') + \.')\ze' + +execute 'syntax match vue3Keyword display /' + \.s:vue3_keywords_regexp + \.'\_s*\(/' + \.s:match_option + +highlight default link vueObjectKey vueObjectKeyword +highlight default link vueObjectFuncName vueObjectKeyword +highlight default link vue3Keyword vueObjectKeyword +highlight default link vueObjectFuncKey vueObjectKeyword +highlight default link vueObjectKeyword Type diff --git a/syntax/vue.vim b/syntax/vue.vim index 3a8a06d..f35ae71 100644 --- a/syntax/vue.vim +++ b/syntax/vue.vim @@ -2,11 +2,8 @@ " Maintainer: leaf " Credits: Inspired by mxw/vim-jsx. -if !exists('main_syntax') - if exists('b:current_syntax') && b:current_syntax == 'vue' - finish - endif - let main_syntax = 'vue' +if exists('b:current_syntax') && b:current_syntax == 'vue' + finish endif " is replaced with the file name of the sourced file @@ -194,23 +191,39 @@ function! s:SetIsKeyword() endif endfunction +function! s:SetCurrentSyntax() + let b:current_syntax = 'vue' +endfunction + +function! s:SetExtraSyntax() + call s:SetIsKeyword() + call s:HighlightVue() + call s:SetCurrentSyntax() +endfunction + +function! s:ClearTimerVariable() + if exists('s:main_timer') + unlet s:main_timer + endif +endfunction + function! VimVuePluginSyntaxMain(...) call s:Init() call s:SetBlockSyntax(s:config_syntax) let syntax_list = vue#GetSyntaxList(s:config_syntax) call s:LoadSyntaxList(syntax_list) - call s:SetIsKeyword() - call s:HighlightVue() + call s:SetExtraSyntax() + call s:ClearTimerVariable() endfunction +" Entry let s:timer = exists('*timer_start') && !exists('SessionLoad') && !s:test if s:timer - call timer_start(1, 'VimVuePluginSyntaxMain') + if !exists('s:main_timer') + let s:main_timer = timer_start(1, 'VimVuePluginSyntaxMain') + endif else call VimVuePluginSyntaxMain() endif -let b:current_syntax = 'vue' -if main_syntax == 'vue' - unlet main_syntax -endif +call s:SetCurrentSyntax()