📄 jsformatter.java
字号:
* formatted lines with the method nextFormattedLine(), i.e: * * while (formatter.hasMoreFormattedLines()) * System.out.println(formatter.nextFormattedLine() */ public boolean hasMoreFormattedLines() { if (lineBreaker.hasMoreBrokenLines()) return true; else while ((!isSummarized && !isNewLineRequested()) || (isSummarized && hasMoreSummarizedLines())) { String formatResult = format(null); if (formatResult != null) { lineBreaker.breakLine(formatResult); return true; } } return false; } /** * format a line of source code. formatLine should NOT be called * if there are still formatted lines ready to be collected. * This can be checked with the method hasMoreFormattedLines() * * @param line a line of source code to be formatted. */ public void formatLine(String line) { String formatResult = format(line); if (formatResult != null) lineBreaker.breakLine(formatResult); } /** * Get the next formatted line. This should be called ONLY after * checking with the method hasMoreFormattedLines() that there * actualy is a formatted line ready to be collected. */ public String nextFormattedLine() { return lineBreaker.nextBrokenLine(); } /** * summarize() is to be called when there are no more lines * of unformatted source code to be passed to the formatter. */ public void summarize() { formatLine(""); isSummarized = true; } public void setBracketBreak(boolean br) { bracketBreak = br; } public void setBracketIndent(boolean state) { beautifier.setBracketIndent(state); } public void setSwitchIndent(boolean state) { beautifier.setSwitchIndent(state); } public void setPreferredLineLength(int length) { lineBreaker.setPreferredLineLength(length); } public void setLineLengthDeviation(int dev) { lineBreaker.setLineLengthDeviation(dev); } public void setNestedConnection(boolean nest) { lineBreaker.setNestedConnection(nest); } /* * Does the formatter request a new line? * This should be checked only if we have a new line to * actually give the formatter via the format(line) method. */ private boolean isNewLineRequested() { return (tempLine.indexOf("//") == -1 && tempLine.indexOf("/*") == -1 && tempLine.indexOf("*/") == -1); } /* * Does formatter have more formatted lines in its belly? * This should be called only after there are no more original * lines to send the formatter. * Until false, the new formatted lines can be retreived by * calling the format method with an empty string, i.e. format(""); */ private boolean hasMoreSummarizedLines() { return !((tempLine.length() == 0) || (tempLine.length() == 2 && tempLine.charAt(0) == '\r' && tempLine.charAt(1) == '\n')); } /* * Format the original line sent. * Actually, the returned String is the next parsed line that is ready, * and may be a part of a formerly sent original line */ public String format(String line) { boolean isLineComment = false; // true when the current character is in a // comment (such as this line ...) char ch = ' '; // the current character char prevCh = ' '; String outString = null; int i; boolean shouldPublish = false; boolean isBreakCalled = false; currentHeader = null; // connect new unparsed line to former unparsed line. if (line == null) line = ""; else { // remove the white-space around the current line if (!isInComment) { leadingWhiteSpaces = 0; while (leadingWhiteSpaces < line.length() && (line.charAt(leadingWhiteSpaces) == ' ' || line.charAt(leadingWhiteSpaces) == '\t')) leadingWhiteSpaces++; line = line.trim(); } else { int trimSize; for (trimSize = 0; trimSize < line.length() && trimSize < leadingWhiteSpaces && (line.charAt(trimSize) == ' ' || line.charAt(trimSize) == '\t'); trimSize++) ; line = line.substring(trimSize); } //line = line.trim(); if ("".equals(line)) line = "\n"; } line = tempLine + " \r" + line; // parse characters in the current line. for (i = 0; i < line.length(); i++) { prevCh = ch; ch = line.charAt(i); //shouldPublish = false; if (!isInComment && !isLineComment && ch == '\t') ch = ' '; // '\n' exists when an empty line has been sent if (ch == '\n') { /*if (checkClosingHeader) { isDoubleBreak = true; //checkClosingHeader = false; } */ //if (foundOrigLineBreak) // foundOrigLineBreak = false; isBreakCalled = true; break; } // '\r' exists at the connection points between original lines if (ch == '\r') { //System.out.println("found \\r at:" + i +" out of " + line.length()); ch = ' '; if (isBreakCalled) break; else if (checkBlockClose) { checkBlockClose = false; isBreakCalled = true; break; } else { foundOrigLineBreak = true; continue; } } if (ch != ' ' && ch != '\t' && !isInComment && !isLineComment && !isInQuote && !line.regionMatches(false, i, "//", 0, 2) && !line.regionMatches(false, i, "/*", 0, 2)) { prevNonSpaceCh = currentNonSpaceCh; currentNonSpaceCh = ch; } // minimize white-space // and remove spaces that come right after parenthesies... if (!isInComment && !isLineComment && !isInQuote && ch == ' ') { if (currentNonSpaceCh != '(' && currentNonSpaceCh != ')' && currentNonSpaceCh != '[' && currentNonSpaceCh != ']') appendSpace(outBuffer); continue; } //if (!isInComment && !isInQuote && (ch == ' ' || ch == '\t')) //{ // if (prevCh != ' ' && prevCh != '\t') // outBuffer.append(ch); // continue; //} shouldPublish = false; // called specifically AFTER white space is treated. if (checkBlockClose) { checkBlockClose = false; if (ch != '}') isBreakCalled = true; } // handle comments if (!isInQuote && !(isInComment || isLineComment) && line.regionMatches(false, i, "//", 0, 2)) { if (foundOrigLineBreak) { foundOrigLineBreak = false; if (checkClosingHeader) { checkClosingHeader = false; i--; isBreakCalled = true; break; } } isLineComment = true; checkClosingHeader = false; outBuffer.append("//"); i++; continue; } else if (!isInQuote && !(isInComment || isLineComment) && line.regionMatches(false, i, "/*", 0, 2)) { if (foundOrigLineBreak) { foundOrigLineBreak = false; if (checkClosingHeader) { checkClosingHeader = false; i--; isBreakCalled = true; break; } } isInComment = true; outBuffer.append("/*"); i++; continue; } else if (!isInQuote && (isInComment || isLineComment) && line.regionMatches(false, i, "*/", 0, 2)) { isInComment = false; outBuffer.append("*/"); shouldPublish = true; i++; continue; } if (isInComment || isLineComment) { outBuffer.append(ch); if (outBuffer.toString().regionMatches(false, 0, "/*", 0, 2)) outBuffer.insert(0, ' '); continue; } // if we have reached here, then we are NOT in a comment if (isInHeader) { isInHeader = false; currentHeader = (String) openingStack.peek(); } else currentHeader = null; foundOrigLineBreak = false; if (isBreakCalled) { i--; break; } /**/ if (checkClosingHeader) { checkClosingHeader = false; if (bracketBreak) if (ch != ';') { i--; isBreakCalled = true; break; } else { i--; continue; } while (!"{".equals(openingStack.pop())) ; if (!openingStack.isEmpty()) { String openingHeader = (String) openingStack.peek(); String closingHeader = (String) closingHeaders.get(openingHeader); i--; if (closingHeader == null || !line.regionMatches(false, i + 1, closingHeader, 0, closingHeader.length())) { if (ch != ';') { outString = outBuffer.toString(); outBuffer.setLength(0); break; } else i++; } else { int lastBufCharPlace = outBuffer.length() - 1; if ((lastBufCharPlace >= 0) && (outBuffer.charAt(lastBufCharPlace) != ' ')) appendSpace(outBuffer);//outBuffer.append(' '); ch = ' '; openingStack.pop(); // pop the opening header continue; } } } /**/ if (checkIf) { checkIf = false; if (line.regionMatches(false, i, "if", 0, 2)) isNewLineNeeded = false; } if (!isParenNeeded && checkBlockOpen) { checkBlockOpen = false; if (ch == '{' || "static".equals(currentHeader)) isNewLineNeeded = false; } if (isNewLineNeeded && !isParenNeeded) { isNewLineNeeded = false; //if (!isParenNeeded) { i--; isBreakCalled = true; continue; } } // handle special characters (i.e. backslash+character such as \n, \t, ...) if (isSpecialChar) { outBuffer.append(ch); isSpecialChar = false; continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -