Move: Fix top-level declaration detection for scripts

#KT-25858 Fixed
This commit is contained in:
Alexey Sedunov
2018-08-03 14:13:48 +03:00
parent 1797a50fc9
commit 1b15ce3ddc
6 changed files with 35 additions and 1 deletions
@@ -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<out PsiElement>, targetContainer: PsiElement?, callback: MoveCallback?, editor: Editor?
@@ -0,0 +1,3 @@
class <caret>Foo
fun bar() = 1
val baz = 0
@@ -0,0 +1,3 @@
class Foo
fun <caret>bar() = 1
val baz = 0
@@ -0,0 +1,3 @@
class Foo
fun bar() = 1
val <caret>baz = 0
@@ -202,4 +202,19 @@ class MoveKotlinDeclarationsHandlerTest : KotlinMultiFileTestCase() {
val typeAlias = getElementAtCaret(rootDir, "test.kt").getNonStrictParentOfType<KtTypeAlias>()!!
assert(handler.canMove(arrayOf<PsiElement>(typeAlias), null))
}
fun testTopLevelClassInScript() = doTest { rootDir, handler ->
val klass = getElementAtCaret(rootDir, "test.kts").getNonStrictParentOfType<KtClass>()!!
assert(handler.canMove(arrayOf<PsiElement>(klass), null))
}
fun testTopLevelFunInScript() = doTest { rootDir, handler ->
val function = getElementAtCaret(rootDir, "test.kts").getNonStrictParentOfType<KtNamedFunction>()!!
assert(handler.canMove(arrayOf<PsiElement>(function), null))
}
fun testTopLevelValInScript() = doTest { rootDir, handler ->
val property = getElementAtCaret(rootDir, "test.kts").getNonStrictParentOfType<KtProperty>()!!
assert(handler.canMove(arrayOf<PsiElement>(property), null))
}
}
@@ -203,4 +203,14 @@ class MoveKotlinDeclarationsHandlerTest : KotlinMultiFileTestCase() {
val typeAlias = getElementAtCaret(rootDir, "test.kt").getNonStrictParentOfType<KtTypeAlias>()!!
assert(handler.canMove(arrayOf<PsiElement>(typeAlias), null))
}
fun testTopLevelFunInScript() = doTest { rootDir, handler ->
val function = getElementAtCaret(rootDir, "test.kts").getNonStrictParentOfType<KtNamedFunction>()!!
assert(handler.canMove(arrayOf<PsiElement>(function), null))
}
fun testTopLevelValInScript() = doTest { rootDir, handler ->
val property = getElementAtCaret(rootDir, "test.kts").getNonStrictParentOfType<KtProperty>()!!
assert(handler.canMove(arrayOf<PsiElement>(property), null))
}
}