From b29508797933fcac7613e382147e5ca8e8afa35f Mon Sep 17 00:00:00 2001 From: Jacobi Carter Date: Wed, 15 Aug 2012 20:04:58 -0500 Subject: [PATCH] Update javascript.vim --- vim/indent/javascript.vim | 466 ++++++++++++++++++++++---------------- 1 file changed, 271 insertions(+), 195 deletions(-) diff --git a/vim/indent/javascript.vim b/vim/indent/javascript.vim index fda205f..b0e190c 100644 --- a/vim/indent/javascript.vim +++ b/vim/indent/javascript.vim @@ -1,5 +1,4 @@ -" Vim indent file -" Language: JavaScript +" Vim indent file Language: JavaScript " Author: Preston Koprivica (pkopriv2@gmail.com) " URL: " Last Change: April 30, 2010 @@ -11,56 +10,55 @@ if exists('b:did_indent') finish endif + let b:did_indent = 1 " Set the global log variable 1 = logging enabled, 0 = logging disabled if !exists("g:js_indent_log") - let g:js_indent_log = 1 + let g:js_indent_log = 0 endif setlocal indentexpr=GetJsIndent(v:lnum) -setlocal indentkeys=0{,0},o,O,e,!,* +setlocal indentkeys= + + +setlocal cindent +setlocal autoindent + " 1. Variables " ============ - " Inline comments (for anchoring other statements) -let s:js_line_comment = '\s*\(//.*\)*' +let s:js_mid_line_comment = '\s*\(\/\*.*\*\/\)*\s*' +let s:js_end_line_comment = s:js_mid_line_comment . '\s*\(//.*\)*' +let s:js_line_comment = s:js_end_line_comment -" Simple Objects -let s:js_object_beg = '[{\[]\s*' -let s:js_object_end = '^[^][{}]*[}\]][;,]\=\s*' +" Comment/String Syntax Key +let s:syn_comment = '\(Comment\|String\|Regexp\)' -" Simple control blocks (those not beginngin with "{") -let s:js_s_cntrl_beg = '^\s*\(\(\(if\|for\|with\|while\)\s*(.*)\)\|\(try\|do\)\)\s*' -let s:js_s_cntrl_mid = '^\s*\(\(\(else\s*if\|catch\)\s*(.*)\)\|\(finally\|else\)\)\s*' - -" Multi line control blocks (those beginning with "{") -let s:js_m_cntrl_beg = s:js_s_cntrl_beg . '\s*{\s*' -let s:js_m_cntrl_mid = '}\=\s*' . s:js_s_cntrl_mid . '\s*{\s*' -let s:js_m_cntrl_end = '^[^{]*}\s*\(while\s*(.*)\)\=\s*;\=\s*' - -" Multi line declarations & invocations -let s:js_multi_beg = '([^()]*\s*' -let s:js_s_multi_end = '^[^()]*)\s*' -let s:js_m_multi_end = s:js_s_multi_end . '\s*{\s*' - -" Multi line invocation -let s:js_multi_invok_beg = s:js_multi_beg -let s:js_multi_invok_end = s:js_s_multi_end . '[;,]\{1}\s*' - -" Special switch control -let s:js_s_switch_beg = 'switch\s*(.*)\s*' "Actually not allowed. -let s:js_m_switch_beg = s:js_s_switch_beg . '\s*{\s*' -let s:js_switch_mid = '^.*\(case.*\|default\)\s*:\s*' - -" Single line comment (// xxx) -let s:syn_comment = '\(Comment\|String\)' " 2. Aux. Functions " ================= +" = Method: IsInComment +" +" Determines whether the specified position is contained in a comment. "Note: +" This depends on a +function! s:IsInComment(lnum, cnum) + return synIDattr(synID(a:lnum, a:cnum, 1), 'name') =~? s:syn_comment +endfunction + + +" = Method: IsComment +" +" Determines whether a line is a comment or not. +function! s:IsComment(lnum) + let line = getline(a:lnum) + + return s:IsInComment(a:lnum, 1) && s:IsInComment(a:lnum, strlen(line)) "Doesn't absolutely work. Only Probably! +endfunction + " = Method: GetNonCommentLine " @@ -79,29 +77,150 @@ function! s:GetNonCommentLine(lnum) return lnum endfunction - - -" = Method: IsInComment +" = Method: SearchForPair " -" Determines whether the specified position is contained in a comment. "Note: -" This depends on a -function! s:IsInComment(lnum, cnum) - return synIDattr(synID(a:lnum, a:cnum, 1), 'name') =~? s:syn_comment +" Returns the beginning tag of a given pair starting from the given line. +function! s:SearchForPair(lnum, beg, end) + " Save the cursor position. + let curpos = getpos(".") + + " Set the cursor position to the beginning of the line (default + " behavior when using ==) + call cursor(a:lnum, 0) + + " Search for the opening tag + let mnum = searchpair(a:beg, '', a:end, 'bW', + \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? s:syn_comment' ) + + "Restore the cursor position + call cursor(curpos) + + " Finally, return the matched line number + return mnum +endfunction + + +" Object Helpers +" ============== +let s:object_beg = '{[^}]*' . s:js_end_line_comment . '$' +let s:object_end = '^' . s:js_mid_line_comment . '}[;,]\=' + + +function! s:IsObjectBeg(line) + return a:line =~ s:object_beg +endfunction + +function! s:IsObjectEnd(line) + return a:line =~ s:object_end +endfunction + +function! s:GetObjectBeg(lnum) + return s:SearchForPair(a:lnum, '{', '}') +endfunction + + +" Array Helpers +" ============== +let s:array_beg = '\[[^\]]*' . s:js_end_line_comment . '$' +let s:array_end = '^' . s:js_mid_line_comment . '[^\[]*\][;,]*' . s:js_end_line_comment . '$' + + +function! s:IsArrayBeg(line) + return a:line =~ s:array_beg +endfunction + +function! s:IsArrayEnd(line) + return a:line =~ s:array_end +endfunction + +function! s:GetArrayBeg(lnum) + return s:SearchForPair(a:lnum, '\[', '\]') +endfunction + + +" MultiLine Declaration/Invocation Helpers +" ======================================== +let s:paren_beg = '([^)]*' . s:js_end_line_comment . '$' +let s:paren_end = '^' . s:js_mid_line_comment . '[^(]*)[;,]*' + +function! s:IsParenBeg(line) + return a:line =~ s:paren_beg +endfunction + +function! s:IsParenEnd(line) + return a:line =~ s:paren_end +endfunction + +function! s:GetParenBeg(lnum) + return s:SearchForPair(a:lnum, '(', ')') endfunction -" = Method: IsComment -" -" Determines whether a line is a comment or not. -function! s:IsComment(lnum) - let line = getline(a:lnum) +" Continuation Helpers +" ==================== +let s:continuation = '\(+\|\\\)\{1}' . s:js_line_comment . '$' - return s:IsInComment(a:lnum, 1) && s:IsInComment(a:lnum, strlen(line)) "Doesn't absolutely work. Only Probably! +function! s:IsContinuationLine(line) + return a:line =~ s:continuation endfunction +function! s:GetContinuationBegin(lnum) + let cur = a:lnum + + while s:IsContinuationLine(getline(cur)) + let cur -= 1 + endwhile + + return cur + 1 +endfunction +" Switch Helpers +" ============== +let s:switch_beg_next_line = 'switch\s*(.*)\s*' . s:js_mid_line_comment . s:js_end_line_comment . '$' +let s:switch_beg_same_line = 'switch\s*(.*)\s*' . s:js_mid_line_comment . '{\s*' . s:js_line_comment . '$' +let s:switch_mid = '^.*\(case.*\|default\)\s*:\s*' + +function! s:IsSwitchBeginNextLine(line) + return a:line =~ s:switch_beg_next_line +endfunction + +function! s:IsSwitchBeginSameLine(line) + return a:line =~ s:switch_beg_same_line +endfunction + +function! s:IsSwitchMid(line) + return a:line =~ s:switch_mid +endfunction + + +" Control Helpers +" =============== +let s:cntrl_beg_keys = '\(\(\(if\|for\|with\|while\)\s*(.*)\)\|\(try\|do\)\)\s*' +let s:cntrl_mid_keys = '\(\(\(else\s*if\|catch\)\s*(.*)\)\|\(finally\|else\)\)\s*' + +let s:cntrl_beg = s:cntrl_beg_keys . s:js_end_line_comment . '$' +let s:cntrl_mid = s:cntrl_mid_keys . s:js_end_line_comment . '$' + +let s:cntrl_end = '\(while\s*(.*)\)\s*;\=\s*' . s:js_end_line_comment . '$' + +function! s:IsControlBeg(line) + return a:line =~ s:cntrl_beg +endfunction + +function! s:IsControlMid(line) + return a:line =~ s:cntrl_mid +endfunction + +function! s:IsControlMidStrict(line) + return a:line =~ s:cntrl_mid +endfunction + +function! s:IsControlEnd(line) + return a:line =~ s:cntrl_end +endfunction + " = Method: Log " " Logs a message to the stdout. @@ -112,7 +231,6 @@ function! s:Log(msg) endfunction - " 3. Indenter " =========== function! GetJsIndent(lnum) @@ -121,7 +239,7 @@ function! GetJsIndent(lnum) " First line, start at indent = 0 if pnum == 0 - call s:Log("No, noncomment lines prior to: ") + call s:Log("No, noncomment lines prior to the current line.") return 0 endif @@ -140,72 +258,83 @@ function! GetJsIndent(lnum) " Determine the current level of indentation let ind = indent(pnum) - " Handle: Mutli-Line Block Invocation/Function Declaration - " ======================================================== - if pline =~ s:js_multi_beg . s:js_line_comment . '$' - if line !~ s:js_multi_invok_end - call s:Log("Pline matched multi invoke/declare") - return ind + &sw - endif - endif - if pline =~ s:js_s_multi_end . s:js_line_comment . '$' - call s:Log("Pline matched multi end without inline {") - if line =~ s:js_object_beg . s:js_line_comment . '$' - call s:Log("Line matched object beg") - return ind - &sw - else - call s:Log("line didn't match object beginning") - return ind - endif - endif - - if pline =~ s:js_m_multi_end . s:js_line_comment . '$' - call s:Log("Pline matched multi end with inline {") - if line =~ s:js_object_end . s:js_line_comment . '$' - call s:Log("Line matched object end") - return ind - &sw - else - call s:Log("Line didn't matched object end") - return ind - endif - endif - - if ppline =~ s:js_s_multi_end . s:js_line_comment . '$' && - \ pline !~ s:js_object_beg . s:js_line_comment . '$' - call s:Log("PPLine matched multi invoke/declaration end without inline {") - return ind - &sw - endif - - " Handle: Multi-Line Invocation + " Handle: Object Closers (ie }) " ============================= - if pline =~ s:js_multi_invok_beg . s:js_line_comment . '$' - call s:Log("PLine matched multi line invoke") - if line =~ s:js_multi_invok_end . s:js_line_comment . '$' - call s:Log("Pline matched multi line invoke end") - return ind - else - call s:Log("Pline didn't match multi line invoke end") - return ind + &sw - endif + if s:IsObjectEnd(line) && !s:IsComment(a:lnum) + call s:Log("Line matched object end") + + let obeg = s:GetObjectBeg(a:lnum) + let oind = indent(obeg) + let oline = getline(obeg) + + call s:Log("The object beg was found at: " . obeg) + return oind endif - if line =~ s:js_multi_invok_end . s:js_line_comment . '$' - call s:Log("Pline matched multi invocation end") + if s:IsObjectBeg(pline) + call s:Log("Pline matched object beg") + return ind + &sw + endif + + + " Handle: Array Closer (ie ]) + " ============================ + if s:IsArrayEnd(line) && !s:IsComment(a:lnum) + call s:Log("Line matched array end") + + let abeg = s:GetArrayBeg(a:lnum) + let aind = indent(abeg) + + call s:Log("The array beg was found at: " . abeg) + return aind + endif + + if s:IsArrayBeg(pline) + call s:Log("Pline matched array beg") + return ind + &sw + endif + + " Handle: Parens + " ============== + if s:IsParenEnd(line) && !s:IsComment(a:lnum) + call s:Log("Line matched paren end") + + let abeg = s:GetParenBeg(a:lnum) + let aind = indent(abeg) + + call s:Log("The paren beg was found at: " . abeg) + return aind + endif + + if s:IsParenBeg(pline) + call s:Log("Pline matched paren beg") + return ind + &sw + endif + + + " Handle: Continuation Lines. + " ======================================================== + if s:IsContinuationLine(pline) + call s:Log('Pline is a continuation line.') + + let cbeg = s:GetContinuationBegin(pnum) + let cind = indent(cbeg) + + call s:Log('The continuation block begin found at: ' . cbeg) + return cind + &sw + endif + + if s:IsContinuationLine(ppline) + call s:Log('PPline was a continuation line but pline wasnt.') return ind - &sw endif - " Handle: Switch Control Blocks " ============================= - if pline =~ s:js_m_switch_beg . s:js_line_comment . '$' - call s:Log("PLine matched switch cntrl beginning") - return ind - endif - - if pline =~ s:js_switch_mid + if s:IsSwitchMid(pline) call s:Log("PLine matched switch cntrl mid") - if line =~ s:js_switch_mid || line =~ s:js_object_end . s:js_line_comment . '$' + if s:IsSwitchMid(line) || s:IsObjectEnd(line) call s:Log("Line matched a cntrl mid") return ind else @@ -214,39 +343,49 @@ function! GetJsIndent(lnum) endif endif - if line =~ s:js_switch_mid " Doesn't need end anchor + if s:IsSwitchMid(line) call s:Log("Line matched switch cntrl mid") return ind - &sw endif + " Handle: Single Line Control Blocks - " ========================== - if pline =~ s:js_s_cntrl_beg . s:js_line_comment . '$' - call s:Log("Pline matched single line control beg") - if line =~ s:js_s_cntrl_mid. s:js_line_comment . '$' || line =~ s:js_object_beg. s:js_line_comment . '$' - call s:Log("Line matched single line control mid") + " ================================== + if s:IsControlBeg(pline) + call s:Log("Pline matched control beginning") + + if s:IsControlMid(line) + call s:Log("Line matched a control mid") + return ind + elseif line =~ '^\s*{\s*$' + call s:Log("Line matched an object beg") return ind else - call s:Log("Line didn't match single line control mid") + return ind + &sw + endif + + endif + + if s:IsControlMid(pline) + call s:Log("Pline matched a control mid") + + if s:IsControlMid(line) + call s:Log("Line matched a control mid") + return ind + elseif s:IsObjectBeg(line) + call s:Log("Line matched an object beg") + return ind + else + call s:Log("Line didn't match a control mid or object beg." return ind + &sw endif endif - if pline =~ s:js_s_cntrl_mid . s:js_line_comment . '$' - call s:Log("Pline matched single line control mid") - if line =~ s:js_s_cntrl_mid . s:js_line_comment . '$' || line =~ s:js_object_beg . s:js_line_comment . '$' - call s:Log("Line matched single line control mid") - return ind - else - call s:Log("Line didn't match single line control mid") - return ind + &sw - endif - endif + if s:IsControlMid(line) + call s:Log("Line matched a control mid.") - if line =~ s:js_s_cntrl_mid . s:js_line_comment . '$' - call s:Log("Line matched single line control mid") - if pline =~ s:js_m_cntrl_end . s:js_line_comment . '$' - call s:Log("PLine matched multi line control end") + if s:IsControlEnd(pline) || s:IsObjectEnd(pline) + call s:Log("PLine matched control end") return ind else call s:Log("Pline didn't match object end") @@ -254,78 +393,15 @@ function! GetJsIndent(lnum) endif endif - if ( ppline =~ s:js_s_cntrl_beg . s:js_line_comment . '$' || ppline =~ s:js_s_cntrl_mid . s:js_line_comment . '$' ) && - \ pline !~ s:js_object_beg . s:js_line_comment . '$' + + if ( s:IsControlBeg(ppline) || s:IsControlMid(ppline) ) && + \ !s:IsObjectBeg(pline) && !s:IsObjectEnd(pline) call s:Log("PPLine matched single line control beg or mid") return ind - &sw endif - - " Handle: {} - " ========== - if line =~ '^[^{]*}' && !s:IsComment(a:lnum) && line !~ '"[^}]*}[^}]*"' - call s:Log("Line matched closing bracket") - - " Save the cursor position. - let curpos = getpos(".") - - " Set the cursor position to the beginning of the line (default - " behavior when using ==) - call setpos(".", [0, a:lnum, 1, 0]) - - " Search for the opening tag - let mnum = searchpair('{', '', '}', 'bW', - \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? s:syn_comment' ) - - "Restore the cursor position - call setpos(".", curpos) - - let mind = indent(mnum) - let mline = getline(mnum) - - call s:Log("Matched found at: " . mnum) - - if mline =~ s:js_m_multi_end " Fixes multi line invocation - call s:Log("MLine matched multi line invocation") - return mind - &sw - else - return mind - endif - endif - - if pline =~ '{[^}]*$' && pline !~ '"[^{]*{[^{]*"' - call s:Log("Pline matched opening {") - return ind + &sw - endif - - " Handle: [] - " ========== - if line =~ '^[^\[]*\]' && !s:IsComment(a:lnum) && line !~ '"[^\]]*\][^\]]*"' - call s:Log("Line matched closing ]") - - " Save the cursor position. - let curpos = getpos(".") - - " Set the cursor position to the beginning of the line (default - " behavior when using ==) - call setpos(".", [0, a:lnum, 1, 0]) - - " Search for the opening tag - let mnum = searchpair('\[', '', '\]', 'bW', - \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? s:syn_comment' ) - - "Restore the cursor position - call setpos(".", curpos) - - call s:Log("Matched found at: " . mnum) - return indent(mnum) - endif - - if pline =~ '\[[^\]]*$' && pline !~ '"[^\[]*\[[^\[]*"' - call s:Log("Pline matched opening [") - return ind + &sw - endif - - call s:Log("Line didn't match anything. Retaining indent") + " Handle: No matches + " ================== + "call s:Log("Line didn't match anything. Retaining indent") return ind endfunction