Search in sources :

Example 6 with FileDiffRegion

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

the class DiffEditorOutlinePage method createContextMenu.

private void createContextMenu(TreeViewer viewer) {
    MenuManager contextMenu = new MenuManager();
    contextMenu.setRemoveAllWhenShown(true);
    contextMenu.addMenuListener(menuManager -> {
        setFocus();
        Collection<FileDiffRegion> selected = getSelectedFileDiffs();
        if (selected.isEmpty()) {
            return;
        }
        Collection<FileDiffRegion> haveNew = selected.stream().filter(diff -> !diff.getDiff().getChange().equals(DiffEntry.ChangeType.DELETE)).collect(Collectors.toList());
        Collection<FileDiffRegion> haveOld = selected.stream().filter(diff -> !diff.getDiff().getChange().equals(DiffEntry.ChangeType.ADD)).collect(Collectors.toList());
        Collection<FileDiffRegion> existing = haveNew.stream().filter(diff -> new Path(diff.getRepository().getWorkTree().getAbsolutePath()).append(diff.getDiff().getNewPath()).toFile().exists()).collect(Collectors.toList());
        if (!existing.isEmpty()) {
            menuManager.add(new Action(UIText.CommitFileDiffViewer_OpenWorkingTreeVersionInEditorMenuLabel) {

                @Override
                public void run() {
                    for (FileDiffRegion fileDiff : existing) {
                        File file = new Path(fileDiff.getRepository().getWorkTree().getAbsolutePath()).append(fileDiff.getDiff().getNewPath()).toFile();
                        DiffViewer.openFileInEditor(file, -1);
                    }
                }
            });
        }
        if (!haveNew.isEmpty()) {
            menuManager.add(new Action(UIText.CommitFileDiffViewer_OpenInEditorMenuLabel) {

                @Override
                public void run() {
                    for (FileDiffRegion fileDiff : haveNew) {
                        DiffViewer.openInEditor(fileDiff.getRepository(), fileDiff.getDiff(), DiffEntry.Side.NEW, -1);
                    }
                }
            });
        }
        if (!haveOld.isEmpty()) {
            menuManager.add(new Action(UIText.CommitFileDiffViewer_OpenPreviousInEditorMenuLabel) {

                @Override
                public void run() {
                    for (FileDiffRegion fileDiff : haveOld) {
                        DiffViewer.openInEditor(fileDiff.getRepository(), fileDiff.getDiff(), DiffEntry.Side.OLD, -1);
                    }
                }
            });
        }
        if (selected.size() == 1) {
            menuManager.add(new Separator());
            menuManager.add(new Action(UIText.CommitFileDiffViewer_CompareMenuLabel) {

                @Override
                public void run() {
                    FileDiffRegion fileDiff = selected.iterator().next();
                    DiffViewer.showTwoWayFileDiff(fileDiff.getRepository(), fileDiff.getDiff());
                }
            });
        }
    });
    Menu menu = contextMenu.createContextMenu(viewer.getTree());
    viewer.getTree().setMenu(menu);
}
Also used : FileDiff(org.eclipse.egit.ui.internal.history.FileDiff) ResourceManager(org.eclipse.jface.resource.ResourceManager) Image(org.eclipse.swt.graphics.Image) HashMap(java.util.HashMap) JFaceResources(org.eclipse.jface.resource.JFaceResources) FileDiffRegion(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.FileDiffRegion) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) IDocument(org.eclipse.jface.text.IDocument) Composite(org.eclipse.swt.widgets.Composite) Map(java.util.Map) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) Separator(org.eclipse.jface.action.Separator) Viewer(org.eclipse.jface.viewers.Viewer) PlatformUI(org.eclipse.ui.PlatformUI) Collection(java.util.Collection) MenuManager(org.eclipse.jface.action.MenuManager) Action(org.eclipse.jface.action.Action) Collectors(java.util.stream.Collectors) File(java.io.File) ISharedImages(org.eclipse.ui.ISharedImages) List(java.util.List) UIText(org.eclipse.egit.ui.internal.UIText) Path(org.eclipse.core.runtime.Path) IOpenListener(org.eclipse.jface.viewers.IOpenListener) ISelection(org.eclipse.jface.viewers.ISelection) TreeViewer(org.eclipse.jface.viewers.TreeViewer) LocalResourceManager(org.eclipse.jface.resource.LocalResourceManager) DiffEntry(org.eclipse.jgit.diff.DiffEntry) Menu(org.eclipse.swt.widgets.Menu) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) SafeRunnable(org.eclipse.jface.util.SafeRunnable) LabelProvider(org.eclipse.jface.viewers.LabelProvider) OpenEvent(org.eclipse.jface.viewers.OpenEvent) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Path(org.eclipse.core.runtime.Path) Action(org.eclipse.jface.action.Action) FileDiffRegion(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.FileDiffRegion) MenuManager(org.eclipse.jface.action.MenuManager) Menu(org.eclipse.swt.widgets.Menu) File(java.io.File) Separator(org.eclipse.jface.action.Separator)

Example 7 with FileDiffRegion

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

the class DiffEditorPage method createSourceViewer.

// TextEditor specifics:
@Override
protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
    DiffViewer viewer = new DiffViewer(parent, ruler, getOverviewRuler(), isOverviewRulerVisible(), styles) {

        @Override
        protected void setFont(Font font) {
        // Don't do anything; AbstractTextEditor handles this.
        }
    };
    getSourceViewerDecorationSupport(viewer);
    ProjectionSupport projector = new ProjectionSupport(viewer, getAnnotationAccess(), getSharedColors());
    projector.install();
    viewer.getTextWidget().addCaretListener(event -> {
        if (outlinePage != null) {
            FileDiffRegion region = getFileDiffRange(event.caretOffset);
            if (region != null && !region.equals(currentFileDiffRange)) {
                currentFileDiffRange = region;
                outlinePage.setSelection(new StructuredSelection(region));
            } else {
                currentFileDiffRange = region;
            }
        }
    });
    return viewer;
}
Also used : ProjectionSupport(org.eclipse.jface.text.source.projection.ProjectionSupport) FileDiffRegion(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.FileDiffRegion) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) Font(org.eclipse.swt.graphics.Font)

Example 8 with FileDiffRegion

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

the class DiffEditorPage method setFolding.

// Diff specifics:
private void setFolding() {
    ProjectionViewer viewer = (ProjectionViewer) getSourceViewer();
    if (viewer == null) {
        return;
    }
    IDocument document = viewer.getDocument();
    if (document instanceof DiffDocument) {
        FileDiffRegion[] regions = ((DiffDocument) document).getFileRegions();
        if (regions == null || regions.length <= 1) {
            viewer.disableProjection();
            return;
        }
        viewer.enableProjection();
        Map<Annotation, Position> newAnnotations = new HashMap<>();
        for (FileDiffRegion region : regions) {
            newAnnotations.put(new ProjectionAnnotation(), new Position(region.getOffset(), region.getLength()));
        }
        viewer.getProjectionAnnotationModel().modifyAnnotations(currentFoldingAnnotations, newAnnotations, null);
        currentFoldingAnnotations = newAnnotations.keySet().toArray(new Annotation[newAnnotations.size()]);
    } else {
        viewer.disableProjection();
        currentFoldingAnnotations = null;
    }
}
Also used : ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation) Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) FileDiffRegion(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.FileDiffRegion) ProjectionViewer(org.eclipse.jface.text.source.projection.ProjectionViewer) IDocument(org.eclipse.jface.text.IDocument) Annotation(org.eclipse.jface.text.source.Annotation) ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation)

Aggregations

FileDiffRegion (org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.FileDiffRegion)8 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)5 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 IDocument (org.eclipse.jface.text.IDocument)2 ISelection (org.eclipse.jface.viewers.ISelection)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 File (java.io.File)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Path (org.eclipse.core.runtime.Path)1 UIText (org.eclipse.egit.ui.internal.UIText)1 FileDiff (org.eclipse.egit.ui.internal.history.FileDiff)1 Action (org.eclipse.jface.action.Action)1 MenuManager (org.eclipse.jface.action.MenuManager)1 Separator (org.eclipse.jface.action.Separator)1