简体   繁体   中英

Find string under the caret in Eclipse default editor

I wanna get string under the caret position programmatically in 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. 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)editorPart.getAdapter(Control.class);

StyledText has a getCaretOffset method:

int offset = styledText.getCaretOffset();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM