Merge remote-tracking branch 'origin/master'

This commit is contained in:
Yann Cébron
2013-12-02 11:07:02 +01:00
14 changed files with 493 additions and 495 deletions
@@ -31,46 +31,49 @@ import org.jetbrains.annotations.NotNull;
* @author <a href="mailto:aefimov.box@gmail.com">Alexey Efimov</a>
*/
final class ImageFileEditorProvider implements FileEditorProvider, DumbAware {
@NonNls private static final String EDITOR_TYPE_ID = "images";
@NonNls private static final String EDITOR_TYPE_ID = "images";
private final ImageFileTypeManager typeManager;
private final ImageFileTypeManager typeManager;
ImageFileEditorProvider(ImageFileTypeManager typeManager) {
this.typeManager = typeManager;
}
ImageFileEditorProvider(ImageFileTypeManager typeManager) {
this.typeManager = typeManager;
}
public boolean accept(@NotNull Project project, @NotNull VirtualFile file) {
return typeManager.isImage(file);
}
@Override
public boolean accept(@NotNull Project project, @NotNull VirtualFile file) {
return typeManager.isImage(file);
}
@NotNull
public FileEditor createEditor(@NotNull Project project, @NotNull VirtualFile file) {
return new ImageFileEditorImpl(project, file);
}
@Override
@NotNull
public FileEditor createEditor(@NotNull Project project, @NotNull VirtualFile file) {
return new ImageFileEditorImpl(project, file);
}
public void disposeEditor(@NotNull FileEditor editor) {
Disposer.dispose(editor);
}
@Override
public void disposeEditor(@NotNull FileEditor editor) {
Disposer.dispose(editor);
}
@NotNull
public FileEditorState readState(@NotNull Element sourceElement, @NotNull Project project, @NotNull VirtualFile file) {
return new FileEditorState() {
public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) {
return false;
}
};
}
@Override
@NotNull
public FileEditorState readState(@NotNull Element sourceElement, @NotNull Project project, @NotNull VirtualFile file) {
return new NullFileEditorState();
}
public void writeState(@NotNull FileEditorState state, @NotNull Project project, @NotNull Element targetElement) {
}
@Override
public void writeState(@NotNull FileEditorState state, @NotNull Project project, @NotNull Element targetElement) {
}
@NotNull
public String getEditorTypeId() {
return EDITOR_TYPE_ID;
}
@Override
@NotNull
public String getEditorTypeId() {
return EDITOR_TYPE_ID;
}
@NotNull
public FileEditorPolicy getPolicy() {
return FileEditorPolicy.HIDE_DEFAULT_EDITOR;
}
@Override
@NotNull
public FileEditorPolicy getPolicy() {
return FileEditorPolicy.HIDE_DEFAULT_EDITOR;
}
}
@@ -31,7 +31,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.api.CanceledStatus;
import org.jetbrains.jps.service.SharedThreadPool;
import javax.tools.*;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import java.io.File;
import java.util.*;
@@ -72,6 +73,7 @@ public class JavacServer {
}
public static void main(String[] args) {
JavacServer server = null;
try {
int port = DEFAULT_SERVER_PORT;
if (args.length > 0) {
@@ -84,12 +86,13 @@ public class JavacServer {
}
}
final JavacServer server = new JavacServer();
server = new JavacServer();
server.start(port);
final JavacServer finalServer = server;
Runtime.getRuntime().addShutdownHook(new Thread("Shutdown hook thread") {
@Override
public void run() {
server.stop();
finalServer.stop();
}
});
@@ -99,7 +102,14 @@ public class JavacServer {
catch (Throwable e) {
System.err.println(SERVER_ERROR_START_MESSAGE + e.getMessage());
e.printStackTrace(System.err);
System.exit(-1);
try {
if (server != null) {
server.stop();
}
}
finally {
System.exit(-1);
}
}
}
@@ -17,7 +17,6 @@ package com.intellij.openapi.editor.actionSystem;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.event.DocumentEvent;
@@ -137,11 +137,11 @@ public abstract class FileEditorManager {
/**
* @deprecated use addTopComponent
*/
public abstract void showEditorAnnotation(@NotNull FileEditor editor, @NotNull JComponent annotationComoponent);
public abstract void showEditorAnnotation(@NotNull FileEditor editor, @NotNull JComponent annotationComponent);
/**
* @deprecated use removeTopComponent
*/
public abstract void removeEditorAnnotation(@NotNull FileEditor editor, @NotNull JComponent annotationComoponent);
public abstract void removeEditorAnnotation(@NotNull FileEditor editor, @NotNull JComponent annotationComponent);
public abstract void addTopComponent(@NotNull final FileEditor editor, @NotNull final JComponent component);
public abstract void removeTopComponent(@NotNull final FileEditor editor, @NotNull final JComponent component);
@@ -62,7 +62,7 @@ public interface FileEditorProvider {
void disposeEditor(@NotNull FileEditor editor);
/**
* Deserializes state from the specified <code>sourceElemet</code>
* Deserialize state from the specified <code>sourceElement</code>
*/
@NotNull
FileEditorState readState(@NotNull Element sourceElement, @NotNull Project project, @NotNull VirtualFile file);
@@ -23,8 +23,8 @@ package com.intellij.openapi.fileEditor;
* @author Vladimir Kondratyev
*/
public interface FileEditorState {
FileEditorState INSTANCE = new FileEditorState() {
@Override
public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) {
return true;
}
@@ -0,0 +1,23 @@
/*
* Copyright 2000-2013 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.fileEditor;
public final class NullFileEditorState implements FileEditorState {
@Override
public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) {
return false;
}
}
@@ -28,7 +28,6 @@ import org.jetbrains.annotations.NonNls;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
/**
@@ -63,6 +62,7 @@ public class EditorNotificationPanel extends JPanel {
public HyperlinkLabel createActionLabel(final String text, @NonNls final String actionId) {
return createActionLabel(text, new Runnable() {
@Override
public void run() {
executeAction(actionId);
}
@@ -71,11 +71,10 @@ public class EditorNotificationPanel extends JPanel {
public HyperlinkLabel createActionLabel(final String text, final Runnable action) {
HyperlinkLabel label = new HyperlinkLabel(text, PlatformColors.BLUE, getBackground(), PlatformColors.BLUE);
label.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(final HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
action.run();
}
label.addHyperlinkListener(new HyperlinkAdapter() {
@Override
protected void hyperlinkActivated(HyperlinkEvent e) {
action.run();
}
});
myLinksPanel.add(label);
@@ -18,6 +18,7 @@ package com.intellij.openapi.fileEditor.impl;
import com.intellij.AppTopics;
import com.intellij.CommonBundle;
import com.intellij.codeStyle.CodeStyleFacade;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
@@ -49,6 +50,7 @@ import com.intellij.openapi.ui.DialogBuilder;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.util.text.StringUtil;
@@ -63,6 +65,7 @@ import com.intellij.testFramework.LightVirtualFile;
import com.intellij.ui.UIBundle;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.util.Function;
import com.intellij.util.PairProcessor;
import com.intellij.util.ThrowableRunnable;
import com.intellij.util.containers.ConcurrentHashSet;
import com.intellij.util.messages.MessageBus;
@@ -649,47 +652,67 @@ public class FileDocumentManagerImpl extends FileDocumentManager implements Appl
myMultiCaster.fileContentReloaded(file, document);
}
protected boolean askReloadFromDisk(final VirtualFile file, final Document document) {
private PairProcessor<VirtualFile, Document> askReloadFromDisk = new PairProcessor<VirtualFile, Document>() {
@Override
public boolean process(final VirtualFile file, final Document document) {
String message = UIBundle.message("file.cache.conflict.message.text", file.getPresentableUrl());
final DialogBuilder builder = new DialogBuilder((Project)null);
builder.setCenterPanel(new JLabel(message, Messages.getQuestionIcon(), SwingConstants.CENTER));
builder.addOkAction().setText(UIBundle.message("file.cache.conflict.load.fs.changes.button"));
builder.addCancelAction().setText(UIBundle.message("file.cache.conflict.keep.memory.changes.button"));
builder.addAction(new AbstractAction(UIBundle.message("file.cache.conflict.show.difference.button")) {
@Override
public void actionPerformed(ActionEvent e) {
String title = UIBundle.message("file.cache.conflict.for.file.dialog.title", file.getPresentableUrl());
final ProjectEx project = (ProjectEx)ProjectLocator.getInstance().guessProjectForFile(file);
SimpleDiffRequest request = new SimpleDiffRequest(project, title);
FileType fileType = file.getFileType();
String fsContent = LoadTextUtil.loadText(file).toString();
request.setContents(new SimpleContent(fsContent, fileType),
new DocumentContent(project, document, fileType));
request.setContentTitles(UIBundle.message("file.cache.conflict.diff.content.file.system.content"),
UIBundle.message("file.cache.conflict.diff.content.memory.content"));
DialogBuilder diffBuilder = new DialogBuilder(project);
DiffPanelImpl diffPanel = (DiffPanelImpl)DiffManager.getInstance().createDiffPanel(diffBuilder.getWindow(), project, diffBuilder, null);
diffPanel.getOptions().setShowSourcePolicy(DiffPanelOptions.ShowSourcePolicy.DONT_SHOW);
diffBuilder.setCenterPanel(diffPanel.getComponent());
diffBuilder.setDimensionServiceKey("FileDocumentManager.FileCacheConflict");
diffPanel.setDiffRequest(request);
diffBuilder.addOkAction().setText(UIBundle.message("file.cache.conflict.save.changes.button"));
diffBuilder.addCancelAction();
diffBuilder.setTitle(title);
if (diffBuilder.show() == DialogWrapper.OK_EXIT_CODE) {
builder.getDialogWrapper().close(DialogWrapper.CANCEL_EXIT_CODE);
}
}
});
builder.setTitle(UIBundle.message("file.cache.conflict.dialog.title"));
builder.setButtonsAlignment(SwingConstants.CENTER);
builder.setHelpId("reference.dialogs.fileCacheConflict");
return builder.show() == 0;
}
};
@TestOnly
public void setAskReloadFromDisk(@NotNull Disposable disposable,
@NotNull PairProcessor<VirtualFile, Document> newProcessor) {
final PairProcessor<VirtualFile, Document> old = askReloadFromDisk;
askReloadFromDisk = newProcessor;
Disposer.register(disposable, new Disposable() {
@Override
public void dispose() {
askReloadFromDisk = old;
}
});
}
private boolean askReloadFromDisk(final VirtualFile file, final Document document) {
ApplicationManager.getApplication().assertIsDispatchThread();
if (!isDocumentUnsaved(document)) return true;
String message = UIBundle.message("file.cache.conflict.message.text", file.getPresentableUrl());
if (ApplicationManager.getApplication().isUnitTestMode()) throw new RuntimeException(message);
final DialogBuilder builder = new DialogBuilder((Project)null);
builder.setCenterPanel(new JLabel(message, Messages.getQuestionIcon(), SwingConstants.CENTER));
builder.addOkAction().setText(UIBundle.message("file.cache.conflict.load.fs.changes.button"));
builder.addCancelAction().setText(UIBundle.message("file.cache.conflict.keep.memory.changes.button"));
builder.addAction(new AbstractAction(UIBundle.message("file.cache.conflict.show.difference.button")) {
@Override
public void actionPerformed(ActionEvent e) {
String title = UIBundle.message("file.cache.conflict.for.file.dialog.title", file.getPresentableUrl());
final ProjectEx project = (ProjectEx)ProjectLocator.getInstance().guessProjectForFile(file);
SimpleDiffRequest request = new SimpleDiffRequest(project, title);
FileType fileType = file.getFileType();
String fsContent = LoadTextUtil.loadText(file).toString();
request.setContents(new SimpleContent(fsContent, fileType),
new DocumentContent(project, document, fileType));
request.setContentTitles(UIBundle.message("file.cache.conflict.diff.content.file.system.content"),
UIBundle.message("file.cache.conflict.diff.content.memory.content"));
DialogBuilder diffBuilder = new DialogBuilder(project);
DiffPanelImpl diffPanel = (DiffPanelImpl)DiffManager.getInstance().createDiffPanel(diffBuilder.getWindow(), project, diffBuilder, null);
diffPanel.getOptions().setShowSourcePolicy(DiffPanelOptions.ShowSourcePolicy.DONT_SHOW);
diffBuilder.setCenterPanel(diffPanel.getComponent());
diffBuilder.setDimensionServiceKey("FileDocumentManager.FileCacheConflict");
diffPanel.setDiffRequest(request);
diffBuilder.addOkAction().setText(UIBundle.message("file.cache.conflict.save.changes.button"));
diffBuilder.addCancelAction();
diffBuilder.setTitle(title);
if (diffBuilder.show() == DialogWrapper.OK_EXIT_CODE) {
builder.getDialogWrapper().close(DialogWrapper.CANCEL_EXIT_CODE);
}
}
});
builder.setTitle(UIBundle.message("file.cache.conflict.dialog.title"));
builder.setButtonsAlignment(SwingConstants.CENTER);
builder.setHelpId("reference.dialogs.fileCacheConflict");
return builder.show() == 0;
return askReloadFromDisk.process(file, document);
}
@Override
@@ -799,7 +822,13 @@ public class FileDocumentManagerImpl extends FileDocumentManager implements Appl
return FileDocumentManagerListener.EP_NAME.getExtensions();
}
protected void handleErrorsOnSave(@NotNull Map<Document, IOException> failures) {
private void handleErrorsOnSave(@NotNull Map<Document, IOException> failures) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
for (IOException exception : failures.values()) {
throw new RuntimeException(exception);
}
return;
}
for (IOException exception : failures.values()) {
LOG.warn(exception);
}
@@ -22,7 +22,6 @@ import com.intellij.openapi.command.CommandListener;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.impl.CommandMerger;
import com.intellij.openapi.components.*;
import com.intellij.openapi.components.StoragePathMacros;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.EditorFactory;
@@ -310,7 +309,7 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory implements Projec
}
}
return VfsUtil.toVirtualFileArray(files);
return VfsUtilCore.toVirtualFileArray(files);
}
@Override
@@ -521,7 +520,7 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory implements Projec
private final VirtualFile myFile;
private final FileEditorState myNavigationState;
private final String myEditorTypeId;
private WeakReference<EditorWindow> myWindow;
private final WeakReference<EditorWindow> myWindow;
public PlaceInfo(@NotNull VirtualFile file, FileEditorState navigationState, String editorTypeId, @Nullable EditorWindow window) {
myNavigationState = navigationState;
@@ -15,87 +15,54 @@
*/
package com.intellij.openapi.fileEditor;
import com.intellij.codeStyle.CodeStyleFacade;
import com.intellij.codeStyle.DefaultCodeStyleFacade;
import com.intellij.ide.DataManager;
import com.intellij.ide.impl.DataManagerImpl;
import com.intellij.mock.MockCommandProcessor;
import com.intellij.mock.MockEditorFactory;
import com.intellij.AppTopics;
import com.intellij.mock.MockVirtualFile;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.UndoConfirmationPolicy;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl;
import com.intellij.openapi.fileTypes.*;
import com.intellij.openapi.fileTypes.impl.FileTypeManagerImpl;
import com.intellij.openapi.fileTypes.impl.InternalFileTypeFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectLocator;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.project.impl.DefaultProjectLocator;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.IoTestUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileEvent;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.testFramework.MockSchemesManagerFactory;
import com.intellij.testFramework.PlatformLiteFixture;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.PlatformLangTestCase;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.LocalTimeCounter;
import org.easymock.classextension.EasyMock;
import com.intellij.util.PairProcessor;
import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
public class FileDocumentManagerImplTest extends PlatformLiteFixture {
private MyMockFileDocumentManager myDocumentManager;
public class FileDocumentManagerImplTest extends PlatformLangTestCase {
private FileDocumentManagerImpl myDocumentManager;
public Boolean myReloadFromDisk;
@Override
protected void setUp() throws Exception {
super.setUp();
initApplication();
registerExtension(FileTypeFactory.FILE_TYPE_FACTORY_EP, new InternalFileTypeFactory());
registerExtensionPoint(FileDocumentSynchronizationVetoer.EP_NAME, FileDocumentSynchronizationVetoer.class);
getApplication().registerService(CommandProcessor.class, new MyMockCommandProcessor());
getApplication().registerService(CodeStyleFacade.class, new DefaultCodeStyleFacade());
getApplication().registerService(ProjectLocator.class, new DefaultProjectLocator());
MockEditorFactory editorFactory = new MockEditorFactory();
getApplication().registerService(EditorFactory.class, editorFactory);
final LanguageFileType[] fileType = {null};
getApplication().addComponent(FileTypeManager.class, new FileTypeManagerImpl(null, new MockSchemesManagerFactory()) {
@NotNull
myReloadFromDisk = Boolean.TRUE;
FileDocumentManagerImpl impl = (FileDocumentManagerImpl)FileDocumentManager.getInstance();
impl.setAskReloadFromDisk(getTestRootDisposable(), new PairProcessor<VirtualFile, Document>() {
@Override
public FileType getFileTypeByFileName(@NotNull String fileName) {
return fileType[0];
}
@NotNull
@Override
public FileType getFileTypeByFile(@NotNull VirtualFile file) {
return fileType[0];
}
@NotNull
@Override
public FileType getFileTypeByExtension(@NotNull String extension) {
return fileType[0];
public boolean process(VirtualFile file, Document document) {
if (myReloadFromDisk == null) {
fail();
return false;
}
return myReloadFromDisk.booleanValue();
}
});
fileType[0] = StdFileTypes.JAVA;
getApplication().getComponent(FileTypeManager.class);
final VirtualFileManager virtualFileManager = EasyMock.createMock(VirtualFileManager.class);
final ProjectManager projectManager = EasyMock.createMock(ProjectManager.class);
myDocumentManager = new MyMockFileDocumentManager(virtualFileManager, projectManager);
getApplication().registerService(FileDocumentManager.class, myDocumentManager);
getApplication().registerService(DataManager.class, new DataManagerImpl());
myDocumentManager = impl;
}
public void testGetCachedDocument_Cached() throws Exception {
@@ -104,14 +71,14 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testGetCachedDocument_NotCached() throws Exception {
final VirtualFile file = newTextFile();
final VirtualFile file = createFile();
final Document document = myDocumentManager.getDocument(file);
assertSame(myDocumentManager.getCachedDocument(file), document);
}
public void testGetDocument_CreateNew() throws Exception {
final VirtualFile file = newTextFile();
final VirtualFile file = createFile();
final Document document = myDocumentManager.getDocument(file);
assertNotNull(document);
assertEquals("test", document.getText());
@@ -120,7 +87,7 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testGetDocument_CreateNew_ReadOnly() throws Exception {
final MockVirtualFile file = newTextFile();
final VirtualFile file = createFile();
file.setWritable(false);
final Document document = myDocumentManager.getDocument(file);
assertNotNull(document);
@@ -130,7 +97,7 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testGetDocument_ReturnCachedValueTwice() throws Exception {
final VirtualFile file = newTextFile();
final VirtualFile file = createFile();
final Document document = myDocumentManager.getDocument(file);
assertNotNull(document);
assertEquals("test", document.getText());
@@ -140,7 +107,7 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testGetDocument_CreatesNewAfterGCed() throws Exception {
final VirtualFile file = newTextFile();
final VirtualFile file = createFile();
Document document = myDocumentManager.getDocument(file);
int idCode = System.identityHashCode(document);
//noinspection UnusedAssignment
@@ -159,7 +126,7 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testGetUnsavedDocuments_CreatedDocument() throws Exception {
final VirtualFile file = newTextFile();
final VirtualFile file = createFile();
myDocumentManager.getDocument(file);
final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments();
@@ -167,10 +134,16 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testGetUnsavedDocuments_ModifiedDocument() throws Exception {
final VirtualFile file = newTextFile();
Document document = myDocumentManager.getDocument(file);
final VirtualFile file = createFile();
final Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "xxx");
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
document.insertString(0, "xxx");
}
});
final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments();
assertEquals(1, unsavedDocuments.length);
@@ -179,10 +152,16 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testGetUnsavedDocuments_afterSaveAllDocuments() throws Exception {
final VirtualFile file = newTextFile();
Document document = myDocumentManager.getDocument(file);
final VirtualFile file = createFile();
final Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "xxx");
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
document.insertString(0, "xxx");
}
});
myDocumentManager.saveAllDocuments();
final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments();
@@ -190,10 +169,16 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testGetUnsavedDocuments_afterSaveDocuments() throws Exception {
final VirtualFile file = newTextFile();
Document document = myDocumentManager.getDocument(file);
final VirtualFile file = createFile();
final Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "xxx");
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
document.insertString(0, "xxx");
}
});
myDocumentManager.saveDocument(document);
final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments();
@@ -201,34 +186,55 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testGetUnsavedDocuments_afterSaveDocumentWithProblems() throws Exception {
final VirtualFile file = new MockVirtualFile("test.txt", "test") {
@Override
@NotNull
public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException {
throw new IOException("");
try {
final VirtualFile file = new MockVirtualFile("test.txt", "test") {
@Override
@NotNull
public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException {
throw new IOException("");
}
};
final Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
document.insertString(0, "xxx");
}
});
try {
myDocumentManager.saveDocument(document);
fail("must throw IOException");
}
catch (RuntimeException e) {
assertTrue(e.getCause() instanceof IOException);
}
};
Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "xxx");
myDocumentManager.saveDocument(document);
assertNotNull(myDocumentManager.myExceptionOnSave);
final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments();
assertEquals(1, unsavedDocuments.length);
assertSame(document, unsavedDocuments[0]);
assertTrue(Arrays.equals("test".getBytes("UTF-8"), file.contentsToByteArray()));
final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments();
assertEquals(1, unsavedDocuments.length);
assertSame(document, unsavedDocuments[0]);
assertTrue(Arrays.equals("test".getBytes("UTF-8"), file.contentsToByteArray()));
}
finally {
myDocumentManager.dropAllUnsavedDocuments();
}
}
public void testUnsavedDocument_DoNotGC() throws Exception {
final VirtualFile file = newTextFile();
final VirtualFile file = createFile();
Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "xxx");
int idCode = System.identityHashCode(document);
assertNotNull(file.toString(), document);
WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
@Override
public void run() {
myDocumentManager.getDocument(file).insertString(0, "xxx");
}
});
//noinspection UnusedAssignment
document = null;
@@ -240,10 +246,16 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testUnsavedDocument_GcAfterSave() throws Exception {
final VirtualFile file = newTextFile();
final VirtualFile file = createFile();
Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "xxx");
WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
@Override
public void run() {
myDocumentManager.getDocument(file).insertString(0, "xxx");
}
});
int idCode = System.identityHashCode(document);
//noinspection UnusedAssignment
document = null;
@@ -258,7 +270,7 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testSaveDocument_DocumentWasNotChanged() throws Exception {
final VirtualFile file = newTextFile();
final VirtualFile file = createFile();
final long stamp = file.getModificationStamp();
Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
@@ -267,11 +279,17 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testSaveDocument_DocumentWasChanged() throws Exception {
final VirtualFile file = newTextFile();
final VirtualFile file = createFile();
final long stamp = file.getModificationStamp();
Document document = myDocumentManager.getDocument(file);
final Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "xxx ");
WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
@Override
public void run() {
document.insertString(0, "xxx ");
}
});
myDocumentManager.saveDocument(document);
assertTrue(stamp != file.getModificationStamp());
@@ -280,11 +298,17 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testSaveAllDocuments_DocumentWasChanged() throws Exception {
final VirtualFile file = newTextFile();
final VirtualFile file = createFile();
final long stamp = file.getModificationStamp();
Document document = myDocumentManager.getDocument(file);
final Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "xxx ");
WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
@Override
public void run() {
document.insertString(0, "xxx ");
}
});
myDocumentManager.saveAllDocuments();
assertTrue(stamp != file.getModificationStamp());
@@ -292,7 +316,7 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testGetFile() throws Exception {
final VirtualFile file = newTextFile();
final VirtualFile file = createFile();
Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
assertSame(file, myDocumentManager.getFile(document));
@@ -307,35 +331,51 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
public void testRememberSeparators() throws Exception {
final VirtualFile file = new MockVirtualFile("test.txt", "test\rtest");
Document document = myDocumentManager.getDocument(file);
final Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "xxx ");
WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
@Override
public void run() {
document.insertString(0, "xxx ");
}
});
myDocumentManager.saveAllDocuments();
assertTrue(Arrays.equals("xxx test\rtest".getBytes("UTF-8"), file.contentsToByteArray()));
}
public void testContentChanged_noDocument() throws Exception {
final MockVirtualFile file = new MockVirtualFile("test.txt", "test\rtest");
file.setListener(myDocumentManager);
file.setContent(null, "xxx", true);
final VirtualFile file = createFile();
setContent(file, "xxx");
assertNull(myDocumentManager.getCachedDocument(file));
}
VirtualFile createFile(String name, String content) throws IOException {
File file = createTempFile(name, content);
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(file);
assertNotNull(virtualFile);
return virtualFile;
}
VirtualFile createFile() throws IOException {
return createFile("test.txt", "test");
}
void setContent(VirtualFile file, String content) throws IOException {
file.setBinaryContent(content.getBytes(CharsetToolkit.UTF8_CHARSET));
}
public void testContentChanged_documentPresent() throws Exception {
final MockVirtualFile file = new MockVirtualFile("test.txt", "test\rtest");
file.setListener(myDocumentManager);
VirtualFile file = createFile();
Document document = myDocumentManager.getDocument(file);
file.setContent(null, "xxx", true);
setContent(file, "xxx");
assertNotNull(file.toString(), document);
assertEquals("xxx", document.getText());
assertEquals(file.getModificationStamp(), document.getModificationStamp());
}
public void testContentChanged_ignoreEventsFromSelf() throws Exception {
final MockVirtualFile file = new MockVirtualFile("test.txt", "test\rtest");
file.setListener(myDocumentManager);
final VirtualFile file = createFile("test.txt", "test\rtest");
Document document = myDocumentManager.getDocument(file);
file.setContent(myDocumentManager, "xxx", true);
file.setBinaryContent("xxx".getBytes(CharsetToolkit.UTF8_CHARSET), -1,-1,myDocumentManager);
assertNotNull(file.toString(), document);
assertEquals("test\ntest", document.getText());
}
@@ -358,9 +398,15 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
};
}
};
Document document = myDocumentManager.getDocument(file);
final Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "xxx");
WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
@Override
public void run() {
document.insertString(0, "xxx");
}
});
final long stamp = document.getModificationStamp();
myDocumentManager.saveAllDocuments();
@@ -368,59 +414,63 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
public void testContentChanged_reloadChangedDocument() throws Exception {
final MockVirtualFile file = new MockVirtualFile("test.txt", "test\rtest");
file.setListener(myDocumentManager);
Document document = myDocumentManager.getDocument(file);
final VirtualFile file = createFile();
final Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "zzz");
WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
@Override
public void run() {
document.insertString(0, "zzz");
}
});
myDocumentManager.myReloadFromDisk = Boolean.TRUE;
try {
file.setContent(null, "xxx", true);
assertEquals("xxx", document.getText());
assertEquals(file.getModificationStamp(), document.getModificationStamp());
assertEquals(0, myDocumentManager.getUnsavedDocuments().length);
}
finally {
myDocumentManager.myReloadFromDisk = null;
}
myReloadFromDisk = Boolean.TRUE;
setContent(file, "xxx");
assertEquals("xxx", document.getText());
assertEquals(file.getModificationStamp(), document.getModificationStamp());
assertEquals(0, myDocumentManager.getUnsavedDocuments().length);
}
public void testContentChanged_DoNotReloadChangedDocument() throws Exception {
final MockVirtualFile file = newTextFile();
file.setListener(myDocumentManager);
Document document = myDocumentManager.getDocument(file);
final VirtualFile file = createFile();
final Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "old ");
WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
@Override
public void run() {
document.insertString(0, "old ");
}
});
myDocumentManager.myReloadFromDisk = Boolean.FALSE;
try {
long oldDocumentStamp = document.getModificationStamp();
myReloadFromDisk = Boolean.FALSE;
long oldDocumentStamp = document.getModificationStamp();
file.setContent(null, "xxx", true);
file.setBinaryContent("xxx".getBytes(CharsetToolkit.UTF8_CHARSET));
assertEquals("old test", document.getText());
assertEquals(oldDocumentStamp, document.getModificationStamp());
}
finally {
myDocumentManager.myReloadFromDisk = null;
}
assertEquals("old test", document.getText());
assertEquals(oldDocumentStamp, document.getModificationStamp());
}
public void testSaveDocument_DoNotSaveIfModStampEqualsToFile() throws Exception {
final VirtualFile file = new MockVirtualFile("test.txt", "test") {
@NotNull
@Override
public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException {
fail();
throw new IOException();
}
};
DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file);
final VirtualFile file = createFile();
final DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "zzz");
document.setModificationStamp(file.getModificationStamp());
WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
@Override
public void run() {
document.insertString(0, "zzz");
document.setModificationStamp(file.getModificationStamp());
}
});
getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {
@Override
public void beforeDocumentSaving(@NotNull Document documentToSave) {
assertNotSame(document, documentToSave);
}
});
myDocumentManager.saveDocument(document);
}
@@ -442,23 +492,19 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
document.insertString(0, "zzz");
file.setContent(null, "xxx", false);
myDocumentManager.myReloadFromDisk = Boolean.TRUE;
try {
myDocumentManager.saveAllDocuments();
long fileStamp = file.getModificationStamp();
myReloadFromDisk = Boolean.TRUE;
myDocumentManager.saveAllDocuments();
long fileStamp = file.getModificationStamp();
assertEquals("xxx", document.getText());
assertEquals(file.getModificationStamp(), document.getModificationStamp());
assertEquals(file.getModificationStamp(), fileStamp);
assertEquals(0, myDocumentManager.getUnsavedDocuments().length);
}
finally {
myDocumentManager.myReloadFromDisk = null;
}
assertEquals("xxx", document.getText());
assertEquals(file.getModificationStamp(), document.getModificationStamp());
assertEquals(file.getModificationStamp(), fileStamp);
assertEquals(0, myDocumentManager.getUnsavedDocuments().length);
}
public void testContentChanged_doNotReloadChangedDocumentOnSave() throws Exception {
final MockVirtualFile file = new MockVirtualFile("test.txt", "test") {
final MockVirtualFile file =
new MockVirtualFile("test.txt", "test") {
@Override
public void refresh(boolean asynchronous, boolean recursive, Runnable postRunnable) {
long oldStamp = getModificationStamp();
@@ -467,154 +513,150 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture {
}
};
myDocumentManager.myReloadFromDisk = Boolean.FALSE;
try {
Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
document.insertString(0, "old ");
long documentStamp = document.getModificationStamp();
myReloadFromDisk = Boolean.FALSE;
final Document document = myDocumentManager.getDocument(file);
assertNotNull(file.toString(), document);
WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
@Override
public void run() {
document.insertString(0, "old ");
}
});
file.setContent(null, "xxx", false);
long documentStamp = document.getModificationStamp();
myDocumentManager.saveAllDocuments();
file.setContent(null, "xxx", false);
assertEquals("old test", document.getText());
assertEquals(file.getModificationStamp(), document.getModificationStamp());
assertTrue(Arrays.equals("old test".getBytes("UTF-8"), file.contentsToByteArray()));
assertEquals(documentStamp, document.getModificationStamp());
}
finally {
myDocumentManager.myReloadFromDisk = null;
}
myDocumentManager.saveAllDocuments();
assertEquals("old test", document.getText());
assertEquals(file.getModificationStamp(), document.getModificationStamp());
assertTrue(Arrays.equals("old test".getBytes("UTF-8"), file.contentsToByteArray()));
assertEquals(documentStamp, document.getModificationStamp());
}
public void testReplaceDocumentTestWithTheSameTest() throws Exception {
final boolean[] canCallSave = {true};
final VirtualFile file = new MockVirtualFile("test.txt", "test") {
@NotNull
@Override
public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException {
if (!canCallSave[0]) fail();
return super.getOutputStream(requestor, newModificationStamp, newTimeStamp);
}
};
DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file);
public void testReplaceDocumentTextWithTheSameText() throws Exception {
final VirtualFile file = createFile();
final DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file);
String newText = "test text";
final String newText = "test text";
assertNotNull(file.toString(), document);
document.replaceString(0, document.getTextLength(), newText);
assertTrue(myDocumentManager.isDocumentUnsaved(document));
myDocumentManager.saveDocument(document);
WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
@Override
public void run() {
document.replaceString(0, document.getTextLength(), newText);
assertTrue(myDocumentManager.isDocumentUnsaved(document));
myDocumentManager.saveDocument(document);
canCallSave[0] = false;
getProject().getMessageBus().connect(getTestRootDisposable())
.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {
@Override
public void beforeDocumentSaving(@NotNull Document documentToSave) {
assertNotSame(document, documentToSave);
}
});
final long modificationStamp = document.getModificationStamp();
final long modificationStamp = document.getModificationStamp();
document.replaceString(0, document.getTextLength(), newText);
if (myDocumentManager.isDocumentUnsaved(document)) {
assertTrue(document.getModificationStamp() > modificationStamp);
}
else {
assertEquals(modificationStamp, document.getModificationStamp());
}
document.replaceString(0, document.getTextLength(), newText);
if (myDocumentManager.isDocumentUnsaved(document)) {
assertTrue(document.getModificationStamp() > modificationStamp);
}
else {
assertEquals(modificationStamp, document.getModificationStamp());
}
}
});
}
public void testExternalReplaceWithTheSameText() throws Exception {
final long[] modificationStamp = new long[1];
modificationStamp[0] = 1;
final VirtualFile file = new MockVirtualFile("test.txt", "test") {
@NotNull
@Override
public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException {
fail();
throw new IOException();
}
@Override
public long getModificationStamp() {
return modificationStamp[0];
}
@Override
public void refresh(boolean asynchronous, boolean recursive, Runnable postRunnable) {
myDocumentManager.contentsChanged(new VirtualFileEvent(null, this, null, 1, 2));
}
};
final VirtualFile file = createFile();
long modificationStamp = file.getModificationStamp();
DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file);
modificationStamp[0] = 2;
FileUtil.writeToFile(new File(file.getPath()), "xxx");
file.refresh(false, false);
assertNotNull(file.toString(), document);
assertEquals(2, document.getModificationStamp());
assertNotSame(file.getModificationStamp(), modificationStamp);
assertEquals(file.getModificationStamp(), document.getModificationStamp());
}
private static MockVirtualFile newTextFile() {
return new MockVirtualFile("test.txt", "test");
public void testFileTypeModificationDocumentPreservation() throws Exception {
File ioFile = IoTestUtil.createTestFile("test.html", "<html>some text</html>");
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile);
assertNotNull(ioFile.getPath(), file);
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document original = documentManager.getDocument(file);
assertNotNull(file.getPath(), original);
renameFile(file, "test.wtf");
Document afterRename = documentManager.getDocument(file);
assertTrue(afterRename + " != " + original, afterRename == original);
}
private static class MyMockFileDocumentManager extends FileDocumentManagerImpl {
private static final FileDocumentManagerListener[] LISTENERS = new FileDocumentManagerListener[0];
public void testFileTypeChangeDocumentDetach() throws Exception {
File ioFile = IoTestUtil.createTestFile("test.html", "<html>some text</html>");
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile);
assertNotNull(ioFile.getPath(), file);
private Collection<IOException> myExceptionOnSave = null;
private Boolean myReloadFromDisk = null;
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document original = documentManager.getDocument(file);
assertNotNull(file.getPath(), original);
public MyMockFileDocumentManager(VirtualFileManager virtualFileManager, ProjectManager projectManager) {
super(virtualFileManager, projectManager);
renameFile(file, "test.png");
Document afterRename = documentManager.getDocument(file);
assertNull(afterRename + " != null", afterRename);
}
private static void renameFile(VirtualFile file, String newName) throws IOException {
AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(null);
try {
file.rename(null, newName);
}
@Override
protected void handleErrorsOnSave(@NotNull Map<Document, IOException> failures) {
myExceptionOnSave = failures.values();
finally {
token.finish();
}
}
@Override
protected boolean askReloadFromDisk(VirtualFile file, Document document) {
if (myReloadFromDisk == null) {
fail();
return false;
public void testNoPSIModificationsDuringSave() throws IOException {
File ioFile = IoTestUtil.createTestFile("test.txt", "<html>some text</html>");
VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile);
assertNotNull(ioFile.getPath(), virtualFile);
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document original = documentManager.getDocument(virtualFile);
assertNotNull(virtualFile.getPath(), original);
final PsiFile file = getPsiFile(original);
FileDocumentManagerListener saveListener = new FileDocumentManagerAdapter() {
@Override
public void beforeDocumentSaving(@NotNull Document document) {
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
try {
file.getFirstChild().delete();
fail("Must not modify PSI inside save listener");
}
catch (IncorrectOperationException e) {
assertEquals("Must not modify PSI inside save listener", e.getMessage());
}
}
});
}
else {
return myReloadFromDisk.booleanValue();
};
getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(AppTopics.FILE_DOCUMENT_SYNC, saveListener);
final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
document.insertString(1,"y");
}
}
});
@NotNull
@Override
protected FileDocumentManagerListener[] getListeners() {
return LISTENERS;
}
}
private static class MyMockCommandProcessor extends MockCommandProcessor {
@Override
public void executeCommand(Project project, @NotNull Runnable runnable, String name, Object groupId) {
runnable.run();
}
@Override
public void executeCommand(Project project,
@NotNull Runnable runnable,
String name,
Object groupId,
@NotNull UndoConfirmationPolicy confirmationPolicy,
Document document) {
runnable.run();
}
@Override
public void executeCommand(Project project,
@NotNull Runnable runnable,
String name,
Object groupId,
@NotNull UndoConfirmationPolicy confirmationPolicy) {
runnable.run();
}
@Override
public void executeCommand(@NotNull Runnable runnable, String name, Object groupId) {
runnable.run();
}
FileDocumentManager.getInstance().saveAllDocuments();
}
}
@@ -1,113 +0,0 @@
/*
* Copyright 2000-2013 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.fileEditor;
import com.intellij.AppTopics;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.io.IoTestUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.PlatformLangTestCase;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
public class RealFileDocumentManagerTest extends PlatformLangTestCase {
public void testFileTypeModificationDocumentPreservation() throws Exception {
File ioFile = IoTestUtil.createTestFile("test.html", "<html>some text</html>");
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile);
assertNotNull(ioFile.getPath(), file);
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document original = documentManager.getDocument(file);
assertNotNull(file.getPath(), original);
renameFile(file, "test.wtf");
Document afterRename = documentManager.getDocument(file);
assertTrue(afterRename + " != " + original, afterRename == original);
}
public void testFileTypeChangeDocumentDetach() throws Exception {
File ioFile = IoTestUtil.createTestFile("test.html", "<html>some text</html>");
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile);
assertNotNull(ioFile.getPath(), file);
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document original = documentManager.getDocument(file);
assertNotNull(file.getPath(), original);
renameFile(file, "test.png");
Document afterRename = documentManager.getDocument(file);
assertNull(afterRename + " != null", afterRename);
}
private static void renameFile(VirtualFile file, String newName) throws IOException {
AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(RealFileDocumentManagerTest.class);
try {
file.rename(RealFileDocumentManagerTest.class, newName);
}
finally {
token.finish();
}
}
public void testNoPSIModificationsDuringSave() throws IOException {
File ioFile = IoTestUtil.createTestFile("test.txt", "<html>some text</html>");
VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile);
assertNotNull(ioFile.getPath(), virtualFile);
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document original = documentManager.getDocument(virtualFile);
assertNotNull(virtualFile.getPath(), original);
final PsiFile file = getPsiFile(original);
FileDocumentManagerListener saveListener = new FileDocumentManagerAdapter() {
@Override
public void beforeDocumentSaving(@NotNull Document document) {
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
try {
file.getFirstChild().delete();
fail("Must not modify PSI inside save listener");
}
catch (IncorrectOperationException e) {
assertEquals("Must not modify PSI inside save listener", e.getMessage());
}
}
});
}
};
getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(AppTopics.FILE_DOCUMENT_SYNC, saveListener);
final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
document.insertString(1,"y");
}
});
FileDocumentManager.getInstance().saveAllDocuments();
}
}
@@ -27,31 +27,38 @@ import org.jetbrains.annotations.NotNull;
* @author peter
*/
public abstract class PerspectiveFileEditorProvider extends WeighedFileEditorProvider {
@Override
@NotNull
public abstract PerspectiveFileEditor createEditor(@NotNull Project project, @NotNull VirtualFile file);
@Override
public void disposeEditor(@NotNull FileEditor editor) {
Disposer.dispose(editor);
}
@Override
@NotNull
public FileEditorState readState(@NotNull Element sourceElement, @NotNull Project project, @NotNull VirtualFile file) {
return new FileEditorState() {
@Override
public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) {
return true;
}
};
}
@Override
public void writeState(@NotNull FileEditorState state, @NotNull Project project, @NotNull Element targetElement) {
}
@Override
@NotNull
@NonNls
public final String getEditorTypeId() {
return getComponentName();
}
@Override
@NotNull
public final FileEditorPolicy getPolicy() {
return FileEditorPolicy.PLACE_AFTER_DEFAULT_EDITOR;
@@ -117,7 +117,7 @@ public class SchemaReferencesProvider extends PsiReferenceProvider {
}
public TextRange getRangeInElement() {
return new TextRange(1,myElement.getTextLength()-1);
return ElementManipulators.getValueTextRange(myElement);
}
@Nullable