0

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?

devmaria
  • 27
  • 7

1 Answers1

0

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();
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Thanks for the answer. Unfortunately, this it not what i want actually. It seems i couldn't explain myself correctly. I have edited question, would you look at it again? – devmaria Apr 03 '19 at 11:40
  • Well what do mean by 'word'? What if the caret is in the "Hello, World" string? If you want the current programming language element, that is a much more complex question. – greg-449 Apr 03 '19 at 12:32
  • İf caret is in somewhere in Hello, World i have to get 'Hello' or 'World' again. Actually i wanna find the word( i mean any word some string or statement, anything in the source code) to find the caret is in any method or not. When i find the caret in a method, i want to invoke an action. I did it in Visual Studio for my plugins one of action. Now, i want to add that action to my Eclipse plugin. – devmaria Apr 03 '19 at 14:36