How to get the selected code in Eclipse?

For my plugin, I am trying to access the selected code in CompilationUnitEditor. Therefore, I added a contribution to the context menu and used the following code:

public class ContextMenuHandler implements IEditorActionDelegate { private IEditorPart editorPart; @Override public void setActiveEditor(IAction action, IEditorPart editorPart) { this.editorPart = editorPart; } @Override public void run(IAction action) { JavaUI.getEditorInputJavaElement(editorPart.getEditorInput()); } @Override public void selectionChanged(IAction action, ISelection selection) { if (selection instanceof TextSelection) { TextSelection text = (TextSelection) selection; System.out.println("Text: " + text.getText()); } else { System.out.println(selection); } } } 

Now the problem is that the selectionChanged (...) method is only called when I really select something so that I can copy / paste it. But I want to access the code elements that are highlighted this way (here I would like to get "IEditorPart")

enter image description here

Unfortunately, I have no idea what I should look for.

+6
source share
3 answers

Using inputs from other answers, I got the following solution:

 @Override public void setActiveEditor(IAction action, IEditorPart editorPart) { ((CompilationUnitEditor) editorPart).getViewer().addTextListener(new ITextListener() { @Override public void textChanged(TextEvent event) { selectedText = event.getText(); } }); } 
+1
source

You must do this:

  ((CompilationUnitEditor) editorPart).getViewer().getSelectedRange(); 

The ISourceViewer class has many useful and interesting methods regarding the location of sources and the editor. You can also see the JavaSourceViewer .


EDIT

It seems that I did not quite answer your question. The problem is that selectionChanged events are raised only when the selection length is> 0. I don’t know why this is so, but action delegates do this.

If you want to be notified of every change in carriage, you need to register the selected listener using the viewer for the editor. Do something like this:

 ((CompilationUnitEditor) editorPart).getViewer() .addSelectionChangedListener(mySelectionListener); 

mySelectionListener is of type org.eclipse.jface.viewers.ISelectionChangedListener . By registering in this way, you must provide you with all the events you are looking for. Just be careful to unregister when the editor closes.

+7
source

It would not be easier to determine the current position of the carriage. Having a position allows you to easily determine if the carriage is on the word (define the word as you like, for example, separated by spaces, java identifier or regular expression).

I cannot start eclipse here, but I would use the CaretListener class to detect carriage movements and thus extract the word under it. CaretEvent specified as a parameter to the caretMoved method will contain the offset.

CaretListener can bind CaretListener to the Adapter your StyledText component, which you can get from your EditorPart (at the moment there is no more information, since I do not have eclipse here).

Hope this helps.

Edit: some code.

 final StyledText text = (StyledText)editorPart.getAdapter(Control.class); text.addCaretListener(new CaretListener() { public void caretMoved(CaretEvent event) { int offset = event.caretOffset; String word = findWord(offset, text); if (word.length() > 0) { System.out.println("Word under caret: " + word); } } }); private String findWord(int offset, StyledText text) { int lineIndex = text.getLineAtOffset(offset); int offsetInLine = offset - text.getOffsetAtLine(lineIndex); String line = text.getLine(lineIndex); StringBuilder word = new StringBuilder(); if (offsetInLine > 0 && offsetInLine < line.length()) { for (int i = offsetInLine; i >= 0; --i) { if (!Character.isSpaceChar(line.charAt(i))) { word.append(line.charAt(i)); } else { break; } } word = word.reverse(); } if (offsetInLine < line.length()) { for (int i = offsetInLine; i < line.length(); ++i) { if (i == offsetInLine) continue; // duplicate if (!Character.isSpaceChar(line.charAt(i))) { word.append(line.charAt(i)); } else { break; } } } return word.toString(); } 

This is a simple implementation to get the word under the cursor based on the space surrounding it. To detect valid Java identifiers, etc. A more robust implementation should be used. For example, using Character.isJavaIdentifierStart and Character.isJavaIdentifierPart or using the library for this.

+3
source

Source: https://habr.com/ru/post/914919/


All Articles