📄 jsformatter.java
字号:
} if (!(isInComment || isLineComment) && line.regionMatches(false, i, "\\\\", 0, 2)) { outBuffer.append("\\\\"); i++; continue; } if (!(isInComment || isLineComment) && ch == '\\') { isSpecialChar = true; outBuffer.append(ch); continue; } // handle quotes (such as 'x' and "Hello Dolly") if (ch == '"' || ch == '\'') if (!isInQuote) { quoteChar = ch; isInQuote = true; } else if (quoteChar == ch) { isInQuote = false; outBuffer.append(ch); continue; } if (isInQuote) { outBuffer.append(ch); continue; } // handle parenthesies if (ch == '(' || ch == '[' || ch == ')' || ch == ']') { if (ch == '(' || ch == '[') parenDepth++; else if (ch == ')' || ch == ']') parenDepth--; if (parenDepth == 0 && isParenNeeded) { isParenNeeded = false; checkBlockOpen = true; } //outBuffer.append(ch); //continue; } //don't do special parsing as long as parenthesies are open... /* if (parenDepth != 0) { outBuffer.append(ch); continue; } */ /* if (isNewLineNeeded && !isParenNeeded) { isNewLineNeeded = false; i--; isBreakCalled = true; continue; } */ if (prevCh == ' ') { boolean foundHeader = false; for (int h = 0; h < headers.length; h++) { if (line.regionMatches(false, i, headers[h], 0, headers[h].length())) { int lineLength = line.length(); int headerEnd = i + headers[h].length(); char endCh = 0; if (headerEnd < lineLength) endCh = line.charAt(headerEnd); if ((headerEnd > lineLength) || (endCh >= 'a' && endCh <= 'z') || (endCh >= 'A' && endCh <= 'Z') || (endCh >= '0' && endCh <= '9')) break; foundHeader = true; outBuffer.append(headers[h]); i += headers[h].length() - 1; if ("else".equals(headers[h])) checkIf = true; checkBlockOpen = true; isNewLineNeeded = true; isBlockNeeded = false; openingStack.push(headers[h]); appendSpace(outBuffer); ; //outBuffer.append(' '); ch = ' '; int p; for (p = 0; p < parenHeaders.length; p++) if (headers[h].equals(parenHeaders[p])) { isParenNeeded = true; break; } break; } } if (foundHeader) { isInHeader = true; continue; } } if (ch == '?') isInQuestion = true; if (ch == ':')//isInCase) { if (isInQuestion) isInQuestion = false; else { //isInCase = false; outBuffer.append(ch); isBreakCalled = true; continue; } } if (ch == ';' && parenDepth == 0) { outBuffer.append(ch); isBreakCalled = true; continue; } if (ch == '{') { if (!(bracketBreak && isInBracketOpen)) { boolean isBlockOpener = false; // first, check if '{' is a block-opener or an static-array opener isBlockOpener |= (prevNonSpaceCh == '{' && ((Boolean) bracketBlockStateStack.peek()). booleanValue()); isBlockOpener |= (prevNonSpaceCh == ')' || prevNonSpaceCh == ';'); isBlockOpener |= isInClassStatement; isBlockOpener |= (prevNonSpaceCh == ':' && !isInQuestion); isInClassStatement = false; if (!isBlockOpener && currentHeader != null) { for (int n = 0; n < nonParenHeaders.length; n++) if (currentHeader.equals(nonParenHeaders[n])) { isBlockOpener = true; break; } } bracketBlockStateStack.push(new Boolean(isBlockOpener)); if (!isBlockOpener) { outBuffer.append('{'); continue; } } // if I have reached here, then I am in a block... if (bracketBreak) { if (isInBracketOpen) { isInBracketOpen = false; } else { isInBracketOpen = true; isBreakCalled = true; i--; break; } } checkBlockClose = true; int lastBufCharPlace = outBuffer.length() - 1; if ((lastBufCharPlace >= 0) && (outBuffer.charAt(lastBufCharPlace) != ' ')) appendSpace(outBuffer); //outBuffer.append(' '); outBuffer.append('{'); openingStack.push("{"); //checkBlockClose = true; parenDepthsStack.push(new Integer(parenDepth)); parenDepth = 0; continue; } if (ch == '}') { // first check if this '}' closes a previous block, or a static array... if (!((Boolean) bracketBlockStateStack.pop()).booleanValue()) { outBuffer.append(ch); continue; } if (!parenDepthsStack.isEmpty()) parenDepth = ((Integer) parenDepthsStack.pop()).intValue(); outBuffer.append(ch); checkClosingHeader = true; continue; } if (prevCh == ' ') { boolean foundHeader = false; for (int h = 0; h < statementHeaders.length; h++) { if (line.regionMatches(false, i, statementHeaders[h], 0, statementHeaders[h].length())) { int lineLength = line.length(); int headerEnd = i + statementHeaders[h].length(); char endCh = 0; if (headerEnd < lineLength) endCh = line.charAt(headerEnd); if ((headerEnd > lineLength) || (endCh >= 'a' && endCh <= 'z') || (endCh >= 'A' && endCh <= 'Z') || (endCh >= '0' && endCh <= '9')) break; isInClassStatement = true; break; } } } if (prevCh == ' ' && line.regionMatches(false, i, "return", 0, 6)) { int lineLength = line.length(); int headerEnd = i + 6; char endCh = 0; if (headerEnd < lineLength) endCh = line.charAt(headerEnd); if (!((headerEnd > lineLength) || (endCh >= 'a' && endCh <= 'z') || (endCh >= 'A' && endCh <= 'Z') || (endCh >= '0' && endCh <= '9'))) { outBuffer.append("return"); i += 5; currentNonSpaceCh = '-'; // treat 'return' a an operator. continue; } } // add space when a non-operator follows a closing parenthesis if ((prevNonSpaceCh == ')' || prevNonSpaceCh == ']') && Character.isLetterOrDigit(ch) && ch != '.' && ch != '_' && ch != '$' && ch != '(' && ch != '[' && ch != ')' && ch != ']') appendSpace(outBuffer); if ((!Character.isLetterOrDigit(ch) && ch != '.' && ch != '_' && ch != '$' && ch != '(' && ch != '[' && ch != ')' && ch != ']') && (Character.isLetterOrDigit(prevNonSpaceCh) || prevNonSpaceCh == '.' || prevNonSpaceCh == '_' || prevNonSpaceCh == '$' || prevNonSpaceCh == ')' || prevNonSpaceCh == ']')) { boolean isLongOperator = false; String longOperator = null; for (int l = 0; l < longOperators.length; l++) { if (line.regionMatches(false, i, longOperators[l], 0, longOperators[l].length())) { isLongOperator = true; longOperator = longOperators[l]; break; } } if (isLongOperator) { if (!"--".equals(longOperator) && !"++".equals(longOperator) && !".*".equals(longOperator)) { appendSpace(outBuffer); outBuffer.append(longOperator); appendSpace(outBuffer); ch = ' '; } else { outBuffer.append(longOperator); currentNonSpaceCh = '0'; // hide the operator } i += 1; // since all long operators are 2 chars long... } else if (!(ch == '*' && prevNonSpaceCh == '.'))// not '.*' { if (ch != ',' && ch != ';')// && ch != ')' && ch != ']') appendSpace(outBuffer); outBuffer.append(ch); appendSpace(outBuffer); ch = ' '; } else outBuffer.append(ch); continue; } if (ch == ')' || ch == ']') clearPaddingSpace(outBuffer); // default outBuffer.append(ch); } try { tempLine = line.substring(i + (i < line.length() ? 1 : 0)); } catch (Exception e) { /*is this exception really needed??? - check if the above ?: solves the problem...*/ tempLine = ""; } if (isBreakCalled || isInComment || isLineComment || shouldPublish) { outString = outBuffer.toString(); outBuffer.setLength(0); } if (outString != null && !"".equals(outString)) outString = beautifier.beautify(outString); else if (ch != '[' && ch != ']' && ch != '(' && ch != ')' && ch != '.' && ch != '_' && ch != '$') appendSpace(outBuffer); //outBuffer.append(" ");/* if (outString != null && !"".equals(outString)) { if (isInComment && !isLineComment) { System.err.println("In Block Comment: " + outString); StringBuffer s = new StringBuffer(outString); if ("\t".equals(beautifier.getIndentString())) { int p = outString.indexOf("\t *"); if (p > -1) { s.delete(p, p + 4); s.insert(p, "\t *"); } } outString = s.toString(); System.err.println("After replace: " + outString); } }*/ return outString; } private void appendSpace(StringBuffer buf) { if (buf.length() == 0 || buf.charAt(buf.length() - 1) != ' ') buf.append(' '); } private void clearPaddingSpace(StringBuffer buf) { int bufLength = buf.length(); if (bufLength != 0 && buf.charAt(bufLength - 1) == ' ') buf.setLength(bufLength - 1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -