mirror of
https://github.com/leafOfTree/vim-vue-plugin.git
synced 2025-12-08 13:44:46 +08:00
Support custom blocks syntax and indent
This commit is contained in:
64
syntax/vue-custom-blocks.vim
Normal file
64
syntax/vue-custom-blocks.vim
Normal file
@@ -0,0 +1,64 @@
|
||||
let s:custom_blocks = vue#GetConfig("custom_blocks", {})
|
||||
|
||||
if empty(s:custom_blocks)
|
||||
finish
|
||||
endif
|
||||
|
||||
function! s:LoadSyntax()
|
||||
let syntax_list = []
|
||||
for syntax in values(s:custom_blocks)
|
||||
let type = type(syntax)
|
||||
if type == v:t_string
|
||||
if !count(syntax_list, syntax)
|
||||
call add(syntax_list, syntax)
|
||||
endif
|
||||
elseif type == v:t_list && len(syntax)
|
||||
for syn in syntax
|
||||
if !count(syntax_list, syn)
|
||||
call add(syntax_list, syn)
|
||||
endif
|
||||
endfor
|
||||
else
|
||||
echoerr '[vim-vue-plugin] custom_blocks value type'
|
||||
\.' must be either string or list'
|
||||
endif
|
||||
endfor
|
||||
for syntax in syntax_list
|
||||
let syntaxGroup = '@'.syntax
|
||||
call vue#LoadFullSyntax(syntaxGroup, syntax)
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
function! s:SetSyntax(block, syntax, lang = 0)
|
||||
let block = a:block
|
||||
let syntax = a:syntax
|
||||
let lang = a:lang
|
||||
|
||||
let region_name = syntax.toupper(block[0]).block[1:].'Block'
|
||||
let syntax_lang = lang ? 'lang=["'']'.syntax.'["''][^>]*' : ''
|
||||
let start = '<'.block.'[^>]*'.syntax_lang.'>'
|
||||
let end = '</'.block.'>'
|
||||
let syntaxGroup = '@'.syntax
|
||||
|
||||
execute 'syntax region '.region_name.' fold matchgroup=vueTag'
|
||||
\.' start=+'.start.'+'
|
||||
\.' end=+'.end.'+'
|
||||
\.' keepend contains='.syntaxGroup
|
||||
endfunction
|
||||
|
||||
function! s:Highlight()
|
||||
for [block, syntax] in items(s:custom_blocks)
|
||||
let type = type(syntax)
|
||||
if type == v:t_string
|
||||
call s:SetSyntax(block, syntax)
|
||||
elseif type == v:t_list && len(syntax)
|
||||
call s:SetSyntax(block, syntax[0])
|
||||
for syn in syntax
|
||||
call s:SetSyntax(block, syn, 1)
|
||||
endfor
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
call s:LoadSyntax()
|
||||
call s:Highlight()
|
||||
@@ -18,7 +18,6 @@ let b:current_loading_main_syntax = 'vue'
|
||||
" Config {{{
|
||||
"
|
||||
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||
let s:load_full_syntax = vue#GetConfig("load_full_syntax", 0)
|
||||
let s:use_pug = vue#GetConfig("use_pug", 0)
|
||||
let s:use_less = vue#GetConfig("use_less", 0)
|
||||
let s:use_sass = vue#GetConfig("use_sass", 0)
|
||||
@@ -28,50 +27,6 @@ let s:use_coffee = vue#GetConfig("use_coffee", 0)
|
||||
let s:use_typescript = vue#GetConfig("use_typescript", 0)
|
||||
"}}}
|
||||
|
||||
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||
"
|
||||
" Functions {{{
|
||||
"
|
||||
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||
function! s:LoadSyntax(group, type)
|
||||
if s:load_full_syntax
|
||||
call s:LoadFullSyntax(a:group, a:type)
|
||||
else
|
||||
call s:LoadDefaultSyntax(a:group, a:type)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:LoadDefaultSyntax(group, type)
|
||||
unlet! b:current_syntax
|
||||
let syntaxPaths = ['$VIMRUNTIME', '$VIM/vimfiles', '$HOME/.vim']
|
||||
for path in syntaxPaths
|
||||
let file = expand(path).'/syntax/'.a:type.'.vim'
|
||||
if filereadable(file)
|
||||
execute 'syntax include '.a:group.' '.file
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
" Load all syntax files in 'runtimepath'
|
||||
" Useful if there is no default syntax file provided by vim
|
||||
function! s:LoadFullSyntax(group, type)
|
||||
call s:SetCurrentSyntax(a:type)
|
||||
execute 'syntax include '.a:group.' syntax/'.a:type.'.vim'
|
||||
endfunction
|
||||
|
||||
" Settings to avoid syntax overload
|
||||
function! s:SetCurrentSyntax(type)
|
||||
if a:type == 'coffee'
|
||||
syntax cluster coffeeJS contains=@htmlJavaScript
|
||||
|
||||
" Avoid overload of `javascript.vim`
|
||||
let b:current_syntax = 'vue'
|
||||
else
|
||||
unlet! b:current_syntax
|
||||
endif
|
||||
endfunction
|
||||
"}}}
|
||||
|
||||
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||
"
|
||||
" Load main syntax {{{
|
||||
@@ -80,17 +35,17 @@ endfunction
|
||||
|
||||
" Load syntax/html.vim to syntax group, which loads full JavaScript and CSS
|
||||
" syntax. It defines group @html, @htmlJavaScript, and @htmlCss.
|
||||
call s:LoadSyntax('@html', 'html')
|
||||
call vue#LoadSyntax('@html', 'html')
|
||||
|
||||
" Avoid overload
|
||||
if !hlexists('cssTagName')
|
||||
call s:LoadSyntax('@htmlCss', 'css')
|
||||
call vue#LoadSyntax('@htmlCss', 'css')
|
||||
endif
|
||||
|
||||
" Avoid overload
|
||||
if !hlexists('javaScriptComment')
|
||||
call vue#Log('load javascript cluster')
|
||||
call s:LoadSyntax('@htmlJavaScript', 'javascript')
|
||||
call vue#LoadSyntax('@htmlJavaScript', 'javascript')
|
||||
endif
|
||||
|
||||
" Load vue-html syntax
|
||||
@@ -107,43 +62,43 @@ runtime syntax/vue-javascript.vim
|
||||
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||
" If pug is enabled, load vim-pug syntax
|
||||
if s:use_pug
|
||||
call s:LoadFullSyntax('@PugSyntax', 'pug')
|
||||
call vue#LoadFullSyntax('@PugSyntax', 'pug')
|
||||
syn cluster htmlJavascript remove=javascriptParenthesisBlock
|
||||
endif
|
||||
|
||||
" If less is enabled, load less syntax
|
||||
if s:use_less
|
||||
call s:LoadSyntax('@LessSyntax', 'less')
|
||||
call vue#LoadSyntax('@LessSyntax', 'less')
|
||||
runtime! after/syntax/less.vim
|
||||
endif
|
||||
|
||||
" If sass is enabled, load sass syntax
|
||||
if s:use_sass
|
||||
call s:LoadSyntax('@SassSyntax', 'sass')
|
||||
call vue#LoadSyntax('@SassSyntax', 'sass')
|
||||
runtime! after/syntax/sass.vim
|
||||
endif
|
||||
|
||||
" If scss is enabled, load sass syntax
|
||||
if s:use_scss
|
||||
call s:LoadSyntax('@ScssSyntax', 'scss')
|
||||
call vue#LoadSyntax('@ScssSyntax', 'scss')
|
||||
runtime! after/syntax/scss.vim
|
||||
endif
|
||||
|
||||
" If stylus is enabled, load stylus syntax
|
||||
if s:use_stylus
|
||||
call s:LoadFullSyntax('@StylusSyntax', 'stylus')
|
||||
call vue#LoadFullSyntax('@StylusSyntax', 'stylus')
|
||||
runtime! after/syntax/stylus.vim
|
||||
endif
|
||||
|
||||
" If CoffeeScript is enabled, load the syntax. Keep name consistent with
|
||||
" vim-coffee-script/after/html.vim
|
||||
if s:use_coffee
|
||||
call s:LoadFullSyntax('@htmlCoffeeScript', 'coffee')
|
||||
call vue#LoadFullSyntax('@htmlCoffeeScript', 'coffee')
|
||||
endif
|
||||
|
||||
" If TypeScript is enabled, load the syntax.
|
||||
if s:use_typescript
|
||||
call s:LoadFullSyntax('@TypeScript', 'typescript')
|
||||
call vue#LoadFullSyntax('@TypeScript', 'typescript')
|
||||
endif
|
||||
"}}}
|
||||
|
||||
@@ -226,7 +181,14 @@ syntax region vueTag
|
||||
highlight default link vueTag htmlTag
|
||||
highlight default link cssUnitDecorators2 Number
|
||||
highlight default link cssKeyFrameProp2 Constant
|
||||
"}}}
|
||||
|
||||
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||
"
|
||||
" Custom blocks {{{
|
||||
"
|
||||
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||
runtime syntax/vue-custom-blocks.vim
|
||||
"}}}
|
||||
|
||||
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||
|
||||
Reference in New Issue
Block a user