}
val newDigest = element!!.digest()
- if (externalInfo != null && currentFileNameWithoutExtension === fileNameWithoutExtension && externalInfo.isDigestEquals(newDigest)) {
- return
- }
-
- // save only if scheme differs from bundled
- if (isEqualToBundledScheme(externalInfo, newDigest, scheme)) {
- return
- }
+ when {
+ externalInfo != null && currentFileNameWithoutExtension === fileNameWithoutExtension && externalInfo.isDigestEquals(newDigest) -> return
+ isEqualToBundledScheme(externalInfo, newDigest, scheme) -> return
- // we must check it only here to avoid delete old scheme just because it is empty (old idea save -> new idea delete on open)
- if (processor is LazySchemeProcessor && processor.isSchemeDefault(scheme, newDigest)) {
- externalInfo?.scheduleDelete()
- return
+ // we must check it only here to avoid delete old scheme just because it is empty (old idea save -> new idea delete on open)
+ processor is LazySchemeProcessor && processor.isSchemeDefault(scheme, newDigest) -> {
+ externalInfo?.scheduleDelete()
+ return
+ }
}
val fileName = fileNameWithoutExtension!! + schemeExtension
providerPath = null
}
- // if another new scheme uses old name of this scheme, so, we must not delete it (as part of rename operation)
- val renamed = externalInfo != null && fileNameWithoutExtension !== currentFileNameWithoutExtension && nameGenerator.value(currentFileNameWithoutExtension)
+ // if another new scheme uses old name of this scheme, we must not delete it (as part of rename operation)
+ val renamed = externalInfo != null && fileNameWithoutExtension !== currentFileNameWithoutExtension && nameGenerator.isUnique(currentFileNameWithoutExtension)
if (providerPath == null) {
if (useVfs) {
var file: VirtualFile? = null
}
if (renamed) {
- file = dir.findChild(externalInfo!!.fileName)
- if (file != null) {
- runWriteAction {
- file!!.rename(this, fileName)
+ val oldFile = dir.findChild(externalInfo!!.fileName)
+ oldFile?.let {
+ // VFS doesn't allow to rename to existing file, so, check it
+ if (dir!!.findChild(fileName) == null) {
+ runWriteAction { it.rename(this, fileName) }
+ file = oldFile
+ }
+ else {
+ externalInfo!!.scheduleDelete()
}
}
}
}
runWriteAction {
- file!!.getOutputStream(this).use {
- byteOut.writeTo(it)
- }
+ file!!.getOutputStream(this).use { byteOut.writeTo(it) }
}
}
else {
*/
package com.intellij.configurationStore
+import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.options.ExternalizableScheme
import com.intellij.openapi.options.SchemeManagerFactory
import com.intellij.openapi.util.io.FileUtil
import com.intellij.testFramework.PlatformTestUtil
import com.intellij.testFramework.ProjectRule
import com.intellij.testFramework.TemporaryDirectory
+import com.intellij.testFramework.runInEdtAndWait
import com.intellij.util.SmartList
import com.intellij.util.io.createDirectories
import com.intellij.util.io.directoryStreamIfExists
+import com.intellij.util.io.readText
import com.intellij.util.io.write
import com.intellij.util.lang.CompoundRuntimeException
import com.intellij.util.loadElement
assertThat(dir.resolve("s2.xml")).isRegularFile()
}
+ @Test fun `rename A to B and B to A`() {
+ val dir = tempDirManager.newPath()
+ val schemeManager = createSchemeManager(dir)
+
+ val a = TestScheme("a", "a")
+ val b = TestScheme("b", "b")
+ schemeManager.setSchemes(listOf(a, b))
+ schemeManager.save()
+
+ assertThat(dir.resolve("a.xml")).isRegularFile()
+ assertThat(dir.resolve("b.xml")).isRegularFile()
+
+ a.name = "b"
+ b.name = "a"
+
+ schemeManager.save()
+
+ assertThat(dir.resolve("a.xml").readText()).isEqualTo("""<scheme name="a" data="b" />""")
+ assertThat(dir.resolve("b.xml").readText()).isEqualTo("""<scheme name="b" data="a" />""")
+ }
+
+ @Test fun `VFS - rename A to B and B to A`() {
+ val dir = tempDirManager.newPath()
+ val schemeManager = SchemeManagerImpl(FILE_SPEC, TestSchemesProcessor(), null, dir, messageBus = ApplicationManager.getApplication().messageBus)
+
+ val a = TestScheme("a", "a")
+ val b = TestScheme("b", "b")
+ schemeManager.setSchemes(listOf(a, b))
+ runInEdtAndWait { schemeManager.save() }
+
+ assertThat(dir.resolve("a.xml")).isRegularFile()
+ assertThat(dir.resolve("b.xml")).isRegularFile()
+
+ a.name = "b"
+ b.name = "a"
+
+ runInEdtAndWait { schemeManager.save() }
+
+ assertThat(dir.resolve("a.xml").readText()).isEqualTo("""<scheme name="a" data="b" />""")
+ assertThat(dir.resolve("b.xml").readText()).isEqualTo("""<scheme name="b" data="a" />""")
+ }
+
@Test fun `path must not contains ROOT_CONFIG macro`() {
assertThatThrownBy({ SchemeManagerFactory.getInstance().create("\$ROOT_CONFIG$/foo", TestSchemesProcessor()) }).hasMessage("Path must not contains ROOT_CONFIG macro, corrected: foo")
}