diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsHandler.kt index 44ddb8c052f..de86e428c67 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsHandler.kt @@ -62,7 +62,7 @@ class MoveKotlinDeclarationsHandler : MoveHandlerDelegate() { return elements.mapNotNullTo(LinkedHashSet(), getContainer).singleOrNull() } - private fun KtNamedDeclaration.canMove() = if (this is KtClassOrObject) !isLocal else parent is KtFile + private fun KtNamedDeclaration.canMove() = if (this is KtClassOrObject) !isLocal else isTopLevelInFileOrScript(this) private fun doMoveWithCheck( project: Project, elements: Array, targetContainer: PsiElement?, callback: MoveCallback?, editor: Editor? diff --git a/idea/testData/refactoring/moveHandler/declarations/topLevelClassInScript/test.kts b/idea/testData/refactoring/moveHandler/declarations/topLevelClassInScript/test.kts new file mode 100644 index 00000000000..b129e556885 --- /dev/null +++ b/idea/testData/refactoring/moveHandler/declarations/topLevelClassInScript/test.kts @@ -0,0 +1,3 @@ +class Foo +fun bar() = 1 +val baz = 0 \ No newline at end of file diff --git a/idea/testData/refactoring/moveHandler/declarations/topLevelFunInScript/test.kts b/idea/testData/refactoring/moveHandler/declarations/topLevelFunInScript/test.kts new file mode 100644 index 00000000000..6eda9f21f11 --- /dev/null +++ b/idea/testData/refactoring/moveHandler/declarations/topLevelFunInScript/test.kts @@ -0,0 +1,3 @@ +class Foo +fun bar() = 1 +val baz = 0 \ No newline at end of file diff --git a/idea/testData/refactoring/moveHandler/declarations/topLevelValInScript/test.kts b/idea/testData/refactoring/moveHandler/declarations/topLevelValInScript/test.kts new file mode 100644 index 00000000000..a19707d89b8 --- /dev/null +++ b/idea/testData/refactoring/moveHandler/declarations/topLevelValInScript/test.kts @@ -0,0 +1,3 @@ +class Foo +fun bar() = 1 +val baz = 0 \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveKotlinDeclarationsHandlerTest.kt b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveKotlinDeclarationsHandlerTest.kt index b2aa9e52232..957b0730927 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveKotlinDeclarationsHandlerTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveKotlinDeclarationsHandlerTest.kt @@ -202,4 +202,19 @@ class MoveKotlinDeclarationsHandlerTest : KotlinMultiFileTestCase() { val typeAlias = getElementAtCaret(rootDir, "test.kt").getNonStrictParentOfType()!! assert(handler.canMove(arrayOf(typeAlias), null)) } + + fun testTopLevelClassInScript() = doTest { rootDir, handler -> + val klass = getElementAtCaret(rootDir, "test.kts").getNonStrictParentOfType()!! + assert(handler.canMove(arrayOf(klass), null)) + } + + fun testTopLevelFunInScript() = doTest { rootDir, handler -> + val function = getElementAtCaret(rootDir, "test.kts").getNonStrictParentOfType()!! + assert(handler.canMove(arrayOf(function), null)) + } + + fun testTopLevelValInScript() = doTest { rootDir, handler -> + val property = getElementAtCaret(rootDir, "test.kts").getNonStrictParentOfType()!! + assert(handler.canMove(arrayOf(property), null)) + } } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveKotlinDeclarationsHandlerTest.kt.181 b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveKotlinDeclarationsHandlerTest.kt.181 index fe2ab245670..f6c9334cefa 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveKotlinDeclarationsHandlerTest.kt.181 +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveKotlinDeclarationsHandlerTest.kt.181 @@ -203,4 +203,14 @@ class MoveKotlinDeclarationsHandlerTest : KotlinMultiFileTestCase() { val typeAlias = getElementAtCaret(rootDir, "test.kt").getNonStrictParentOfType()!! assert(handler.canMove(arrayOf(typeAlias), null)) } + + fun testTopLevelFunInScript() = doTest { rootDir, handler -> + val function = getElementAtCaret(rootDir, "test.kts").getNonStrictParentOfType()!! + assert(handler.canMove(arrayOf(function), null)) + } + + fun testTopLevelValInScript() = doTest { rootDir, handler -> + val property = getElementAtCaret(rootDir, "test.kts").getNonStrictParentOfType()!! + assert(handler.canMove(arrayOf(property), null)) + } } \ No newline at end of file