Commit 1d8ed856 authored by Clemens Pflaum's avatar Clemens Pflaum

changed DamagerRepairer for javaCode, fixed memoryleak

parent f53a2c64
......@@ -13,10 +13,10 @@ public class ParseJavaVisitor extends Visitor<Object> {
}
public void postVisit(ActionCodeBlock node, Object data) {
node.parseJavaCode(classpathEntries, errMan);
node.parseJavaCode(classpathEntries, true, errMan);
};
public void postVisit(SpecialCodeBlock node, Object data) {
node.parseJavaCode(classpathEntries, errMan);
node.parseJavaCode(classpathEntries, true, errMan);
};
}
......@@ -43,17 +43,22 @@ public class ActionCodeBlock extends CodeBlock implements IProductionRightPart {
@Override
public void parseJavaCode(String[] classpathEntries, ErrorManager errMan) {
CupVariableLocator visitor = new CupVariableLocator(this);
this.getParent().accept(visitor, null);
public void parseJavaCode(String[] classpathEntries, Boolean recalcCupVars, ErrorManager errMan) {
if(recalcCupVars){
this.variableLabels.clear();;
CupVariableLocator visitor = new CupVariableLocator(this);
this.getParent().accept(visitor, null);
}
this.cu = (CompilationUnit) JavaCompiler.parseJava(this, classpathEntries);
BindingCollector variableCollector = new BindingCollector();
this.cu.accept(variableCollector);
this.variableMap = variableCollector.getVariableMap();
this.methodList = variableCollector.getMethodList();
this.setCupOffset(this.getRange().getBegin().getOffsetFromStart());
reportProblems(errMan);
variablePositions();
if(errMan != null)
reportProblems(errMan);
}
public void addVariable(LabeledProductionSymbolRef l) {
......
......@@ -33,7 +33,7 @@ public abstract class CodeBlock extends AbstractNode {
private int dummyClassOffset;
private int cupOffset;
public abstract void parseJavaCode(String[] classpathEntries, ErrorManager errMan);
public abstract void parseJavaCode(String[] classpathEntries, Boolean recalcCupVars, ErrorManager errMan);
public CodeBlock(int debugId, BlockType bt, String blob, Range range) {
super(range);
......@@ -142,6 +142,10 @@ public abstract class CodeBlock extends AbstractNode {
public String getBlob() {
return blob;
}
public void setBlob(String blob) {
this.blob = blob;
}
public BlockType getBt() {
return bt;
......
......@@ -32,7 +32,7 @@ public class SpecialCodeBlock extends CodeBlock implements IProductionRightPart
}
@Override
public void parseJavaCode(String[] classpathEntries, ErrorManager errMan) {
public void parseJavaCode(String[] classpathEntries, Boolean recalcCupVars, ErrorManager errMan) {
//Parse JavaCode to CompilationUnit
this.cu = (CompilationUnit) JavaCompiler.parseJava(this, classpathEntries);
//Collect variable and method Bindings
......@@ -42,6 +42,7 @@ public class SpecialCodeBlock extends CodeBlock implements IProductionRightPart
this.methodList = variableCollector.getMethodList();
this.setCupOffset(this.getRange().getBegin().getOffsetFromStart());
//Report Java parseProblems
reportProblems(errMan);
if(errMan != null)
reportProblems(errMan);
}
}
package de.tum.in.www2.cupplugin.editors;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.ITokenScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.swt.custom.StyleRange;
import de.tum.in.www2.cupplugin.syntax.CupTokenScanner;
import de.tum.in.www2.cupplugin.syntax.JavaTokenScanner;
public class CupDamagerRepairer extends DefaultDamagerRepairer {
......@@ -74,4 +74,34 @@ public class CupDamagerRepairer extends DefaultDamagerRepairer {
addRange(presentation, lastStart, length, lastAttribute);
}
@Override
public IRegion getDamageRegion(ITypedRegion partition, DocumentEvent e, boolean documentPartitioningChanged) {
if( partition.getType() == CupPartionScanner.JAVA_CODE){
return partition;
}
if (!documentPartitioningChanged) {
try {
IRegion info= fDocument.getLineInformationOfOffset(e.getOffset());
int start= Math.max(partition.getOffset(), info.getOffset());
int end= e.getOffset() + (e.getText() == null ? e.getLength() : e.getText().length());
if (info.getOffset() <= end && end <= info.getOffset() + info.getLength()) {
// optimize the case of the same line
end= info.getOffset() + info.getLength();
} else
end= endOfLineOf(end);
end= Math.min(partition.getOffset() + partition.getLength(), end);
return new Region(start, end - start);
} catch (BadLocationException x) {
}
}
return partition;
}
}
......@@ -23,6 +23,7 @@ import de.in.tum.www2.cup.ast.AbstractNode;
import de.in.tum.www2.cup.ast.ActionCodeBlock;
import de.in.tum.www2.cup.ast.CodeBlock;
import de.in.tum.www2.cup.ast.ParserResult;
import de.tum.in.www2.cupplugin.PluginUtility;
import de.tum.in.www2.cupplugin.editors.CupTextEditor;
/**
......@@ -117,27 +118,33 @@ public class JavaTokenScanner implements ITokenScanner {
*
* @param pos
*/
private void refreshSpecialSymbols(Position pos) {
private void refreshSpecialSymbols(Position pos, String javaCode) {
// System.out.println("TRYING TO FIND POSITION: " + pos);
AbstractNode node = ast.findAtPosition(pos);
if (node == null)
return;
previousCodeBlockRange = node.getRange();
if (node instanceof CodeBlock) {
CodeBlock codeBlock = (CodeBlock) node;
methodSet = codeBlock.methodPositions();
variableSet = codeBlock.variablePositions();
if(codeBlock instanceof ActionCodeBlock){
cupVars = ((ActionCodeBlock)codeBlock).getVariableLabels();
}else{
cupVars.clear();
boolean reparse = !codeBlock.getBlob().equals(javaCode);
if(reparse){
//reparse
codeBlock.setBlob(javaCode);
codeBlock.parseJavaCode(PluginUtility.createClasspath(), false, null );
}
if(reparse || !(previousCodeBlockRange != null
&& previousCodeBlockRange.contains(pos)) ||methodSet.isEmpty() || variableSet.isEmpty() || cupVars.isEmpty()){
methodSet = codeBlock.methodPositions();
variableSet = codeBlock.variablePositions();
if(codeBlock instanceof ActionCodeBlock){
cupVars = ((ActionCodeBlock)codeBlock).getVariableLabels();
}else{
cupVars.clear();
}
}
}
previousCodeBlockRange = node.getRange();
}
/**
......@@ -148,8 +155,8 @@ public class JavaTokenScanner implements ITokenScanner {
//Create eclipse javascanner
this.scanner = ToolFactory.createScanner(true, false, false, false);
this.scanner.setSource(document.get().substring(
offset, offset + length).toCharArray());
String source = document.get().substring(offset, offset + length);
this.scanner.setSource(source.toCharArray());
//this.last = null;
this.offset = offset;
......@@ -175,17 +182,7 @@ public class JavaTokenScanner implements ITokenScanner {
Position pos = new Position(line, column, -1);
if (previousCodeBlockRange != null
&& previousCodeBlockRange.contains(pos)) {
// the old AST is still valid and we are within the same code
// block.
// -> just keep the existing labels.
return;
} else {
refreshSpecialSymbols(pos);
}
refreshSpecialSymbols(pos, source.substring(2, source.length()-2));
}
}
......
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