Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to version 2.3 #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 48 additions & 3 deletions README
Expand Up @@ -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',
Expand Down Expand Up @@ -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:)
Expand Down
80 changes: 77 additions & 3 deletions 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



Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -647,3 +720,4 @@ function! g:_Jsbeautify()
endfunction

nnoremap <silent> <leader>_ff :call g:_Jsbeautify()<cr>
vmap <F4> :call g:_Jsbeautify()<cr>