]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | package no.uio.ifi.refaktor.analyze.collectors; |
2 | ||
3 | import no.uio.ifi.refaktor.textselection.CompilationUnitTextSelection; | |
4 | ||
5 | import org.eclipse.jdt.core.dom.ASTNode; | |
6 | import org.eclipse.jdt.core.dom.Statement; | |
7 | ||
8 | public class LastStatementCollector extends PropertyCollector { | |
9 | private final ASTNode coveringNode; | |
10 | private Statement lastStatement; | |
11 | ||
12 | public LastStatementCollector(CompilationUnitTextSelection selection, ASTNode coveringNode) { | |
13 | super(selection); | |
14 | this.coveringNode = coveringNode; | |
15 | } | |
16 | ||
17 | @Override | |
18 | public boolean preVisit2(ASTNode node) { | |
19 | if (node == coveringNode) | |
20 | return true; | |
21 | ||
22 | if (!nodeInSelection(node)) | |
23 | return false; | |
24 | ||
25 | if (node instanceof Statement && isPlacedAfterLastStatement(node)) { | |
26 | lastStatement = (Statement) node; | |
27 | } | |
28 | ||
29 | // only visit the statements on the top level | |
30 | return false; | |
31 | } | |
32 | ||
33 | private boolean isPlacedAfterLastStatement(ASTNode node) { | |
34 | if (lastStatement == null) | |
35 | return true; | |
36 | ||
37 | int nodeEndPosition = node.getStartPosition() + node.getLength(); | |
38 | int lastStatementEndPosition = lastStatement.getStartPosition() + lastStatement.getLength(); | |
39 | return nodeEndPosition > lastStatementEndPosition; | |
40 | } | |
41 | ||
42 | public Statement getLastStatement() { | |
43 | return lastStatement; | |
44 | } | |
45 | ||
46 | @Override | |
47 | public void clearData() { | |
48 | lastStatement = null; | |
49 | } | |
50 | } |