List of usage examples for org.eclipse.jdt.core.formatter DefaultCodeFormatterConstants WRAP_ONE_PER_LINE
int WRAP_ONE_PER_LINE
To view the source code for org.eclipse.jdt.core.formatter DefaultCodeFormatterConstants WRAP_ONE_PER_LINE.
Click Source Link
FORMATTER / The wrapping is done by putting each element on its own line.Usage
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 {//from w ww. jav a 2 s.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. ja v a 2 s . co 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:org.dyno.visual.swing.base.JavaUtil.java
License:Open Source License
private static CodeFormatter getCodeFormatter() { if (codeFormatter == null) { Map 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); options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, DefaultCodeFormatterConstants.INDENT_ON_COLUMN)); options.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, "160"); //$NON-NLS-1$ codeFormatter = ToolFactory.createCodeFormatter(options); }//from w w w . j a v a 2s .c o m return codeFormatter; }From source file:org.eclipse.che.jdt.quickfix.LocalCorrectionsQuickFixTest.java
License:Open Source License
@Test public void testUndefinedConstructorWithLineBreaks() throws Exception { Hashtable hashtable = JavaCore.getOptions(); hashtable.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, "30"); String optionValue = DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, DefaultCodeFormatterConstants.INDENT_DEFAULT); hashtable.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_CONSTRUCTOR_DECLARATION, optionValue);//from ww w .j a v a2 s .c om hashtable.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_EXPLICIT_CONSTRUCTOR_CALL, optionValue); JavaCore.setOptions(hashtable); IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append("package test1;\n"); buf.append("public class F {\n"); buf.append(" public F(Runnable runnable, boolean isGreen, boolean isBlue, boolean isRed) {\n"); buf.append(" }\n"); buf.append("}\n"); pack1.createCompilationUnit("F.java", buf.toString(), false, null); buf = new StringBuffer(); buf.append("package test1;\n"); buf.append("public class E extends F {\n"); buf.append("}\n"); ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null); CompilationUnit astRoot = getASTRoot(cu); ArrayList proposals = collectCorrections(cu, astRoot); assertNumberOfProposals(proposals, 1); assertCorrectLabels(proposals); CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0); String preview = getPreviewContent(proposal); buf = new StringBuffer(); buf.append("package test1;\n"); buf.append("public class E extends F {\n"); buf.append("\n"); buf.append(" public E(\n"); buf.append(" Runnable runnable,\n"); buf.append(" boolean isGreen,\n"); buf.append(" boolean isBlue,\n"); buf.append(" boolean isRed) {\n"); buf.append(" super(\n"); buf.append(" runnable,\n"); buf.append(" isGreen,\n"); buf.append(" isBlue,\n"); buf.append(" isRed);\n"); buf.append(" }\n"); buf.append("}\n"); assertEqualString(preview, buf.toString()); }From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTFacadeHelper.java
License:Open Source License
/** * Gets JavaCore options from JavaCore and updates tab and indentation settings from ControlModel. * //from w w w.ja v a 2 s. c om * @return map of options * * @see #getJavaCoreOptions() * @see JavaCore#getOptions() * @see JControlModel#getLeadingTabReplacement() */ private Map<?, ?> getDefaultJavaCoreOptions() { Map<Object, String> javaCoreOptions = new HashMap<Object, String>(JavaCore.getOptions()); // Set of options that we want to copy from the current definition or use defaults // if (compilerCompliance != null) { javaCoreOptions.put(JavaCore.COMPILER_COMPLIANCE, compilerCompliance); javaCoreOptions.put(JavaCore.COMPILER_SOURCE, compilerCompliance); javaCoreOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, compilerCompliance); } else { useCurrentOption(javaCoreOptions, JavaCore.COMPILER_COMPLIANCE, "1.5"); useCurrentOption(javaCoreOptions, JavaCore.COMPILER_SOURCE, "1.5"); useCurrentOption(javaCoreOptions, JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "1.5"); } useCurrentOption(javaCoreOptions, DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE); useCurrentOption(javaCoreOptions, DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "2"); useCurrentOption(javaCoreOptions, DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, "2"); javaCoreOptions.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, "enabled"); if (getControlModel() != null) { String indent = getControlModel().getLeadingTabReplacement(); if (indent != null && indent.length() > 0) { String size = Integer.toString(indent.length()); if (indent.charAt(0) == '\t') { javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB); javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, size); javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, size); } else if (indent.charAt(0) == ' ') { javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE); javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, size); javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, size); } else { if (DEBUG) { logInfo("Unable to recognize indent string, using java core options."); } } } else { if (DEBUG) { logInfo("Indent is not set, using java core options."); } } } if (DEBUG) { logInfo("Tab char: " + javaCoreOptions.get(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR) + ", Indent size: " + javaCoreOptions.get(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE) + ", Tab size: " + javaCoreOptions.get(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE)); } // Set of options that we want to control javaCoreOptions.put("org.eclipse.jdt.core.incompleteClasspath", "warning"); javaCoreOptions.put("org.eclipse.jdt.core.circularClasspath", "warning"); // javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ENUM_DECLARATION, DefaultCodeFormatterConstants.NEXT_LINE); javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_TYPE_DECLARATION, DefaultCodeFormatterConstants.NEXT_LINE); // separate fields with an empty line javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BEFORE_FIELD, "1"); // make all enum constants to be on separate lines javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, DefaultCodeFormatterConstants.INDENT_DEFAULT)); return javaCoreOptions; }From source file:org.eclipse.ocl.examples.build.utilities.StandaloneASTFacadeHelper.java
License:Open Source License
/** * Gets JavaCore options from JavaCore and updates tab and indentation * settings from ControlModel./* ww w .j a va 2 s . co m*/ * * @return map of options * * @see #getJavaCoreOptions() * @see JavaCore#getOptions() * @see JControlModel#getLeadingTabReplacement() */ @SuppressWarnings("unchecked") private Map<?, ?> getDefaultJavaCoreOptions() { String compilerCompliance = JavaCore.VERSION_1_5; Map<Object, String> javaCoreOptions = JavaCore.getOptions(); // Set of options that we want to copy from the current definition javaCoreOptions.put(JavaCore.COMPILER_COMPLIANCE, compilerCompliance); javaCoreOptions.put(JavaCore.COMPILER_SOURCE, compilerCompliance); javaCoreOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, compilerCompliance); useCurrentOption(javaCoreOptions, DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE); useCurrentOption(javaCoreOptions, DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "2"); useCurrentOption(javaCoreOptions, DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, "2"); if (getControlModel() != null) { String indent = getControlModel().getLeadingTabReplacement(); if (indent != null && indent.length() > 0) { String size = Integer.toString(indent.length()); if (indent.charAt(0) == '\t') { javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB); javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, size); javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, size); } else if (indent.charAt(0) == ' ') { javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE); javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, size); javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE, size); } else { if (DEBUG) { logInfo("Unable to recognize indent string, using java core options."); } } } else { if (DEBUG) { logInfo("Indent is not set, using java core options."); } } } if (DEBUG) { logInfo("Tab char: " + javaCoreOptions.get(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR) + ", Indent size: " + javaCoreOptions.get(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE) + ", Tab size: " + javaCoreOptions.get(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE)); } // Set of options that we want to control javaCoreOptions.put("org.eclipse.jdt.core.incompleteClasspath", "warning"); javaCoreOptions.put("org.eclipse.jdt.core.circularClasspath", "warning"); // javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ENUM_DECLARATION, DefaultCodeFormatterConstants.NEXT_LINE); javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_TYPE_DECLARATION, DefaultCodeFormatterConstants.NEXT_LINE); // separate fields with an empty line javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BEFORE_FIELD, "1"); // make all enum constants to be on separate lines javaCoreOptions.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, DefaultCodeFormatterConstants.INDENT_DEFAULT)); return javaCoreOptions; }From source file:org.eclipse.umlgen.gen.java.services.WorkspaceServices.java
License:Open Source License
/** * Format code of the given project.//from w w w. java 2 s .c om * * @param projectName * The name of the project to format. */ public void formatProjectCode(String projectName) { if (!EMFPlugin.IS_ECLIPSE_RUNNING) { return; } 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)); // Instantiate the default code formatter with the given options final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options); // CHECKSTYLE:OFF project.accept(new IResourceVisitor() { // CHECKSTYLE:ON 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:org.gw4e.eclipse.facade.JDTManager.java
License:Open Source License
/** * Format a Unit Source Code//from w ww . j a v a2 s.c o m * * @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); }From source file:org.onehippo.cms7.essentials.dashboard.utils.JavaSourceUtils.java
License:Apache License
/** * Format java code for given input/*from w ww . ja v a 2 s .c o m*/ * * @param source source to format * @return formatted source code or original on error */ @SuppressWarnings({ RAWTYPES, UNCHECKED }) public static String formatCode(final String source) { final 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); final String alignmentValue = DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, DefaultCodeFormatterConstants.INDENT_ON_COLUMN); options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, alignmentValue); final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options); final TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, source, 0, source.length(), 0, System.getProperty("line.separator")); final ASTParser parser = ASTParser.newParser(AST.JLS3); final IDocument document = new Document(source); parser.setSource(document.get().toCharArray()); try { edit.apply(document); return document.get(); } catch (BadLocationException e) { log.error("Error formatting java code", e); } return source; }