|
Re: Using the CDT code formatter from another plugin [message #807679 is a reply to message #807618] |
Sun, 26 February 2012 21:54 |
Stephan Korsholm Messages: 3 Registered: February 2012 |
Junior Member |
|
|
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.03790 seconds