From 0252781d131fa23bdac593060f47339652ef9951 Mon Sep 17 00:00:00 2001 From: Michael Uhl Date: Fri, 19 Apr 2019 20:50:13 +0200 Subject: [PATCH] Richtiges Einsetzen ohne Speichern. --- .../src/typinferenzplugin/ErrorMarker.java | 115 ++++++++++-------- .../src/typinferenzplugin/JavMarker.java | 70 ++++++----- .../typinferenzplugin/TypeReplaceMarker.java | 21 ++-- .../src/typinferenzplugin/Typinferenz.java | 7 +- .../typinferenzplugin/editor/JavEditor.java | 29 +++-- 5 files changed, 127 insertions(+), 115 deletions(-) diff --git a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/ErrorMarker.java b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/ErrorMarker.java index 53f7afc..b3b943e 100644 --- a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/ErrorMarker.java +++ b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/ErrorMarker.java @@ -1,54 +1,61 @@ -package typinferenzplugin; - -import java.util.Vector; - -import org.eclipse.core.resources.IMarker; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.source.Annotation; - -import typinferenzplugin.editor.JavEditor; - -public class ErrorMarker extends JavMarker{ - - private String message; - private CodePoint point; - - public ErrorMarker(String message ,CodePoint p){ - this.message = message; - this.point = p; - } - - @Override - public String getMessage(){ - return message; - } - - @Override - public String toString(){ - String ret = ""; - ret+="ErrorMarker: "+this.getMessage(); - return ret; - } - - @Override - public CodePoint getPoint() { - return this.point; - } - - @Override - public boolean equals(Object eq){ - if(! (eq instanceof ErrorMarker))return false; - ErrorMarker equals = (ErrorMarker) eq; - if(!(equals.point.equals(this.point)))return false; - if(!(equals.getMessage().equals(this.getMessage())))return false; - - return true; - } - - @Override - public void setAnnotation(Annotation addAnnotation) { - // TODO Auto-generated method stub - - } - -} +package typinferenzplugin; + +import java.util.Vector; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.source.Annotation; + +import typinferenzplugin.editor.JavEditor; + +public class ErrorMarker extends JavMarker{ + + private String message; + private CodePoint point; + + public ErrorMarker(String message ,CodePoint p){ + this.message = message; + this.point = p; + } + + @Override + public String getMessage(){ + return message; + } + + @Override + public String toString(){ + String ret = ""; + ret+="ErrorMarker: "+this.getMessage(); + return ret; + } + + @Override + public CodePoint getPoint() { + return this.point; + } + + + + @Override + public int getPositionInCode() { + return point.getPositionInCode(); + } + + @Override + public boolean equals(Object eq){ + if(! (eq instanceof ErrorMarker))return false; + ErrorMarker equals = (ErrorMarker) eq; + if(!(equals.point.equals(this.point)))return false; + if(!(equals.getMessage().equals(this.getMessage())))return false; + + return true; + } + + @Override + public void setAnnotation(Annotation addAnnotation) { + // TODO Auto-generated method stub + + } + +} diff --git a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/JavMarker.java b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/JavMarker.java index 4d67d7c..dfd125f 100644 --- a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/JavMarker.java +++ b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/JavMarker.java @@ -1,36 +1,34 @@ -package typinferenzplugin; - -import java.util.Vector; - -import org.eclipse.jface.text.source.Annotation; - -public abstract class JavMarker { - - private Annotation annotation; - - public abstract CodePoint getPoint(); - public abstract String getMessage(); - - public void setAnnotation(Annotation addAnnotation) { - this.annotation = addAnnotation; - } - - public Annotation getAnnotation() { - return annotation; - } - @Override - public boolean equals(Object o) { - if (! (o instanceof JavMarker)) { - return false; - } - - JavMarker other = (JavMarker)o; - return getMessage().equals(other.getMessage()) && getPoint().getPositionInCode() == other.getPoint().getPositionInCode(); - } - @Override - public int hashCode() { - return getPoint().getPositionInCode(); - } - - -} +package typinferenzplugin; + +import org.eclipse.jface.text.source.Annotation; + +public abstract class JavMarker { + + private Annotation annotation; + + public abstract CodePoint getPoint(); + public abstract String getMessage(); + public abstract int getPositionInCode(); + + public void setAnnotation(Annotation addAnnotation) { + this.annotation = addAnnotation; + } + + public Annotation getAnnotation() { + return annotation; + } + + @Override + public boolean equals(Object o) { + if (! (o instanceof JavMarker)) { + return false; + } + + JavMarker other = (JavMarker)o; + return getMessage().equals(other.getMessage()) && getPoint().getPositionInCode() == other.getPoint().getPositionInCode(); + } + @Override + public int hashCode() { + return getPoint().getPositionInCode(); + } +} diff --git a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/TypeReplaceMarker.java b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/TypeReplaceMarker.java index 239dba6..cbff44b 100644 --- a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/TypeReplaceMarker.java +++ b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/TypeReplaceMarker.java @@ -1,9 +1,8 @@ package typinferenzplugin; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typedeployment.TypeInsert; -import de.dhbwstuttgart.typedeployment.TypeInsertPoint; -import de.dhbwstuttgart.typeinference.result.ResultPair; +import de.dhbwstuttgart.typeinference.result.ResultPairMap; +import de.dhbwstuttgart.typeinference.result.ResultSet; import typinferenzplugin.editor.JavEditor; public class TypeReplaceMarker extends JavMarker { @@ -11,13 +10,22 @@ public class TypeReplaceMarker extends JavMarker { private JavEditor editor; private CodePoint point; private TypeInsert tip; - private ResultPair resultPair; + private ResultSet resultSet; - public TypeReplaceMarker(JavEditor editor, TypeInsert point){ + public TypeReplaceMarker(JavEditor editor, ResultSet resultSet, TypeInsert point){ this.editor = editor; this.point = new CodePoint(point.point.point); this.tip = point; - this.resultPair = resultPair; + this.resultSet = resultSet; + } + + @Override + public int getPositionInCode() { + return tip.point.getPositionInCode(); + } + + public boolean isConsistent(TypeReplaceMarker other) { + return this.resultSet.contains(ResultPairMap.RESULT_PAIRS.get(other.point.offset, other.tip.getInsertString())); } public void run(){ @@ -76,5 +84,4 @@ public class TypeReplaceMarker extends JavMarker { return ((TypeReplaceMarker)obj).tip.equals(this.tip); } } - } diff --git a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/Typinferenz.java b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/Typinferenz.java index b6ed554..2f0dec9 100644 --- a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/Typinferenz.java +++ b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/Typinferenz.java @@ -97,14 +97,13 @@ public class Typinferenz { return this.tiResults; } - public Vector updateWithResult(Vector ret, List tiResults) { + public Vector updateWithResult(Vector ret, List tiResults) { this.tiResults.addAll(tiResults); Set tips = new HashSet<>(); for (ResultSet tiResult : tiResults) { tips.addAll(TypeInsertFactory.createTypeInsertPoints(parsedSource, tiResult)); - int numberOfSolutions = 0; // Zählt mit, wie viele verschiedene Lösungen angeboten werden for (TypeInsert p : tips) { - TypeReplaceMarker toAdd = new TypeReplaceMarker(editor, p); + TypeReplaceMarker toAdd = new TypeReplaceMarker(editor, tiResult, p); if (!ret.contains(toAdd)) { ret.add(toAdd); } else { @@ -116,10 +115,8 @@ public class Typinferenz { String outputDirectory = editor.getFilePath().toString(); outputDirectory = outputDirectory.substring(0, outputDirectory.length() - editor.getFilePath().lastSegment().length());// ".jav" hat Länge 4 - // if(tips.size() == 0 && tiResults.size()>0) { if (tiResults.size() > 0) { for (SourceFile sf : compiler.sourceFiles.values()) { - // HashMap bytecode = getBytecode(sf, tiResults.get(0)); HashMap bytecode = getBytecode(sf, tiResults, outputDirectory); this.writeClassFile(outputDirectory, bytecode); } diff --git a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JavEditor.java b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JavEditor.java index c2f0ee9..9fe149c 100644 --- a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JavEditor.java +++ b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JavEditor.java @@ -168,12 +168,12 @@ public class JavEditor extends TextEditor implements UnifyResultListener { CodePoint point = rm.getPoint(); try { IMarker m = inDocument.createMarker(Activator.TYPINFERENZMARKER_NAME); - m.setAttribute(IMarker.CHAR_START, point.getPositionInCode()); - m.setAttribute(IMarker.CHAR_END, point.getPositionInCode() + 1); + m.setAttribute(IMarker.CHAR_START, rm.getPositionInCode()); + m.setAttribute(IMarker.CHAR_END, rm.getPositionInCode() + 1); String message = rm.getMessage(); m.setAttribute(IMarker.MESSAGE, message); m.setAttribute(IMarker.SEVERITY, 1); - rm.setAnnotation(this.addAnnotation(m, point.getPositionInCode())); + rm.setAnnotation(this.addAnnotation(m, rm.getPositionInCode())); } catch (CoreException e) { new ErrorOutput("Fehler beim Generieren eines Markers"); e.printStackTrace(); @@ -237,7 +237,7 @@ public class JavEditor extends TextEditor implements UnifyResultListener { /** * Diese Funktion führt einen ReplaceMarker aus. - * + * Minli * @param typeReplaceMarker */ public void runReplaceMarker(TypeReplaceMarker typeReplaceMarker) { @@ -250,17 +250,18 @@ public class JavEditor extends TextEditor implements UnifyResultListener { this.typeReconstruction(); */ - typeReplaceMarkers.remove(typeReplaceMarker); + typeReplaceMarkers.removeIf(trm -> trm.getPoint().getPositionInCode() == typeReplaceMarker.getPoint().getPositionInCode()); + typeReplaceMarkers.removeIf(trm -> !trm.isConsistent(typeReplaceMarker)); int lengthOfInsert = typeReplaceMarker.getInsertPoint().getInsertString().length(); - for (int i = 0; i < typeReplaceMarkers.size(); i++) { - TypeReplaceMarker toCheck = typeReplaceMarkers.get(i); - + for (TypeReplaceMarker toCheck : typeReplaceMarkers) { if (toCheck.getPoint().getPositionInCode() > typeReplaceMarker.getPoint().getPositionInCode()) { - toCheck.getInsertPoint().point.addExtraOffset(lengthOfInsert);; + toCheck.getInsertPoint().point.addExtraOffset(lengthOfInsert); } } + updateGuiWithNewMarkers(typeReplaceMarkers); + /* ResultPair resultPairOfCurrentTrm = RESULT_PAIRS.get(typeReplaceMarker.getPoint().offset); @@ -391,7 +392,7 @@ public class JavEditor extends TextEditor implements UnifyResultListener { @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void onNewTypeResultFound(UnifyResultEvent evt) { - Set markers = updateMarkers(evt); + Vector markers = updateMarkers(evt); // Anschließend die TypeReplaceMarker im Quellcode anzeigen: // https://stackoverflow.com/questions/8945371/how-to-implement-quick-fix-quick-assist-for-custom-eclipse-editor @@ -404,11 +405,13 @@ public class JavEditor extends TextEditor implements UnifyResultListener { } - private Set updateMarkers(UnifyResultEvent evt) { + private Vector updateMarkers(UnifyResultEvent evt) { typeinference.updateresultSets(new Vector(evt.getNewTypeResult())); - Set markers = new HashSet(); - typeReplaceMarkers.addAll(typeinference.updateWithResult(new Vector(markers), evt.getNewTypeResult())); + Vector markers = new Vector(); + Vector newMarkers = typeinference.updateWithResult(markers, evt.getNewTypeResult()); + typeReplaceMarkers.removeAll(newMarkers); + typeReplaceMarkers.addAll(newMarkers); for (TypeReplaceMarker m : this.getTypeReplaceMarkers()) { markers.add(m);