001: package visualdebugger.views;
002:
003: import org.eclipse.jdt.core.dom.*;
004: import org.eclipse.jface.text.TextSelection;
005:
006: public class FindStatementVisitor extends ASTVisitor {
007:
008: private int currentId = 0;
009:
010: private int id = 0;
011:
012: private int offset = -1;
013:
014: private int length = -1;
015:
016: private int toFind;
017:
018: private Statement statement;
019:
020: public FindStatementVisitor(int toFind) {
021: super ();
022: this .toFind = toFind;
023: }
024:
025: /* public void preVisit(ASTNode node) {
026:
027:
028: if (node instanceof Statement) {
029: currentId++;
030: System.out.println("st "+id+""+node);
031: Statement st = (Statement) node;
032: if (st.getStartPosition() < toFind
033: && st.getStartPosition() > offset) {
034: offset = st.getStartPosition();
035: length = st.getLength();
036: id=currentId;
037: }
038: }
039:
040:
041:
042: }*/
043:
044: public boolean visit(Block node) {
045: for (int i = 0; i < node.statements().size(); i++) {
046: currentId++;
047: Statement st = (Statement) node.statements().get(i);
048: if (st.getStartPosition() <= toFind
049: && st.getStartPosition() >= offset) {
050: offset = st.getStartPosition();
051: length = st.getLength();
052: statement = st;
053: id = currentId;
054:
055: }
056: }
057: return true;
058: }
059:
060: public TextSelection getTextSelection() {
061: //System.out.println("ID "+id);
062: if (offset == -1)
063: return null;
064: else
065: return new TextSelection(offset, length);
066: }
067:
068: public int getStatementId() {
069: return id;
070: }
071:
072: public Statement getStatement() {
073: return statement;
074: }
075:
076: public void endVisit(FieldAccess node) {
077: currentId++;
078: // System.out.println(node+" "+currentId);
079: // if(currentId==idToFind){
080: // expr=node;
081: // }
082: }
083:
084: public void endVisit(QualifiedName node) {
085:
086: if (node.getParent() instanceof QualifiedName)
087: return;
088:
089: if ((node.getQualifier().resolveBinding().getKind() == IBinding.PACKAGE))
090: return;
091:
092: if (node.getQualifier().resolveTypeBinding().isArray())
093: return;
094: currentId++;
095: // System.out.println(node+" "+currentId);
096: // if(currentId==idToFind){
097: // expr=node;
098: // }
099:
100: }
101:
102: public void endVisit(ArrayAccess node) {
103: currentId++;
104: // System.out.println(node+" "+currentId);
105: // if(currentId==idToFind){
106: // expr=node;
107: // }
108:
109: }
110:
111: // public void endVisit(IfStatement node){
112: // final Expression index = node.getExpression();
113: // currentId++;
114: // }
115:
116: public void endVisit(ForStatement node) {
117: final Expression guard = node.getExpression();
118: currentId++;
119: }
120:
121: public void endVisit(WhileStatement node) {
122: final Expression guard = node.getExpression();
123: currentId++;
124: }
125:
126: }
|