本文整理汇总了Java中org.eclipse.jdt.core.ToolFactory.createCodeFormatter方法的典型用法代码示例。如果您正苦于以下问题:Java ToolFactory.createCodeFormatter方法的具体用法?Java ToolFactory.createCodeFormatter怎么用?Java ToolFactory.createCodeFormatter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.core.ToolFactory
的用法示例。
在下文中一共展示了ToolFactory.createCodeFormatter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: formatEclipseStyle
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的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.ToolFactory; //导入方法依赖的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.ToolFactory; //导入方法依赖的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: JavaCodeFormatter
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的package包/类
/**
* Creates a JavaCodeFormatter using the default formatter options and
* optionally applying user provided options on top.
*
* @param overrideOptions user provided options to apply on top of defaults
*/
public JavaCodeFormatter(final Map<String, Object> overrideOptions) {
Map formatterOptions = new HashMap<>(DEFAULT_FORMATTER_OPTIONS);
if (overrideOptions != null) {
formatterOptions.putAll(overrideOptions);
}
this.codeFormatter = ToolFactory.createCodeFormatter(formatterOptions,
ToolFactory.M_FORMAT_EXISTING);
}
示例5: setFormatterSettings
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的package包/类
@SuppressWarnings({"unchecked", "deprecation"})
public void setFormatterSettings(List<Setting> settings) {
// // change the option to wrap each enum constant on a new line
// options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS,
// DefaultCodeFormatterConstants.createAlignmentValue(true,
// DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE,
// DefaultCodeFormatterConstants.INDENT_ON_COLUMN));
//
if (settings != null) {
options = newHashMap();
for (Setting s : settings) {
options.put(s.getId(), s.getValue());
}
} else {
options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8);
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
options.put(JavaCore.FORMATTER_LINE_SPLIT, "160");
options.put(JavaCore.FORMATTER_TAB_CHAR, JavaCore.SPACE);
options.put(JavaCore.FORMATTER_TAB_SIZE, "4");
}
// instanciate the default code formatter with the given options
codeFormatter = ToolFactory.createCodeFormatter(options);
}
示例6: formatUnitSourceCode
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的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();
}
}
示例7: start
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的package包/类
/**
* Runs the Java code formatter application
*/
public Object start(IApplicationContext context) throws Exception {
File[] filesToFormat = processCommandLine((String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS));
if (filesToFormat == null) {
return IApplication.EXIT_OK;
}
if (!this.quiet) {
if (this.configName != null) {
System.out.println(Messages.bind(Messages.CommandLineConfigFile, this.configName));
}
System.out.println(Messages.bind(Messages.CommandLineStart));
}
final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(this.options);
// format the list of files and/or directories
for (int i = 0, max = filesToFormat.length; i < max; i++) {
final File file = filesToFormat[i];
if (file.isDirectory()) {
formatDirTree(file, codeFormatter);
} else if (Util.isJavaLikeFileName(file.getPath())) {
formatFile(file, codeFormatter);
}
}
if (!this.quiet) {
System.out.println(Messages.bind(Messages.CommandLineDone));
}
return IApplication.EXIT_OK;
}
示例8: JavaFormatter
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的package包/类
public JavaFormatter(Properties settings) {
if (settings == null) {
// if no settings run with jdk 5 as default
settings = new Properties();
settings.put(JavaCore.COMPILER_SOURCE, "1.5");
settings.put(JavaCore.COMPILER_COMPLIANCE, "1.5");
settings.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "1.5");
}
this.codeFormatter = ToolFactory.createCodeFormatter(settings);
}
示例9: formatFile
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的package包/类
private String formatFile(String code, VisitorContext ctx) {
TextEdit te = null;
if (formatter == null) {
log.debug("starting Eclipse formatter");
Map<String, String> options = getFormattingOptions(ctx);
formatter = ToolFactory.createCodeFormatter(options);
if (formatter != null) {
log.debug("Eclipse formatter [ok]");
}
}
if (formatter != null) {
te = formatter.format(CodeFormatter.K_COMPILATION_UNIT, code, 0, code.length(), 0, String.valueOf('\n'));
if (te == null) {
log.warn("The source cannot be formatted with the selected configuration. Applying a default formatting");
return code;
}
IDocument doc = new org.eclipse.jface.text.Document(code);
try {
te.apply(doc);
} catch (Exception e) {
throw new WalkModException(e);
}
String formattedCode = doc.get();
if (formattedCode == null || "".equals(formattedCode)) {
return code;
}
return formattedCode;
} else {
throw new WalkModException("Eclipse formatter cannot be initialized");
}
}
示例10: format
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的package包/类
private String format(final String code, int startOffset, int endOffset, SortedSet<Pair> changedElements) {
final int opts
= CodeFormatter.K_COMPILATION_UNIT + CodeFormatter.F_INCLUDE_COMMENTS /*+ CodeFormatter.K_CLASS_BODY_DECLARATIONS + CodeFormatter.K_STATEMENTS*/;
Map<String, String> allConfig = readConfig();
CodeFormatter formatter = ToolFactory.createCodeFormatter(allConfig);
//see http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fformatter%2FCodeFormatter.html&anchor=format(int,
String linefeed = getLineFeed(lineFeedSetting);
final TextEdit te;
// org.eclipse.jface.text.Region
List<IRegion> regions = new ArrayList<>();
if (null != changedElements && !changedElements.isEmpty()) {
for (Pair e : changedElements) {
final int length = e.getSecond() - e.getFirst();
regions.add(new org.eclipse.jface.text.Region(e.getFirst(), length));
}
LOG.finest("regions = " + regions);
IRegion[] toArray = regions.toArray(new IRegion[regions.size()]);
LOG.finest("use regions " + regions);
te = formatter.format(opts, code, toArray, 0, linefeed);
} else {
te = formatter.format(opts, code, startOffset, endOffset - startOffset, 0, linefeed);
}
final IDocument dc = new Document(code);
String formattedCode = null;
if ((te != null) && (te.getChildrenSize() > 0)) {
try {
te.apply(dc);
} catch (Exception ex) {
LOG.warning("Code could not be formatted!" + ex);
return null;
}
formattedCode = dc.get();
}
return formattedCode;
}
示例11: init
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的package包/类
@Override
public void init(Map<String, String> options, ConfigurationSource cfg) {
if (options.isEmpty()) {
options.put(JavaCore.COMPILER_SOURCE, cfg.getCompilerSources());
options.put(JavaCore.COMPILER_COMPLIANCE,
cfg.getCompilerCompliance());
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
cfg.getCompilerCodegenTargetPlatform());
}
super.initCfg(cfg);
formatter = ToolFactory.createCodeFormatter(options);
}
示例12: format
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的package包/类
/**
* Format a String . Use the default code formatter
*
* @param sourceToFormat
* String to format
*
*/
public static String format(String inputSource, IJavaProject project) {
Map<?, ?> options = project != null ? project.getOptions(true)
: JavaCore.getOptions();
String sourceToFormat = preformat(inputSource);
CodeFormatter formatter = ToolFactory.createCodeFormatter(options, ToolFactory.M_FORMAT_EXISTING);
IDocument document = new Document(sourceToFormat);
TextEdit textEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT
| CodeFormatter.F_INCLUDE_COMMENTS , sourceToFormat, 0,
sourceToFormat.length(), 0, null);
if (textEdit != null) {
try {
textEdit.apply(document);
String destination = document.get();
return destination;
} catch (Exception e) {
Activator.getDefault().logError(
"The compilation unit could not be"
+ " read because of thrown Exception.", e);
// In this case, return initial content
return sourceToFormat;
}
} else {
// In this case, return initial content
return sourceToFormat;
}
}
示例13: createCodeFormatter
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的package包/类
private static Object createCodeFormatter(IProject project) {
IJavaProject javaProject = JavaCore.create(project);
Map options = javaProject.getOptions(true);
return ToolFactory.createCodeFormatter(options);
}
示例14: createCodeFormatter
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的package包/类
private void createCodeFormatter() {
Map<String, String> options = getFormattingOptions();
codeFormatter = ToolFactory.createCodeFormatter(options);
}
示例15: EclipseFormatterStepImpl
import org.eclipse.jdt.core.ToolFactory; //导入方法依赖的package包/类
public EclipseFormatterStepImpl(Properties settings) {
this.codeFormatter = ToolFactory.createCodeFormatter(settings, ToolFactory.M_FORMAT_EXISTING);
}