diff --git a/indent/vue.vim b/indent/vue.vim index e68e8bf..37dd436 100644 --- a/indent/vue.vim +++ b/indent/vue.vim @@ -19,12 +19,14 @@ function! s:Init() " To adjust HTML let s:empty_tagname = '(area|base|br|col|embed|hr|input|img|' \.'keygen|link|meta|param|source|track|wbr)' - let s:empty_tag = '\v\<'.s:empty_tagname.'[^/]*\>' - let s:empty_tag_start = '\v\<'.s:empty_tagname.'[^\>]*$' - let s:empty_tag_end = '\v^\s*[^\<\>\/]*\/?\>\s*' + let s:empty_tag = '\v\<'.s:empty_tagname.'.*(/)@' + let s:empty_tag_start = '\v\<'.s:empty_tagname.'[^>]*$' + let s:empty_tag_end = '\v^\s*[^<>/]*\/?\>\s*' let s:tag_start = '\v^\s*\<\w*' " < let s:tag_end = '\v^\s*\/?\>\s*' " /> let s:full_tag_end = '\v^\s*\<\/' " + let s:ternary_q = '^\s\+?' + let s:ternary_e = '^\s\+:.*,\s*$' endfunction function! s:SetVueIndent() @@ -122,6 +124,8 @@ function! s:AdjustBlockIndent(syntax, ind) let ind = s:AdjustHTMLIndent(ind) elseif syntax == 'javascript' let ind = s:AdjustJavaScriptIndent(ind) + elseif syntax == 'css' + let ind = s:AdjustCSSIndent(ind) endif return ind @@ -194,18 +198,38 @@ function! s:AdjustHTMLIndent(ind) endif endif - " Indent for multiline array/object in attribute like v-*="[ + " Multiline array/object in attribute like v-*="[ " ... " ] - if prevline =~ '"[[{]\s*$' + if prevline =~ '[[{]\s*$' call vue#LogWithLnum('previous line is an open bracket') let ind = indent(prevlnum) + &sw endif - if curline =~ '^\s*[]}][^"]*"\s*$' + if curline =~ '^\s*[]}][^"]*"\?\s*$' call vue#LogWithLnum('current line is a closing bracket') let ind = indent(prevlnum) - &sw endif + " Multiline ternary 'a ? b : c' in attribute + if curline =~ s:ternary_q + call vue#LogWithLnum('current line is ?...') + let ind = indent(prevlnum) + &sw + endif + if curline =~ s:ternary_e && prevline =~ s:ternary_q + call vue#LogWithLnum('current line is :...') + let ind = indent(prevlnum) + endif + if prevline =~ s:ternary_e + call vue#LogWithLnum('prevline line is :...') + let ind = indent(prevlnum) - &sw + endif + + " Angle bracket in attribute, like v-if="isEnabled('item.')" + if prevline =~ '="[^"]*<[^"]*>[^"]*"' + call vue#LogWithLnum('prevline line is angle bracket in attribute') + let ind = ind - &sw + endif + return ind endfunction @@ -220,10 +244,31 @@ function! s:AdjustJavaScriptIndent(ind) let ind = indent(prevlnum) + &sw endif - if prevline =~ ':.*\s=>\s.*,\s*$' && curline !~ '^\s*}\s*$' + if prevline =~ '\s=>\s.*,\s*$' && curline !~ '^\s*[]}])\?,\?\s*$' call vue#LogWithLnum('previous line is arrow function property') let ind = indent(prevlnum) endif + if prevline =~ '\s||\s*$' + call vue#LogWithLnum('previous line ends with "||"') + let ind = indent(prevlnum) + &sw + endif + return ind +endfunction + +function! s:AdjustCSSIndent(ind) + let ind = a:ind + let prevlnum = prevnonblank(v:lnum - 1) + let prevline = getline(prevlnum) + let curline = getline(v:lnum) + + if prevline =~ ':\s.*,\s*$' + call vue#LogWithLnum('previous line is css function') + let ind = indent(prevlnum) + &sw + endif + if curline =~ '^\s*);\?\s*$' + call vue#LogWithLnum('curline is closing round bracket') + let ind = indent(prevlnum) - &sw + endif return ind endfunction diff --git a/lib/indent/typescript.vim b/lib/indent/typescript.vim index 28b122c..4cabafb 100644 --- a/lib/indent/typescript.vim +++ b/lib/indent/typescript.vim @@ -60,6 +60,7 @@ let s:msl_exclude_regex = '++' let s:one_line_scope_regex = '\<\%(if\|else\|for\|while\)\>[^{;]*' . s:line_term let s:one_line_scope_exclue_regex = '\' +let s:closing_bracket = '[]}],\?\s*$' " Regex that defines blocks. let s:block_regex = '\%([{[]\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s:line_term @@ -124,6 +125,10 @@ endfunction function s:GetMSL(lnum, in_one_line_scope) " Start on the line we're at and use its indent. let msl = a:lnum + if s:Match(msl, s:closing_bracket) + return msl + endif + let lnum = s:PrevNonBlankNonString(a:lnum - 1) while lnum > 0 " If we have a continuation line, or we're in a string, use line as MSL. diff --git a/syntax/patch/html.vim b/syntax/patch/html.vim index 37bb917..e5b7527 100644 --- a/syntax/patch/html.vim +++ b/syntax/patch/html.vim @@ -64,7 +64,9 @@ syntax match VueKey contained '\vwx[^\=]+' syntax match VueCustomTag containedin=htmlTagN '\v<(view|text|block|image)>' syntax cluster simpleJavascriptExpression - \ contains=javaScriptStringS,javaScriptStringD,javaScriptTemplateString,javascriptNumber,javaScriptOperator + \ contains=javaScriptStringS,javaScriptStringD + \,javaScriptTemplateString,javascriptNumber + \,javaScriptOperator,htmlJavaScriptOperator " JavaScript syntax syntax region javaScriptStringS @@ -80,9 +82,9 @@ syntax region javaScriptTemplateExpression \ contains=@simpleJavascriptExpression syntax match javaScriptNumber '\v<-?\d+L?>|0[xX][0-9a-fA-F]+>' contained -syntax match javaScriptOperator '[-!|&+<>=%*~^]' contained -syntax match javaScriptOperator '\v(*)@=%*~^]' contained +syntax match htmlJavaScriptOperator '\v(*)@