package com.redhat.ceylon.eclipse.code.open;

import static com.redhat.ceylon.eclipse.code.editor.Navigation.gotoNode;
import static com.redhat.ceylon.eclipse.util.EditorUtil.getCurrentEditor;
import static com.redhat.ceylon.eclipse.util.EditorUtil.getSelection;
import static com.redhat.ceylon.eclipse.util.Nodes.findNode;
import static com.redhat.ceylon.eclipse.util.Nodes.getReferencedModel;
import static com.redhat.ceylon.eclipse.util.Nodes.getReferencedNode;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.ui.IEditorPart;

import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.eclipse.code.editor.CeylonEditor;
import com.redhat.ceylon.eclipse.code.parse.CeylonParseController;

public class OpenSelectedDeclarationHandler extends AbstractHandler {
    
    private Node getSelectedNode(ITextSelection textSel) {
        CeylonEditor editor = (CeylonEditor) getCurrentEditor();
        CeylonParseController pc = editor.getParseController();
        if (pc==null) {
            return null;
        }
        else {
            Tree.CompilationUnit ast = pc.getRootNode();
            if (ast == null) {
                return null;
            }
            else {
                return findNode(ast, textSel.getOffset());
            }
        }
    }
    
    public boolean isEnabled() {
        IEditorPart editor = getCurrentEditor();
        if (super.isEnabled() && editor instanceof CeylonEditor) {
            CeylonEditor ce = (CeylonEditor) editor;
            return getReferencedModel(getSelectedNode(getSelection(ce)))!=null;
        }
        else {
            return false;
        }
                
    }
    
    @Override
    public Object execute(ExecutionEvent event) 
            throws ExecutionException {
        IEditorPart editor = getCurrentEditor();
        if (editor instanceof CeylonEditor) {
            CeylonEditor ceylonEditor = (CeylonEditor) editor;
            Node selectedNode = getSelectedNode(getSelection(ceylonEditor));
            Node ref = getReferencedNode(selectedNode);
            if (ref!=null) {
                gotoNode(ref, ceylonEditor);
            }
        }
        return null;
    }
        
}
@@ -106,20 +106,27 @@
 ion.goto
-Node
+Declaration
 ;%0Aimport
@@ -398,78 +398,8 @@
 del;
-%0Aimport static com.redhat.ceylon.eclipse.util.Nodes.getReferencedNode;
 %0A%0Aim
@@ -630,16 +630,83 @@
 rPart;%0A%0A
+import com.redhat.ceylon.compiler.typechecker.model.Referenceable;%0A
 import c
@@ -2115,26 +2115,16 @@
 ditor ce
-ylonEditor
  = (Ceyl
@@ -2204,26 +2204,16 @@
 ction(ce
-ylonEditor
 ));%0A    
@@ -2220,19 +2220,28 @@
         
-Nod
+Referenceabl
 e ref = 
@@ -2253,20 +2253,21 @@
 ferenced
-N
+M
 ode
+l
 (selecte
@@ -2327,30 +2327,23 @@
 goto
-Node(ref, ceylonEditor
+Declaration(ref
 );%0A 

Revert "for consistency, use same strategy as with hyperlink navigation"