VCS: patch creation separated from patch application, small cleanup
authorirengrig <Irina.Chernushina@jetbrains.com>
Mon, 15 Mar 2010 16:11:39 +0000 (19:11 +0300)
committerirengrig <Irina.Chernushina@jetbrains.com>
Mon, 15 Mar 2010 16:11:39 +0000 (19:11 +0300)
platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/apply/ApplyFilePatchBase.java
platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/apply/ApplyTextFilePatch.java

index b6fda11b4e0b1d557a984bab7966cff482764bbb..502dc684693f24620fd2feef8faa95a78c183221 100644 (file)
@@ -174,7 +174,6 @@ public abstract class ApplyFilePatchBase<T extends FilePatch> implements ApplyFi
     return patchedDir;
   }
 
-  // todo move somewhere?
   @Nullable
   public static ApplyPatchStatus applyModifications(final TextFilePatch patch, final CharSequence text, final StringBuilder newText) throws
                                                                                                                                      ApplyPatchException {
index d59542a67df5852ec2ab815257e14ec7cdde7480..307cfda70cbdb212c7ccc5a9f3f3c973e315e0dd 100644 (file)
@@ -17,19 +17,14 @@ package com.intellij.openapi.diff.impl.patch.apply;
 
 import com.intellij.openapi.diff.impl.patch.ApplyPatchException;
 import com.intellij.openapi.diff.impl.patch.ApplyPatchStatus;
-import com.intellij.openapi.diff.impl.patch.PatchHunk;
 import com.intellij.openapi.diff.impl.patch.TextFilePatch;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.fileEditor.impl.LoadTextUtil;
-import com.intellij.openapi.util.text.LineTokenizer;
 import com.intellij.openapi.vfs.VirtualFile;
 import org.jetbrains.annotations.Nullable;
 
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
 
 public class ApplyTextFilePatch extends ApplyFilePatchBase<TextFilePatch> {
   public ApplyTextFilePatch(final TextFilePatch patch) {
@@ -41,7 +36,7 @@ public class ApplyTextFilePatch extends ApplyFilePatchBase<TextFilePatch> {
     byte[] fileContents = fileToPatch.contentsToByteArray();
     CharSequence text = LoadTextUtil.getTextByBinaryPresentation(fileContents, fileToPatch);
     StringBuilder newText = new StringBuilder();
-    ApplyPatchStatus status = applyModifications(text, newText);
+    ApplyPatchStatus status = ApplyFilePatchBase.applyModifications(myPatch, text, newText);
     if (status != ApplyPatchStatus.ALREADY_APPLIED) {
       final Document document = FileDocumentManager.getInstance().getDocument(fileToPatch);
       if (document == null) {
@@ -53,28 +48,6 @@ public class ApplyTextFilePatch extends ApplyFilePatchBase<TextFilePatch> {
     return status;
   }
 
-  // todo taken to another place also, cheeeeeck
-  @Nullable
-  public ApplyPatchStatus applyModifications(final CharSequence text, final StringBuilder newText) throws ApplyPatchException {
-    final List<PatchHunk> hunks = myPatch.getHunks();
-    if (hunks.size() == 0) {
-      return ApplyPatchStatus.SUCCESS;
-    }
-    List<String> lines = new ArrayList<String>();
-    Collections.addAll(lines, LineTokenizer.tokenize(text, false));
-    ApplyPatchStatus result = null;
-    for(PatchHunk hunk: hunks) {
-      result = ApplyPatchStatus.and(result, new ApplyPatchHunk(hunk).apply(lines));
-    }
-    for(int i=0; i<lines.size(); i++) {
-      newText.append(lines.get(i));
-      if (i < lines.size()-1 || !hunks.get(hunks.size()-1).isNoNewLineAtEnd()) {
-        newText.append("\n");
-      }
-    }
-    return result;
-  }
-
   protected void applyCreate(final VirtualFile newFile) throws IOException, ApplyPatchException {
     final Document document = FileDocumentManager.getInstance().getDocument(newFile);
     if (document == null) {