Example usage for org.eclipse.jdt.core.formatter DefaultCodeFormatterConstants INDENT_ON_COLUMN

List of usage examples for org.eclipse.jdt.core.formatter DefaultCodeFormatterConstants INDENT_ON_COLUMN

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.formatter DefaultCodeFormatterConstants INDENT_ON_COLUMN.

Prototype

int INDENT_ON_COLUMN

To view the source code for org.eclipse.jdt.core.formatter DefaultCodeFormatterConstants INDENT_ON_COLUMN.

Click Source Link

Document

 FORMATTER / The wrapping is done by indenting on column under the splitting location. 

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 w w  . j a  va 2s .  c  om*/
        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;//from   w  w  w.  java 2 s. 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: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);
    }// w  w  w.  ja va 2 s.  com
    return codeFormatter;
}

From source file:org.eclipse.umlgen.gen.java.services.WorkspaceServices.java

License:Open Source License

/**
 * Format code of the given project.//  ww w .j  ava  2  s  .c  o  m
 *
 * @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  w  w  .  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  w  w.  ja  va2  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;
}