IDEA-132813 correct fix — don't save module file twice (1 - regular save 2 - forced save)

This commit is contained in:
Vladimir Krivosheev
2015-06-25 17:18:26 +02:00
parent 3cd5386003
commit 508c2a0b7a
6 changed files with 25 additions and 41 deletions
@@ -20,13 +20,10 @@ import com.intellij.openapi.components.StateStorageOperation;
import com.intellij.openapi.components.StoragePathMacros;
import com.intellij.openapi.components.TrackingPathMacroSubstitutor;
import com.intellij.openapi.module.impl.ModuleImpl;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
public class ModuleStateStorageManager extends StateStorageManagerImpl {
@@ -52,20 +49,11 @@ public class ModuleStateStorageManager extends StateStorageManagerImpl {
@NotNull
@Override
public List<StateStorage.SaveSession> createSaveSessions() {
final ModuleStoreImpl.ModuleFileData data = myModule.getStateStore().getMainStorageData();
List<StateStorage.SaveSession> sessions = super.createSaveSessions();
if (!data.isDirty()) {
return sessions;
if (myModule.getStateStore().getMainStorageData().isDirty()) {
// force XmlElementStorageSaveSession creation
getExternalizationSession(myModule.getStateStore().getMainStorage());
}
return ContainerUtil.concat(sessions, Collections.singletonList(new StateStorage.SaveSession() {
@Override
public void save() throws IOException {
if (data.isDirty()) {
myModule.getStateStore().getMainStorage().forceSave();
}
}
}));
return super.createSaveSessions();
}
};
}
@@ -81,4 +69,4 @@ public class ModuleStateStorageManager extends StateStorageManagerImpl {
protected StateStorage.Listener createStorageTopicListener() {
return myModule.getProject().getMessageBus().syncPublisher(StateStorage.PROJECT_STORAGE_TOPIC);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 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.
@@ -108,6 +108,7 @@ public class ModuleStoreImpl extends BaseFileConfigurableStoreImpl implements IM
myOptions = new TreeMap<String, String>();
}
@Override
public boolean isDirty() {
return dirty;
}
@@ -103,13 +103,6 @@ public class FileBasedStorage extends XmlElementStorage {
return new FileSaveSession(storageData);
}
public void forceSave() throws IOException {
XmlElementStorageSaveSession externalizationSession = startExternalization();
if (externalizationSession != null) {
externalizationSession.forceSave();
}
}
private class FileSaveSession extends XmlElementStorageSaveSession {
protected FileSaveSession(@NotNull StorageData storageData) {
super(storageData);
@@ -320,7 +320,7 @@ public abstract class StateStorageManagerImpl implements StateStorageManager, Di
}
@Nullable
private StateStorage.ExternalizationSession getExternalizationSession(@NotNull StateStorage stateStorage) {
protected final StateStorage.ExternalizationSession getExternalizationSession(@NotNull StateStorage stateStorage) {
StateStorage.ExternalizationSession session = mySessions.get(stateStorage);
if (session == null) {
session = stateStorage.startExternalization();
@@ -29,7 +29,6 @@ import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
@@ -160,8 +159,9 @@ public abstract class XmlElementStorage extends StateStorageBase<StorageData> {
}
}
protected abstract class XmlElementStorageSaveSession extends SaveSessionBase {
public abstract class XmlElementStorageSaveSession extends SaveSessionBase {
private final StorageData myOriginalStorageData;
@Nullable
private StorageData myCopiedStorageData;
private final Map<String, Element> myNewLiveStates = new THashMap<String, Element>();
@@ -173,7 +173,7 @@ public abstract class XmlElementStorage extends StateStorageBase<StorageData> {
@Nullable
@Override
public final SaveSession createSaveSession() {
return checkIsSavingDisabled() || myCopiedStorageData == null ? null : this;
return checkIsSavingDisabled() || (myCopiedStorageData == null && !myOriginalStorageData.isDirty()) ? null : this;
}
@Override
@@ -186,24 +186,22 @@ public abstract class XmlElementStorage extends StateStorageBase<StorageData> {
}
}
public void forceSave() throws IOException {
LOG.assertTrue(myCopiedStorageData == null);
if (myBlockSavingTheContent) {
return;
}
doSave(getElement(myOriginalStorageData, isCollapsePathsOnSave(), Collections.<String, Element>emptyMap()));
}
@Override
public final void save() throws IOException {
if (myBlockSavingTheContent) {
return;
}
doSave(getElement(myCopiedStorageData, isCollapsePathsOnSave(), myNewLiveStates));
myStorageData = myCopiedStorageData;
StorageData storageData = myCopiedStorageData;
if (storageData == null) {
storageData = myOriginalStorageData;
if (!storageData.isDirty()) {
LOG.warn("Copied storage data must be not null because original storage data is not dirty");
}
}
doSave(getElement(storageData, isCollapsePathsOnSave(), myNewLiveStates));
myStorageData = storageData;
}
// only because default project store hack
@@ -46,6 +46,10 @@ public class StorageData extends StorageDataBase {
this(COMPONENT);
}
public boolean isDirty() {
return false;
}
public StorageData(@NotNull String rootElementName) {
myStates = new StateMap();
myRootElementName = rootElementName;