private DefaultCodeFormatter getCommentCodeFormatter() { if (this.commentCodeFormatter == null) { Map<String, String> options2 = this.options.getMap(); options2.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_LINE_LENGTH, String.valueOf(this.options.comment_line_length - this.commentIndent - COMMENT_LINE_SEPARATOR_LENGTH)); options2.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, String.valueOf(this.options.page_width - this.commentIndent - COMMENT_LINE_SEPARATOR_LENGTH)); options2.put(CompilerOptions.OPTION_Source, this.sourceLevel); this.commentCodeFormatter = new DefaultCodeFormatter(options2); } return this.commentCodeFormatter; }
/** * @see org.eclipse.jdt.core.formatter.CodeFormatter#format(int, java.lang.String, int, int, int, java.lang.String) */ @Override public TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) { return format(kind, source, new IRegion[] { new Region(offset, length) }, indentationLevel, lineSeparator); }
/** * {@inheritDoc} */ public TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) { if (!regionsSatisfiesPreconditions(regions, source.length())) { throw new IllegalArgumentException(); } this.codeSnippetParsingUtil = new CodeSnippetParsingUtil(); boolean includeComments = (kind & F_INCLUDE_COMMENTS) != 0; switch(kind & K_MASK) { case K_CLASS_BODY_DECLARATIONS : return formatClassBodyDeclarations(source, indentationLevel, lineSeparator, regions, includeComments); case K_COMPILATION_UNIT : return formatCompilationUnit(source, indentationLevel, lineSeparator, regions, includeComments); case K_EXPRESSION : return formatExpression(source, indentationLevel, lineSeparator, regions, includeComments); case K_STATEMENTS : return formatStatements(source, indentationLevel, lineSeparator, regions, includeComments); case K_UNKNOWN : return probeFormatting(source, indentationLevel, lineSeparator, regions, includeComments); case K_JAVA_DOC : case K_MULTI_LINE_COMMENT : case K_SINGLE_LINE_COMMENT : //https://bugs.eclipse.org/bugs/show_bug.cgi?id=204091 throw new IllegalArgumentException(); } return null; }
private List<Token> prepareFormattedCode(String source, int kind) { if (!init(source, kind)) return null; this.astRoot = parseSourceCode(kind); if (this.astRoot == null) return null; if (kind != CodeFormatter.K_UNKNOWN) findHeader(); prepareSpaces(); prepareLineBreaks(); prepareComments(); prepareWraps(kind); return this.tokens; }
IRegion coveredRegion = getCoveredRegion(regions); int offset = coveredRegion.getOffset(); int length = coveredRegion.getLength(); return formatComment(kind, source, indentationLevel, lineSeparator, regions); Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true); if (expression != null) { return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions, includeComments); ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true); if (bodyDeclarations != null) { return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions, includeComments); ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false); if (constructorDeclaration.statements != null) { return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions, includeComments); return formatCompilationUnit(source, indentationLevel, lineSeparator, regions, includeComments);
newOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_CLOSING_BRACE_IN_ARRAY_INITIALIZER, JavaCore.INSERT); DefaultCodeFormatter defaultCodeFormatter = new DefaultCodeFormatter(newOptions); TextEdit textEdit = defaultCodeFormatter.format(org.eclipse.jdt.core.formatter.CodeFormatter.K_UNKNOWN, string, 0, string.length(), indentLevel, lineSeparator); if (positions != null && textEdit != null) {
/** * {@inheritDoc} */ public TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) { if (!regionsSatisfiesPreconditions(regions, source.length())) { throw new IllegalArgumentException(); } this.formatRegions = Arrays.asList(regions); updateWorkingOptions(indentationLevel, lineSeparator, kind); if ((kind & K_COMMENTS_MASK) != 0) return formatComments(source, kind & K_COMMENTS_MASK); if (prepareFormattedCode(source, kind) == null) return this.tokens.isEmpty() ? new MultiTextEdit() : null; MultiTextEdit result = new MultiTextEdit(); TextEditsBuilder resultBuilder = new TextEditsBuilder(this.sourceString, this.formatRegions, this.tokenManager, this.workingOptions); this.tokenManager.traverse(0, resultBuilder); for (TextEdit edit : resultBuilder.getEdits()) { result.addChild(edit); } return result; }
/** * @see org.eclipse.jdt.core.formatter.CodeFormatter#format(int, java.lang.String, int, int, int, java.lang.String) */ public TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) { if (offset < 0 || length < 0 || length > source.length()) { throw new IllegalArgumentException(); } switch(kind & K_MASK) { case K_JAVA_DOC : // https://bugs.eclipse.org/bugs/show_bug.cgi?id=102780 // use the integrated comment formatter to format comment return formatComment(kind & K_MASK, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)}); // $FALL-THROUGH$ - fall through next case when old comment formatter is activated case K_MULTI_LINE_COMMENT : case K_SINGLE_LINE_COMMENT : return formatComment(kind & K_MASK, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)}); } return format(kind, source, new IRegion[] {new Region(offset, length)}, indentationLevel, lineSeparator); }
private void updateWorkingOptions(int indentationLevel, String lineSeparator, int kind) { this.workingOptions.line_separator = lineSeparator != null ? lineSeparator : this.originalOptions.line_separator; if (this.workingOptions.line_separator == null) this.workingOptions.line_separator = Util.LINE_SEPARATOR; this.workingOptions.initial_indentation_level = indentationLevel; this.workingOptions.comment_format_javadoc_comment = this.originalOptions.comment_format_javadoc_comment && canFormatComment(kind, K_JAVA_DOC); this.workingOptions.comment_format_block_comment = this.originalOptions.comment_format_block_comment && canFormatComment(kind, K_MULTI_LINE_COMMENT); this.workingOptions.comment_format_line_comment = this.originalOptions.comment_format_line_comment && canFormatComment(kind, K_SINGLE_LINE_COMMENT); }
private List<Token> prepareFormattedCode(String source, int kind) { if (!init(source, kind)) return null; this.astRoot = parseSourceCode(kind); if (this.astRoot == null) return null; if (kind != CodeFormatter.K_UNKNOWN) findHeader(); prepareSpaces(); prepareLineBreaks(); prepareComments(); prepareWraps(kind); return this.tokens; }
IRegion coveredRegion = getCoveredRegion(regions); int offset = coveredRegion.getOffset(); int length = coveredRegion.getLength(); return formatComment(kind, source, indentationLevel, lineSeparator, regions); Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true); if (expression != null) { return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions, includeComments); ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true); if (bodyDeclarations != null) { return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions, includeComments); ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false); if (constructorDeclaration.statements != null) { return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions, includeComments); return formatCompilationUnit(source, indentationLevel, lineSeparator, regions, includeComments);
newOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_CLOSING_BRACE_IN_ARRAY_INITIALIZER, JavaCore.INSERT); DefaultCodeFormatter defaultCodeFormatter = new DefaultCodeFormatter(newOptions); TextEdit textEdit = defaultCodeFormatter.format(org.eclipse.jdt.core.formatter.CodeFormatter.K_UNKNOWN, string, 0, string.length(), indentLevel, lineSeparator); if (positions != null && textEdit != null) {
/** * {@inheritDoc} */ @Override public TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) { if (!regionsSatisfiesPreconditions(regions, source.length())) { throw new IllegalArgumentException(); } this.formatRegions = Arrays.asList(regions); updateWorkingOptions(indentationLevel, lineSeparator, kind); if ((kind & K_COMMENTS_MASK) != 0) return formatComments(source, kind & K_COMMENTS_MASK); if (prepareFormattedCode(source, kind) == null) return this.tokens.isEmpty() ? new MultiTextEdit() : null; MultiTextEdit result = new MultiTextEdit(); TextEditsBuilder resultBuilder = new TextEditsBuilder(this.sourceString, this.formatRegions, this.tokenManager, this.workingOptions); this.tokenManager.traverse(0, resultBuilder); for (TextEdit edit : resultBuilder.getEdits()) { result.addChild(edit); } return result; }
/** * @see org.eclipse.jdt.core.formatter.CodeFormatter#format(int, java.lang.String, int, int, int, java.lang.String) */ public TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) { if (offset < 0 || length < 0 || length > source.length()) { throw new IllegalArgumentException(); } switch(kind & K_MASK) { case K_JAVA_DOC : // https://bugs.eclipse.org/bugs/show_bug.cgi?id=102780 // use the integrated comment formatter to format comment return formatComment(kind & K_MASK, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)}); // $FALL-THROUGH$ - fall through next case when old comment formatter is activated case K_MULTI_LINE_COMMENT : case K_SINGLE_LINE_COMMENT : return formatComment(kind & K_MASK, source, indentationLevel, lineSeparator, new IRegion[] {new Region(offset, length)}); } return format(kind, source, new IRegion[] {new Region(offset, length)}, indentationLevel, lineSeparator); }
private void updateWorkingOptions(int indentationLevel, String lineSeparator, int kind) { this.workingOptions.line_separator = lineSeparator != null ? lineSeparator : this.originalOptions.line_separator; if (this.workingOptions.line_separator == null) this.workingOptions.line_separator = Util.LINE_SEPARATOR; this.workingOptions.initial_indentation_level = indentationLevel; this.workingOptions.comment_format_javadoc_comment = this.originalOptions.comment_format_javadoc_comment && canFormatComment(kind, K_JAVA_DOC); this.workingOptions.comment_format_block_comment = this.originalOptions.comment_format_block_comment && canFormatComment(kind, K_MULTI_LINE_COMMENT); this.workingOptions.comment_format_line_comment = this.originalOptions.comment_format_line_comment && canFormatComment(kind, K_SINGLE_LINE_COMMENT); }
List<Token> prepareFormattedCode(String source, int kind) { if (!init(source)) return null; this.astRoot = parseSourceCode(kind); if (this.astRoot == null) return null; if (kind != CodeFormatter.K_UNKNOWN) findHeader(); prepareSpaces(); prepareLineBreaks(); prepareComments(); prepareWraps(kind); this.tokenManager.applyFormatOff(); return this.tokens; }
IRegion coveredRegion = getCoveredRegion(regions); int offset = coveredRegion.getOffset(); int length = coveredRegion.getLength(); return formatComment(kind, source, indentationLevel, lineSeparator, regions); Expression expression = this.codeSnippetParsingUtil.parseExpression(source.toCharArray(), getDefaultCompilerOptions(), true); if (expression != null) { return internalFormatExpression(source, indentationLevel, lineSeparator, expression, regions, includeComments); ASTNode[] bodyDeclarations = this.codeSnippetParsingUtil.parseClassBodyDeclarations(source.toCharArray(), getDefaultCompilerOptions(), true); if (bodyDeclarations != null) { return internalFormatClassBodyDeclarations(source, indentationLevel, lineSeparator, bodyDeclarations, regions, includeComments); ConstructorDeclaration constructorDeclaration = this.codeSnippetParsingUtil.parseStatements(source.toCharArray(), getDefaultCompilerOptions(), true, false); if (constructorDeclaration.statements != null) { return internalFormatStatements(source, indentationLevel, lineSeparator, constructorDeclaration, regions, includeComments); return formatCompilationUnit(source, indentationLevel, lineSeparator, regions, includeComments);
/** * {@inheritDoc} */ public TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) { if (!regionsSatisfiesPreconditions(regions, source.length())) { throw new IllegalArgumentException(); } this.codeSnippetParsingUtil = new CodeSnippetParsingUtil(); boolean includeComments = (kind & F_INCLUDE_COMMENTS) != 0; switch(kind & K_MASK) { case K_CLASS_BODY_DECLARATIONS : return formatClassBodyDeclarations(source, indentationLevel, lineSeparator, regions, includeComments); case K_COMPILATION_UNIT : return formatCompilationUnit(source, indentationLevel, lineSeparator, regions, includeComments); case K_EXPRESSION : return formatExpression(source, indentationLevel, lineSeparator, regions, includeComments); case K_STATEMENTS : return formatStatements(source, indentationLevel, lineSeparator, regions, includeComments); case K_UNKNOWN : return probeFormatting(source, indentationLevel, lineSeparator, regions, includeComments); case K_JAVA_DOC : case K_MULTI_LINE_COMMENT : case K_SINGLE_LINE_COMMENT : //https://bugs.eclipse.org/bugs/show_bug.cgi?id=204091 throw new IllegalArgumentException(); } return null; }
newOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_CLOSING_BRACE_IN_ARRAY_INITIALIZER, JavaCore.INSERT); DefaultCodeFormatter defaultCodeFormatter = new DefaultCodeFormatter(newOptions); TextEdit textEdit = defaultCodeFormatter.format(org.eclipse.jdt.core.formatter.CodeFormatter.K_UNKNOWN, string, 0, string.length(), indentLevel, lineSeparator); if (positions != null && textEdit != null) {
/** * {@inheritDoc} */ public TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) { if (!regionsSatisfiesPreconditions(regions, source.length())) { throw new IllegalArgumentException(); } this.formatRegions = regions; updateWorkingOptions(indentationLevel, lineSeparator, kind); if ((kind & K_COMMENTS_MASK) != 0) return formatComments(source, kind & K_COMMENTS_MASK); if (prepareFormattedCode(source, kind) == null) return this.tokens.isEmpty() ? new MultiTextEdit() : null; MultiTextEdit result = new MultiTextEdit(); TextEditsBuilder resultBuilder = new TextEditsBuilder(this.sourceString, regions, this.tokenManager, this.workingOptions); this.tokenManager.traverse(0, resultBuilder); for (TextEdit edit : resultBuilder.getEdits()) { result.addChild(edit); } return result; }