本文整理汇总了Java中org.eclipse.jdt.core.formatter.CodeFormatter.format方法的典型用法代码示例。如果您正苦于以下问题:Java CodeFormatter.format方法的具体用法?Java CodeFormatter.format怎么用?Java CodeFormatter.format使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.core.formatter.CodeFormatter
的用法示例。
在下文中一共展示了CodeFormatter.format方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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());
}
示例2: 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;
}
示例3: formatUnitSourceCode
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入方法依赖的package包/类
/**
* Format a Unit Source Code
*
* @param testInterface
* @param monitor
* @throws CoreException
*/
@SuppressWarnings("unchecked")
public static void formatUnitSourceCode(IFile file, IProgressMonitor monitor) throws CoreException {
@SuppressWarnings("rawtypes")
SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file);
subMonitor.split(50);
ICompilationUnit workingCopy = unit.getWorkingCopy(monitor);
Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS,
DefaultCodeFormatterConstants.createAlignmentValue(true,
DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE,
DefaultCodeFormatterConstants.INDENT_ON_COLUMN));
final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options);
ISourceRange range = unit.getSourceRange();
TextEdit formatEdit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, unit.getSource(),
range.getOffset(), range.getLength(), 0, null);
subMonitor.split(30);
if (formatEdit != null /* && formatEdit.hasChildren()*/) {
workingCopy.applyTextEdit(formatEdit, monitor);
workingCopy.reconcile(ICompilationUnit.NO_AST, false, null, null);
workingCopy.commitWorkingCopy(true, null);
workingCopy.discardWorkingCopy();
}
file.refreshLocal(IResource.DEPTH_INFINITE, subMonitor);
subMonitor.split(20);
}
示例4: formatUnitSourceCode
import org.eclipse.jdt.core.formatter.CodeFormatter; //导入方法依赖的package包/类
/**
* Formats the specified compilation unit.
*
* @param unit the compilation unit to format
* @param monitor the monitor for the operation
* @throws JavaModelException
*/
public static void formatUnitSourceCode(ICompilationUnit unit,
IProgressMonitor monitor) throws JavaModelException {
CodeFormatter formatter = ToolFactory.createCodeFormatter(null);
ISourceRange range = unit.getSourceRange();
TextEdit formatEdit = formatter.format(
CodeFormatter.K_COMPILATION_UNIT, unit.getSource(),
range.getOffset(), range.getLength(), 0, null);
if (formatEdit != null && formatEdit.hasChildren()) {
unit.applyTextEdit(formatEdit, monitor);
} else {
monitor.done();
}
}