package com.redhat.ceylon.eclipse.util;

import com.redhat.ceylon.compiler.typechecker.tree.NaturalVisitor;
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 FindStatementVisitor extends Visitor 
		implements NaturalVisitor {
	
	private final Node term;
	private Tree.Statement statement;
	private Tree.Statement currentStatement;
	private final boolean toplevel;
	private boolean currentlyToplevel=true;
	private boolean resultIsToplevel;
	
	public Tree.Statement getStatement() {
		return statement;
	}
	
	public boolean isToplevel() {
        return resultIsToplevel;
    }
	
	public FindStatementVisitor(Node term, boolean toplevel) {
		this.term = term;
		this.toplevel = toplevel;
	}

	@Override
	public void visit(Tree.Statement that) {
    	if (!toplevel || currentlyToplevel) {
    		if (!(that instanceof Tree.Variable ||
    				that instanceof Tree.TypeConstraint ||
    				that instanceof Tree.TypeParameterDeclaration)) {
    			currentStatement = that;
                resultIsToplevel = currentlyToplevel;
	    	}
	    }
	    boolean octl = currentlyToplevel;
	    currentlyToplevel = false;
		super.visit(that);
		currentlyToplevel = octl;
	}
	
	public void visitAny(Node node) {
        if (node==term) {
            statement = currentStatement;
            resultIsToplevel = currentlyToplevel;
        }
		if (statement==null) {
			super.visitAny(node);
		}
	}
	
}
@@ -571,16 +571,46 @@
 plevel;%0A
+%09private boolean inParameter;%0A
 %09%0A%09publi
@@ -850,16 +850,182 @@
 evel;%0A%09%7D
+%0A%09%0A%09@Override%0A    public void visit(Tree.Parameter that) %7B%0A%09    boolean oip = inParameter;%0A%09    inParameter = true;%0A%09    super.visit(that);%0A%09    inParameter = oip;%0A%09%7D
 %0A%0A%09@Over
@@ -1079,16 +1079,17 @@
     %09if 
+(
 (!toplev
@@ -1112,16 +1112,33 @@
 oplevel)
+ && !inParameter)
  %7B%0A    %09

fix bug in Create local dec proposals for parameter lists