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

import com.redhat.ceylon.compiler.typechecker.model.Declaration;
import com.redhat.ceylon.compiler.typechecker.model.Referenceable;
import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.compiler.typechecker.tree.Visitor;

public class FindReferencedNodeVisitor extends Visitor {
	
	private final Referenceable declaration;
	private Node declarationNode;
	
	public FindReferencedNodeVisitor(Referenceable declaration) {
		this.declaration = declaration;
	}
	
	public Node getDeclarationNode() {
		return declarationNode;
	}
	
    private boolean isDeclaration(Declaration dec) {
        return dec!=null && dec.equals(declaration);
    }
    
    @Override
    public void visit(Tree.ModuleDescriptor that) {
    	super.visit(that);
    	Referenceable m = that.getImportPath().getModel();
    	if (m!=null && m.equals(declaration)) {
    		declarationNode = that;
    	}
    }
    
    @Override
    public void visit(Tree.PackageDescriptor that) {
    	super.visit(that);
    	Referenceable p = that.getImportPath().getModel();
    	if (p!=null && p.equals(declaration)) {
    		declarationNode = that;
    	}
    }
    
	@Override
	public void visit(Tree.Declaration that) {
		if (isDeclaration(that.getDeclarationModel())) {
			declarationNode = that;
		}
		super.visit(that);
	}
	
    @Override
    public void visit(Tree.ObjectDefinition that) {
        if (isDeclaration(that.getDeclarationModel()
        		.getTypeDeclaration())) {
            declarationNode = that;
        }
        super.visit(that);
    }
    
	public void visitAny(Node node) {
		if (declarationNode==null) {
			super.visitAny(node);
		}
	}
	
}

@@ -1650,16 +1650,278 @@
  %7D%0A    %0A
+    @Override%0A    public void visit(Tree.SpecifierStatement that) %7B%0A        if (that.getRefinement()) %7B%0A            if (isDeclaration(that.getDeclaration())) %7B%0A                declarationNode = that;%0A            %7D%0A        %7D%0A        super.visit(that);%0A    %7D%0A    %0A
 %09public 

fix navigation to shortcut refinement