在Eclipse默认编辑器中查找插入符号下的字符串

问题描述 投票:0回答:1

我想在Eclipse中以编程方式在插入位置下获取字符串。

我已经看过这个链接:How to get cursor position in an eclipse TextEditor但是在这个链接中,必须选择这个词(我想得到的)。在我的问题中,它没有被选中。例如,源代码是:

class HelloWorld 
{ 
   public static void main(String args[]) 
   { 
       System.out.println("Hello, World"); 
   } 
} 

想象一下,插入符号位于此方法中的任何单词的中间。我使用|符号而不是插入符号来解释自己。

Syst|em.out.println("Hello, World"); 

对于这个例子,我必须得到“系统”字样。

有没有办法得到这个?

java eclipse eclipse-plugin editor eclipse-rcp
1个回答
0
投票

您可以使用以下命令获取文本编辑器部件使用的StyledText控件:

StyledText styledText = (StyledText)editorPart.getAdapter(Control.class);

StyledText有一个getCaretOffset方法:

int offset = styledText.getCaretOffset();
© www.soinside.com 2019 - 2024. All rights reserved.