private TextEdit internalFormatStatements(String source, int indentationLevel, String lineSeparator, ConstructorDeclaration constructorDeclaration, IRegion[] regions, boolean includeComments) { if (lineSeparator != null) { this.preferences.line_separator = lineSeparator; } else { this.preferences.line_separator = Util.LINE_SEPARATOR; } this.preferences.initial_indentation_level = indentationLevel; this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, includeComments); return this.newCodeFormatter.format(source, constructorDeclaration); }
private boolean isGuardClause(Block block) { return !commentStartsBlock(block.sourceStart, block.sourceEnd) && block.statements != null && block.statements.length == 1 && (block.statements[0] instanceof ReturnStatement || block.statements[0] instanceof ThrowStatement); }
return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameAND, scope); case OperatorIds.DIVIDE : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameDIVIDE, scope); case OperatorIds.GREATER : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameGREATER, scope); case OperatorIds.GREATER_EQUAL : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameGREATER_EQUAL, scope); case OperatorIds.LEFT_SHIFT : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameLEFT_SHIFT, scope); case OperatorIds.LESS : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameLESS, scope); case OperatorIds.LESS_EQUAL : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameLESS_EQUAL, scope); case OperatorIds.MINUS : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameMINUS, scope); case OperatorIds.MULTIPLY : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameMULTIPLY, scope); case OperatorIds.OR : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameOR, scope); case OperatorIds.PLUS : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNamePLUS, scope); case OperatorIds.REMAINDER : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameREMAINDER, scope); case OperatorIds.RIGHT_SHIFT : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameRIGHT_SHIFT, scope); case OperatorIds.UNSIGNED_RIGHT_SHIFT : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT, scope); case OperatorIds.XOR : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameXOR, scope);
manageOpeningParenthesizedExpression(messageSend, numberOfParens); CascadingMethodInvocationFragmentBuilder builder = buildFragments(messageSend, scope); formatCascadingMessageSends(builder, scope); } else { Alignment messageAlignment = null; formatMessageSend(messageSend, scope, messageAlignment); ok = true; } catch (AlignmentException e) { this.scribe.exitAlignment(messageAlignment, true); } else { formatMessageSend(messageSend, scope, null); manageClosingParenthesizedExpression(messageSend, numberOfParens);
manageOpeningParenthesizedExpression(qualifiedAllocationExpression, numberOfParens); if (isClosingGenericToken()) { this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_arguments); formatLeftCurlyBrace(line, this.preferences.brace_position_for_anonymous_type_declaration); formatAnonymousTypeDeclaration(anonymousType); manageClosingParenthesizedExpression(qualifiedAllocationExpression, numberOfParens);
final int numberOfParens = (parameterizedQualifiedTypeReference.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT; if (numberOfParens > 0) { manageOpeningParenthesizedExpression(parameterizedQualifiedTypeReference, numberOfParens); for (int i = 0; i < length; i++) { if (parameterizedQualifiedTypeReference.annotations != null) { formatInlineAnnotations(parameterizedQualifiedTypeReference.annotations[i], false); if (isClosingGenericToken()) { this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_parameterized_type_reference); formatLeadingDimensions(parameterizedQualifiedTypeReference); if (numberOfParens > 0) { manageClosingParenthesizedExpression(parameterizedQualifiedTypeReference, numberOfParens);
manageOpeningParenthesizedExpression(lambdaExpression, numberOfParens); if (isNextToken(TerminalTokens.TokenNameLPAREN)) { formatMethodArguments( null, lambdaExpression.arguments(), final Statement body = lambdaExpression.body(); if (body instanceof Block) { formatBlock((Block) body, scope, this.preferences.brace_position_for_lambda_body, this.preferences.insert_space_before_opening_brace_in_block); } else { body.traverse(this, scope); manageClosingParenthesizedExpression(lambdaExpression, numberOfParens);
final int numberOfParens = (parameterizedQualifiedTypeReference.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT; if (numberOfParens > 0) { manageOpeningParenthesizedExpression(parameterizedQualifiedTypeReference, numberOfParens); if (isClosingGenericToken()) { this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_parameterized_type_reference); int dimensions = getDimensions(); if (dimensions != 0 && dimensions <= parameterizedQualifiedTypeReference.dimensions()) { if (this.preferences.insert_space_before_opening_bracket_in_array_type_reference) { manageClosingParenthesizedExpression(parameterizedQualifiedTypeReference, numberOfParens);
private void formatClassBodyDeclarations(ASTNode[] nodes) { final int FIELD = 1, METHOD = 2, TYPE = 3; this.scribe.lastNumberOfNewLines = 1; ASTNode[] mergedNodes = computeMergedMemberDeclarations(nodes); Alignment memberAlignment = this.scribe.createMemberAlignment( Alignment.TYPE_MEMBERS, if (member instanceof MultiFieldDeclaration){ MultiFieldDeclaration multiField = (MultiFieldDeclaration) member; format(multiField, this, null, isChunkStart, i == 0); } else if (member instanceof Initializer) { int newLineBeforeChunk = isChunkStart ? this.preferences.blank_lines_before_new_chunk : 0; } else { FieldDeclaration field = (FieldDeclaration) member; format(field, this, null, isChunkStart, i == 0); format((AbstractMethodDeclaration) member, null, isChunkStart, i == 0); } else { isChunkStart = memberAlignment.checkChunkStart(TYPE, i, this.scribe.scanner.currentPosition); format((TypeDeclaration)member, null, isChunkStart, i == 0); while (isNextToken(TerminalTokens.TokenNameSEMICOLON)) { this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT); if (hasComments()) { this.scribe.printNewLine();
manageOpeningParenthesizedExpression(binaryExpression, numberOfParens); BinaryExpressionFragmentBuilder builder = buildFragments(binaryExpression, scope); final int fragmentsSize = builder.size(); if (isNextToken(TerminalTokens.TokenNameMINUS) || isNextToken(TerminalTokens.TokenNameMINUS_MINUS)) { if (isNextToken(TerminalTokens.TokenNamePLUS) || isNextToken(TerminalTokens.TokenNamePLUS_PLUS)) { this.expressionsPos |= EXPRESSIONS_POS_BETWEEN_TWO; this.scribe.printNextToken(operator, this.preferences.insert_space_before_binary_operator, Scribe.PRESERVE_EMPTY_LINES_IN_BINARY_EXPRESSION); if (operator == TerminalTokens.TokenNameMINUS && isNextToken(TerminalTokens.TokenNameMINUS)) { manageClosingParenthesizedExpression(binaryExpression, numberOfParens);
formatStatements(null, statements, false); if (hasComments()) { this.scribe.printNewLine(); return failedToFormat();
this.scribe.scanner.currentPosition); this.scribe.enterMemberAlignment(memberAlignment); ASTNode[] members = computeMergedMemberDeclarations(typeDeclaration); boolean isChunkStart = false; boolean ok = false; try { for (int i = startIndex, max = members.length; i < max; i++) { while (isNextToken(TerminalTokens.TokenNameSEMICOLON)) { this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon); this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT); format(multiField, this, typeDeclaration.staticInitializerScope, isChunkStart, i == 0); } else { format(multiField, this, typeDeclaration.initializerScope, isChunkStart, i == 0); FieldDeclaration field = (FieldDeclaration) member; if (field.isStatic()) { format(field, this, typeDeclaration.staticInitializerScope, isChunkStart, i == 0); } else { format(field, this, typeDeclaration.initializerScope, isChunkStart, i == 0); format((AbstractMethodDeclaration) member, typeDeclaration.scope, isChunkStart, i == 0); } else if (member instanceof TypeDeclaration) { isChunkStart = memberAlignment.checkChunkStart(Alignment.CHUNK_TYPE, i, this.scribe.scanner.currentPosition); format((TypeDeclaration)member, typeDeclaration.scope, isChunkStart, i == 0); while (isNextToken(TerminalTokens.TokenNameSEMICOLON)) { this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
final boolean hasPackage = currentPackage != null; if (hasPackage) { printComment(); int blankLinesBeforePackage = this.preferences.blank_lines_before_package; if (blankLinesBeforePackage > 0) { format(imports[0], false); for (int i = 1; i < importLength - 1; i++) { format(imports[i], false); format(imports[importLength - 1], true); } else { format(imports[0], true); formatEmptyTypeDeclaration(true); for (int i = 0; i < typesLength - 1; i++) { types[i].traverse(this, scope); formatEmptyTypeDeclaration(false); if (blankLineBetweenTypeDeclarations != 0) { this.scribe.printEmptyLines(blankLineBetweenTypeDeclarations);
/** * @see org.eclipse.jdt.core.formatter.CodeFormatter#format(int, String, int, int, int, String) */ public TextEdit format(String string, ASTNode[] nodes) { // reset the scribe this.scribe.reset(); long startTime = 0; if (DEBUG) { startTime = System.currentTimeMillis(); } final char[] compilationUnitSource = string.toCharArray(); this.localScanner.setSource(compilationUnitSource); this.scribe.resetScanner(compilationUnitSource); if (nodes == null) { return null; } this.lastLocalDeclarationSourceStart = -1; try { formatClassBodyDeclarations(nodes); } catch(AbortFormatting e){ return failedToFormat(); } if (DEBUG){ System.out.println("Formatting time: " + (System.currentTimeMillis() - startTime)); //$NON-NLS-1$ } return this.scribe.getRootEdit(); }
if (this.codeSnippetParsingUtil == null) this.codeSnippetParsingUtil = new CodeSnippetParsingUtil(); this.codeSnippetParsingUtil.parseCompilationUnit(source.toCharArray(), getDefaultCompilerOptions(), true); this.newCodeFormatter = new CodeFormatterVisitor(this.preferences, this.options, regions, this.codeSnippetParsingUtil, true); IRegion coveredRegion = getCoveredRegion(regions); int start = coveredRegion.getOffset(); int end = start + coveredRegion.getLength(); this.newCodeFormatter.formatComment(kind, source, start, end, indentationLevel); return this.newCodeFormatter.scribe.getRootEdit();
formatOpeningBrace(switch_brace, this.preferences.insert_space_before_opening_brace_in_switch); this.scribe.printNewLine(); this.scribe.printComment(); String bracePosition = wasACase ? this.preferences.brace_position_for_block_in_case : this.preferences.brace_position_for_block; formatBlock((Block) statement, scope, bracePosition, this.preferences.insert_space_before_opening_brace_in_block); wasACase = false; wasABreak = false;
/** * @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration, org.eclipse.jdt.internal.compiler.lookup.ClassScope) */ public boolean visit(TypeDeclaration memberTypeDeclaration, ClassScope scope) { format(memberTypeDeclaration); return false; }
/** * @see org.eclipse.jdt.core.formatter.CodeFormatter#format(int, String, int, int, int, String) */ public TextEdit format(String string, CompilationUnitDeclaration compilationUnitDeclaration) { // reset the scribe this.scribe.reset(); if (compilationUnitDeclaration == null || compilationUnitDeclaration.ignoreFurtherInvestigation) { return failedToFormat(); } long startTime = 0; if (DEBUG) { startTime = System.currentTimeMillis(); } final char[] compilationUnitSource = string.toCharArray(); this.localScanner.setSource(compilationUnitSource); this.scribe.resetScanner(compilationUnitSource); this.lastLocalDeclarationSourceStart = -1; try { compilationUnitDeclaration.traverse(this, compilationUnitDeclaration.scope); } catch(AbortFormatting e){ return failedToFormat(); } if (DEBUG){ System.out.println("Formatting time: " + (System.currentTimeMillis() - startTime)); //$NON-NLS-1$ } return this.scribe.getRootEdit(); }
/** * @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.EqualExpression, org.eclipse.jdt.internal.compiler.lookup.BlockScope) */ public boolean visit(EqualExpression equalExpression, BlockScope scope) { if ((equalExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT == OperatorIds.EQUAL_EQUAL) { return dumpEqualityExpression(equalExpression, TerminalTokens.TokenNameEQUAL_EQUAL, scope); } else { return dumpEqualityExpression(equalExpression, TerminalTokens.TokenNameNOT_EQUAL, scope); } }
/** * @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.Block, org.eclipse.jdt.internal.compiler.lookup.BlockScope) */ public boolean visit(Block block, BlockScope scope) { formatBlock(block, scope, this.preferences.brace_position_for_block, this.preferences.insert_space_before_opening_brace_in_block); return false; }