package org.javacs;

import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;

import javax.tools.JavaFileObject;
import java.util.logging.Logger;

/**
 * Removes all statements after the cursor
 */
// TODO if we ever implement incremental parsing, this will need to make a partial copy of the AST rather than modify it
public class AutocompletePruner extends CursorScanner {
    public AutocompletePruner(JavaFileObject file, long cursor, Context context) {
        super(file, cursor, context);
    }

    @Override
    public void visitBlock(JCTree.JCBlock tree) {
        List<JCTree.JCStatement> stats = tree.stats;

        int countStatements = 0;

        // Scan up to statement containing cursor
        while (countStatements < stats.size()) {
            JCTree.JCStatement s = stats.get(countStatements);

            if (containsCursor(s))
                break;
            else
                s.accept(this);

            countStatements++;
        }

        // Advance over statement containing cursor
        countStatements++;

        // Remove all statements after statement containing cursor
        tree.stats = stats.take(countStatements);
    }
}

@@ -635,53 +635,469 @@
     
-List%3CJCTree.JCStatement%3E stats = tree.stats;%0A
+tree.stats = pruneStatements(tree.stats);%0A%0A        super.visitBlock(tree);%0A    %7D%0A%0A    @Override%0A    public void visitCase(JCTree.JCCase tree) %7B%0A        tree.stats = pruneStatements(tree.stats);%0A%0A        super.visitCase(tree);%0A    %7D%0A%0A    @Override%0A    public void visitSwitch(JCTree.JCSwitch tree) %7B%0A        tree.cases = pruneStatements(tree.cases);%0A%0A        super.visitSwitch(tree);%0A    %7D%0A%0A    private %3CT extends JCTree%3E List%3CT%3E pruneStatements(List%3CT%3E stats) %7B
 %0A   
@@ -1242,26 +1242,9 @@
     
-JCTree.JCStatement
+T
  s =
@@ -1578,28 +1578,22 @@
         
-t
 re
-e.stats =
+turn
  stats.t

switch / case are like block