[patch]: get rid of Getters, remove unnecessary invokeAndWait

This commit is contained in:
Nadya Zabrodina
2017-05-17 17:22:46 +03:00
parent 5f0ed26635
commit 2d3041c2dc
5 changed files with 39 additions and 43 deletions
@@ -60,7 +60,8 @@ public class ApplyTextFilePatch extends ApplyFilePatchBase<TextFilePatch> {
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);
}
};
}
@@ -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<String> myWarnings;
@NotNull
@CalledInAny
public static ApplyPatchForBaseRevisionTexts create(final Project project, final VirtualFile file, final FilePath pathBeforeRename,
final TextFilePatch patch, @Nullable final Getter<CharSequence> 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<CharSequence> 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<PatchHunk> 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;
}
@@ -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<ApplyPatchForBaseRevisionTexts> 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<ApplyPatchForBaseRevisionTexts> 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) {
@@ -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<TextFil
final PatchChange change = getChange();
final FilePatch patch = getPatch();
final String path = patch.getBeforeName() == null ? patch.getAfterName() : patch.getBeforeName();
final Getter<CharSequence> baseContentGetter = () -> patchReader.getBaseRevision(project, path);
return new DiffRequestProducer() {
@NotNull
@Override
@@ -88,12 +86,13 @@ public class TextFilePatchInProgress extends AbstractFilePatchInProgress<TextFil
if (isConflictingChange()) {
final VirtualFile file = getCurrentBase();
Getter<ApplyPatchForBaseRevisionTexts> 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);
@@ -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<CharSequence> baseContentGetter = () -> {
BaseRevisionTextPatchEP baseRevisionTextPatchEP =
Extensions.findExtension(PatchEP.EP_NAME, project, BaseRevisionTextPatchEP.class);
return baseRevisionTextPatchEP.provideContent(relativePath, commitContext);
};
Getter<ApplyPatchForBaseRevisionTexts> 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);