/
GetNodeForExtractLocalVisitor.java
86 lines (72 loc) · 2.66 KB
/
GetNodeForExtractLocalVisitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package org.python.pydev.parser.visitors.scope;
import org.python.pydev.core.structure.FastStack;
import org.python.pydev.parser.jython.SimpleNode;
import org.python.pydev.parser.jython.ast.VisitorBase;
import org.python.pydev.parser.jython.ast.exprType;
import org.python.pydev.parser.jython.ast.stmtType;
import org.python.pydev.parser.prettyprinterv2.PrettyPrinterUtilsV2;
/**
* This visitor will try to get the node where the extracted local should actually be added.
*/
public class GetNodeForExtractLocalVisitor extends VisitorBase{
protected final FastStack<FastStack<SimpleNode>> contextStack = new FastStack<FastStack<SimpleNode>>();
private int initialExtractLocalLine;
private boolean keepGoing = true;
private stmtType lastStmt;
public GetNodeForExtractLocalVisitor(int initialExtractLocalLine){
this.initialExtractLocalLine = initialExtractLocalLine;
contextStack.push(new FastStack<SimpleNode>()); //start with a stack.
}
@Override
public void traverse(SimpleNode node) throws Exception {
if(!keepGoing){
return;
}
boolean multiLineStmt = false;
if(node instanceof stmtType){
multiLineStmt = PrettyPrinterUtilsV2.isMultiLineStmt((stmtType) node);
if(multiLineStmt){
contextStack.push(new FastStack<SimpleNode>());
}
lastStmt = (stmtType) node;
node.traverse(this);
if(multiLineStmt && keepGoing){
contextStack.pop();
}
}else{
FastStack<SimpleNode> peek = contextStack.peek();
boolean inExpr = peek.size() != 0;
if(!inExpr && node instanceof exprType){
peek.push(node);
}
node.traverse(this);
if(!inExpr && node instanceof exprType && keepGoing){
peek.pop();
}
}
}
@Override
protected Object unhandled_node(SimpleNode node) throws Exception {
if(node instanceof stmtType){
return null;
}
if(node.beginLine >= initialExtractLocalLine){
keepGoing = false;
}
return null;
}
public SimpleNode getLastInContextBeforePassedLine() {
FastStack<SimpleNode> peeked = contextStack.peek();
if(peeked.size() > 0){
SimpleNode expr = peeked.peek();
if(lastStmt != null){
if(expr.beginLine<lastStmt.beginLine){
return expr;
}else{
return lastStmt;
}
}
}
return lastStmt;
}
}