Support vue3 API keywords

This commit is contained in:
leafOfTree
2021-08-13 16:29:57 +08:00
parent ab96ba2585
commit 8d464e9506
4 changed files with 118 additions and 96 deletions
+5 -84
View File
@@ -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 " Number with minus
syntax match javaScriptNumber '\v<-?\d+L?>|0[xX][0-9a-fA-F]+>' syntax match javaScriptNumber '\v<-?\d+L?>|0[xX][0-9a-fA-F]+>'
\ containedin=@javascript display \ containedin=@javascript display
highlight link javaScriptNumber Constant highlight link javaScriptNumber Constant
" Check if Vue keywords syntax are enabled let s:config = vue#GetConfig('config', {})
if !s:keyword | finish | endif let s:keyword = s:config.keyword
if s:keyword
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' source <sfile>:h/vue-keyword.vim
endif
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
+5
View File
@@ -0,0 +1,5 @@
let s:config = vue#GetConfig('config', {})
let s:keyword = s:config.keyword
if s:keyword
source <sfile>:h/vue-keyword.vim
endif
+83
View File
@@ -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
+25 -12
View File
@@ -2,11 +2,8 @@
" Maintainer: leaf <https://github.com/leafOfTree> " Maintainer: leaf <https://github.com/leafOfTree>
" Credits: Inspired by mxw/vim-jsx. " Credits: Inspired by mxw/vim-jsx.
if !exists('main_syntax') if exists('b:current_syntax') && b:current_syntax == 'vue'
if exists('b:current_syntax') && b:current_syntax == 'vue' finish
finish
endif
let main_syntax = 'vue'
endif endif
" <sfile> is replaced with the file name of the sourced file " <sfile> is replaced with the file name of the sourced file
@@ -194,23 +191,39 @@ function! s:SetIsKeyword()
endif endif
endfunction 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(...) function! VimVuePluginSyntaxMain(...)
call s:Init() call s:Init()
call s:SetBlockSyntax(s:config_syntax) call s:SetBlockSyntax(s:config_syntax)
let syntax_list = vue#GetSyntaxList(s:config_syntax) let syntax_list = vue#GetSyntaxList(s:config_syntax)
call s:LoadSyntaxList(syntax_list) call s:LoadSyntaxList(syntax_list)
call s:SetIsKeyword() call s:SetExtraSyntax()
call s:HighlightVue() call s:ClearTimerVariable()
endfunction endfunction
" Entry
let s:timer = exists('*timer_start') && !exists('SessionLoad') && !s:test let s:timer = exists('*timer_start') && !exists('SessionLoad') && !s:test
if s:timer if s:timer
call timer_start(1, 'VimVuePluginSyntaxMain') if !exists('s:main_timer')
let s:main_timer = timer_start(1, 'VimVuePluginSyntaxMain')
endif
else else
call VimVuePluginSyntaxMain() call VimVuePluginSyntaxMain()
endif endif
let b:current_syntax = 'vue' call s:SetCurrentSyntax()
if main_syntax == 'vue'
unlet main_syntax
endif