Move refactoring on kts fixes

Set file target filenames to kts if moved declarations are from kts
Fixed exceptions throwing on script declaration move that could lead to not valid file state (Fixed #KT-36129)
This commit is contained in:
Igor Yakovlev
2020-01-29 14:20:43 +03:00
parent e05df9d838
commit ff831aded4
2 changed files with 4 additions and 2 deletions
@@ -29,6 +29,7 @@ abstract class ScriptChangeListener(protected val project: Project) {
protected fun getAnalyzableKtFileForScript(vFile: VirtualFile): KtFile? { protected fun getAnalyzableKtFileForScript(vFile: VirtualFile): KtFile? {
if (project.isDisposed) return null if (project.isDisposed) return null
if (!vFile.isValid) return null
return runReadAction { return runReadAction {
(PsiManager.getInstance(project).findFile(vFile) as? KtFile)?.takeIf { (PsiManager.getInstance(project).findFile(vFile) as? KtFile)?.takeIf {
@@ -399,8 +399,9 @@ fun guessNewFileName(declarationsToMove: Collection<KtNamedDeclaration>): String
if (declarationsToMove.isEmpty()) return null if (declarationsToMove.isEmpty()) return null
val representative = declarationsToMove.singleOrNull() val representative = declarationsToMove.singleOrNull()
?: declarationsToMove.filterIsInstance<KtClassOrObject>().singleOrNull() ?: declarationsToMove.filterIsInstance<KtClassOrObject>().singleOrNull()
val newFileName = representative?.run { "$name.${KotlinFileType.EXTENSION}" } val newFileName = representative?.run {
?: declarationsToMove.first().containingFile.name if (containingKtFile.isScript()) "$name.kts" else "$name.${KotlinFileType.EXTENSION}"
} ?: declarationsToMove.first().containingFile.name
return newFileName.capitalize() return newFileName.capitalize()
} }