[英]Find string under the caret in Eclipse default editor
I wanna get string under the caret position programmatically in Eclipse. 我想在Eclipse中以编程方式在插入位置下获取字符串。
I've already seen this link: How to get cursor position in an eclipse TextEditor but in this link, the word(which i want to get) has to selected. 我已经看过这个链接: 如何在日食TextEditor中获取光标位置,但在此链接中,必须选择单词(我想要获得)。 In my question it has not to be selected.
在我的问题中,它没有被选中。 For example source code is:
例如,源代码是:
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello, World");
}
}
Imagine the caret is in the middle of the any word in this method. 想象一下,插入符号位于此方法中的任何单词的中间。 I used
|
我用过
|
symbol instead of a caret to explain myself. 符号而不是插入符号来解释自己。
Syst|em.out.println("Hello, World");
For this example i have to get the "System" word. 对于这个例子,我必须得到“系统”字样。
Is there any way to get this? 有没有办法得到这个?
You can get the StyledText
control used by a text editor part using: 您可以使用以下命令获取文本编辑器部件使用的
StyledText
控件:
StyledText styledText = (StyledText)editorPart.getAdapter(Control.class);
StyledText
has a getCaretOffset
method: StyledText
有一个getCaretOffset
方法:
int offset = styledText.getCaretOffset();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.