List of usage examples for org.eclipse.jdt.core.formatter DefaultCodeFormatterConstants getEclipseDefaultSettings
public static Map getEclipseDefaultSettings()
From source file:com.jaxio.celerio.output.EclipseCodeFormatter.java
License:Apache License
@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)); ///* www .j a va 2s. c o m*/ 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); }
From source file:com.motorola.studio.android.model.java.JavaClass.java
License:Apache License
/** * Gets the class content//from ww w. j a va2s . c o m * * @return an IDocument object containing the class content */ public IDocument getClassContent() throws AndroidException { String content = compUnit.toString(); document = new Document(content); // Formats the code using the Eclipse settings CodeFormatter codeFormatter = ToolFactory .createCodeFormatter(DefaultCodeFormatterConstants.getEclipseDefaultSettings()); TextEdit textEdit = codeFormatter.format( CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, content, 0, content.length(), 0, null); try { textEdit.apply(document); } catch (MalformedTreeException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorFormattingSourceCode, className); StudioLogger.error(JavaClass.class, errMsg, e); throw new AndroidException(errMsg); } catch (BadLocationException e) { String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorFormattingSourceCode, className); StudioLogger.error(JavaClass.class, errMsg, e); throw new AndroidException(errMsg); } addComments(); return document; }
From source file:com.spidasoftware.EclipseFormatter.JavaFormat.java
License:Apache License
@SuppressWarnings({ "unchecked", "deprecation" }) private static CodeFormatter initializeFormatter() { @SuppressWarnings("rawtypes") 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); // This is where you will add your java formatting preferences. I have an example below that // is commented out that would not allow for a line to be split at the selectors in a method invocation. // options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_SELECTOR_IN_METHOD_INVOCATION, // DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_NO_SPLIT, DefaultCodeFormatterConstants.INDENT_DEFAULT)); options.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, "120"); final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options); return codeFormatter; }
From source file:com.thoratou.exact.processors.IndentUtil.java
License:Open Source License
public String javaCode(String input) { // take default Eclipse formatting options 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_TAB_CHAR, JavaCore.SPACE); // 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)); // change the option to wrap each enum constant on a new line options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ASSIGNMENT, DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_NO_SPLIT, DefaultCodeFormatterConstants.INDENT_ON_COLUMN)); // change the option to wrap each enum constant on a new line options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_SELECTOR_IN_METHOD_INVOCATION, DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_NO_SPLIT, DefaultCodeFormatterConstants.INDENT_ON_COLUMN)); CodeFormatter cf = ToolFactory.createCodeFormatter(options); IDocument dc = new Document(input); TextEdit te = cf.format(CodeFormatter.K_COMPILATION_UNIT, dc.get(), 0, dc.get().length(), 0, null); try {// w w w. j a v a 2s .c o m te.apply(dc); } catch (MalformedTreeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace(); } return dc.get(); }
From source file:eu.artist.migration.modernization.uml2java.repackaged.gen.java.services.WorkspaceServices.java
License:Open Source License
public void formatProjectCode(String projectName) { if (!EMFPlugin.IS_ECLIPSE_RUNNING) { return;// www. j a v a 2s .c o m } try { IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); Map<String, String> options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); // initialize the compiler settings to be able to format 1.5 code options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5); // 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)); // instanciate the default code formatter with the given options final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options); project.accept(new IResourceVisitor() { public boolean visit(IResource resource) throws CoreException { if (resource.isAccessible() && resource instanceof IFile && "java".equals(((IFile) resource).getFileExtension())) { IFile iFile = (IFile) resource; ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom(iFile); String contents = compilationUnit.getBuffer().getContents(); final TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, contents, // source to format 0, // starting position contents.length(), // length 0, // initial indentation System.getProperty("line.separator") // line separator ); IDocument document = new Document(contents); try { if (edit != null) { edit.apply(document); } } catch (MalformedTreeException e) { e.printStackTrace(); } catch (BadLocationException e) { e.printStackTrace(); } iFile.setContents(new ByteArrayInputStream(document.get().getBytes()), IResource.FORCE, new NullProgressMonitor()); return true; } return true; } }); project.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor()); } catch (CoreException e) { e.printStackTrace(); } }
From source file:fr.inria.diverse.trace.commons.CodeGenUtil.java
License:Open Source License
/** * From http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc * .isv%2Freference%2Fapi%2Forg%2Feclipse%2F jdt%2Fcore%2Fformatter%2Fpackage-summary.html * /*from ww w .j a v a 2 s.c o m*/ * @param source * The raw java source code to format. * @return Formatted java code. */ public static String formatJavaCode(String source) { // take default Eclipse formatting options @SuppressWarnings("unchecked") Map<String, String> options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); // initialize the compiler settings to be able to format 1.7 code 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_COMMENT_LINE_LENGTH, Integer.toString(120)); options.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, Integer.toString(120)); // instantiate the default code formatter with the given options final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options); final TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, // format a compilation unit source, // source to format 0, // starting position source.length(), // length 0, // initial indentation System.getProperty("line.separator") // line separator ); IDocument document = new Document(source); try { edit.apply(document); } catch (MalformedTreeException e) { e.printStackTrace(); } catch (org.eclipse.jface.text.BadLocationException e) { // TODO Bloc catch gnr automatiquement e.printStackTrace(); } // display the formatted string on the System out return document.get(); }
From source file:ma.glasnost.orika.impl.generator.EclipseJdtCompiler.java
License:Apache License
/** * Return the options to be passed when creating {@link CodeFormatter} * instance.//from ww w .j a v a 2 s . co m * * @return */ private Map<Object, Object> getFormattingOptions() { @SuppressWarnings("unchecked") Map<Object, Object> options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); options.put(JavaCore.COMPILER_SOURCE, JAVA_COMPILER_SOURCE_VERSION); options.put(JavaCore.COMPILER_COMPLIANCE, JAVA_COMPILER_COMPLIANCE_VERSION); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JAVA_COMPILER_CODEGEN_TARGET_PLATFORM_VERSION); return options; }
From source file:mx.itesm.mexadl.util.Util.java
License:Open Source License
/** * Format the contents of a Java file using Eclipse's built-in code * formatter.//from w w w. ja v a 2s . c om * * @param outputFile * @param contentFile * @throws IOException * @throws MalformedTreeException * @throws BadLocationException */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void formatFileContent(final File outputFile, final File contentFile) throws IOException, MalformedTreeException, BadLocationException { Map options; String source; TextEdit edit; Writer writer; int contentKind; IDocument document; CodeFormatter codeFormatter; // use Eclipse's default formatting options writer = new BufferedWriter(new FileWriter(outputFile)); options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); // Compiler settings to be able to format 1.6 code options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6); // Instantiate the default code formatter with the given options codeFormatter = ToolFactory.createCodeFormatter(options); source = Util.getFileContents(contentFile); document = new org.eclipse.jface.text.Document(source); // Decide code kind and try to apply format contentKind = CodeFormatter.K_UNKNOWN; if (outputFile.toString().endsWith(Util.JAVA_EXTENSION)) { contentKind = CodeFormatter.K_COMPILATION_UNIT; } edit = codeFormatter.format(contentKind, source, 0, source.length(), 0, System.getProperty("line.separator")); if (edit != null) { edit.apply(document); } // Save to output file writer.write(document.get()); writer.close(); }
From source file:net.enilink.komma.generator.java.merge.SourceMerger.java
License:Open Source License
private CodeFormatter getCodeFormatter() { if (codeFormatter == null) { @SuppressWarnings("unchecked") final Map<Object, Object> options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5); codeFormatter = ToolFactory.createCodeFormatter(options); }//www.j a v a 2s . c om return codeFormatter; }
From source file:org.autorefactor.test.TestHelper.java
License:Open Source License
private static Map<String, String> getJava7Options() { Map<String, String> options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); options.put(COMPILER_COMPLIANCE, VERSION_1_7); options.put(COMPILER_CODEGEN_TARGET_PLATFORM, VERSION_1_7); options.put(COMPILER_SOURCE, VERSION_1_7); return options; }