From 2d3041c2dcad5eddf4f990621885083ac80e3a34 Mon Sep 17 00:00:00 2001 From: Nadya Zabrodina Date: Mon, 10 Apr 2017 17:35:11 +0300 Subject: [PATCH] [patch]: get rid of Getters, remove unnecessary invokeAndWait --- .../impl/patch/apply/ApplyTextFilePatch.java | 3 +- .../patch/ApplyPatchForBaseRevisionTexts.java | 35 +++++++++++-------- .../patch/PatchDiffRequestFactory.java | 15 +++----- .../patch/TextFilePatchInProgress.java | 9 +++-- .../shelf/DiffShelvedChangesAction.java | 20 +++++------ 5 files changed, 39 insertions(+), 43 deletions(-) diff --git a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/apply/ApplyTextFilePatch.java b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/apply/ApplyTextFilePatch.java index 592c86806d02..b94a0d3b048d 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/apply/ApplyTextFilePatch.java +++ b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/apply/ApplyTextFilePatch.java @@ -60,7 +60,8 @@ public class ApplyTextFilePatch extends ApplyFilePatchBase { return new Result(ApplyPatchStatus.FAILURE) { @Override public ApplyPatchForBaseRevisionTexts getMergeData() { - return ApplyPatchForBaseRevisionTexts.create(project, fileToPatch, pathBeforeRename, myPatch, baseContents); + return ApplyPatchForBaseRevisionTexts + .create(project, fileToPatch, pathBeforeRename, myPatch, baseContents != null ? baseContents.get() : null); } }; } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchForBaseRevisionTexts.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchForBaseRevisionTexts.java index 02fc9ed8b8e5..9bd2f9814c5f 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchForBaseRevisionTexts.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchForBaseRevisionTexts.java @@ -15,6 +15,7 @@ */ package com.intellij.openapi.vcs.changes.patch; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.diff.impl.patch.PatchHunk; import com.intellij.openapi.diff.impl.patch.TextFilePatch; import com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier; @@ -22,11 +23,11 @@ import com.intellij.openapi.editor.Document; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.fileEditor.impl.LoadTextUtil; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Getter; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vcs.FilePath; import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.CalledInAny; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -40,8 +41,9 @@ public class ApplyPatchForBaseRevisionTexts { private final List myWarnings; @NotNull + @CalledInAny public static ApplyPatchForBaseRevisionTexts create(final Project project, final VirtualFile file, final FilePath pathBeforeRename, - final TextFilePatch patch, @Nullable final Getter baseContents) { + final TextFilePatch patch, @Nullable final CharSequence baseContents) { assert ! patch.isNewFile(); final String beforeVersionId = patch.getBeforeVersionId(); DefaultPatchBaseVersionProvider provider = null; @@ -55,26 +57,20 @@ public class ApplyPatchForBaseRevisionTexts { } } + @CalledInAny private ApplyPatchForBaseRevisionTexts(final DefaultPatchBaseVersionProvider provider, final FilePath pathBeforeRename, final TextFilePatch patch, final VirtualFile file, - @Nullable Getter baseContents) { + @Nullable CharSequence baseContents) { myWarnings = new ArrayList<>(); - final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance(); - Document document = fileDocumentManager.getDocument(file); - if (document != null) { - fileDocumentManager.saveDocument(document); - } - myLocal = LoadTextUtil.loadText(file); + myLocal = getLocalFileContent(file); final List hunks = patch.getHunks(); - CharSequence contents = baseContents != null ? baseContents.get() : null; - if (contents != null) { - contents = StringUtil.convertLineSeparators(contents.toString()); - myBase = contents; - final GenericPatchApplier applier = new GenericPatchApplier(contents, hunks); + if (baseContents != null) { + myBase = StringUtil.convertLineSeparators(baseContents.toString()); + final GenericPatchApplier applier = new GenericPatchApplier(myBase, hunks); if (!applier.execute()) { applier.trySolveSomehow(); } @@ -107,6 +103,17 @@ public class ApplyPatchForBaseRevisionTexts { setPatched(applier.getAfter()); } + @NotNull + private static CharSequence getLocalFileContent(@NotNull VirtualFile file) { + return ReadAction.compute(() -> { + Document document = FileDocumentManager.getInstance().getDocument(file); + if (document != null) { + return document.getText(); + } + return LoadTextUtil.loadText(file); + }); + } + public CharSequence getLocal() { return myLocal; } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/PatchDiffRequestFactory.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/PatchDiffRequestFactory.java index 26d0fa205802..535ea7bf2dbc 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/PatchDiffRequestFactory.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/PatchDiffRequestFactory.java @@ -25,15 +25,12 @@ import com.intellij.diff.merge.MergeResult; import com.intellij.diff.requests.DiffRequest; import com.intellij.diff.requests.SimpleDiffRequest; import com.intellij.diff.util.DiffUtil; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diff.impl.patch.TextFilePatch; import com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier; import com.intellij.openapi.editor.Document; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Getter; -import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.UserDataHolder; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vcs.VcsBundle; @@ -44,6 +41,7 @@ import com.intellij.openapi.vcs.changes.patch.tool.ApplyPatchMergeRequest; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.Consumer; import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.CalledInAny; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -63,22 +61,17 @@ public class PatchDiffRequestFactory { } @NotNull + @CalledInAny public static DiffRequest createConflictDiffRequest(@Nullable Project project, @Nullable VirtualFile file, @NotNull TextFilePatch patch, @NotNull String afterTitle, - @NotNull final Getter textsGetter, - @NotNull String name, - @NotNull UserDataHolder context, - @NotNull ProgressIndicator indicator) + @NotNull final ApplyPatchForBaseRevisionTexts texts, + @NotNull String name) throws DiffRequestProducerException { if (file == null) throw new DiffRequestProducerException("Can't show diff for '" + name + "'"); if (file.getFileType().isBinary()) throw new DiffRequestProducerException("Can't show diff for binary file '" + name + "'"); - final Ref textsRef = new Ref<>(); - ApplicationManager.getApplication().invokeAndWait(() -> textsRef.set(textsGetter.get()), indicator.getModalityState()); - ApplyPatchForBaseRevisionTexts texts = textsRef.get(); - if (texts.getLocal() == null) throw new DiffRequestProducerException("Can't show diff for '" + file.getPresentableUrl() + "'"); if (texts.getBase() == null) { diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/TextFilePatchInProgress.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/TextFilePatchInProgress.java index 13ac5d6a79cf..404c03730a56 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/TextFilePatchInProgress.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/TextFilePatchInProgress.java @@ -27,7 +27,6 @@ import com.intellij.openapi.fileTypes.UnknownFileType; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Getter; import com.intellij.openapi.util.UserDataHolder; import com.intellij.openapi.vcs.FilePath; import com.intellij.openapi.vcs.changes.ContentRevision; @@ -75,7 +74,6 @@ public class TextFilePatchInProgress extends AbstractFilePatchInProgress baseContentGetter = () -> patchReader.getBaseRevision(project, path); return new DiffRequestProducer() { @NotNull @Override @@ -88,12 +86,13 @@ public class TextFilePatchInProgress extends AbstractFilePatchInProgress getter = - () -> ApplyPatchForBaseRevisionTexts.create(project, file, VcsUtil.getFilePath(file), getPatch(), baseContentGetter); + ApplyPatchForBaseRevisionTexts texts = + ApplyPatchForBaseRevisionTexts + .create(project, file, VcsUtil.getFilePath(file), getPatch(), patchReader.getBaseRevision(project, path)); String afterTitle = getPatch().getAfterVersionId(); if (afterTitle == null) afterTitle = "Patched Version"; - return PatchDiffRequestFactory.createConflictDiffRequest(project, file, getPatch(), afterTitle, getter, getName(), context, indicator); + return PatchDiffRequestFactory.createConflictDiffRequest(project, file, getPatch(), afterTitle, texts, getName()); } else { return PatchDiffRequestFactory.createDiffRequest(project, change, getName(), context, indicator); diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesAction.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesAction.java index daa7f7c7fff8..ac7578eaee77 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesAction.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesAction.java @@ -35,7 +35,6 @@ import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Getter; import com.intellij.openapi.util.UserDataHolder; import com.intellij.openapi.util.UserDataHolderBase; import com.intellij.openapi.util.io.FileUtil; @@ -62,6 +61,8 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.util.*; +import static com.intellij.util.ObjectUtils.chooseNotNull; + public class DiffShelvedChangesAction extends AnAction implements DumbAware { public void update(final AnActionEvent e) { e.getPresentation().setEnabled(isEnabled(e.getDataContext())); @@ -190,18 +191,13 @@ public class DiffShelvedChangesAction extends AnAction implements DumbAware { final CommitContext commitContext = new CommitContext(); final TextFilePatch patch = preloader.getPatch(shelvedChange, commitContext); final FilePath pathBeforeRename = patchContext.getPathBeforeRename(file); - final String relativePath = patch.getAfterName() == null ? patch.getBeforeName() : patch.getAfterName(); - final Getter baseContentGetter = () -> { - BaseRevisionTextPatchEP baseRevisionTextPatchEP = - Extensions.findExtension(PatchEP.EP_NAME, project, BaseRevisionTextPatchEP.class); - return baseRevisionTextPatchEP.provideContent(relativePath, commitContext); - }; - - Getter getter = - () -> ApplyPatchForBaseRevisionTexts.create(project, file, pathBeforeRename, patch, baseContentGetter); - - return PatchDiffRequestFactory.createConflictDiffRequest(project, file, patch, "Shelved Version", getter, getName(), context, indicator); + CharSequence baseContents = Extensions.findExtension(PatchEP.EP_NAME, project, BaseRevisionTextPatchEP.class) + .provideContent(chooseNotNull(patch.getAfterName(), patch.getBeforeName()), commitContext); + ApplyPatchForBaseRevisionTexts texts = + ApplyPatchForBaseRevisionTexts.create(project, file, pathBeforeRename, patch, baseContents); + return PatchDiffRequestFactory + .createConflictDiffRequest(project, file, patch, "Shelved Version", texts, getName()); } catch (VcsException e) { throw new DiffRequestProducerException("Can't show diff for '" + getName() + "'", e);