本文整理汇总了Java中org.eclipse.jdt.core.formatter.CodeFormatter类的典型用法代码示例。如果您正苦于以下问题:Java CodeFormatter类的具体用法?Java CodeFormatter怎么用?Java CodeFormatter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CodeFormatter类属于org.eclipse.jdt.core.formatter包,在下文中一共展示了CodeFormatter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: apply
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
public String apply(String contents) {
final TextEdit edit = codeFormatter.format(
CodeFormatter.K_COMPILATION_UNIT
| CodeFormatter.F_INCLUDE_COMMENTS, contents, 0,
contents.length(), 0, Constants.LF);
if (edit == null) {
// TODO log a fatal or warning here. Throwing an exception is causing the actual freemarker error to be lost
return contents;
}
IDocument document = new Document(contents);
try {
edit.apply(document);
} catch (Exception e) {
throw new RuntimeException(
"Failed to format the generated source code.", e);
}
return document.get();
}
示例2: formatEclipseStyle
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
public static String formatEclipseStyle(final Properties prop, final String content) {
final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(prop);
final IDocument document = new Document(content);
try {
final TextEdit textEdit =
codeFormatter.format(
CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS,
content,
0,
content.length(),
0,
null);
if (textEdit != null) {
textEdit.apply(document);
} else {
return content;
}
} catch (final BadLocationException e) {
return content;
}
return ensureCorrectNewLines(document.get());
}
示例3: updateReplacementString
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
/**
* @param document
* @param offset
* @param importRewrite
* @param completionSnippetsSupported
* @return
* @throws CoreException
* @throws BadLocationException
*/
public String updateReplacementString(IDocument document, int offset, ImportRewrite importRewrite, boolean completionSnippetsSupported) throws CoreException, BadLocationException {
int flags= Flags.AccPublic | (fField.getFlags() & Flags.AccStatic);
String stub;
if (fIsGetter) {
String getterName= GetterSetterUtil.getGetterName(fField, null);
stub = GetterSetterUtil.getGetterStub(fField, getterName, true, flags);
} else {
String setterName= GetterSetterUtil.getSetterName(fField, null);
stub = GetterSetterUtil.getSetterStub(fField, setterName, true, flags);
}
// use the code formatter
String lineDelim= TextUtilities.getDefaultLineDelimiter(document);
String replacement = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, 0, lineDelim, fField.getJavaProject());
if (replacement.endsWith(lineDelim)) {
replacement = replacement.substring(0, replacement.length() - lineDelim.length());
}
return replacement;
}
示例4: formatCode
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
private static String formatCode(String contents, Object codeFormatter) {
if (codeFormatter instanceof CodeFormatter) {
IDocument doc = new Document(contents);
TextEdit edit = ((CodeFormatter) codeFormatter).format(CodeFormatter.K_COMPILATION_UNIT, doc.get(), 0,
doc.get().length(), 0, null);
if (edit != null) {
try {
edit.apply(doc);
contents = doc.get();
} catch (Exception e) {
System.out.println(e);
}
}
}
return contents;
}
示例5: format
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
private void format(IDocument doc, CompilationUnitContext context) throws BadLocationException {
Map<String, String> options;
IJavaProject project = context.getJavaProject();
if (project != null) options = project.getOptions(true);
else options = JavaCore.getOptions();
String contents = doc.get();
int[] kinds = {CodeFormatter.K_EXPRESSION, CodeFormatter.K_STATEMENTS, CodeFormatter.K_UNKNOWN};
TextEdit edit = null;
for (int i = 0; i < kinds.length && edit == null; i++) {
edit =
CodeFormatterUtil.format2(
kinds[i], contents, fInitialIndentLevel, fLineDelimiter, options);
}
if (edit == null) throw new BadLocationException(); // fall back to indenting
edit.apply(doc, TextEdit.UPDATE_REGIONS);
}
示例6: appendMethodNameReplacement
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
/**
* Appends everything up to the method name including the opening parenthesis.
*
* <p>In case of {@link org.eclipse.jdt.core.CompletionProposal#METHOD_REF_WITH_CASTED_RECEIVER}
* it add cast.
*
* @param buffer the string buffer
* @since 3.4
*/
protected void appendMethodNameReplacement(StringBuffer buffer) {
if (fProposal.getKind() == CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER) {
String coreCompletion = String.valueOf(fProposal.getCompletion());
String lineDelimiter = TextUtilities.getDefaultLineDelimiter(getTextViewer().getDocument());
String replacement =
CodeFormatterUtil.format(
CodeFormatter.K_EXPRESSION,
coreCompletion,
0,
lineDelimiter,
fInvocationContext.getProject());
buffer.append(replacement.substring(0, replacement.lastIndexOf('.') + 1));
}
if (fProposal.getKind() != CompletionProposal.CONSTRUCTOR_INVOCATION)
buffer.append(fProposal.getName());
FormatterPrefs prefs = getFormatterPrefs();
if (prefs.beforeOpeningParen) buffer.append(SPACE);
buffer.append(LPAREN);
}
示例7: reformatJavaSourceAsString
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
/**
* Given a String containing the text of a Java source file, return the same
* Java source, but reformatted by the Eclipse auto-format code, with the
* user's current Java preferences.
*/
public static String reformatJavaSourceAsString(String source) {
TextEdit reformatTextEdit = CodeFormatterUtil.format2(
CodeFormatter.K_COMPILATION_UNIT, source, 0, (String) null,
JavaCore.getOptions());
if (reformatTextEdit != null) {
Document document = new Document(source);
try {
reformatTextEdit.apply(document, TextEdit.NONE);
source = document.get();
} catch (BadLocationException ble) {
CorePluginLog.logError(ble);
}
}
return source;
}
示例8: formatJava
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
private String formatJava(IType type) throws JavaModelException {
String source = type.getCompilationUnit().getSource();
CodeFormatter formatter = ToolFactory.createCodeFormatter(type.getJavaProject().getOptions(true));
TextEdit formatEdit = formatter.format(CodeFormatterFlags.getFlagsForCompilationUnitFormat(), source, 0,
source.length(), 0, lineDelimiter);
if (formatEdit == null) {
CorePluginLog.logError("Could not format source for " + type.getCompilationUnit().getElementName());
return source;
}
Document document = new Document(source);
try {
formatEdit.apply(document);
source = document.get();
} catch (BadLocationException e) {
CorePluginLog.logError(e);
}
source = Strings.trimLeadingTabsAndSpaces(source);
return source;
}
示例9: format
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
private void format(IDocument doc, CompilationUnitContext context) throws BadLocationException {
Map<String, String> options;
IJavaProject project= context.getJavaProject();
if (project != null)
options= project.getOptions(true);
else
options= JavaCore.getOptions();
String contents= doc.get();
int[] kinds= { CodeFormatter.K_EXPRESSION, CodeFormatter.K_STATEMENTS, CodeFormatter.K_UNKNOWN};
TextEdit edit= null;
for (int i= 0; i < kinds.length && edit == null; i++) {
edit= CodeFormatterUtil.format2(kinds[i], contents, fInitialIndentLevel, fLineDelimiter, options);
}
if (edit == null)
throw new BadLocationException(); // fall back to indenting
edit.apply(doc, TextEdit.UPDATE_REGIONS);
}
示例10: createFieldWithCastedReceiverProposal
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
/**
* Creates the Java completion proposal for the JDT Core
* {@link CompletionProposal#FIELD_REF_WITH_CASTED_RECEIVER} proposal.
*
* @param proposal the JDT Core proposal
* @return the Java completion proposal
* @since 3.4
*/
private IJavaCompletionProposal createFieldWithCastedReceiverProposal(CompletionProposal proposal) {
String completion= String.valueOf(proposal.getCompletion());
completion= CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, completion, 0, "\n", fJavaProject); //$NON-NLS-1$
int start= proposal.getReplaceStart();
int length= getLength(proposal);
StyledString label= fLabelProvider.createStyledLabel(proposal);
Image image= getImage(fLabelProvider.createFieldImageDescriptor(proposal));
int relevance= computeRelevance(proposal);
JavaCompletionProposal javaProposal= new JavaFieldWithCastedReceiverCompletionProposal(completion, start, length, image, label, relevance, getContext().isInJavadoc(), getInvocationContext(), proposal);
if (fJavaProject != null)
javaProposal.setProposalInfo(new FieldProposalInfo(fJavaProject, proposal));
javaProposal.setTriggerCharacters(VAR_TRIGGER);
return javaProposal;
}
示例11: appendMethodNameReplacement
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
/**
* Appends everything up to the method name including
* the opening parenthesis.
* <p>
* In case of {@link CompletionProposal#METHOD_REF_WITH_CASTED_RECEIVER}
* it add cast.
* </p>
*
* @param buffer the string buffer
* @since 3.4
*/
protected void appendMethodNameReplacement(StringBuffer buffer) {
if (fProposal.getKind() == CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER) {
String coreCompletion= String.valueOf(fProposal.getCompletion());
String lineDelimiter= TextUtilities.getDefaultLineDelimiter(getTextViewer().getDocument());
String replacement= CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, coreCompletion, 0, lineDelimiter, fInvocationContext.getProject());
buffer.append(replacement.substring(0, replacement.lastIndexOf('.') + 1));
}
if (fProposal.getKind() != CompletionProposal.CONSTRUCTOR_INVOCATION)
buffer.append(fProposal.getName());
FormatterPrefs prefs= getFormatterPrefs();
if (prefs.beforeOpeningParen)
buffer.append(SPACE);
buffer.append(LPAREN);
}
示例12: printNextToken
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
public void printNextToken(int[] expectedTokenTypes, boolean considerSpaceIfAny){
printComment(CodeFormatter.K_UNKNOWN, NO_TRAILING_COMMENT);
try {
this.currentToken = this.scanner.getNextToken();
if (Arrays.binarySearch(expectedTokenTypes, this.currentToken) < 0) {
StringBuffer expectations = new StringBuffer(5);
for (int i = 0; i < expectedTokenTypes.length; i++){
if (i > 0) {
expectations.append(',');
}
expectations.append(expectedTokenTypes[i]);
}
throw new AbortFormatting("unexpected token type, expecting:["+expectations.toString()+"], actual:"+this.currentToken);//$NON-NLS-1$//$NON-NLS-2$
}
print(this.scanner.currentPosition - this.scanner.startPosition, considerSpaceIfAny);
} catch (InvalidInputException e) {
throw new AbortFormatting(e);
}
}
示例13: format
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
private void format(ImportReference importRef, boolean isLast) {
this.scribe.printNextToken(TerminalTokens.TokenNameimport);
if (!isLast) this.scribe.blank_lines_between_import_groups = this.preferences.blank_lines_between_import_groups;
this.scribe.space();
if (importRef.isStatic()) {
this.scribe.printNextToken(TerminalTokens.TokenNamestatic);
this.scribe.space();
}
if ((importRef.bits & ASTNode.OnDemand) != 0) {
this.scribe.printQualifiedReference(importRef.sourceEnd, false/*do not expect parenthesis*/);
this.scribe.printNextToken(TerminalTokens.TokenNameDOT);
this.scribe.printNextToken(TerminalTokens.TokenNameMULTIPLY);
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
} else {
this.scribe.printQualifiedReference(importRef.sourceEnd, false/*do not expect parenthesis*/);
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
}
if (isLast) {
this.scribe.blank_lines_between_import_groups = -1;
this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.IMPORT_TRAILING_COMMENT);
} else {
this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.NO_TRAILING_COMMENT);
this.scribe.blank_lines_between_import_groups = -1;
}
this.scribe.printNewLine();
}
示例14: getPrefixAndSuffix
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
public String[] getPrefixAndSuffix(int indent, ASTNode node, RewriteEventStore events) {
String nodeString= ASTRewriteFlattener.asString(node, events);
int nodeStart= this.prefix.length();
int nodeEnd= nodeStart + nodeString.length() - 1;
String str= this.prefix + nodeString + this.suffix;
Position pos1= new Position(this.start, nodeStart + 1 - this.start);
Position pos2= new Position(nodeEnd, 2);
TextEdit res= formatString(CodeFormatter.K_STATEMENTS, str, 0, str.length(), indent);
if (res != null) {
str= evaluateFormatterEdit(str, res, new Position[] { pos1, pos2 });
}
return new String[] {
str.substring(pos1.offset + 1, pos1.offset + pos1.length - 1),
str.substring(pos2.offset + 1, pos2.offset + pos2.length - 1)
};
}
示例15: visit
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入依赖的package包/类
/**
* @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.AssertStatement, org.eclipse.jdt.internal.compiler.lookup.BlockScope)
*/
public boolean visit(AssertStatement assertStatement, BlockScope scope) {
this.scribe.printNextToken(TerminalTokens.TokenNameassert);
this.scribe.space();
assertStatement.assertExpression.traverse(this, scope);
if (assertStatement.exceptionArgument != null) {
this.scribe.printNextToken(TerminalTokens.TokenNameCOLON, this.preferences.insert_space_before_colon_in_assert);
if (this.preferences.insert_space_after_colon_in_assert) {
this.scribe.space();
}
assertStatement.exceptionArgument.traverse(this, scope);
}
this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
return false;
}