From dc42fd3306acdb477ed2c6dcc9e56e33c9357613 Mon Sep 17 00:00:00 2001 From: will Date: Wed, 13 Mar 2013 18:01:52 +0800 Subject: [PATCH 1/5] Update _jsbeautify from version 2.2 to 2.3 improved the format of the result: Example: in version 2.2 var a = [[[{ a: 'a' }, { A: 'A' }], { b: 'b' ,B: 'B' }], [{ c: 'c' }, { C: 'C' }], { d: 'd' }, { D: 'D' }] in this version 2.3: var a = [[[{ a: 'a' }, { A: 'A' }], { b: 'b' ,B: 'B' }], [{ c: 'c' }, { C: 'C' }], { d: 'd' }, { D: 'D' }] it's good for fold method in VIM when fold method's value is "indent" --- plugin/_jsbeautify.vim | 58 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/plugin/_jsbeautify.vim b/plugin/_jsbeautify.vim index c73cb1f..a5007a1 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 @@ -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,47 @@ function! s:get_next_token() " return next array of string and type. the string return [c, "TK_WORD"] endif if c == "(" || c == "[" + call s:indent() return [c, "TK_START_EXPR"] endif - + if c == "(" " prepare for next version + return [c,"TK_START_PARENTHESIS"] + endif + if c == "[" " prepare for next change + return [c,"TK_START_BRACKET"] + endif if c == ")" || c == "]" + call s:unindent() + if c == "]" "for deindent the line ] is in + 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 + return [c,"TK_END_BRACKET"] + endif - if c == "{" + if c == "{" "BRACE return [c, "TK_START_BLOCK"] endif @@ -291,6 +337,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 +417,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 +698,4 @@ function! g:_Jsbeautify() endfunction nnoremap _ff :call g:_Jsbeautify() +vmap :call g:_Jsbeautify() From af3cbcd6ba2f4b9942907cc59068cae451be5f5c Mon Sep 17 00:00:00 2001 From: will Date: Wed, 13 Mar 2013 18:04:21 +0800 Subject: [PATCH 2/5] Update README version 2.3 --- README | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README b/README index ba8141c..eefbf6e 100644 --- a/README +++ b/README @@ -57,7 +57,25 @@ 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' + }] +⇱ END This vim-script also works at a low performance:) From b0fa141f0762bc26f087737215379d604950f2a3 Mon Sep 17 00:00:00 2001 From: will Date: Thu, 14 Mar 2013 16:29:13 +0800 Subject: [PATCH 3/5] Update _jsbeautify.vim (function(){})(). may cause a little bug. fixed in this version. --- plugin/_jsbeautify.vim | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/plugin/_jsbeautify.vim b/plugin/_jsbeautify.vim index a5007a1..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.3 +let loaded__jsbeautify = 2.3.1 @@ -159,18 +159,21 @@ function! s:get_next_token() " return next array of string and type. the string return [c, "TK_WORD"] endif if c == "(" || c == "[" - call s:indent() + 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 == "]" - call s:unindent() 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 @@ -196,6 +199,25 @@ function! s:get_next_token() " return next array of string and type. the string 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 From 4d55ecb3944b738570ef076ac5dc60edd1d3090a Mon Sep 17 00:00:00 2001 From: will Date: Tue, 19 Mar 2013 10:26:50 +0800 Subject: [PATCH 4/5] Update README update README, specify the version of the change. --- README | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README b/README index eefbf6e..5bd04b3 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,6 +57,7 @@ var initRoleMutexGroupGrid = function() { }, RMGGridRowClick); // There won be a newline after } , when it's in an Expression. } ⇱ END +------------------------------ in versino 2.3+ ⇲ var a = [[[{ From 33b54c876ce0eaf36ea90c8e19f178d961c0bcb9 Mon Sep 17 00:00:00 2001 From: will Date: Tue, 19 Mar 2013 10:31:58 +0800 Subject: [PATCH 5/5] Update README add commit, in 2.3+ code format is more kind for :set foldmethod = indent --- README | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/README b/README index 5bd04b3..902607b 100644 --- a/README +++ b/README @@ -76,7 +76,33 @@ in versino 2.3+ }, { D: 'D' }] -⇱ END +⇱ +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:)