Search in sources :

Example 1 with CodeFormatter

use of org.eclipse.cdt.core.formatter.CodeFormatter in project ch.hsr.ifs.cdttesting by IFS-HSR.

the class TestProjectHolder method formatFileAsync.

@Override
public ProjectHolderJob formatFileAsync(IPath path) {
    return ProjectHolderJob.create("Formatting project " + projectName, ITestProjectHolder.FORMATT_FILE_JOB_FAMILY, mon -> {
        if (!formattedDocuments.contains(path)) {
            final IDocument doc = getDocument(getFile(path));
            final Map<String, Object> options = new HashMap<>(cProject.getOptions(true));
            try {
                final ITranslationUnit tu = CoreModelUtil.findTranslationUnitForLocation(path, cProject);
                options.put(DefaultCodeFormatterConstants.FORMATTER_TRANSLATION_UNIT, tu);
                final CodeFormatter formatter = ToolFactory.createCodeFormatter(options);
                final TextEdit te = formatter.format(CodeFormatter.K_TRANSLATION_UNIT, path.toOSString(), 0, doc.getLength(), 0, NL);
                te.apply(doc);
                formattedDocuments.add(path);
            } catch (CModelException | MalformedTreeException | BadLocationException e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : CodeFormatter(org.eclipse.cdt.core.formatter.CodeFormatter) HashMap(java.util.HashMap) TextEdit(org.eclipse.text.edits.TextEdit) CModelException(org.eclipse.cdt.core.model.CModelException) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IDocument(org.eclipse.jface.text.IDocument) ITranslationUnit(org.eclipse.cdt.core.model.ITranslationUnit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

HashMap (java.util.HashMap)1 CodeFormatter (org.eclipse.cdt.core.formatter.CodeFormatter)1 CModelException (org.eclipse.cdt.core.model.CModelException)1 ITranslationUnit (org.eclipse.cdt.core.model.ITranslationUnit)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)1 TextEdit (org.eclipse.text.edits.TextEdit)1