How can I use an eclipse indenter from my code?

I noticed that the eclipse indenter supports the latest version of java, and it would be nice if I could use this class to create javas source code. Is there any way to integrate it?

EDIT: I need to include code formatting code in my code. No external calls.

EDIT2: I managed to get it to work. You can read the story here . Thanks VonC!

+3
source share
3 answers

You can try running the formatter as a standalone application (also more details here ).

eclipse -vm <path to virtual machine> -application org.eclipse.jdt.core.JavaCodeFormatter [ OPTIONS ] <files>

eclipse IDE, , eclipse.exe.
. " Formatter"

eclipse [...] -config <myExportedSettings> 

java- :


Geo , , DefaultCodeFormatter

    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);
    IDocument dc = new Document(code);
    try {
        te.apply(dc);
        System.out.println(dc.get());
    } catch (MalformedTreeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

, . , Geo !


Thorbjørn Ravn Andersen :

Maven2 Java Formatter Plugin v0.4 maven, Maven Eclipse.
0.4, Eclipse 3.5, Java 8.

+5

, VonC: DefaultCodeFormatter !

stackoverflow .

, ToolFactory,

ToolFactory.createCodeFormatter(null);
+1

CodeFormatter Eclipse . , ToolFactory.createCodeFormatter(null);, - format() .

:

Hashtable<String, String> options = new Hashtable<>();
options.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "1.8");
options.put("org.eclipse.jdt.core.compiler.compliance", "1.8");
options.put("org.eclipse.jdt.core.compiler.source", "1.8");
CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options);
0

Source: https://habr.com/ru/post/1698550/


All Articles