From cb4d680790589f27a9cbdc7212fb1ca6f338f010 Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Mon, 18 Mar 2013 21:08:27 +0400 Subject: [PATCH] Removed migrate tuples quick fix. --- .../jetbrains/jet/plugin/JetBundle.properties | 1 - .../quickfix/MigrateSureInProjectFix.java | 30 ++- .../quickfix/MigrateTuplesInProjectFix.java | 248 ------------------ .../jet/plugin/quickfix/QuickFixes.java | 2 - .../migration/afterTuplesWithRuntime.kt | 53 ---- .../migration/beforeTuplesWithRuntime.kt | 53 ---- .../quickfix/QuickFixTestGenerated.java | 5 - 7 files changed, 29 insertions(+), 363 deletions(-) delete mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/MigrateTuplesInProjectFix.java delete mode 100644 idea/testData/quickfix/migration/afterTuplesWithRuntime.kt delete mode 100644 idea/testData/quickfix/migration/beforeTuplesWithRuntime.kt diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index 05f1e2045e1..01ba6441028 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -119,7 +119,6 @@ options.jet.attribute.descriptor.variable.as.function.call=Variable as function options.jet.attribute.descriptor.auto.casted=Smart-cast value options.jet.attribute.descriptor.label=Label change.to.function.invocation=Change to function invocation -migrate.tuple=Migrate tuples in project\: e.g., \#(,) and \#(,,) will be replaced by Pair and Triple migrate.sure=Replace sure() calls by !! in project remove.val.var.from.parameter=Remove val/var from function, loop and catch parameters in project add.when.else.branch.action.family.name=Add Else Branch diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/MigrateSureInProjectFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/MigrateSureInProjectFix.java index 7aa2bb62494..42bc7f5bf8a 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/MigrateSureInProjectFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/MigrateSureInProjectFix.java @@ -16,6 +16,7 @@ package org.jetbrains.jet.plugin.quickfix; +import com.google.common.base.Predicates; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; @@ -24,14 +25,20 @@ import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.analyzer.AnalyzeExhaust; +import org.jetbrains.jet.lang.ModuleConfiguration; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter; import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.resolve.BodiesResolveContext; +import org.jetbrains.jet.lang.resolve.DelegatingBindingTrace; import org.jetbrains.jet.plugin.JetBundle; +import org.jetbrains.jet.plugin.project.AnalyzerFacadeProvider; import org.jetbrains.jet.plugin.project.PluginJetFilesProvider; import java.util.Collection; +import java.util.Collections; public class MigrateSureInProjectFix extends JetIntentionAction { public MigrateSureInProjectFix(@NotNull PsiElement element) { @@ -89,7 +96,7 @@ public class MigrateSureInProjectFix extends JetIntentionAction { JetFile initialFile = (JetFile) file; Collection files = PluginJetFilesProvider.WHOLE_PROJECT_DECLARATION_PROVIDER.fun(initialFile); - AnalyzeExhaust analyzeExhaust = MigrateTuplesInProjectFix.analyzeFiles(initialFile, files); + AnalyzeExhaust analyzeExhaust = analyzeFiles(initialFile, files); for (JetFile jetFile : files) { replaceUnresolvedSure(jetFile, analyzeExhaust.getBindingContext()); @@ -139,4 +146,25 @@ public class MigrateSureInProjectFix extends JetIntentionAction { } }; } + + private static AnalyzeExhaust analyzeFiles(JetFile initialFile, Collection files) { + AnalyzeExhaust analyzeExhaustHeaders = AnalyzerFacadeProvider.getAnalyzerFacadeForFile(initialFile).analyzeFiles( + initialFile.getProject(), + files, + Collections.emptyList(), + Predicates.alwaysFalse()); + + BodiesResolveContext context = analyzeExhaustHeaders.getBodiesResolveContext(); + ModuleConfiguration moduleConfiguration = analyzeExhaustHeaders.getModuleConfiguration(); + assert context != null : "Headers resolver should prepare and stored information for bodies resolve"; + + // Need to resolve bodies in given file and all in the same package + return AnalyzerFacadeProvider.getAnalyzerFacadeForFile(initialFile).analyzeBodiesInFiles( + initialFile.getProject(), + Collections.emptyList(), + Predicates.alwaysTrue(), + new DelegatingBindingTrace(analyzeExhaustHeaders.getBindingContext(), "trace in migrate sure fix"), + context, + moduleConfiguration); + } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/MigrateTuplesInProjectFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/MigrateTuplesInProjectFix.java deleted file mode 100644 index 87a833f92a9..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/MigrateTuplesInProjectFix.java +++ /dev/null @@ -1,248 +0,0 @@ -/* - * Copyright 2010-2013 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.jet.plugin.quickfix; - -import com.google.common.base.Predicates; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.analyzer.AnalyzeExhaust; -import org.jetbrains.jet.lang.ModuleConfiguration; -import org.jetbrains.jet.lang.descriptors.ClassDescriptor; -import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.diagnostics.Diagnostic; -import org.jetbrains.jet.lang.psi.*; -import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter; -import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.jet.lang.resolve.BodiesResolveContext; -import org.jetbrains.jet.lang.resolve.DelegatingBindingTrace; -import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; -import org.jetbrains.jet.plugin.JetBundle; -import org.jetbrains.jet.plugin.project.AnalyzerFacadeProvider; -import org.jetbrains.jet.plugin.project.PluginJetFilesProvider; - -import java.util.Collection; -import java.util.Collections; - -@Deprecated // Tuples are to be removed in Kotlin M4 -public class MigrateTuplesInProjectFix extends JetIntentionAction { - public MigrateTuplesInProjectFix(@NotNull PsiElement element) { - super(element); - } - - @NotNull - @Override - public String getText() { - return JetBundle.message("migrate.tuple"); - } - - @NotNull - @Override - public String getFamilyName() { - return JetBundle.message("migrate.tuple"); - } - - @Override - public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { - return super.isAvailable(project, editor, file) - && (file instanceof JetFile) - && (element instanceof JetTupleExpression || element instanceof JetTupleType); - } - - @Override - public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { - JetFile initialFile = (JetFile) file; - Collection files = PluginJetFilesProvider.WHOLE_PROJECT_DECLARATION_PROVIDER.fun(initialFile); - - AnalyzeExhaust exhaust = analyzeFiles(initialFile, files); - - for (JetFile jetFile : files) { - replaceTupleComponentCalls(jetFile, exhaust.getBindingContext()); - replaceTuple(jetFile); - } - } - - public static AnalyzeExhaust analyzeFiles(JetFile initialFile, Collection files) { - AnalyzeExhaust analyzeExhaustHeaders = AnalyzerFacadeProvider.getAnalyzerFacadeForFile(initialFile).analyzeFiles( - initialFile.getProject(), - files, - Collections.emptyList(), - Predicates.alwaysFalse()); - - BodiesResolveContext context = analyzeExhaustHeaders.getBodiesResolveContext(); - ModuleConfiguration moduleConfiguration = analyzeExhaustHeaders.getModuleConfiguration(); - assert context != null : "Headers resolver should prepare and stored information for bodies resolve"; - - // Need to resolve bodies in given file and all in the same package - return AnalyzerFacadeProvider.getAnalyzerFacadeForFile(initialFile).analyzeBodiesInFiles( - initialFile.getProject(), - Collections.emptyList(), - Predicates.alwaysTrue(), - new DelegatingBindingTrace(analyzeExhaustHeaders.getBindingContext(), "trace in migrate tuples fix"), - context, - moduleConfiguration); - } - - private void replaceTupleComponentCalls(JetFile file, final BindingContext context) { - for (JetDeclaration declaration : file.getDeclarations()) { - declaration.acceptChildren(new JetVisitorVoid() { - - @Override - public void visitSimpleNameExpression(JetSimpleNameExpression expression) { - DeclarationDescriptor referenceTarget = context.get(BindingContext.REFERENCE_TARGET, expression); - if (referenceTarget == null) return; - - DeclarationDescriptor containingDeclaration = referenceTarget.getContainingDeclaration(); - if (containingDeclaration == null) return; - - ImmutableSet supportedTupleClasses = - ImmutableSet.of(KotlinBuiltIns.getInstance().getTuple(2), KotlinBuiltIns.getInstance().getTuple(3)); - //noinspection SuspiciousMethodCalls - if (!supportedTupleClasses.contains(containingDeclaration)) return; - - ImmutableMap supportedComponents = ImmutableMap.builder() - .put("_1", "first") - .put("_2", "second") - .put("_3", "third") - .build(); - String newName = supportedComponents.get(expression.getReferencedName()); - if (newName == null) return; - - expression.replace(JetPsiFactory.createExpression(expression.getProject(), newName)); - } - - @Override - public void visitElement(PsiElement element) { - element.acceptChildren(this); - } - }); - } - } - - private void replaceTuple(JetFile file) { - for (JetDeclaration declaration : file.getDeclarations()) { - declaration.acceptChildren(new JetVisitorVoid() { - - @Override - public void visitTupleExpression(JetTupleExpression expression) { - expression.acceptChildren(this); - - int size = expression.getEntries().size(); - switch (size) { - case 0: - expression.replace(JetPsiFactory.createExpression(expression.getProject(), "Unit.VALUE")); - return; - case 1: - expression.replace(expression.getEntries().get(0)); - return; - - case 2: - replaceWithExpression(expression, "Pair"); - return; - - case 3: - replaceWithExpression(expression, "Triple"); - return; - - default: - // Can't do anything - } - } - - @Override - public void visitTupleType(JetTupleType type) { - type.acceptChildren(this); - - int size = type.getComponentTypeRefs().size(); - switch (size) { - case 0: - type.replace(JetPsiFactory.createType(type.getProject(), "Unit").getTypeElement()); - return; - case 1: - type.replace(type.getComponentTypeRefs().get(0).getTypeElement()); - return; - - case 2: - replaceWithType(type, "Pair"); - return; - - case 3: - replaceWithType(type, "Triple"); - return; - - default: - // Can't do anything - } - } - - @Override - public void visitElement(PsiElement element) { - element.acceptChildren(this); - } - }); - } - } - - private static void replaceWithExpression(JetTupleExpression expression, String name) { - String tupleContents = getTupleContents(expression.getText()); - if (tupleContents == null) return; - - String newText = name + "(" + tupleContents + ")"; - JetExpression newExpression = JetPsiFactory.createExpression(expression.getProject(), newText); - - expression.replace(newExpression); - } - - private static void replaceWithType(JetTupleType type, String name) { - String tupleContents = getTupleContents(type.getText()); - if (tupleContents == null) return; - - String newText = name + "<" + tupleContents + ">"; - JetTypeReference newExpression = JetPsiFactory.createType(type.getProject(), newText); - - type.replace(newExpression.getTypeElement()); - } - - @Nullable - private static String getTupleContents(String text) { - int indexOfHash = text.indexOf("#("); - assert indexOfHash >= 0; - String noOpenPar = text.substring(indexOfHash + 2); - int lastClosingPar = noOpenPar.lastIndexOf(')'); - if (lastClosingPar < 0) { - return null; - } - return noOpenPar.substring(0, lastClosingPar); - } - - public static JetIntentionActionFactory createFactory() { - return new JetIntentionActionFactory() { - @Nullable - @Override - public JetIntentionAction createAction(Diagnostic diagnostic) { - PsiElement element = diagnostic.getPsiElement(); - return new MigrateTuplesInProjectFix(element); - } - }; - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index 21aae5ad114..f361e3d1b95 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -139,8 +139,6 @@ public class QuickFixes { factories.put(CANNOT_CHANGE_ACCESS_PRIVILEGE, ChangeVisibilityModifierFix.createFactory()); factories.put(CANNOT_WEAKEN_ACCESS_PRIVILEGE, ChangeVisibilityModifierFix.createFactory()); - factories.put(TUPLES_ARE_NOT_SUPPORTED, MigrateTuplesInProjectFix.createFactory()); - factories.put(UNRESOLVED_REFERENCE, MigrateSureInProjectFix.createFactory()); factories.put(REDUNDANT_NULLABLE, RemoveNullableFix.createFactory(RemoveNullableFix.NullableKind.REDUNDANT)); diff --git a/idea/testData/quickfix/migration/afterTuplesWithRuntime.kt b/idea/testData/quickfix/migration/afterTuplesWithRuntime.kt deleted file mode 100644 index 5edf336a547..00000000000 --- a/idea/testData/quickfix/migration/afterTuplesWithRuntime.kt +++ /dev/null @@ -1,53 +0,0 @@ -// "Migrate tuples in project: e.g., #(,) and #(,,) will be replaced by Pair and Triple" "true" -// ERROR: Tuples are not supported.
Use data classes instead. For example:
data class FourThings(val a: A, val b: B, val c: C, val d: D) -// ERROR: Tuples are not supported.
Use data classes instead. For example:
data class FourThings(val a: A, val b: B, val c: C, val d: D) - -fun foo2() : Pair { - return Pair(1, 1) -} - -fun test2() { - foo2().first - foo2().second -} - -fun foo3() : Triple> { - return Triple(1, 1, arrayList("")) -} - -fun test3() { - foo3().first - foo3().second - foo3().third -} - -fun foo0(): Unit { - return Unit.VALUE -} - -fun foo4(): #(Int, Int, Int, Int) { - return #(1, 2, 3, 4) -} - -fun test4() { - foo4()._1 - foo4()._2 - foo4()._3 - foo4()._4 -} - -fun fooRec() : Triple, List>> { - Pair(1, Pair(1, "")) - throw Exception() -} - -fun foo1(): Int { - return 1 -} - -class Fake(val _1: Int, val _2: String) - -fun testFake(f: Fake) { - f._1 - f._2 -} \ No newline at end of file diff --git a/idea/testData/quickfix/migration/beforeTuplesWithRuntime.kt b/idea/testData/quickfix/migration/beforeTuplesWithRuntime.kt deleted file mode 100644 index dd82996f198..00000000000 --- a/idea/testData/quickfix/migration/beforeTuplesWithRuntime.kt +++ /dev/null @@ -1,53 +0,0 @@ -// "Migrate tuples in project: e.g., #(,) and #(,,) will be replaced by Pair and Triple" "true" -// ERROR: Tuples are not supported.
Use data classes instead. For example:
data class FourThings(val a: A, val b: B, val c: C, val d: D) -// ERROR: Tuples are not supported.
Use data classes instead. For example:
data class FourThings(val a: A, val b: B, val c: C, val d: D) - -fun foo2() : #(Int, Int) { - return #(1, 1) -} - -fun test2() { - foo2()._1 - foo2()._2 -} - -fun foo3() : #(Int, Int, List) { - return #(1, 1, arrayList("")) -} - -fun test3() { - foo3()._1 - foo3()._2 - foo3()._3 -} - -fun foo0(): #() { - return #() -} - -fun foo4(): #(Int, Int, Int, Int) { - return #(1, 2, 3, 4) -} - -fun test4() { - foo4()._1 - foo4()._2 - foo4()._3 - foo4()._4 -} - -fun fooRec() : #(Int, #(Int, String), List<#(Int, Int)>) { - #(1, #(1, "")) - throw Exception() -} - -fun foo1(): #(Int) { - return #(1) -} - -class Fake(val _1: Int, val _2: String) - -fun testFake(f: Fake) { - f._1 - f._2 -} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 064fa629eb9..ff328651671 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -445,11 +445,6 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/migration/beforeSure.kt"); } - @TestMetadata("beforeTuplesWithRuntime.kt") - public void testTuplesWithRuntime() throws Exception { - doTest("idea/testData/quickfix/migration/beforeTuplesWithRuntime.kt"); - } - @TestMetadata("beforeValVarFromParameters.kt") public void testValVarFromParameters() throws Exception { doTest("idea/testData/quickfix/migration/beforeValVarFromParameters.kt");