Search in sources :

Example 1 with DiffRegion

use of org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.DiffRegion in project egit by eclipse.

the class DiffEditorPage method setOverviewAnnotations.

private void setOverviewAnnotations() {
    IDocumentProvider documentProvider = getDocumentProvider();
    IDocument document = documentProvider.getDocument(getEditorInput());
    if (!(document instanceof DiffDocument)) {
        return;
    }
    IAnnotationModel annotationModel = documentProvider.getAnnotationModel(getEditorInput());
    if (annotationModel == null) {
        return;
    }
    DiffRegion[] diffs = ((DiffDocument) document).getRegions();
    if (diffs == null || diffs.length == 0) {
        return;
    }
    Map<Annotation, Position> newAnnotations = new HashMap<>();
    for (DiffRegion region : diffs) {
        if (DiffRegion.Type.ADD.equals(region.getType())) {
            newAnnotations.put(new Annotation(ADD_ANNOTATION_TYPE, true, null), new Position(region.getOffset(), region.getLength()));
        } else if (DiffRegion.Type.REMOVE.equals(region.getType())) {
            newAnnotations.put(new Annotation(REMOVE_ANNOTATION_TYPE, true, null), new Position(region.getOffset(), region.getLength()));
        }
    }
    if (annotationModel instanceof IAnnotationModelExtension) {
        ((IAnnotationModelExtension) annotationModel).replaceAnnotations(currentOverviewAnnotations, newAnnotations);
    } else {
        if (currentOverviewAnnotations != null) {
            for (Annotation existing : currentOverviewAnnotations) {
                annotationModel.removeAnnotation(existing);
            }
        }
        for (Map.Entry<Annotation, Position> entry : newAnnotations.entrySet()) {
            annotationModel.addAnnotation(entry.getKey(), entry.getValue());
        }
    }
    currentOverviewAnnotations = newAnnotations.keySet().toArray(new Annotation[newAnnotations.size()]);
}
Also used : Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) FileDiffRegion(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.FileDiffRegion) DiffRegion(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.DiffRegion) Annotation(org.eclipse.jface.text.source.Annotation) ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) Map(java.util.Map) HashMap(java.util.HashMap) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with DiffRegion

use of org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.DiffRegion in project egit by eclipse.

the class DiffRegionFormatterTest method testRanges.

@Test
public void testRanges() throws Exception {
    IDocument document = new Document();
    DiffRegion[] regions;
    try (DiffRegionFormatter formatter = new DiffRegionFormatter(document)) {
        formatter.setRepository(repository);
        formatter.format(commit.getTree(), commit.getParent(0).getTree());
        assertTrue(document.getLength() > 0);
        regions = formatter.getRegions();
    }
    assertNotNull(regions);
    assertTrue(regions.length > 0);
    for (DiffRegion region : regions) {
        assertNotNull(region);
        assertTrue(region.getOffset() >= 0);
        assertTrue(region.getLength() >= 0);
        assertTrue(region.getOffset() < document.getLength());
    }
}
Also used : DiffRegionFormatter(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DiffRegion(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.DiffRegion) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 3 with DiffRegion

use of org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.DiffRegion in project egit by eclipse.

the class DiffDocument method getLogicalLine.

int getLogicalLine(int physicalLine, @NonNull DiffEntry.Side side) {
    int offset;
    try {
        offset = getLineOffset(physicalLine);
        DiffRegion region = findRegion(offset);
        if (region == null) {
            return DiffRegion.NO_LINE;
        }
        int logicalStart = region.getLine(side);
        if (logicalStart == DiffRegion.NO_LINE) {
            return DiffRegion.NO_LINE;
        }
        int physicalStart = getLineOfOffset(region.getOffset());
        return logicalStart + (physicalLine - physicalStart);
    } catch (BadLocationException e) {
        return DiffRegion.NO_LINE;
    }
}
Also used : DiffRegion(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.DiffRegion) FileDiffRegion(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.FileDiffRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

DiffRegion (org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.DiffRegion)3 FileDiffRegion (org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.FileDiffRegion)2 IDocument (org.eclipse.jface.text.IDocument)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DiffRegionFormatter (org.eclipse.egit.ui.internal.commit.DiffRegionFormatter)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Document (org.eclipse.jface.text.Document)1 Position (org.eclipse.jface.text.Position)1 Annotation (org.eclipse.jface.text.source.Annotation)1 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)1 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)1 ProjectionAnnotation (org.eclipse.jface.text.source.projection.ProjectionAnnotation)1 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)1 Test (org.junit.Test)1