Eclipse JDT:调用“正确缩进”以编程方式?

发布于 2024-09-02 19:06:25 字数 387 浏览 5 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身情人 2024-09-09 19:06:25

好吧,我想我可能已经找到了我想要的解决方案。我想我应该在询问之前花更多时间搜索...但为了将来的参考,这就是我所做的!好东西在 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();

这会重新格式化整个文件。如果您需要重新格式化更少,还有其他选择...

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...

兔姬 2024-09-09 19:06:25

在处理 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文