Search in sources :

Example 1 with DiffRegionFormatter

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

the class BlameInformationControl method createDiffLinkAndText.

private void createDiffLinkAndText(final RevCommit parent, final Diff diff) throws IOException {
    String parentId = parent.toObjectId().abbreviate(7).name();
    String parentMessage = parent.getShortMessage();
    EditList interestingDiff = getInterestingDiff(diff.getEditList());
    final Integer parentLine;
    if (!interestingDiff.isEmpty())
        parentLine = Integer.valueOf(interestingDiff.get(0).getBeginA());
    else
        parentLine = null;
    Composite header = new Composite(diffComposite, SWT.NONE);
    header.setLayout(GridLayoutFactory.fillDefaults().numColumns(2).create());
    Label diffHeaderLabel = new Label(header, SWT.NONE);
    diffHeaderLabel.setText(NLS.bind(UIText.BlameInformationControl_DiffHeaderLabel, parentId, parentMessage));
    showAnnotationsLink = new Link(header, SWT.NONE);
    showAnnotationsLink.setText(UIText.BlameInformationControl_ShowAnnotationsLink);
    showAnnotationsLinkSelectionAdapter = new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            blameParent(parent, diff, parentLine);
        }
    };
    showAnnotationsLink.addSelectionListener(showAnnotationsLinkSelectionAdapter);
    DiffViewer diffText = new DiffViewer(diffComposite, null, SWT.NONE);
    diffText.configure(new DiffViewer.Configuration(EditorsUI.getPreferenceStore()));
    diffText.getControl().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
    DiffDocument document = new DiffDocument();
    try (DiffRegionFormatter diffFormatter = new DiffRegionFormatter(document)) {
        diffFormatter.setContext(1);
        diffFormatter.setRepository(revision.getRepository());
        diffFormatter.format(interestingDiff, diff.getOldText(), diff.getNewText());
        try (ObjectReader reader = revision.getRepository().newObjectReader()) {
            DiffEntry diffEntry = CompareCoreUtils.getChangeDiffEntry(revision.getRepository(), revision.getSourcePath(), revision.getCommit(), parent, reader);
            if (diffEntry != null) {
                FileDiff fileDiff = new FileDiff(revision.getCommit(), diffEntry);
                document.setDefault(revision.getRepository(), fileDiff);
            }
        }
        document.connect(diffFormatter);
    }
    diffText.setDocument(document);
}
Also used : Composite(org.eclipse.swt.widgets.Composite) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) DiffViewer(org.eclipse.egit.ui.internal.commit.DiffViewer) DiffRegionFormatter(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ObjectReader(org.eclipse.jgit.lib.ObjectReader) FileDiff(org.eclipse.egit.ui.internal.history.FileDiff) EditList(org.eclipse.jgit.diff.EditList) DiffDocument(org.eclipse.egit.ui.internal.commit.DiffDocument) Link(org.eclipse.swt.widgets.Link) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 2 with DiffRegionFormatter

use of org.eclipse.egit.ui.internal.commit.DiffRegionFormatter 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 DiffRegionFormatter

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

the class GitHistoryPage method formatDiffs.

private void formatDiffs(final List<FileDiff> diffs) {
    Job.getJobManager().cancel(JobFamilies.HISTORY_DIFF);
    if (diffs.isEmpty()) {
        if (UIUtils.isUsable(diffViewer)) {
            IDocument document = new Document();
            diffViewer.setDocument(document);
            resizeCommentAndDiffScrolledComposite();
        }
        return;
    }
    final Repository repository = fileViewer.getRepository();
    Job formatJob = new Job(UIText.GitHistoryPage_FormatDiffJobName) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            if (monitor.isCanceled()) {
                return Status.CANCEL_STATUS;
            }
            int maxLines = Activator.getDefault().getPreferenceStore().getInt(UIPreferences.HISTORY_MAX_DIFF_LINES);
            final DiffDocument document = new DiffDocument();
            try (DiffRegionFormatter formatter = new DiffRegionFormatter(document, document.getLength(), maxLines)) {
                SubMonitor progress = SubMonitor.convert(monitor, diffs.size());
                for (FileDiff diff : diffs) {
                    if (progress.isCanceled() || diff.getCommit().getParentCount() > 1 || document.getNumberOfLines() > maxLines) {
                        break;
                    }
                    progress.subTask(diff.getPath());
                    try {
                        formatter.write(repository, diff);
                    } catch (IOException ignore) {
                    // Ignored
                    }
                    progress.worked(1);
                }
                if (progress.isCanceled()) {
                    return Status.CANCEL_STATUS;
                }
                document.connect(formatter);
            }
            UIJob uiJob = new UIJob(UIText.GitHistoryPage_FormatDiffJobName) {

                @Override
                public IStatus runInUIThread(IProgressMonitor uiMonitor) {
                    if (uiMonitor.isCanceled()) {
                        return Status.CANCEL_STATUS;
                    }
                    if (UIUtils.isUsable(diffViewer)) {
                        diffViewer.setDocument(document);
                        resizeCommentAndDiffScrolledComposite();
                    }
                    return Status.OK_STATUS;
                }

                @Override
                public boolean belongsTo(Object family) {
                    return JobFamilies.HISTORY_DIFF.equals(family);
                }
            };
            uiJob.setRule(pageSchedulingRule);
            GitHistoryPage.this.schedule(uiJob);
            return Status.OK_STATUS;
        }

        @Override
        public boolean belongsTo(Object family) {
            return JobFamilies.HISTORY_DIFF.equals(family);
        }
    };
    formatJob.setRule(pageSchedulingRule);
    schedule(formatJob);
}
Also used : SubMonitor(org.eclipse.core.runtime.SubMonitor) IOException(java.io.IOException) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DiffDocument(org.eclipse.egit.ui.internal.commit.DiffDocument) Point(org.eclipse.swt.graphics.Point) Repository(org.eclipse.jgit.lib.Repository) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) DiffRegionFormatter(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter) UIJob(org.eclipse.ui.progress.UIJob) RevObject(org.eclipse.jgit.revwalk.RevObject) DiffDocument(org.eclipse.egit.ui.internal.commit.DiffDocument) UIJob(org.eclipse.ui.progress.UIJob) Job(org.eclipse.core.runtime.jobs.Job) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

DiffRegionFormatter (org.eclipse.egit.ui.internal.commit.DiffRegionFormatter)3 DiffDocument (org.eclipse.egit.ui.internal.commit.DiffDocument)2 Document (org.eclipse.jface.text.Document)2 IDocument (org.eclipse.jface.text.IDocument)2 IOException (java.io.IOException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 DiffRegion (org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.DiffRegion)1 DiffViewer (org.eclipse.egit.ui.internal.commit.DiffViewer)1 FileDiff (org.eclipse.egit.ui.internal.history.FileDiff)1 DiffEntry (org.eclipse.jgit.diff.DiffEntry)1 EditList (org.eclipse.jgit.diff.EditList)1 ObjectReader (org.eclipse.jgit.lib.ObjectReader)1 Repository (org.eclipse.jgit.lib.Repository)1 RevObject (org.eclipse.jgit.revwalk.RevObject)1 ScrolledComposite (org.eclipse.swt.custom.ScrolledComposite)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 Point (org.eclipse.swt.graphics.Point)1