diff --git a/README b/README index ba8141c..902607b 100644 --- a/README +++ b/README @@ -2,8 +2,8 @@ This is a mirror of http://www.vim.org/scripts/script.php?script_id=4063 Another plugin to format your JavaScript source code -I modified the jsbeautify.vim as I need. (Original jsbeautify.vim is here: http://www.vim.org/scripts/script.php?script_id=2727 ) (changes happened on lines 1, 4, 532, 534, 538, 540 and 503) - +I modified the jsbeautify.vim as I need. (Original jsbeautify.vim is here: http://www.vim.org/scripts/script.php?script_id=2727 ) (changes happened on lines 1, 4, 532, 534, 538, 540 and 503(version 2.1)) +in version 2.1+  { a: 'a', @@ -57,7 +57,52 @@ var initRoleMutexGroupGrid = function() { }, RMGGridRowClick); // There won be a newline after } , when it's in an Expression. } ⇱ END - +------------------------------ +in versino 2.3+ +⇲ + var a = [[[{ + a: 'a' + }, { + A: 'A' + }], { + b: 'b' + ,B: 'B' + }], [{ + c: 'c' + }, { + C: 'C' + }], { + d: 'd' + }, { + D: 'D' + }] +⇱ +also good for indent fold method: +before 2.3 +⇲ +var a = { + columns:[{ + field:'A' + ,label:'A' + }, { + field:'B' + ,label:'B' + }] +} +⇱ +in version 2.3+ +⇲ +var a = { + columns:[{ + field:'A' + ,label:'A' + }, { + field:'B' + ,label:'B' + }] +} +⇱ +END This vim-script also works at a low performance:) diff --git a/plugin/_jsbeautify.vim b/plugin/_jsbeautify.vim index c73cb1f..4f64b93 100644 --- a/plugin/_jsbeautify.vim +++ b/plugin/_jsbeautify.vim @@ -1,7 +1,7 @@ if &cp || exists("loaded__jsbeautify") finish endif -let loaded__jsbeautify = 2.2 +let loaded__jsbeautify = 2.3.1 @@ -61,10 +61,23 @@ endfunction function! s:remove_indent() " if the last item of output (stringArray) is indent string, remove it. if len(s:output)>0 && s:output[len(s:output) -1] == s:indent_string + "unlet s:output[-1] call remove(s:output, -1) endif endfunction +function! s:remove_last_indent() + let i = len(s:output) + while i >= 0 + let i -= 1 + if s:output[i] == s:indent_string + "call remove(s:output, i) + unlet s:output[i] + break + endif + endwhile +endfunction + function! s:set_mode(mode) " push last mode into modes and update current mode. call add(s:modes, s:current_mode) let s:current_mode = a:mode @@ -146,14 +159,69 @@ function! s:get_next_token() " return next array of string and type. the string return [c, "TK_WORD"] endif if c == "(" || c == "[" + if c == "[" + call s:indent() + endif return [c, "TK_START_EXPR"] endif - + if c == "(" " prepare for next version + return [c,"TK_START_PARENTHESIS"] + endif + if c == "[" " prepare for next change + all s:indent() + return [c,"TK_START_BRACKET"] + endif if c == ")" || c == "]" + if c == "]" "for deindent the line ] is in + call s:unindent() + let i = len(s:output) + let ignore = 0 " if there's a [ match ], ignore the deindent + while i >= 0 + let i -= 1 + if s:output[i] == "[" + let ignore += 1 + elseif s:output[i] == "]" + let ignore -= 1 + elseif s:output[i] == s:indent_string + "call remove(s:output, i) + unlet s:output[i] + break + endif + if ignore == 1 + break + endif + endwhile + "call s:remove_last_indent() + endif return [c, "TK_END_EXPR"] endif + if c == ")" " prepare for next version + return [c, "TK_END_PARENTHESIS"] + endif + if c == "]" " prepare for nextversion + "for deindent the line ] is in + call s:unindent() + let i = len(s:output) + let ignore = 0 " if there's a [ match ], ignore the deindent + while i >= 0 + let i -= 1 + if s:output[i] == "[" + let ignore += 1 + elseif s:output[i] == "]" + let ignore -= 1 + elseif s:output[i] == s:indent_string + "call remove(s:output, i) + unlet s:output[i] + break + endif + if ignore == 1 + break + endif + endwhile + return [c,"TK_END_BRACKET"] + endif - if c == "{" + if c == "{" "BRACE return [c, "TK_START_BLOCK"] endif @@ -291,6 +359,10 @@ function! s:is_js() return expand("%:e") == "js" endfunction +function! s:currentMode() + return mode() +endfunction + "function! g:_Jsbeautify(js_source_text, options) function! g:_Jsbeautify() if !s:is_js() @@ -367,6 +439,7 @@ function! g:_Jsbeautify() call s:print_token() elseif s:token_type == "TK_END_EXPR" + "call s:remove_last_indent() call s:print_token() call s:restore_mode() elseif s:token_type == "TK_START_BLOCK" @@ -647,3 +720,4 @@ function! g:_Jsbeautify() endfunction nnoremap _ff :call g:_Jsbeautify() +vmap :call g:_Jsbeautify()