Skip to content

Commit

Permalink
[FIX][E] #32 Splitting annotations do not honor the history
Browse files Browse the repository at this point in the history
This patch fixes the last outstanding issues regarding #32.

Annotations that were split are now correctly inserted to the history
and so correctly removed.

This patch introduces a new method to the annotation helper as well as
it reduces LOC in the ContributionAnnotationManager.
  • Loading branch information
srossbach committed Jul 29, 2019
1 parent 8ed3fc5 commit f89f081
Show file tree
Hide file tree
Showing 3 changed files with 359 additions and 118 deletions.
51 changes: 33 additions & 18 deletions eclipse/src/saros/editor/internal/AnnotationModelHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package saros.editor.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
Expand All @@ -24,7 +25,6 @@ public class AnnotationModelHelper {
private static Iterable<Annotation> toIterable(final IAnnotationModel model) {
return new Iterable<Annotation>() {
@Override
@SuppressWarnings("unchecked")
public Iterator<Annotation> iterator() {
return model.getAnnotationIterator();
}
Expand Down Expand Up @@ -56,43 +56,58 @@ public void removeAnnotationsFromModel(IAnnotationModel model, Predicate<Annotat
}

/**
* Removes annotations that match a given predicate and replaces them in one step.
* Removes annotations and replaces them in one step.
*
* @param model The {@link IAnnotationModel} that should be cleaned.
* @param predicate The filter to use for cleaning.
* @param annotationsToRemove The annotations to remove.
* @param annotationsToAdd The annotations to add.
*/
public void replaceAnnotationsInModel(
IAnnotationModel model,
Predicate<Annotation> predicate,
Map<Annotation, Position> replacement) {
Collection<? extends Annotation> annotationsToRemove,
Map<? extends Annotation, Position> annotationsToAdd) {

// Collect annotations.
ArrayList<Annotation> annotationsToRemove = new ArrayList<Annotation>(128);

for (Annotation annotation : AnnotationModelHelper.toIterable(model)) {
if (predicate.evaluate(annotation)) {
annotationsToRemove.add(annotation);
}
}

// Remove collected annotations.
if (model instanceof IAnnotationModelExtension) {
IAnnotationModelExtension extension = (IAnnotationModelExtension) model;
extension.replaceAnnotations(
annotationsToRemove.toArray(new Annotation[annotationsToRemove.size()]), replacement);
annotationsToRemove.toArray(new Annotation[annotationsToRemove.size()]),
annotationsToAdd);
} else {
LOG.trace("AnnotationModel does not " + "support IAnnotationModelExtension: " + model);
if (LOG.isTraceEnabled())
LOG.trace("AnnotationModel " + model + " does not support IAnnotationModelExtension");

for (Annotation annotation : annotationsToRemove) {
model.removeAnnotation(annotation);
}

for (Entry<Annotation, Position> entry : replacement.entrySet()) {
for (Entry<? extends Annotation, Position> entry : annotationsToAdd.entrySet()) {
model.addAnnotation(entry.getKey(), entry.getValue());
}
}
}

/**
* Removes annotations that match a given predicate and replaces them in one step.
*
* @param model The {@link IAnnotationModel} that should be cleaned.
* @param predicate The filter to use for cleaning.
*/
public void replaceAnnotationsInModel(
IAnnotationModel model,
Predicate<Annotation> predicate,
Map<Annotation, Position> replacement) {

ArrayList<Annotation> annotationsToRemove = new ArrayList<Annotation>(128);

for (Annotation annotation : AnnotationModelHelper.toIterable(model)) {
if (predicate.evaluate(annotation)) {
annotationsToRemove.add(annotation);
}
}

replaceAnnotationsInModel(model, annotationsToRemove, replacement);
}

public IAnnotationModel retrieveAnnotationModel(IEditorPart editorPart) {
IEditorInput input = editorPart.getEditorInput();
IDocumentProvider provider = DocumentProviderRegistry.getDefault().getDocumentProvider(input);
Expand Down
Loading

0 comments on commit f89f081

Please sign in to comment.