chore: improve code quality

This commit is contained in:
yemai
2019-05-21 11:35:27 +08:00
parent 8480810231
commit 6020f11232
3 changed files with 140 additions and 108 deletions

View File

@@ -7,10 +7,3 @@ function! s:setFiletype()
" then set filetype
let &filetype = 'vue'
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

View File

@@ -8,92 +8,162 @@
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists("b:did_vue_indent")
finish
finish
endif
se sw=2 ts=2
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Variables
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
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")
\ && g:vim_vue_plugin_debug == 1
let s:use_pug = exists("g:vim_vue_plugin_use_pug")
\ && g:vim_vue_plugin_use_pug == 1
let s:use_sass = exists("g:vim_vue_plugin_use_sass")
\ && g:vim_vue_plugin_use_sass == 1
let s:has_init_indent = exists("g:vim_vue_plugin_has_init_indent")
\ && g:vim_vue_plugin_has_init_indent == 1
let s:has_init_indent = 0
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)'
let s:vue_end_tag = '\v\<\/(template|script|style)'
let s:end_tag = '^\s*\/\?>\s*'
" Load javascript indent method
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Load indent method
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
unlet! b:did_indent
runtime! indent/javascript.vim
" Save the current JavaScript indentexpr.
let b:javascript_indentexpr = &indentexpr
" Load xml indent method
unlet! b:did_indent
runtime! indent/xml.vim
" Load css indent method
unlet! b:did_indent
runtime! indent/css.vim
" Load pug indent method
if s:use_pug
unlet! b:did_indent
runtime! indent/pug.vim
endif
" Load sass indent method
if s:use_sass
unlet! b:did_indent
runtime! indent/sass.vim
endif
let b:did_indent = 1
let b:did_vue_indent = 1
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Settings
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
setlocal sw=2 ts=2
" JavaScript indentkeys
setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e
" XML indentkeys
setlocal indentkeys+=*<Return>,<>>,<<>,/
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)
let lnum = prevnonblank(a:lnum)
let col = strlen(getline(lnum))
return map(synstack(lnum, col), 'synIDattr(v:val, "name")')
endfunction
function! s:SynsHTML(syns)
let first_syn = get(a:syns, 0)
return first_syn =~? '\v^(vueTemplate)'
function! s:SynHTML(syn)
return a:syn =~? '\v^(vueTemplate)'
endfunction
function! s:SynsPug(syns)
let first_syn = get(a:syns, 0)
return first_syn =~? '\v^(vueTemplatePug)'
function! s:SynPug(syn)
return a:syn =~? '\v^(vueTemplatePug)'
endfunction
function! s:SynsSASS(syns)
let first_syn = get(a:syns, 0)
return first_syn =~? '\v^(vueStyleSASS)'
function! s:SynSASS(syn)
return a:syn =~? '\v^(vueStyleSASS)'
endfunction
function! s:SynsCSS(syns)
let first_syn = get(a:syns, 0)
return first_syn =~? '\v^(vueStyle)'
function! s:SynCSS(syn)
return a:syn =~? '\v^(vueStyle)'
endfunction
function! s:SynsVueScope(syns)
let first_syn = get(a:syns, 0)
return first_syn =~? '\v^(vueStyle)|(vueScript)'
function! s:SynVueScope(syn)
return a:syn =~? '\v^(vueStyle)|(vueScript)'
endfunction
function! GetVueCurrentTag()
@@ -114,61 +184,12 @@ function! GetVueCurrentTag()
return tag
endfunction
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)
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)
function! LogMsg(msg)
echom 'debug'
if s:debug
echom '['.s:name.'] '.a:msg
endif
endfunction
let b:did_indent = 1
let b:did_vue_indent = 1

View File

@@ -11,6 +11,11 @@ if exists("b:current_syntax") && b:current_syntax == 'vue'
finish
endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Config
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let s:load_full_syntax = exists("g:vim_vue_plugin_load_full_syntax")
\ && g:vim_vue_plugin_load_full_syntax == 1
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")
\ && g:vim_vue_plugin_use_sass == 1
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Functions
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! s:LoadSyntax(group, type)
if s:load_full_syntax
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'
endfunction
"""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Main
" Load main syntax
"
"""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Load syntax/*.vim to syntax group
call s:LoadSyntax('@HTMLSyntax', 'html')
@@ -58,11 +68,11 @@ if hlexists('jsNoise') == 0
call s:LoadSyntax('@jsAll', 'javascript')
endif
"""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" pre-processors
" Load pre-processors syntax
"
"""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" If pug is enabled, load vim-pug syntax
if s:use_pug
call s:LoadFullSyntax('@PugSyntax', 'pug')
@@ -87,9 +97,11 @@ if s:use_sass || s:use_less
\ end="}"
endif
let b:current_syntax = 'vue'
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" Syntax highlight
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Find tag <template> / <script> / <style> and enable currespond syntax
syn region vueTemplate
\ start=+<template\(\s.\{-}\)\?>+
@@ -122,8 +134,12 @@ syn region vueStyleSCSS
\ end=+</style>+
\ keepend contains=@SassSyntax,vueTag
syn region vueTag contained start=+<[^/]+ end=+>+ contains=htmlTagN,htmlString,htmlArg fold
syn region vueTag contained start=+</+ end=+>+ contains=htmlTagN,htmlString,htmlArg
syn region vueTag
\ 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
" Vue attributes should color as JS. Note the trivial end pattern; we let
@@ -133,3 +149,5 @@ syn region xmlString
\ contained contains=jsBlock,javascriptBlock
hi def link vueTag htmlTag
let b:current_syntax = 'vue'