2

I am working on an eclipse plugin, and I need to get the selected element from the active editor. I have already figured out (one way) to do this. Here are the key lines:

ITypeRoot root = EditorUtility.getEditorInputJavaElement(activeJavaEditor, false);
selectedElement = root.getElementAt(offset);

(This is similar to the answer the question Eclipse plugin : Get the enclosing class and member name)

But the problem that I'm having is that this will only return declared methods, types, fields, etc, and never invoked ones. For example, given this code in the active editor:

public void foo() {
    ClassA.run();
}

even if the caret is positioned over "ClassA" or "run," my code still returns the method "foo." Does anyone know how to also get invoked methods?

Community
  • 1
  • 1
Reyan
  • 584
  • 1
  • 5
  • 16
  • This thread may help you: http://stackoverflow.com/questions/10453617/how-can-i-get-the-selected-code-in-eclipse/ – Alex Jun 07 '12 at 20:27

1 Answers1

1

Try ITypeRoot#codeSelect() instead.

nitind
  • 19,089
  • 4
  • 34
  • 43
  • I'm not familiar with the `Class#method()` notation. Could you please clarify? Where in @Reyan's code should this call go? – StockB Feb 13 '13 at 20:11
  • '#' is just a variant of '.' In this case, it just means to call codeSelect() on an object of type ITypeRoot. Thus I replaced "root.getElementAt(offset)" with "root.codeSelect(offset, length)" – Reyan Feb 15 '13 at 17:23