mirror of
https://github.com/leafOfTree/vim-vue-plugin.git
synced 2025-12-08 05:34:44 +08:00
chore: improve code quality
This commit is contained in:
@@ -7,10 +7,3 @@ function! s:setFiletype()
|
|||||||
" then set filetype
|
" then set filetype
|
||||||
let &filetype = 'vue'
|
let &filetype = 'vue'
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
if !exists("g:vim_vue_plugin_has_init_indent")
|
|
||||||
let ext = expand("%:e")
|
|
||||||
if ext == 'wpy'
|
|
||||||
let g:vim_vue_plugin_has_init_indent = 1
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
|
|||||||
199
indent/vue.vim
199
indent/vue.vim
@@ -11,89 +11,159 @@ if exists("b:did_vue_indent")
|
|||||||
finish
|
finish
|
||||||
endif
|
endif
|
||||||
|
|
||||||
se sw=2 ts=2
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
|
"
|
||||||
|
" Variables
|
||||||
|
"
|
||||||
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
let s:name = 'vim-vue-plugin'
|
let s:name = 'vim-vue-plugin'
|
||||||
|
" Let <template> handled by HTML
|
||||||
|
let s:vue_tag = '\v^\<(script|style)'
|
||||||
|
let s:vue_end_tag = '\v\<\/(template|script|style)'
|
||||||
|
let s:end_tag = '^\s*\/\?>\s*'
|
||||||
|
|
||||||
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
|
"
|
||||||
|
" Config
|
||||||
|
"
|
||||||
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
let s:debug = exists("g:vim_vue_plugin_debug")
|
let s:debug = exists("g:vim_vue_plugin_debug")
|
||||||
\ && g:vim_vue_plugin_debug == 1
|
\ && g:vim_vue_plugin_debug == 1
|
||||||
let s:use_pug = exists("g:vim_vue_plugin_use_pug")
|
let s:use_pug = exists("g:vim_vue_plugin_use_pug")
|
||||||
\ && g:vim_vue_plugin_use_pug == 1
|
\ && g:vim_vue_plugin_use_pug == 1
|
||||||
let s:use_sass = exists("g:vim_vue_plugin_use_sass")
|
let s:use_sass = exists("g:vim_vue_plugin_use_sass")
|
||||||
\ && g:vim_vue_plugin_use_sass == 1
|
\ && g:vim_vue_plugin_use_sass == 1
|
||||||
let s:has_init_indent = exists("g:vim_vue_plugin_has_init_indent")
|
let s:has_init_indent = 0
|
||||||
\ && g:vim_vue_plugin_has_init_indent == 1
|
if !exists("g:vim_vue_plugin_has_init_indent")
|
||||||
|
let ext = expand("%:e")
|
||||||
|
if ext == 'wpy'
|
||||||
|
let s:has_init_indent = 1
|
||||||
|
endif
|
||||||
|
elseif g:vim_vue_plugin_has_init_indent == 1
|
||||||
|
let s:has_init_indent = 1
|
||||||
|
endif
|
||||||
|
|
||||||
" Let <template> handled by HTML indent
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
let s:vue_tag = '\v^\<(script|style)'
|
"
|
||||||
|
" Load indent method
|
||||||
let s:vue_end_tag = '\v\<\/(template|script|style)'
|
"
|
||||||
let s:end_tag = '^\s*\/\?>\s*'
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
|
|
||||||
" Load javascript indent method
|
|
||||||
unlet! b:did_indent
|
unlet! b:did_indent
|
||||||
runtime! indent/javascript.vim
|
runtime! indent/javascript.vim
|
||||||
" Save the current JavaScript indentexpr.
|
|
||||||
let b:javascript_indentexpr = &indentexpr
|
let b:javascript_indentexpr = &indentexpr
|
||||||
|
|
||||||
" Load xml indent method
|
|
||||||
unlet! b:did_indent
|
unlet! b:did_indent
|
||||||
runtime! indent/xml.vim
|
runtime! indent/xml.vim
|
||||||
|
|
||||||
" Load css indent method
|
|
||||||
unlet! b:did_indent
|
unlet! b:did_indent
|
||||||
runtime! indent/css.vim
|
runtime! indent/css.vim
|
||||||
|
|
||||||
" Load pug indent method
|
|
||||||
if s:use_pug
|
if s:use_pug
|
||||||
unlet! b:did_indent
|
unlet! b:did_indent
|
||||||
runtime! indent/pug.vim
|
runtime! indent/pug.vim
|
||||||
endif
|
endif
|
||||||
|
|
||||||
" Load sass indent method
|
|
||||||
if s:use_sass
|
if s:use_sass
|
||||||
unlet! b:did_indent
|
unlet! b:did_indent
|
||||||
runtime! indent/sass.vim
|
runtime! indent/sass.vim
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let b:did_indent = 1
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
let b:did_vue_indent = 1
|
"
|
||||||
|
" Settings
|
||||||
|
"
|
||||||
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
|
setlocal sw=2 ts=2
|
||||||
" JavaScript indentkeys
|
" JavaScript indentkeys
|
||||||
setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e
|
setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e
|
||||||
" XML indentkeys
|
" XML indentkeys
|
||||||
setlocal indentkeys+=*<Return>,<>>,<<>,/
|
setlocal indentkeys+=*<Return>,<>>,<<>,/
|
||||||
|
|
||||||
setlocal indentexpr=GetVueIndent()
|
setlocal indentexpr=GetVueIndent()
|
||||||
|
|
||||||
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
|
"
|
||||||
|
" Functions
|
||||||
|
"
|
||||||
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
|
function! GetVueIndent()
|
||||||
|
let curline = getline(v:lnum)
|
||||||
|
let prevline = getline(v:lnum - 1)
|
||||||
|
let cursyns = s:SynsEOL(v:lnum)
|
||||||
|
let prevsyns = s:SynsEOL(v:lnum - 1)
|
||||||
|
let cursyn = get(cursyns, 0)
|
||||||
|
let prevsyn = get(prevsyns, 0)
|
||||||
|
|
||||||
|
if s:SynPug(prevsyn)
|
||||||
|
call LogMsg('syntax: pug')
|
||||||
|
let ind = GetPugIndent()
|
||||||
|
elseif s:SynHTML(prevsyn)
|
||||||
|
call LogMsg('syntax: html')
|
||||||
|
let ind = XmlIndentGet(v:lnum, 0)
|
||||||
|
|
||||||
|
" Align '/>' and '>' with '<' for multiline tags.
|
||||||
|
if curline =~? s:end_tag
|
||||||
|
let ind = ind - &sw
|
||||||
|
endif
|
||||||
|
" Then correct the indentation of any element following '/>' or '>'.
|
||||||
|
if prevline =~? s:end_tag
|
||||||
|
let ind = ind + &sw
|
||||||
|
endif
|
||||||
|
elseif s:SynSASS(prevsyn)
|
||||||
|
call LogMsg('syntax: sass')
|
||||||
|
let ind = GetSassIndent()
|
||||||
|
elseif s:SynCSS(prevsyn)
|
||||||
|
call LogMsg('syntax: css')
|
||||||
|
let ind = GetCSSIndent()
|
||||||
|
else
|
||||||
|
call LogMsg('syntax: javascript')
|
||||||
|
if len(b:javascript_indentexpr)
|
||||||
|
let ind = eval(b:javascript_indentexpr)
|
||||||
|
else
|
||||||
|
let ind = cindent(v:lnum)
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
if curline =~? s:vue_tag || curline =~? s:vue_end_tag
|
||||||
|
call LogMsg('current line is vue tag')
|
||||||
|
let ind = 0
|
||||||
|
elseif s:has_init_indent
|
||||||
|
if s:SynVueScope(cursyn) && ind == 0
|
||||||
|
call LogMsg('add initial indent')
|
||||||
|
let ind = &sw
|
||||||
|
endif
|
||||||
|
elseif prevline =~? s:vue_tag || prevline =~? s:vue_end_tag
|
||||||
|
call LogMsg('prev line is vue tag')
|
||||||
|
let ind = 0
|
||||||
|
endif
|
||||||
|
|
||||||
|
call LogMsg('result indent: '.ind)
|
||||||
|
return ind
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! s:SynsEOL(lnum)
|
function! s:SynsEOL(lnum)
|
||||||
let lnum = prevnonblank(a:lnum)
|
let lnum = prevnonblank(a:lnum)
|
||||||
let col = strlen(getline(lnum))
|
let col = strlen(getline(lnum))
|
||||||
return map(synstack(lnum, col), 'synIDattr(v:val, "name")')
|
return map(synstack(lnum, col), 'synIDattr(v:val, "name")')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:SynsHTML(syns)
|
function! s:SynHTML(syn)
|
||||||
let first_syn = get(a:syns, 0)
|
return a:syn =~? '\v^(vueTemplate)'
|
||||||
return first_syn =~? '\v^(vueTemplate)'
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:SynsPug(syns)
|
function! s:SynPug(syn)
|
||||||
let first_syn = get(a:syns, 0)
|
return a:syn =~? '\v^(vueTemplatePug)'
|
||||||
return first_syn =~? '\v^(vueTemplatePug)'
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:SynsSASS(syns)
|
function! s:SynSASS(syn)
|
||||||
let first_syn = get(a:syns, 0)
|
return a:syn =~? '\v^(vueStyleSASS)'
|
||||||
return first_syn =~? '\v^(vueStyleSASS)'
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:SynsCSS(syns)
|
function! s:SynCSS(syn)
|
||||||
let first_syn = get(a:syns, 0)
|
return a:syn =~? '\v^(vueStyle)'
|
||||||
return first_syn =~? '\v^(vueStyle)'
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:SynsVueScope(syns)
|
function! s:SynVueScope(syn)
|
||||||
let first_syn = get(a:syns, 0)
|
return a:syn =~? '\v^(vueStyle)|(vueScript)'
|
||||||
return first_syn =~? '\v^(vueStyle)|(vueScript)'
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! GetVueCurrentTag()
|
function! GetVueCurrentTag()
|
||||||
@@ -114,61 +184,12 @@ function! GetVueCurrentTag()
|
|||||||
return tag
|
return tag
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! GetVueIndent()
|
function! LogMsg(msg)
|
||||||
let curline = getline(v:lnum)
|
echom 'debug'
|
||||||
let prevline = getline(v:lnum - 1)
|
|
||||||
let cursyns = s:SynsEOL(v:lnum)
|
|
||||||
let prevsyns = s:SynsEOL(v:lnum - 1)
|
|
||||||
|
|
||||||
if s:SynsPug(prevsyns)
|
|
||||||
call s:LogMsg('syntax: pug')
|
|
||||||
let ind = GetPugIndent()
|
|
||||||
elseif s:SynsHTML(prevsyns)
|
|
||||||
call s:LogMsg('syntax: html')
|
|
||||||
let ind = XmlIndentGet(v:lnum, 0)
|
|
||||||
|
|
||||||
" Align '/>' and '>' with '<' for multiline tags.
|
|
||||||
if curline =~? s:end_tag
|
|
||||||
let ind = ind - &sw
|
|
||||||
endif
|
|
||||||
" Then correct the indentation of any element following '/>' or '>'.
|
|
||||||
if prevline =~? s:end_tag
|
|
||||||
let ind = ind + &sw
|
|
||||||
endif
|
|
||||||
elseif s:SynsSASS(prevsyns)
|
|
||||||
call s:LogMsg('syntax: sass')
|
|
||||||
let ind = GetSassIndent()
|
|
||||||
elseif s:SynsCSS(prevsyns)
|
|
||||||
call s:LogMsg('syntax: css')
|
|
||||||
let ind = GetCSSIndent()
|
|
||||||
else
|
|
||||||
call s:LogMsg('syntax: javascript')
|
|
||||||
if len(b:javascript_indentexpr)
|
|
||||||
let ind = eval(b:javascript_indentexpr)
|
|
||||||
else
|
|
||||||
let ind = cindent(v:lnum)
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
|
|
||||||
if curline =~? s:vue_tag || curline =~? s:vue_end_tag
|
|
||||||
call s:LogMsg('current line is vue tag')
|
|
||||||
let ind = 0
|
|
||||||
elseif s:has_init_indent
|
|
||||||
if s:SynsVueScope(cursyns) && ind == 0
|
|
||||||
call s:LogMsg('add initial indent')
|
|
||||||
let ind = &sw
|
|
||||||
endif
|
|
||||||
elseif prevline =~? s:vue_tag || prevline =~? s:vue_end_tag
|
|
||||||
call s:LogMsg('prev line is vue tag')
|
|
||||||
let ind = 0
|
|
||||||
endif
|
|
||||||
|
|
||||||
call s:LogMsg('result indent: '.ind)
|
|
||||||
return ind
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! s:LogMsg(msg)
|
|
||||||
if s:debug
|
if s:debug
|
||||||
echom '['.s:name.'] '.a:msg
|
echom '['.s:name.'] '.a:msg
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
let b:did_indent = 1
|
||||||
|
let b:did_vue_indent = 1
|
||||||
|
|||||||
@@ -11,6 +11,11 @@ if exists("b:current_syntax") && b:current_syntax == 'vue'
|
|||||||
finish
|
finish
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
|
"
|
||||||
|
" Config
|
||||||
|
"
|
||||||
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
let s:load_full_syntax = exists("g:vim_vue_plugin_load_full_syntax")
|
let s:load_full_syntax = exists("g:vim_vue_plugin_load_full_syntax")
|
||||||
\ && g:vim_vue_plugin_load_full_syntax == 1
|
\ && g:vim_vue_plugin_load_full_syntax == 1
|
||||||
let s:use_pug = exists("g:vim_vue_plugin_use_pug")
|
let s:use_pug = exists("g:vim_vue_plugin_use_pug")
|
||||||
@@ -20,6 +25,11 @@ let s:use_less = exists("g:vim_vue_plugin_use_less")
|
|||||||
let s:use_sass = exists("g:vim_vue_plugin_use_sass")
|
let s:use_sass = exists("g:vim_vue_plugin_use_sass")
|
||||||
\ && g:vim_vue_plugin_use_sass == 1
|
\ && g:vim_vue_plugin_use_sass == 1
|
||||||
|
|
||||||
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
|
"
|
||||||
|
" Functions
|
||||||
|
"
|
||||||
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
function! s:LoadSyntax(group, type)
|
function! s:LoadSyntax(group, type)
|
||||||
if s:load_full_syntax
|
if s:load_full_syntax
|
||||||
call s:LoadFullSyntax(a:group, a:type)
|
call s:LoadFullSyntax(a:group, a:type)
|
||||||
@@ -40,11 +50,11 @@ function! s:LoadFullSyntax(group, type)
|
|||||||
exec 'syn include '.a:group.' syntax/'.a:type.'.vim'
|
exec 'syn include '.a:group.' syntax/'.a:type.'.vim'
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
"""""""""""""""""""""""""""""""""""""""""""""""
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
"
|
"
|
||||||
" Main
|
" Load main syntax
|
||||||
"
|
"
|
||||||
"""""""""""""""""""""""""""""""""""""""""""""""
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
" Load syntax/*.vim to syntax group
|
" Load syntax/*.vim to syntax group
|
||||||
call s:LoadSyntax('@HTMLSyntax', 'html')
|
call s:LoadSyntax('@HTMLSyntax', 'html')
|
||||||
|
|
||||||
@@ -58,11 +68,11 @@ if hlexists('jsNoise') == 0
|
|||||||
call s:LoadSyntax('@jsAll', 'javascript')
|
call s:LoadSyntax('@jsAll', 'javascript')
|
||||||
endif
|
endif
|
||||||
|
|
||||||
"""""""""""""""""""""""""""""""""""""""""""""""
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
"
|
"
|
||||||
" pre-processors
|
" Load pre-processors syntax
|
||||||
"
|
"
|
||||||
"""""""""""""""""""""""""""""""""""""""""""""""
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
" If pug is enabled, load vim-pug syntax
|
" If pug is enabled, load vim-pug syntax
|
||||||
if s:use_pug
|
if s:use_pug
|
||||||
call s:LoadFullSyntax('@PugSyntax', 'pug')
|
call s:LoadFullSyntax('@PugSyntax', 'pug')
|
||||||
@@ -87,9 +97,11 @@ if s:use_sass || s:use_less
|
|||||||
\ end="}"
|
\ end="}"
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
let b:current_syntax = 'vue'
|
"
|
||||||
|
" Syntax highlight
|
||||||
|
"
|
||||||
|
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
|
||||||
" Find tag <template> / <script> / <style> and enable currespond syntax
|
" Find tag <template> / <script> / <style> and enable currespond syntax
|
||||||
syn region vueTemplate
|
syn region vueTemplate
|
||||||
\ start=+<template\(\s.\{-}\)\?>+
|
\ start=+<template\(\s.\{-}\)\?>+
|
||||||
@@ -122,8 +134,12 @@ syn region vueStyleSCSS
|
|||||||
\ end=+</style>+
|
\ end=+</style>+
|
||||||
\ keepend contains=@SassSyntax,vueTag
|
\ keepend contains=@SassSyntax,vueTag
|
||||||
|
|
||||||
syn region vueTag contained start=+<[^/]+ end=+>+ contains=htmlTagN,htmlString,htmlArg fold
|
syn region vueTag
|
||||||
syn region vueTag contained start=+</+ end=+>+ contains=htmlTagN,htmlString,htmlArg
|
\ start=+<[^/]+ end=+>+
|
||||||
|
\ contained contains=htmlTagN,htmlString,htmlArg fold
|
||||||
|
syn region vueTag
|
||||||
|
\ start=+</+ end=+>+
|
||||||
|
\ contained contains=htmlTagN,htmlString,htmlArg
|
||||||
" syn keyword vueTagName containedin=htmlTagN template script style
|
" syn keyword vueTagName containedin=htmlTagN template script style
|
||||||
|
|
||||||
" Vue attributes should color as JS. Note the trivial end pattern; we let
|
" Vue attributes should color as JS. Note the trivial end pattern; we let
|
||||||
@@ -133,3 +149,5 @@ syn region xmlString
|
|||||||
\ contained contains=jsBlock,javascriptBlock
|
\ contained contains=jsBlock,javascriptBlock
|
||||||
|
|
||||||
hi def link vueTag htmlTag
|
hi def link vueTag htmlTag
|
||||||
|
|
||||||
|
let b:current_syntax = 'vue'
|
||||||
|
|||||||
Reference in New Issue
Block a user