diff --git a/plugin/jsbeautify.vim b/plugin/jsbeautify.vim deleted file mode 100644 index 52ba5a56..00000000 --- a/plugin/jsbeautify.vim +++ /dev/null @@ -1,625 +0,0 @@ -if &cp || exists("loaded_jsbeautify") - finish -endif -let loaded_jsbeautify = 3 - - - -function! s:trim_output() - while len(s:output) > 0 && (s:output[len(s:output)-1] == " " || s:output[len(s:output)-1] == s:indent_string) - call remove(s:output, -1) - endwhile -endfunction - -function! s:print_newline(ignore_repeated) - let s:if_line_flag = 0 - call s:trim_output() - if len(s:output)==0 - return - endif - if s:output[len(s:output)-1] != "\n" || !a:ignore_repeated - call add(s:output, "\n") - endif - let index = 0 - while index < s:indent_level - call add(s:output, s:indent_string) - let index += 1 - endwhile -endfunction - -function! s:print_space() - let last_output = " " - if len(s:output) > 0 - let last_output = s:output[len(s:output) - 1] - endif - if last_output != " " && last_output != "\n" && last_output != s:indent_string - call add(s:output, " ") - endif -endfunction - -function! s:print_token() - call add(s:output, s:token_text) -endfunctio - -function! s:indent() - let s:indent_level += 1 -endfunction - -function! s:unindent() - if s:indent_level - let s:indent_level -= 1 - endif -endfunction - -function! s:remove_indent() - if len(s:output)>0 && s:output[len(s:output) -1] == s:indent_string - call remove(s:output, -1) - endif -endfunction - -function! s:set_mode(mode) - call add(s:modes, s:current_mode) - let s:current_mode = a:mode -endfunction - -function! s:restore_mode() - if s:current_mode == "DO_BLOCK" - let s:do_block_just_closed = 1 - else - let s:do_block_just_closed = 0 - endif - let s:current_mode = remove(s:modes, -1) -endfunction - -function! s:in_array(what, arr) - return index(a:arr, a:what) != -1 -endfunction - -function! s:get_next_token() - let n_newlines = 0 - - if s:parser_pos >= len(s:input) - return ["", "TK_EOF"] - endif - - let c = s:input[s:parser_pos] - let s:parser_pos += 1 - - while s:in_array(c, s:whitespace) - if s:parser_pos >= len(s:input) - return ["", "TK_EOF"] - endif - - if c == "\n" - let n_newlines += 1 - endif - - let c = s:input[s:parser_pos] - let s:parser_pos += 1 - endwhile - - let wanted_newline = 0 - - if s:opt_preserve_newlines - if n_newlines > 1 - for i in [0, 1] - call s:print_newline(i==0) - endfor - endif - let wanted_newline = n_newlines == 1 - endif - - if s:in_array(c, s:wordchar) - if s:parser_pos < len(s:input) - while s:in_array(s:input[s:parser_pos], s:wordchar) - let c .= s:input[s:parser_pos] - let s:parser_pos += 1 - if s:parser_pos == len(s:input) - break - endif - endwhile - endif - - "if s:parser_pos != len(s:input) && c =~ /^[0-9]+[Ee]$/ && (s:input[s:parser_pos] == "-" || s:input[s:parser_pos] == "+") - "let sign = s:input[s:parser_pos] - "let s:parser_pos += 1 - - "let t = get_next_token(s:parser_pos) - "let c .= sign . t[0] - "return [c, "TK_WORD"] - " endif - - if c == "in" - return [c, "TK_OPERATOR"] - endif - if wanted_newline && s:last_type != "TK_OPERATOR" && !s:if_line_flag - call s:print_newline(1) - endif - return [c, "TK_WORD"] - endif - if c == "(" || c == "[" - return [c, "TK_START_EXPR"] - endif - - if c == ")" || c == "]" - return [c, "TK_END_EXPR"] - endif - - if c == "{" - return [c, "TK_START_BLOCK"] - endif - - if c == "}" - return [c, "TK_END_BLOCK"] - endif - - if c == ";" - return [c, "TK_SEMICOLON"] - endif - - if c == "/" - let comment = "" - if s:input[s:parser_pos] == "*" - let s:parser_pos += 1 - if s:parser_pos < len(s:input) - while !(s:input[s:parser_pos] == "*" && s:parser_pos + 1 < len(s:input) && s:input[s:parser_pos + 1] == "/" && s:parser_pos < len(s:input)) - let comment .= s:input[s:parser_pos] - let s:parser_pos += 1 - if s:parser_pos >= len(s:input) - break - endif - endwhile - endif - let s:parser_pos += 2 - return ['/*' . comment . '*/', 'TK_BLOCK_COMMENT'] - endif - - " peek for comment // ... - if s:input[s:parser_pos] == "/" - let comment = c - while s:input[s:parser_pos] != "\r" && s:input[s:parser_pos] != "\n" - let comment .= s:input[s:parser_pos] - let s:parser_pos += 1 - if s:parser_pos >= len(s:input) - break - endif - endwhile - let s:parser_pos += 1 - if wanted_newline - call s:print_newline(1) - endif - return [comment, "TK_COMMENT"] - endif - endif - - if c == "'" || c =='"' || (c == "/" && ((s:last_type == "TK_WORD" && s:last_text == "return") || (s:last_type == "TK_START_EXPR" || s:last_type == "TK_START_BLOCK" || s:last_type == "TK_END_BLOCK" || s:last_type == "TK_OPERATOR" || s:last_type == "TK_EOF" || s:last_type == "TK_SEMICOLON"))) - let sep = c - let esc = 0 - let resulting_string = c - - if s:parser_pos < len(s:input) - while esc || s:input[s:parser_pos] != sep - let resulting_string .= s:input[s:parser_pos] - if !esc - let esc = s:input[s:parser_pos] == "\\" - else - let esc = 0 - endif - let s:parser_pos += 1 - if s:parser_pos >= len(s:input) - return [resulting_string, "TK_STRING"] - endif - endwhile - endif - - let s:parser_pos += 1 - - let resulting_string .= sep - - if sep == "/" - - while s:parser_pos < len(s:input) && s:in_array(s:input[s:parser_pos], s:wordchar) - let resulting_string .= s:input[s:parser_pos] - let s:parser_pos += 1 - endwhile - endif - return [resulting_string, "TK_STRING"] - endif - - if c == "#" - let sharp = "#" - if s:parser_pos < len(s:input) && s:in_array(s:input[s:parser_pos], s:digits) - let c = s:input[s:parser_pos] - let sharp .= c - let s:parser_pos += 1 - - while s:parser_pos < len(s:input) && c != "#" && c !="=" - let c = s:input[s:parser_pos] - let sharp .= c - let s:parser_pos += 1 - endwhile - - if c == "#" - return [sharp, "TK_WORD"] - else - return [sharp, "TK_OPERATOR"] - endif - endif - endif - - if c == "<" && s:input[s:parser_pos-1 : s:parser_pos+3] == "" - let s:parser_pos += 2 - if wanted_newline - call s:print_newline(1) - endif - return ["-->", "TK_COMMENT"] - endif - - if s:in_array(c, s:punct) - while s:parser_pos < len(s:input) && s:in_array(c . s:input[s:parser_pos], s:punct) - let c .= s:input[s:parser_pos] - let s:parser_pos += 1 - if s:parser_pos >= len(s:input) - break - endif - endwhile - - return [c, "TK_OPERATOR"] - endif - - return [c, "TK_UNKNOWN"] - endif - - - -endfunction - -function! s:is_js() - " lilydjwg: 这样判断文件类型会更好 - return &ft == "javascript" || &ft == "json" - " return expand("%:e") == "js" -endfunction - -"function! g:Jsbeautify(js_source_text, options) -function! g:Jsbeautify() - if !s:is_js() - echo "Not a JS file." - return - endif - - "let a:options = {} - let s:opt_indent_size = 1 - let s:opt_indent_char = " " - let s:opt_preserve_newlines = 1 - let s:opt_indent_level = 0 - - let s:if_line_flag = 0 - "-------------------------------- - - let s:indent_string = "" - while s:opt_indent_size > 0 - let s:indent_string .= s:opt_indent_char - let s:opt_indent_size -= 1 - endwhile - - let s:indent_level = s:opt_indent_level - - let lines = getline(1, "$") - let s:input = join(lines, "\n") - "let s:input = a:js_source_text - - let s:last_word = "" "last 'TK_WORD' passed - let s:last_type = "TK_START_EXPR" "last token type - let s:last_text = "" "last token text - let s:output = [] - - let s:do_block_just_closed = 0 - let s:var_line = 0 - let s:var_line_tainted = 0 - - let s:whitespace = ["\n", "\r", "\t", " "] - let s:wordchar = split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$", '\zs') - let s:digits = split("0123456789", '\zs') - - "