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

import java.util.Iterator;
import java.util.List;

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

class DetectUnusedImportsVisitor extends Visitor {
    
    private final List<Declaration> result;
    
    DetectUnusedImportsVisitor(List<Declaration> result) {
    	this.result = result;
    }
    
    @Override
    public void visit(Tree.Import that) {
        super.visit(that);
        for (Tree.ImportMemberOrType i: that.getImportMemberOrTypeList()
                .getImportMemberOrTypes()) {
            if (i.getDeclarationModel()!=null)
                result.add(i.getDeclarationModel());
            if (i.getImportMemberOrTypeList()!=null) {
                for (Tree.ImportMemberOrType j: i.getImportMemberOrTypeList()
                        .getImportMemberOrTypes()) {
                    if (j.getDeclarationModel()!=null)
                        result.add(j.getDeclarationModel());
                }
            }
        }
    }
    
    
    @Override
    public void visit(Tree.BaseMemberOrTypeExpression that) {
        super.visit(that);
        Declaration d = that.getDeclaration();
        if (d!=null) {
            for (Iterator<Declaration> it = result.iterator();
                    it.hasNext();) {
                if ( it.next().equals(d) ) {
                    it.remove();
                }
            }
        }
    }
    
    @Override
    public void visit(Tree.BaseType that) {
        super.visit(that);
        for (Iterator<Declaration> it = result.iterator();
                it.hasNext();) {
            if ( it.next().equals(that.getDeclarationModel()) ) {
                it.remove();
            }
        }
    } 
    
    @Override
    public void visit(Tree.MemberLiteral that) {
        super.visit(that);
        Declaration d = that.getDeclaration();
        if (d!=null && that.getType()==null) {
            for (Iterator<Declaration> it = result.iterator();
                    it.hasNext();) {
                if ( it.next().equals(d) ) {
                    it.remove();
                }
            }
        }
    }

}

@@ -42,16 +42,102 @@
 ports;%0A%0A
+import static com.redhat.ceylon.compiler.typechecker.model.Util.isOverloadedVersion;%0A%0A
 import j
@@ -1228,158 +1228,44 @@
     
-%0A    @Override%0A    public void visit(Tree.BaseMemberOrTypeExpression that) %7B%0A        super.visit(that);%0A        Declaration d = that.getDeclaration();
+private void remove(Declaration d) %7B
 %0A   
@@ -1506,32 +1506,36 @@
     %7D%0A    %7D%0A    
+    
 %0A    @Override%0A 
@@ -1564,20 +1564,38 @@
 ree.Base
-Type
+MemberOrTypeExpression
  that) %7B
@@ -1634,210 +1634,206 @@
     
-for (Iterator%3CDeclaration%3E it = result.iterator(
+remove(getAbstraction(that.getDeclaration())
 );%0A    
-  
+%7D%0A%0A
     
-      it.hasNext();) %7B%0A            if ( it.next().equals(that.getDeclarationModel()) ) %7B%0A                it.remove();%0A            %7D%0A        %7D
+@Override%0A    public void visit(Tree.BaseType that) %7B%0A        super.visit(that);%0A        remove(getAbstraction(that.getDeclarationModel()));
 %0A   
@@ -1943,70 +1943,12 @@
     
-Declaration d = that.getDeclaration();%0A        if (d!=null && 
+if (
 that
@@ -1983,56 +1983,52 @@
     
-for (Iterator%3CDecla
+remove(getAbst
 ra
+c
 tion
-%3E it = result.iterator(
+(that.getDeclaration())
 );%0A 
@@ -2038,33 +2038,73 @@
     
+%7D%0A
     
+%7D%0A
     
+%0A
     
-it.hasNext();
+private Declaration getAbstraction(Declaration d
 ) %7B%0A
@@ -2115,41 +2115,34 @@
     
-  
-      if ( it.next().equals
+if (isOverloadedVersion
 (d)
- 
 ) %7B%0A
@@ -2153,34 +2153,80 @@
         
-        it.remove(
+return d.getContainer().getDirectMember(d.getName(), null, false
 );%0A     
@@ -2224,32 +2224,34 @@
 e);%0A        
+%7D%0A
         
 %7D%0A          
@@ -2230,33 +2230,38 @@
       %7D%0A        
-%7D
+else %7B
 %0A            %7D%0A 
@@ -2249,33 +2249,41 @@
 e %7B%0A            
-%7D
+return d;
 %0A        %7D%0A    %7D

fix Clean Imports for overloaded constructor calls