package com.redhat.ceylon.eclipse.util;

import java.util.HashSet;
import java.util.Set;

import com.redhat.ceylon.compiler.typechecker.model.Declaration;
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 FindReferenceVisitor extends Visitor {
	
	private final Declaration declaration;
	private final Set<Node> nodes = new HashSet<Node>();
	
	public FindReferenceVisitor(Declaration declaration) {
		this.declaration = declaration;
	}
	
	public Set<Node> getNodes() {
		return nodes;
	}
	
	protected boolean equals(Declaration x, Declaration y) {
	    return x!=null && y!=null && x.equals(y);
	}
	
    @Override
    public void visit(Tree.ExtendedTypeExpression that) {}
    
	@Override
	public void visit(Tree.MemberOrTypeExpression that) {
		//TODO: handle refinement!
		if (equals(that.getDeclaration(), declaration)) {
			nodes.add(that);
		}
		super.visit(that);
	}
		
	@Override
	public void visit(Tree.NamedArgument that) {
		if (equals(that.getParameter(), declaration)) {
			nodes.add(that);
		}
		super.visit(that);
	}
		
	@Override
	public void visit(Tree.SimpleType that) {
		if (equals(that.getTypeModel().getDeclaration(), declaration)) {
			nodes.add(that);
		}
		super.visit(that);
	}
	
	/*@Override
	public void visit(Tree.SyntheticVariable that) {}*/

	@Override
	public void visit(Tree.ImportMemberOrType that) {
		if (equals(that.getDeclarationModel(), declaration)) {
			nodes.add(that);
		}
		super.visit(that);
	}
		
}

@@ -627,15 +627,13 @@
 %0A%09pr
-otec
+iva
 te
-d
  boo
@@ -637,22 +637,27 @@
 boolean 
-equals
+isReference
 (Declara
@@ -661,17 +661,19 @@
 aration 
-x
+ref
 , Declar
@@ -678,17 +678,19 @@
 aration 
-y
+dec
 ) %7B%0A%09   
@@ -697,17 +697,19 @@
  return 
-x
+ref
 !=null &
@@ -710,17 +710,19 @@
 null && 
-y
+dec
 !=null &
@@ -727,18 +727,23 @@
  && 
-x.equals(y
+dec.refines(ref
 );%0A%09
@@ -921,30 +921,35 @@
 ment!%0A%09%09if (
-equals
+isReference
 (that.getDec
@@ -1082,38 +1082,43 @@
 t that) %7B%0A%09%09if (
-equals
+isReference
 (that.getParamet
@@ -1242,38 +1242,43 @@
 e that) %7B%0A%09%09if (
-equals
+isReference
 (that.getTypeMod
@@ -1505,14 +1505,19 @@
 if (
-equals
+isReference
 (tha

fix Find References and Mark Occurrences for refined members