7

我正在使用 JDT 生成一些类。之后我想格式化整个 ICompilationUnit,就像我在没有选择的情况下在打开的编辑器中按下 Ctrl+Shift+F(Source > Format)一样。

高度赞赏 JDT 中任何用于以编程方式格式化源代码的 API 指针。

补充:我试过这样,但代码没有改变。我在发什么?

private void formatUnitSourceCode(ICompilationUnit targetUnit, IProgressMonitor monitor) throws JavaModelException {
    CodeFormatter formatter = ToolFactory.createCodeFormatter(null);
    TextEdit formatEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, targetUnit.getSource(), 0, targetUnit.getSource().length(), 0, null);
    targetUnit.applyTextEdit(formatEdit, monitor);
}
4

3 回答 3

6

这可能是一个错误,但使用 Elcipse 4.2.2 中的 JDK,必须创建 ICompilationUnit 的工作副本才能将 TextEdit 应用于文件。

    targetUnit.becomeWorkingCopy(new SubProgressMonitor(monitor, 1));
    ... do work on the source file ...
    formatUnitSourceCode(targetUnit, new SubProgressMonitor(monitor, 1));
    targetUnit.commitWorkingCopy(true, new SubProgressMonitor(monitor, 1));

格式化本身是这样完成的:

public static void formatUnitSourceCode(ICompilationUnit unit, IProgressMonitor monitor) throws JavaModelException {
    CodeFormatter formatter = ToolFactory.createCodeFormatter(null);
    ISourceRange range = unit.getSourceRange();
    TextEdit formatEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, unit.getSource(), range.getOffset(), range.getLength(), 0, null);
    if (formatEdit != null && formatEdit.hasChildren()) {
        unit.applyTextEdit(formatEdit, monitor);
    } else {
        monitor.done();
    }
}
于 2013-04-16T09:26:09.993 回答
2

使用JDT 生成一些类时,可以在源代码中添加“\t”。或者像你所做的那样,使用代码格式化程序。我已经测试了以下代码:

public static void main(String[] args)
{
    String code = "public class TestFormatter{public static void main(String[] args){System.out.println(\"Hello World\");}}";
    CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);

    TextEdit textEdit = codeFormatter.format(CodeFormatter.K_UNKNOWN, code, 0,code.length(),0,null);
    IDocument doc = new Document(code);
    try {
        textEdit.apply(doc);
        System.out.println(doc.get());
    } catch (MalformedTreeException e) {
        e.printStackTrace();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }   
}

apply()方法在这里解决了问题。

于 2013-04-15T12:43:42.057 回答
0

我使用下面的方法来格式化一个 Java Source 文件

    public static void formatSource(ICompilationUnit cu, IProgressMonitor progressMonitor) throws JavaModelException{
    String source = cu.getSource();
    IJavaProject javaProject = cu.getJavaProject();

    Map options = javaProject.getOptions(true);


            // Instantiate the default code formatter with the given options
            final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options);


            final TextEdit edit = codeFormatter.format(
                CodeFormatter.K_COMPILATION_UNIT, // format a compilation unit
                source, // source to format
                0, // starting position
                source.length(), // length
                0, // initial indentation
                System.getProperty("line.separator") // line separator
            );          

            cu.becomeWorkingCopy(progressMonitor);
            try {
                cu.applyTextEdit(edit, progressMonitor);
                //cu.reconcile();
                cu.commitWorkingCopy(true, progressMonitor);
                //cu.save(progressMonitor, false);

                JavaUI.openInEditor(cu);


            } catch (MalformedTreeException e) {
                e.printStackTrace();
            } catch (PartInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   

}
于 2013-11-29T12:47:44.493 回答