How to use the eclipse code formatter from your code | ssssssssssscripting !!!
Here's how I did it. CodeFormatter is an abstract class, which means you can't instantiate it directly. So, I started to search about classes derived from it, and so I found out about DefaultCodeFormatter , which I presumed was what I was looking for. I was right. DefaultCodeFormatter implements the method : public abstract TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) Method documentation can be read here . I wrote the following code to test it : ... main(String[] args) { String code = "public class geo{public static void main(String[] args){System.out.println(\"geo\");}}"; CodeFormatter cf = new DefaultCodeFormatter(); TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, code, 0,code.length(),0,null); System.out.println(te); } I used the CodeFormatter.K_UNKNOWN constant for the formatting, because I was thinking that the CodeFormatter will "deduce" the type of code I've passed it. I'm not really sure what K_UNKNOWN does,Read full article from How to use the eclipse code formatter from your code | ssssssssssscripting !!!
No comments:
Post a Comment