本文整理汇总了Java中org.eclipse.jdt.internal.formatter.DefaultCodeFormatter类的典型用法代码示例。如果您正苦于以下问题:Java DefaultCodeFormatter类的具体用法?Java DefaultCodeFormatter怎么用?Java DefaultCodeFormatter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DefaultCodeFormatter类属于org.eclipse.jdt.internal.formatter包,在下文中一共展示了DefaultCodeFormatter类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testFormatDefault
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; //导入依赖的package包/类
/**
* Format files using code formatter default options.
*/
public void testFormatDefault() throws CoreException {
for (int i = 0; i < FORMAT_FILES.length; i++) {
if (DACAPO_PRINT)
System.out.print(".");
IJavaElement element = JDT_CORE_PROJECT.findType(FORMAT_FILES[i]);
String source = ((ICompilationUnit) element.getParent()).getSource();
for (int j = 0; j < 2; j++)
new DefaultCodeFormatter().format(CodeFormatter.K_COMPILATION_UNIT, source, 0, source.length(), 0, null);
}
}
示例2: format
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; //导入依赖的package包/类
public static String format(String code, Map<String, String> options) {
DefaultCodeFormatterOptions cfOptions = DefaultCodeFormatterOptions.getJavaConventionsSettings();
cfOptions.tab_char = DefaultCodeFormatterOptions.TAB;
CodeFormatter cf = new DefaultCodeFormatter(cfOptions, options);
TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, code, 0, code.length(), 0, null);
IDocument dc = new Document(code);
try {
te.apply(dc);
} catch (Exception e) {
throw new MinnalGeneratorException("Failed while formatting the code", e);
}
return dc.get();
}
示例3: createCodeFormatter
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; //导入依赖的package包/类
/**
* Create an instance of the built-in code formatter.
*
* <p>The given options should at least provide the source level ({@link
* org.eclipse.jdt.core.JavaCore#COMPILER_SOURCE}), the compiler compliance level ({@link
* org.eclipse.jdt.core.JavaCore#COMPILER_COMPLIANCE}) and the target platform ({@link
* org.eclipse.jdt.core.JavaCore#COMPILER_CODEGEN_TARGET_PLATFORM}). Without these options, it is
* not possible for the code formatter to know what kind of source it needs to format.
*
* <p>The given mode determines what options should be enabled when formatting the code. It can
* have the following values: {@link #M_FORMAT_NEW}, {@link #M_FORMAT_EXISTING}, but other values
* may be added in the future.
*
* @param options the options map to use for formatting with the default code formatter.
* Recognized options are documented on <code>JavaCore#getDefaultOptions()</code>. If set to
* <code>null</code>, then use the current settings from <code>JavaCore#getOptions</code>.
* @param mode the given mode to modify the given options.
* @return an instance of the built-in code formatter
* @see org.eclipse.jdt.core.formatter.CodeFormatter
* @see org.eclipse.jdt.core.JavaCore#getOptions()
* @since 3.3
*/
public static CodeFormatter createCodeFormatter(Map options, int mode) {
if (options == null) options = org.eclipse.jdt.core.JavaCore.getOptions();
Map currentOptions = new HashMap(options);
if (mode == M_FORMAT_NEW) {
// disable the option for not formatting comments starting on first column
currentOptions.put(
DefaultCodeFormatterConstants
.FORMATTER_COMMENT_FORMAT_LINE_COMMENT_STARTING_ON_FIRST_COLUMN,
DefaultCodeFormatterConstants.TRUE);
// disable the option for not indenting comments starting on first column
currentOptions.put(
DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN,
DefaultCodeFormatterConstants.FALSE);
currentOptions.put(
DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN,
DefaultCodeFormatterConstants.FALSE);
}
return new DefaultCodeFormatter(currentOptions);
}
示例4: format
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; //导入依赖的package包/类
public static TextEdit format(IDocument document, TypedPosition partition,
Map<String, String> javaFormattingPrefs,
Map<String, String> javaScriptFormattingPrefs, String original) {
try {
// Extract the JSNI block out of the document
int offset = partition.getOffset();
int length = partition.getLength();
// Determine the line delimiter, indent string, and tab/indent widths
String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);
int tabWidth = IndentManipulation.getTabWidth(javaFormattingPrefs);
int indentWidth = IndentManipulation.getIndentWidth(javaFormattingPrefs);
// Get indentation level of the first line of the JSNI block (this should
// be the line containing the JSNI method declaration)
int methodDeclarationOffset = getMethodDeclarationOffset(document, offset);
int jsniLine1 = document.getLineOfOffset(methodDeclarationOffset);
int methodIndentLevel = getLineIndentLevel(document, jsniLine1, tabWidth,
indentWidth);
DefaultCodeFormatter defaultCodeFormatter = new DefaultCodeFormatter(
javaFormattingPrefs);
String indentLine = defaultCodeFormatter.createIndentationString(methodIndentLevel);
// Extract the JSNI body out of the block and split it up by line
String jsniSource = document.get(offset, length);
String body = JsniParser.extractMethodBody(jsniSource);
String formattedJs;
// JSNI Java references mess up the JS formatter, so replace them
// with place holder values
JsniJavaRefReplacementResult replacementResults = replaceJsniJavaRefs(body);
body = replacementResults.getJsni();
TextEdit formatEdit = CodeFormatterUtil.format2(
CodeFormatter.K_STATEMENTS, body, methodIndentLevel + 1,
lineDelimiter, javaScriptFormattingPrefs);
if (formatEdit != null) {
body = restoreJsniJavaRefs(replacementResults);
Document d = new Document(body);
formatEdit.apply(d);
formattedJs = d.get();
if (!formattedJs.startsWith(lineDelimiter)) {
formattedJs = lineDelimiter + formattedJs;
}
if (!formattedJs.endsWith(lineDelimiter)) {
formattedJs = formattedJs + lineDelimiter;
}
formattedJs = formattedJs + indentLine;
formattedJs = "/*-{" + formattedJs + "}-*/";
} else {
if (original == null) {
return null;
}
formattedJs = original; // formatting failed, use the original string
}
return new ReplaceEdit(offset, length, formattedJs);
} catch (Exception e) {
GWTPluginLog.logError(e);
return null;
}
}
示例5: getIndentToken
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; //导入依赖的package包/类
private String getIndentToken() {
return new DefaultCodeFormatter((Map<String, String>) prefs).createIndentationString(1);
}
示例6: format
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; //导入依赖的package包/类
public TextEdit format(IDocument document, TypedPosition partition, Map<String, String> javaFormattingPrefs,
Map<String, String> javaScriptFormattingPrefs, String original) {
try {
// Extract the JSNI block out of the document
int offset = partition.getOffset();
int length = partition.getLength();
// Determine the line delimiter, indent string, and tab/indent widths
String lineDelimiter = Settings.LINE_SEPARATOR;
int tabWidth = IndentManipulation.getTabWidth(javaFormattingPrefs);
int indentWidth = IndentManipulation.getIndentWidth(javaFormattingPrefs);
// Get indentation level of the first line of the JSNI block (this should
// be the line containing the JSNI method declaration)
int methodDeclarationOffset = getMethodDeclarationOffset(document, offset);
int jsniLine1 = document.getLineOfOffset(methodDeclarationOffset);
int methodIndentLevel = getLineIndentLevel(document, jsniLine1, tabWidth, indentWidth);
DefaultCodeFormatter defaultCodeFormatter = new DefaultCodeFormatter(javaFormattingPrefs);
String indentLine = defaultCodeFormatter.createIndentationString(methodIndentLevel);
// Extract the JSNI body out of the block and split it up by line
String jsniSource = document.get(offset, length);
String body = JsniParser.extractMethodBody(jsniSource);
String formattedJs;
// JSNI Java references mess up the JS formatter, so replace them
// with place holder values
JsniJavaRefReplacementResult replacementResults = replaceJsniJavaRefs(body);
body = replacementResults.getJsni();
CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(javaScriptFormattingPrefs);
TextEdit formatEdit = codeFormatter.format(CodeFormatter.K_STATEMENTS, body, 0, body.length(),
methodIndentLevel + 1, lineDelimiter);
if (formatEdit != null) {
body = restoreJsniJavaRefs(replacementResults);
Document d = new Document(body);
formatEdit.apply(d);
formattedJs = d.get();
if (!formattedJs.startsWith(lineDelimiter)) {
formattedJs = lineDelimiter + formattedJs;
}
if (!formattedJs.endsWith(lineDelimiter)) {
formattedJs = formattedJs + lineDelimiter;
}
formattedJs = formattedJs + indentLine;
formattedJs = "/*-{" + formattedJs + "}-*/";
} else {
if (original == null) {
return null;
}
formattedJs = original; // formatting failed, use the original string
}
return new ReplaceEdit(offset, length, formattedJs);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例7: EclipseJsFormatterAdapter44
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; //导入依赖的package包/类
@SuppressWarnings("unused")
public EclipseJsFormatterAdapter44(Map options) {
defaultCodeFormatter = new DefaultCodeFormatter(options);
}
示例8: EclipseJavaFormatterAdapter44
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; //导入依赖的package包/类
@SuppressWarnings("unused")
public EclipseJavaFormatterAdapter44(Map options) {
defaultCodeFormatter = new DefaultCodeFormatter(options);
}
示例9: EclipseJavaFormatterAdapter
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; //导入依赖的package包/类
@SuppressWarnings("unused")
public EclipseJavaFormatterAdapter(Map options) {
defaultCodeFormatter = new DefaultCodeFormatter(options);
}
示例10: createCodeFormatter
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; //导入依赖的package包/类
/**
* Create an instance of the built-in code formatter.
* <p>The given options should at least provide the source level ({@link JavaCore#COMPILER_SOURCE}),
* the compiler compliance level ({@link JavaCore#COMPILER_COMPLIANCE}) and the target platform
* ({@link JavaCore#COMPILER_CODEGEN_TARGET_PLATFORM}).
* Without these options, it is not possible for the code formatter to know what kind of source it needs to format.
* </p>
* <p>The given mode determines what options should be enabled when formatting the code. It can have the following
* values: {@link #M_FORMAT_NEW}, {@link #M_FORMAT_EXISTING}, but other values may be added in the future.
* </p>
*
* @param options the options map to use for formatting with the default code formatter. Recognized options
* are documented on <code>JavaCore#getDefaultOptions()</code>. If set to <code>null</code>, then use
* the current settings from <code>JavaCore#getOptions</code>.
* @param mode the given mode to modify the given options.
*
* @return an instance of the built-in code formatter
* @see CodeFormatter
* @see JavaCore#getOptions()
* @since 3.3
*/
public static CodeFormatter createCodeFormatter(Map options, int mode) {
if (options == null) options = JavaCore.getOptions();
Map currentOptions = new HashMap(options);
if (mode == M_FORMAT_NEW) {
// disable the option for not formatting comments starting on first column
currentOptions.put(DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_LINE_COMMENT_STARTING_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.TRUE);
// disable the option for not indenting comments starting on first column
currentOptions.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_BLOCK_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE);
currentOptions.put(DefaultCodeFormatterConstants.FORMATTER_NEVER_INDENT_LINE_COMMENTS_ON_FIRST_COLUMN, DefaultCodeFormatterConstants.FALSE);
}
return new DefaultCodeFormatter(currentOptions);
}