/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.diff;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.LineSeparator;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;

public class FileContent extends DiffContent {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.diff.FileContent");
  private final VirtualFile myFile;
  private Document myDocument;
  private final Project myProject;
  private final FileDocumentManager myDocumentManager;
  private FileType myTypeForEmpty;

  public FileContent(Project project, @NotNull VirtualFile file) {
    myProject = project;
    myFile = file;
    myDocumentManager = FileDocumentManager.getInstance();
  }

  public Document getDocument() {
    if (myDocument == null && DiffContentUtil.isTextFile(myFile))
      myDocument = FileDocumentManager.getInstance().getDocument(myFile);
    return myDocument;
  }

  public OpenFileDescriptor getOpenFileDescriptor(int offset) {
    return new OpenFileDescriptor(myProject, myFile, offset);
  }

  public VirtualFile getFile() {
    return myFile;
  }

  public FileType getContentType() {
    if (isEmpty()) return myTypeForEmpty;
    return DiffContentUtil.getContentType(myFile);
  }

  public byte[] getBytes() throws IOException {
    if (myFile.isDirectory()) return null;
    return myFile.contentsToByteArray();
  }

  public boolean isBinary() {
    if (myFile.isDirectory()) return false;
    if (isEmpty()) return myTypeForEmpty.isBinary();
    return myFile.getFileType().isBinary();
  }

  public static FileContent createFromTempFile(Project project, String name, String ext, @NotNull byte[] content) throws IOException {
    final File tempFile;
    if (content.length == 0) {
      tempFile = FileUtil.createTempFile(name, "txt");
      FileUtil.writeToFile(tempFile, "Does not exist");
    } else {
      tempFile = FileUtil.createTempFile(name, ext);
      FileUtil.writeToFile(tempFile, content);
    }
    tempFile.deleteOnExit();
    final LocalFileSystem lfs = LocalFileSystem.getInstance();
    VirtualFile file = lfs.findFileByIoFile(tempFile);
    if (file == null) {
      file = lfs.refreshAndFindFileByIoFile(tempFile);
    }
    if (file != null) {
      final FileContent fileContent = new FileContent(project, file);
      if (content.length == 0) {
        fileContent.setIsEmpty(true);
        fileContent.myTypeForEmpty = FileTypeManager.getInstance().getFileTypeByFileName(name + "." + ext);
      }
      else {
        FileTypeManager.getInstance().detectFileTypeFromContent(file);
      }
      return fileContent;
    }
    throw new IOException("Can not create temp file for revision content");
  }

  @NotNull
  @Override
  public LineSeparator getLineSeparator() {
    return LineSeparator.fromString(myDocumentManager.getLineSeparator(myFile, myProject));
  }

}

@@ -936,32 +936,87 @@
 ileTypeManager;%0A
+import com.intellij.openapi.fileTypes.UnknownFileType;%0A
 import com.intel
@@ -1265,16 +1265,59 @@
 NotNull;
+%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Aimport
@@ -1503,24 +1503,33 @@
 ontent%22);%0A  
+@NotNull 
 private fina
@@ -1671,16 +1671,27 @@
 anager;%0A
+  @Nullable
   privat
@@ -1707,24 +1707,16 @@
 e myType
-ForEmpty
 ;%0A%0A  pub
@@ -2210,32 +2210,43 @@
 , offset);%0A  %7D%0A%0A
+  @NotNull%0A
   public Virtual
@@ -2282,24 +2282,37 @@
 yFile;%0A  %7D%0A%0A
+  @Nullable %0A
   public Fil
@@ -2344,95 +2344,88 @@
     
-if (isEmpty()) return myTypeForEmpty;%0A    return DiffContentUtil.getContentType(myFile)
+FileType type = myFile.getFileType();%0A    return isUnknown(type) ? myType : type
 ;%0A  
@@ -2650,52 +2650,111 @@
 if (
-isEmpty()) return myTypeForEmpty.isBinary();
+myType != null && !myType.isBinary()) %7B%0A      return false;                                      %0A    %7D
 %0A   
@@ -3556,42 +3556,59 @@
     
-if (c
+fileC
 ontent.
-length == 0) %7B
+myType = detectType(file);
 %0A      
- 
+return
  fil
@@ -3619,61 +3619,194 @@
 tent
-.setIsEmpty(true);%0A    
+;%0A    %7D%0A    throw new IOException(%22Can not create temp file for revision content%22);%0A  %7D%0A%0A  @Nullable
   
+%0A
   
-fileContent.myTypeForEmpty
+private static FileType detectType(@NotNull VirtualFile file) %7B%0A    FileType type
  = F
@@ -3855,50 +3855,40 @@
 File
-Name(name + %22.%22 + ext);%0A      %7D%0A      else
+(file);%0A    if (isUnknown(type))
  %7B%0A 
@@ -3888,25 +3888,30 @@
 e)) %7B%0A      
- 
+type =
  FileTypeMan
@@ -3970,117 +3970,329 @@
     
-  
 %7D%0A    
-  return fileContent;%0A    %7D%0A    throw new IOException(%22Can not create temp file for revision content%22
+// the type is left null intentionally: according to the contract of #getContentType, %0A    // if file type is null it may be taken from another diff content%0A    return isUnknown(type) ? null : type;   %0A  %7D%0A%0A  private static boolean isUnknown(@NotNull FileType type) %7B%0A    return type.equals(UnknownFileType.INSTANCE
 );%0A 

Optimize file type detection