Commit 80b72144 authored by Johannes Roith's avatar Johannes Roith
Browse files

Optimize.

parent 4cbbf3fc
......@@ -26,6 +26,7 @@ import de.in.tum.www2.cup.CupSymbol;
import de.in.tum.www2.cup.Declarations;
import de.in.tum.www2.cup.NoopErrorReporter;
import de.in.tum.www2.cup.Position;
import de.in.tum.www2.cup.Range;
import de.in.tum.www2.cup.ast.AbstractNode;
import de.in.tum.www2.cup.ast.ActionCodeBlock;
import de.in.tum.www2.cup.ast.IProductionRightPart;
......@@ -176,7 +177,9 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
private JavaScanner scanner;
private JavaSymbol last;
private int offset;
private Range previousCodeBlockRange;
private AbstractNode previousAstNode;
private HashSet<String> currentLabels = null;
public JavaTokenScanner() {
......@@ -186,6 +189,40 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
public void updateParserResult(ParserResult result) {
this.ast = result;
this.previousCodeBlockRange = null;
}
private void refreshSpecialSymbols(Position pos) {
System.out.println("TRYING TO FIND POSITION: " + pos);
this.currentLabels.clear();
AbstractNode node = ast.findAtPosition(pos);
if (node == null)
return;
previousCodeBlockRange = node.getRange();
if (node instanceof ActionCodeBlock) {
ActionCodeBlock codeBlock = (ActionCodeBlock) node;
AbstractNode parent = codeBlock.getParent();
if (parent instanceof ProductionRight) {
ProductionRight pr = (ProductionRight) parent;
for (IProductionRightPart part : pr.getList()) {
if (part instanceof LabeledProductionSymbolRef) {
LabeledProductionSymbolRef lpsr = (LabeledProductionSymbolRef) part;
String label = lpsr.label;
currentLabels.add(label);
currentLabels.add(label + "xleft");
currentLabels.add(label + "xright");
currentLabels.add(label + "left");
currentLabels.add(label + "right");
}
}
}
}
}
public void setRange(IDocument document, int offset, int length) {
......@@ -195,15 +232,15 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
this.scanner = new JavaScanner(reader);
this.last = null;
this.offset = offset;
this.currentLabels.clear();
if (ast != null) {
// TODO: find a way to avoid computing all this on every keystroke!
if (ast == null) {
this.currentLabels.clear();
} else {
// TODO: we convert offset to line number / position.
// TODO: centralize this type of code, so we have our bugs in
// one place :-)
int line;
int column;
try {
......@@ -216,32 +253,17 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
line += 1;
column += 2; // TODO: adding two here is a hack!
System.out.println("TRYING TO FIND POSITION: " + line + ", "
+ column);
AbstractNode node = ast.findAtPosition(new Position(line,
column, -1));
if (node != null && node instanceof ActionCodeBlock) {
ActionCodeBlock codeBlock = (ActionCodeBlock) node;
AbstractNode parent = codeBlock.getParent();
if (parent instanceof ProductionRight) {
ProductionRight pr = (ProductionRight) parent;
for (IProductionRightPart part : pr.getList()) {
if (part instanceof LabeledProductionSymbolRef) {
LabeledProductionSymbolRef lpsr = (LabeledProductionSymbolRef) part;
String label = lpsr.label;
currentLabels.add(label);
currentLabels.add(label + "xleft");
currentLabels.add(label + "xright");
currentLabels.add(label + "left");
currentLabels.add(label + "right");
}
}
}
}
Position pos = new Position(line, column, -1);
if (previousCodeBlockRange != null && previousCodeBlockRange.contains(pos)) {
// the old AST is till valid and we are within the same code block.
// -> just keep the existing labels.
return;
} else {
refreshSpecialSymbols(pos);
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment