Using the CDT code formatter from another plugin [message #807618] |
Sun, 26 February 2012 14:42  |
Eclipse User |
|
|
|
Hi,
I'm working on a plugin that as one of it's outcomes produces some C code that it writes to a file on the file system.
I would really like to format this C code using the CDT code formatter (I guess the org.eclipse.cdt.core.formatter.CodeFormatter class would be relevant here).
Is it possible to set up and call the CDT code formatter from some Java code in another plugin? Any input on how I might go about this is greatly appreciated.
Kind regards,
- Stephan
|
|
|
Re: Using the CDT code formatter from another plugin [message #807679 is a reply to message #807618] |
Sun, 26 February 2012 16:54   |
Eclipse User |
|
|
|
I found the answer myself It's like finally deciding to go to the doctor, and then you get well....
Here is my current solution,
package icecaptools.compiler;
import org.eclipse.cdt.core.formatter.CodeFormatter;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;
public class EclipseCCodeFormatter implements IcecapCodeFormatter {
private CodeFormatter codeFormatter;
public EclipseCCodeFormatter()
{
codeFormatter = org.eclipse.cdt.core.ToolFactory.createDefaultCodeFormatter(null);
}
public String format(String source) {
// int kind, String source, int offset, int length, int indentationLevel, String lineSeparator
TextEdit edit = codeFormatter.format(0, source, 0, source.length(), 0, null);
IDocument document= new Document(source);
try {
edit.apply(document);
} catch (MalformedTreeException e) {
;
} catch (BadLocationException e) {
;
}
String formattedSource = document.get();
return formattedSource;
}
}
It is absolutely fantastic what you can do with just a couple of lines of code in the open source world 
Thanks to whoever made this stuff available!
- Stephan
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04621 seconds