Eclipse JDT:以编程方式调用“正确的缩进” [英] Eclipse JDT: Call 'correct indentation' programmatically?
问题描述
这个插件的结果基本上是将Java注释添加到某些方法中,所以
void foo(){...}
成为
@MyAnnotation
void foo(){...}
除了它不太像这样;新插入的注释的缩进是wack(具体来说,新注释一直到该行的左侧)。我想对文件进行所有更改,然后以编程方式调用正确缩进。
有谁知道如何做?我找不到答案在这里或在JDT论坛,所有看起来相关的类(IndentAction,JavaIndenter)都是内部的包,我不应该使用...
谢谢!
我想我可能已经想出了我想要的解决方案。猜猜我应该花更多的时间来搜索...但是为了将来参考,这里是我做的!好的东西在ToolFactory ...
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.jdt.core.ICompilationUnit;
...
ICompilationUnit cu = ...
...
CodeFormatter formatter = ToolFactory.createCodeFormatter (空值);
ISourceRange range = cu.getSourceRange();
TextEdit indent_edit =
formatter.format(CodeFormatter.K_COMPILATION_UNIT,
cu.getSource(),range.getOffset(),range.getLength(),0,null);
cu.applyTextEdit(indent_edit,null);
cu.reconcile();
重新格式化整个文件。如果您需要重新格式化,还有其他选项...
I am working on an Eclipse plugin that modifies Java code in a user's project.
Basically the result of this plugin is that Java annotations are added to some methods, so
void foo() { ... }
becomes
@MyAnnotation
void foo() { ... }
Except that it doesn't quite look like that; the indentation on the newly inserted annotation is wack (specifically, the new annotation is all the way to the left-hand side of the line). I'd like to make all my changes to the file, and then programmatically call "Correct Indentation."
Does anyone know how to do this? I can't find the answer here or in the JDT forums, and all the classes that look relevant (IndentAction, JavaIndenter) are in internal packages which I'm not supposed to use...
Thanks!
Well I think I may have figured out the solution I want. Guess I should have spend more time searching before asking... but for future reference, here's what I did! The good stuff was in the ToolFactory...
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.jdt.core.ICompilationUnit;
...
ICompilationUnit cu = ...
...
CodeFormatter formatter = ToolFactory.createCodeFormatter(null);
ISourceRange range = cu.getSourceRange();
TextEdit indent_edit =
formatter.format(CodeFormatter.K_COMPILATION_UNIT,
cu.getSource(), range.getOffset(), range.getLength(), 0, null);
cu.applyTextEdit(indent_edit, null);
cu.reconcile();
This reformats the entire file. There are other options if you need to reformat less...
这篇关于Eclipse JDT:以编程方式调用“正确的缩进”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!