From a208e077ad199ad67841b1fa66de50dc86bb369c Mon Sep 17 00:00:00 2001 From: Michael Uhl Date: Wed, 13 Nov 2019 10:11:32 +0100 Subject: [PATCH] Vor Fertigstelltung Ersetzung Generics --- .../src/typinferenzplugin/Typinferenz.java | 2 +- .../typinferenzplugin/editor/JavEditor.java | 89 +++++++++++++++++-- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/Typinferenz.java b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/Typinferenz.java index 217e6ee..540f6c7 100644 --- a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/Typinferenz.java +++ b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/Typinferenz.java @@ -187,7 +187,7 @@ public class Typinferenz { public void cancel() { try { - compiler.usedTasks.cancel(); + // compiler.usedTasks.cancel(); } catch (Exception e) { e.printStackTrace(); } diff --git a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JavEditor.java b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JavEditor.java index f76adda..4260aa5 100644 --- a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JavEditor.java +++ b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JavEditor.java @@ -5,14 +5,20 @@ import static java.util.stream.Collectors.toCollection; import static org.eclipse.core.runtime.IStatus.ERROR; import static typinferenzplugin.Activator.PLUGIN_ID; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; import java.util.TreeSet; import java.util.Vector; import java.util.stream.Collectors; import javax.inject.Inject; +import org.antlr.v4.parse.BlockSetTransformer.topdown_return; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; @@ -24,7 +30,6 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.e4.ui.di.UISynchronize; -import org.eclipse.jdt.internal.corext.refactoring.reorg.ParentChecker; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.Position; import org.eclipse.jface.text.source.Annotation; @@ -32,9 +37,8 @@ import org.eclipse.jface.text.source.IAnnotationModel; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IFileEditorInput; -import org.eclipse.ui.IPartListener; -import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.editors.text.TextEditor; +import org.eclipse.ui.internal.ide.TipsAndTricksAction; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.ui.texteditor.IDocumentProvider; import org.eclipse.ui.texteditor.SimpleMarkerAnnotation; @@ -44,6 +48,7 @@ import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typedeployment.TypeInsert; +import de.dhbwstuttgart.typedeployment.TypeInsertFactory; import de.dhbwstuttgart.typedeployment.TypeInsertPoint; import de.dhbwstuttgart.typeinference.result.ResultSet; import de.dhbwstuttgart.typeinference.unify.UnifyResultEvent; @@ -242,20 +247,53 @@ public class JavEditor extends TextEditor implements UnifyResultListener { IDocument document = this.getDocumentProvider().getDocument(this.getEditorInput()); document.set(typeReplaceMarker.insertType(document.get())); + Collection additionalIdentityElimination = new HashSet<>(); + typeReplaceMarkers.removeIf(trm -> trm.getPoint().getPositionInCode() == typeReplaceMarker.getPoint().getPositionInCode()); typeReplaceMarkers.removeIf(trm -> !trm.isConsistent(typeReplaceMarker)); int lengthOfInsert = typeReplaceMarker.getInsertPoint().getInsertString().length(); for (TypeReplaceMarker toCheck : typeReplaceMarkers) { - if (toCheck.getPoint().getPositionInCode() > typeReplaceMarker.getPoint().getPositionInCode()) { + if (toCheck.getPoint().getPositionInCode() > typeReplaceMarker.getPoint().getPositionInCode() + lengthOfInsert) { toCheck.getInsertPoint().point.addExtraOffset(lengthOfInsert); } - for (TypeInsertPoint tip : typeReplaceMarker.getInsertPoint().getAdditionalPoints()) { - if (toCheck.getPoint().getPositionInCode() > tip.getPositionInCode()) { + + // Add extra offset for the additional insert points of "typeReplaceMarker" + // to "toCheck" + // ... + for (TypeInsertPoint additionalPoint : typeReplaceMarker.getInsertPoint().getAdditionalPointsUnsorted()) { + int lengthAdditionalInsert = additionalPoint.getInsertString().length(); + int positionAdditionalInsert = additionalPoint.getPositionInCode(); + + if (additionalIdentityElimination.add(additionalPoint) && toCheck.getPositionInCode() > positionAdditionalInsert + lengthAdditionalInsert) { + toCheck.getInsertPoint().point.addExtraOffset(lengthAdditionalInsert); + } + } + + for (TypeInsertPoint tip : typeReplaceMarker.getInsertPoint().getAdditionalPointsUnsorted()) { + // Add to all other type replace markers the extra offsets of the additional + // points of this type replace marker. + // ... + if (toCheck.getPoint().getPositionInCode() > tip.getPositionInCode() - tip.getInsertString().length()) { toCheck.getInsertPoint().point.addExtraOffset(tip.getInsertString().length()); } } + + for (TypeInsertPoint toCheckAddi : toCheck.getInsertPoint().getAdditionalPointsUnsorted()) { + if (toCheckAddi.getPositionInCode() > typeReplaceMarker.getPoint().getPositionInCode() + lengthOfInsert) { + toCheckAddi.addExtraOffset(lengthOfInsert); + } + + for (TypeInsertPoint additionalPoint : typeReplaceMarker.getInsertPoint().getAdditionalPointsUnsorted()) { + int lengthAdditionalInsert = additionalPoint.getInsertString().length(); + int positionAdditionalInsert = additionalPoint.getPositionInCode(); + + if (positionAdditionalInsert + lengthAdditionalInsert < toCheckAddi.getPositionInCode() ) { + toCheckAddi.addExtraOffset(lengthAdditionalInsert); + } + } + } } // Remove resultsets if not consistent. @@ -264,7 +302,35 @@ public class JavEditor extends TextEditor implements UnifyResultListener { } updateGuiWithNewMarkers(typeReplaceMarkers); + boolean actualHasGenericClassInserts = typeReplaceMarker.getInsertPoint().getAdditionalPoints().stream().anyMatch(tip -> tip.isGenericClassInsertPoint()); + if (actualHasGenericClassInserts) { + for (TypeReplaceMarker marker : typeReplaceMarkers) { + Iterator addiPoints = marker.getInsertPoint().getAdditionalPointsUnsorted().iterator(); + while (addiPoints.hasNext()) { + if (addiPoints.next().isGenericClassInsertPoint()) { + addiPoints.remove(); + } + } + } + } + + for (TypeInsertPoint additionalPoint : typeReplaceMarker.getInsertPoint().getAdditionalPoints()) { + for (TypeReplaceMarker other : this.typeReplaceMarkers) { + other.getInsertPoint().getAdditionalPointsUnsorted().remove(additionalPoint); + } + } + Collection newResultSets = typeReplaceMarkers.stream().map(trm -> trm.getResultSets()).flatMap(rs -> rs.stream()).collect(Collectors.toSet()); + + /** + Vector markers = new Vector(); + Vector newMarkers = typeinference.updateWithResult(markers, new Vector<>(newResultSets)); + + updateGuiWithNewMarkersSynchron(newMarkers); + this.errorMarkers.addAll(newMarkers); + **/ + + HashMap bytecode = typeinference.getBytecode(typeinference.getSourceFile(), newResultSets, typeinference.getOutputDirectory()); typeinference.writeClassFile(typeinference.getOutputDirectory(), bytecode); } @@ -400,5 +466,16 @@ public class JavEditor extends TextEditor implements UnifyResultListener { this.updateOutlinePage(); } + + private void updateGuiWithNewMarkersSynchron(Collection markers) { + IResource activeDocument = extractResource(); + if (activeDocument == null) { + LOG.log(new Status(ERROR, PLUGIN_ID, "Kann das aktive Dokument nicht ermitteln")); + } else { + this.placeMarkers(activeDocument, markers); + } + + this.updateOutlinePage(); + } }