Move statement out of top-level lambda in scripts (KT-19322)

#KT-19322 Fixed
This commit is contained in:
Nikolay Krasko
2017-08-03 14:51:46 +03:00
parent 656f8bb5cf
commit 6194bc10f8
9 changed files with 68 additions and 8 deletions
@@ -674,7 +674,7 @@ fun main(args: Array<String>) {
testClass<AbstractMoveStatementTest> {
model("codeInsight/moveUpDown/classBodyDeclarations", testMethod = "doTestClassBodyDeclaration")
model("codeInsight/moveUpDown/closingBraces", testMethod = "doTestExpression")
model("codeInsight/moveUpDown/expressions", testMethod = "doTestExpression")
model("codeInsight/moveUpDown/expressions", pattern = KT_OR_KTS, testMethod = "doTestExpression")
model("codeInsight/moveUpDown/parametersAndArguments", testMethod = "doTestExpression")
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.core.script
import com.intellij.openapi.application.Application
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.components.ServiceManager
@@ -24,6 +25,7 @@ import com.intellij.openapi.project.ProjectUtil
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.roots.ex.ProjectRootManagerEx
import com.intellij.openapi.util.EmptyRunnable
import com.intellij.openapi.util.Key
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.openapi.vfs.newvfs.BulkFileListener
@@ -32,13 +34,15 @@ import kotlinx.coroutines.experimental.CoroutineDispatcher
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.asCoroutineDispatcher
import kotlinx.coroutines.experimental.launch
import org.jetbrains.annotations.TestOnly
import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.psi.NotNullableUserDataProperty
import org.jetbrains.kotlin.script.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
import kotlin.script.experimental.dependencies.AsyncDependenciesResolver
import kotlin.script.experimental.dependencies.DependenciesResolver
import kotlin.script.experimental.dependencies.ScriptDependencies
import kotlin.script.experimental.dependencies.AsyncDependenciesResolver
internal class ScriptDependenciesUpdater(
private val project: Project,
@@ -233,11 +237,14 @@ internal class ScriptDependenciesUpdater(
private fun listenToVfsChanges() {
project.messageBus.connect().subscribe(VirtualFileManager.VFS_CHANGES, object : BulkFileListener.Adapter() {
val projectFileIndex = ProjectRootManager.getInstance(project).fileIndex
val application = ApplicationManager.getApplication()
override fun after(events: List<VFileEvent>) {
if (application.isUnitTestMode && application.isScriptDependenciesUpdaterDisabled == true) {
return
}
if (updateCache(events.mapNotNull {
// The check is partly taken from the BuildManager.java
it.file?.takeIf {
@@ -258,7 +265,6 @@ internal class ScriptDependenciesUpdater(
}
}
private data class TimeStamp(private val stamp: Long) {
operator fun compareTo(other: TimeStamp) = this.stamp.compareTo(other.stamp)
}
@@ -267,4 +273,7 @@ private object TimeStamps {
private var current: Long = 0
fun next() = TimeStamp(current++)
}
}
@set: TestOnly
var Application.isScriptDependenciesUpdaterDisabled by NotNullableUserDataProperty(Key.create("SCRIPT_DEPENDENCIES_UPDATER_DISABLED"), false)
@@ -184,7 +184,7 @@ public class KotlinExpressionMover extends AbstractKotlinUpDownMover {
while (current != null) {
PsiElement parent = current.getParent();
if (parent instanceof KtClassBody ||
parent instanceof KtAnonymousInitializer ||
(parent instanceof KtAnonymousInitializer && !(parent instanceof KtScriptInitializer)) ||
parent instanceof KtNamedFunction ||
(parent instanceof KtProperty && !((KtProperty) parent).isLocal())) {
return null;
@@ -0,0 +1,7 @@
// MOVE: down
fun takeLamb(f: () -> Unit) {}
fun foo() {}
takeLamb {
<caret>foo()
}
@@ -0,0 +1,7 @@
// MOVE: down
fun takeLamb(f: () -> Unit) {}
fun foo() {}
takeLamb {
}
<caret>foo()
@@ -0,0 +1,7 @@
// MOVE: up
fun takeLamb(f: () -> Unit) {}
takeLamb {
<caret>foo()
}
fun foo() {}
@@ -0,0 +1,7 @@
// MOVE: up
fun takeLamb(f: () -> Unit) {}
<caret>foo()
takeLamb {
}
fun foo() {}
@@ -21,7 +21,7 @@ import com.intellij.codeInsight.editorActions.moveLeftRight.MoveElementRightActi
import com.intellij.codeInsight.editorActions.moveUpDown.MoveStatementDownAction
import com.intellij.codeInsight.editorActions.moveUpDown.MoveStatementUpAction
import com.intellij.codeInsight.editorActions.moveUpDown.StatementUpDownMover
import com.intellij.openapi.actionSystem.Presentation
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.editor.actionSystem.EditorAction
import com.intellij.openapi.extensions.Extensions
@@ -33,6 +33,7 @@ import junit.framework.TestCase
import org.jetbrains.kotlin.formatter.FormatSettingsUtil
import org.jetbrains.kotlin.idea.codeInsight.upDownMover.KotlinDeclarationMover
import org.jetbrains.kotlin.idea.codeInsight.upDownMover.KotlinExpressionMover
import org.jetbrains.kotlin.idea.core.script.isScriptDependenciesUpdaterDisabled
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.test.KotlinTestUtils
import java.io.File
@@ -67,6 +68,16 @@ abstract class AbstractMoveLeftRightTest : AbstractCodeMoverTest() {
}
abstract class AbstractCodeMoverTest : LightCodeInsightTestCase() {
override fun setUp() {
super.setUp()
ApplicationManager.getApplication().isScriptDependenciesUpdaterDisabled = true
}
override fun tearDown() {
ApplicationManager.getApplication().isScriptDependenciesUpdaterDisabled = false
super.tearDown()
}
protected fun doTest(path: String, isApplicableChecker: (isApplicableExpected: Boolean, direction: String) -> Unit) {
configureByFile(path)
@@ -821,7 +821,7 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
@RunWith(JUnit3RunnerWithInners.class)
public static class Expressions extends AbstractMoveStatementTest {
public void testAllFilesPresentInExpressions() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/expressions"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/moveUpDown/expressions"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("binaryExpr1.kt")
@@ -1106,6 +1106,18 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
doTestExpression(fileName);
}
@TestMetadata("outOfClosureInScriptsDown.kts")
public void testOutOfClosureInScriptsDown() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/moveUpDown/expressions/outOfClosureInScriptsDown.kts");
doTestExpression(fileName);
}
@TestMetadata("outOfClosureInScriptsUp.kts")
public void testOutOfClosureInScriptsUp() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/moveUpDown/expressions/outOfClosureInScriptsUp.kts");
doTestExpression(fileName);
}
@TestMetadata("outOfClosureWithParams1.kt")
public void testOutOfClosureWithParams1() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/moveUpDown/expressions/outOfClosureWithParams1.kt");