Use module name from userData to get module info for scratch file

This commit is contained in:
Natalia Selezneva
2018-03-19 14:19:21 +03:00
parent 31266e49d0
commit c1dd86d323
3 changed files with 42 additions and 31 deletions
@@ -0,0 +1,31 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.core.script
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileWithId
import com.intellij.openapi.vfs.newvfs.FileAttribute
import kotlin.reflect.KProperty
var VirtualFile.scriptRelatedModuleName: String? by ScratchModuleNameProperty()
private val moduleNameAttribute = FileAttribute("kotlin-script-moduleName", 1, false)
private class ScratchModuleNameProperty {
operator fun getValue(file: VirtualFile, property: KProperty<*>): String? {
if (file !is VirtualFileWithId) return null
return moduleNameAttribute.readAttributeBytes(file)?.let { String(it) }
}
operator fun setValue(file: VirtualFile, property: KProperty<*>, newValue: String?) {
if (file !is VirtualFileWithId) return
if (newValue != null) {
moduleNameAttribute.writeAttributeBytes(file, newValue.toByteArray())
}
}
}