Eclipse JDT:调用“正确缩进”以编程方式?
我正在开发一个 Eclipse 插件,该插件可以修改用户项目中的 Java 代码。
基本上这个插件的结果是Java注释被添加到一些方法中,所以
void foo() { ... }
变成了
@MyAnnotation
void foo() { ... }
除了它看起来不太像那样;新插入的注释上的缩进很奇怪(具体来说,新注释一直到该行的左侧)。我想对文件进行所有更改,然后以编程方式调用“正确缩进”。
有谁知道该怎么做?我在这里或 JDT 论坛中找不到答案,所有看起来相关的类(IndentAction、JavaIndenter)都在我不应该使用的内部包中......
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我想我可能已经找到了我想要的解决方案。我想我应该在询问之前花更多时间搜索...但为了将来的参考,这就是我所做的!好东西在 ToolFactory 中...
这会重新格式化整个文件。如果您需要重新格式化更少,还有其他选择...
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...
This reformats the entire file. There are other options if you need to reformat less...
在处理 Java 代码时添加缩进可能更容易。
您的 Eclipse 插件必须读取
void foo() { ... }
行才能知道添加@MyAnnotation
,对吧?只需从 Java 行获取缩进,并将注释附加到缩进即可。It's probably easier to add the indentation as you process the Java code.
Your Eclipse plugin had to read the
void foo() { ... }
line to know to add the@MyAnnotation
, right? Just get the indentation from the Java line, and append your annotation to the indentation.