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();
}
}
});
}
Aggregations