[英]Highlight text from jdt java text editor using line numbers in eclipse plugin
I am trying to write an eclipse plugin which highlights some text in java editor after user save the text (ResourceChangeListener).我正在尝试编写一个 eclipse 插件,它在用户保存文本(ResourceChangeListener)后在 java 编辑器中突出显示一些文本。 I am implementing ILightweightLabelDecorator and extending BaseLabelProvider , The method
我正在实施ILightweightLabelDecorator并扩展BaseLabelProvider ,该方法
public void decorate(Object arg0, IDecoration arg1)
getting called but I am getting Objects of type org.eclipse.jdt.internal.core.* eg org.eclipse.jdt.internal.core.PackageDeclaration.被调用,但我得到类型为 org.eclipse.jdt.internal.core.* 的对象,例如 org.eclipse.jdt.internal.core.PackageDeclaration。 I need line numbers from that object so I can highlight that text.
我需要来自该对象的行号,以便我可以突出显示该文本。 ASTNode object has a property to get the position (line numbers) but I am not getting that one.
ASTNode 对象有一个属性来获取位置(行号),但我没有得到那个。 How can I get ASTNode from org.eclipse.jdt.internal.core.* objects?
如何从 org.eclipse.jdt.internal.core.* 对象获取 ASTNode?
Thanks in advance.提前致谢。
PackageDeclaration
is part of the JDT Java Model which is a lighter weight version of the AST used by a lot of the Java code. PackageDeclaration
是 JDT Java 模型的一部分,它是许多 Java 代码使用的 AST 的轻量级版本。 As such it isn't related to ASTNode
.因此,它与
ASTNode
。
Many Java Model objects (including PackageDeclaration
) implement ISourceReference
which tells you about the source code.许多 Java 模型对象(包括
PackageDeclaration
)都实现了ISourceReference
,它告诉您有关源代码的信息。 This includes getSource
and getSourceRange
methods.这包括
getSource
和getSourceRange
方法。
We can use the following method for accessing line number,我们可以使用以下方法来访问行号,
private int getLineNumberInSource(SourceRefElement member) throws
JavaModelException {
if (member == null || !member.exists()) {
return -1;
}
ICompilationUnit compilationUnit = member.getCompilationUnit();
if (compilationUnit == null) {
return -1;
}
String fullSource = compilationUnit.getBuffer().getContents();
if (fullSource == null) {
return -1;
}
ISourceRange nameRange = member.getNameRange();
if (nameRange == null) {
return -1;
}
String string2 = fullSource.substring(0, nameRange.getOffset());
return
string2.split(compilationUnit.findRecommendedLineSeparator()).length;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.