diff --git a/annotations/com/intellij/psi/annotations.xml b/annotations/com/intellij/psi/annotations.xml index 1870def42f3..32547c9ac4e 100644 --- a/annotations/com/intellij/psi/annotations.xml +++ b/annotations/com/intellij/psi/annotations.xml @@ -2,6 +2,12 @@ + + + + + + diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeArgumentList.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeArgumentList.java index 50c2a582904..2003d1eb420 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeArgumentList.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeArgumentList.java @@ -37,6 +37,7 @@ public class JetTypeArgumentList extends JetElementImpl { return visitor.visitTypeArgumentList(this, data); } + @NotNull public List getArguments() { return findChildrenByType(JetNodeTypes.TYPE_PROJECTION); } diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinLightMethodForDeclaration.kt b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinLightMethodForDeclaration.kt index 65418154351..288dfa8949e 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinLightMethodForDeclaration.kt +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/KotlinLightMethodForDeclaration.kt @@ -82,7 +82,7 @@ public class KotlinLightMethodForDeclaration(manager: PsiManager, val method: Ps override fun getParameterList(): PsiParameterList = paramsList.getValue()!! - override fun copy(): PsiElement? { + override fun copy(): PsiElement { return KotlinLightMethodForDeclaration(getManager()!!, method, jetDeclaration.copy() as JetDeclaration, getContainingClass()!!) } } diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt index 5e895fe41e2..0250b1457fd 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt @@ -81,6 +81,7 @@ import org.jetbrains.jet.generators.tests.generator.TestClassModel import org.jetbrains.jet.j2k.test.AbstractJavaToKotlinConverterPluginTest import org.jetbrains.jet.j2k.test.AbstractJavaToKotlinConverterBasicTest import org.jetbrains.jet.plugin.conversion.copy.AbstractJavaToKotlinCopyPasteConversionTest +import org.jetbrains.jet.shortenRefs.AbstractShortenRefsTest fun main(args: Array) { System.setProperty("java.awt.headless", "true") @@ -419,6 +420,10 @@ fun main(args: Array) { testClass(javaClass()) { model("copyPaste/conversion", extension = "java") } + + testClass(javaClass()) { + model("shortenRefs") + } } testGroup("j2k/tests/test", "j2k/tests/testData") { diff --git a/idea/src/org/jetbrains/jet/plugin/actions/JetAddFunctionToClassifierAction.java b/idea/src/org/jetbrains/jet/plugin/actions/JetAddFunctionToClassifierAction.java index f9f7ff26f13..853d29129b3 100644 --- a/idea/src/org/jetbrains/jet/plugin/actions/JetAddFunctionToClassifierAction.java +++ b/idea/src/org/jetbrains/jet/plugin/actions/JetAddFunctionToClassifierAction.java @@ -43,11 +43,10 @@ import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.plugin.JetBundle; import org.jetbrains.jet.plugin.codeInsight.CodeInsightUtils; import org.jetbrains.jet.plugin.codeInsight.DescriptorToDeclarationUtil; -import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening; +import org.jetbrains.jet.plugin.codeInsight.ShortenReferences; import javax.swing.*; import java.util.ArrayList; -import java.util.Collections; import java.util.List; /** @@ -116,7 +115,7 @@ public class JetAddFunctionToClassifierAction implements QuestionAction { PsiElement anchor = body.getRBrace(); JetNamedFunction insertedFunctionElement = (JetNamedFunction) body.addBefore(functionElement, anchor); - ReferenceToClassesShortening.compactReferenceToClasses(Collections.singletonList(insertedFunctionElement)); + ShortenReferences.instance$.process(insertedFunctionElement); } }); } diff --git a/idea/src/org/jetbrains/jet/plugin/actions/JetChangeFunctionSignatureAction.java b/idea/src/org/jetbrains/jet/plugin/actions/JetChangeFunctionSignatureAction.java index c4a13f10e54..54dd1bbafb2 100644 --- a/idea/src/org/jetbrains/jet/plugin/actions/JetChangeFunctionSignatureAction.java +++ b/idea/src/org/jetbrains/jet/plugin/actions/JetChangeFunctionSignatureAction.java @@ -33,7 +33,7 @@ import org.jetbrains.jet.lang.psi.JetNamedFunction; import org.jetbrains.jet.lang.psi.JetPsiFactory; import org.jetbrains.jet.plugin.JetBundle; import org.jetbrains.jet.plugin.codeInsight.CodeInsightUtils; -import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening; +import org.jetbrains.jet.plugin.codeInsight.ShortenReferences; import javax.swing.*; import java.util.ArrayList; @@ -150,7 +150,7 @@ public class JetChangeFunctionSignatureAction implements QuestionAction { newElement = JetPsiFactory.createFunction(project, signatureString); } newElement = (JetNamedFunction) element.replace(newElement); - ReferenceToClassesShortening.compactReferenceToClasses(Collections.singletonList(newElement)); + ShortenReferences.instance$.process(newElement); } }); } diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/OverrideImplementMethodsHandler.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/OverrideImplementMethodsHandler.java index e9e16477220..e47d92db512 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/OverrideImplementMethodsHandler.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/OverrideImplementMethodsHandler.java @@ -101,7 +101,7 @@ public abstract class OverrideImplementMethodsHandler implements LanguageCodeIns elementsToCompact.add((JetElement) added); } - ReferenceToClassesShortening.compactReferenceToClasses(elementsToCompact); + ShortenReferences.instance$.process(elementsToCompact); } @Nullable diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/ReferenceToClassesShortening.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/ReferenceToClassesShortening.java deleted file mode 100644 index 3f9cda76105..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/ReferenceToClassesShortening.java +++ /dev/null @@ -1,100 +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.codeInsight; - -import com.intellij.psi.PsiDocumentManager; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.descriptors.ClassDescriptor; -import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor; -import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; -import org.jetbrains.jet.lang.psi.*; -import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.jet.lang.resolve.DescriptorUtils; -import org.jetbrains.jet.lang.resolve.scopes.JetScope; -import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; -import org.jetbrains.jet.plugin.quickfix.ImportInsertHelper; - -import java.util.List; - -public class ReferenceToClassesShortening { - private ReferenceToClassesShortening() { - } - - public static void compactReferenceToClasses(List elementsToCompact) { - if (elementsToCompact.isEmpty()) { - return; - } - PsiDocumentManager.getInstance(elementsToCompact.get(0).getProject()).commitAllDocuments(); - - final JetFile file = (JetFile) elementsToCompact.get(0).getContainingFile(); - final BindingContext bc = AnalyzerFacadeWithCache.analyzeFileWithCache(file).getBindingContext(); - for (JetElement element : elementsToCompact) { - element.accept(new JetVisitorVoid() { - @Override - public void visitJetElement(@NotNull JetElement element) { - element.acceptChildren(this); - } - - @Override - public void visitTypeReference(@NotNull JetTypeReference typeReference) { - super.visitTypeReference(typeReference); - - JetTypeElement typeElement = typeReference.getTypeElement(); - if (typeElement instanceof JetNullableType) { - typeElement = ((JetNullableType) typeElement).getInnerType(); - } - if (typeElement instanceof JetUserType) { - JetUserType userType = (JetUserType) typeElement; - DeclarationDescriptor target = bc.get(BindingContext.REFERENCE_TARGET, - userType.getReferenceExpression()); - if (target instanceof ClassDescriptor) { - ClassDescriptor targetClass = (ClassDescriptor) target; - ClassDescriptor targetTopLevelClass = ImportInsertHelper.getTopLevelClass(targetClass); - - JetScope scope = bc.get(BindingContext.TYPE_RESOLUTION_SCOPE, typeReference); - ClassifierDescriptor classifier = scope.getClassifier(targetTopLevelClass.getName()); - if (targetTopLevelClass == classifier) { - compactReferenceToClass(userType, targetClass); - } - else if (classifier == null) { - ImportInsertHelper.addImportDirectiveIfNeeded(DescriptorUtils.getFqNameSafe(targetTopLevelClass), file); - compactReferenceToClass(userType, targetClass); - } - else { - // leave FQ name - } - } - } - } - - private void compactReferenceToClass(JetUserType userType, ClassDescriptor targetClass) { - String name = targetClass.getName().asString(); - DeclarationDescriptor parent = targetClass.getContainingDeclaration(); - while (parent instanceof ClassDescriptor) { - name = parent.getName() + "." + name; - parent = parent.getContainingDeclaration(); - } - JetTypeArgumentList typeArgumentList = userType.getTypeArgumentList(); - JetTypeElement typeElement = JetPsiFactory.createType(userType.getProject(), - name + (typeArgumentList == null ? "" : typeArgumentList.getText())).getTypeElement(); - assert typeElement != null; - userType.replace(typeElement); - } - }); - } - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/ShortenReferences.kt b/idea/src/org/jetbrains/jet/plugin/codeInsight/ShortenReferences.kt new file mode 100644 index 00000000000..ef364777b62 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/ShortenReferences.kt @@ -0,0 +1,196 @@ +package org.jetbrains.jet.plugin.codeInsight + +import com.intellij.psi.PsiElement; +import org.jetbrains.jet.lang.descriptors.*; +import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.resolve.DescriptorUtils; +import org.jetbrains.jet.lang.resolve.scopes.JetScope; +import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; +import org.jetbrains.jet.plugin.project.CancelableResolveSession; +import org.jetbrains.jet.plugin.quickfix.ImportInsertHelper; +import org.jetbrains.jet.renderer.DescriptorRenderer; + +import java.util.Collections; +import java.util.HashSet; +import com.intellij.psi.util.PsiTreeUtil + +public object ShortenReferences { + public fun process(element: JetElement) { + process(Collections.singleton(element)) + } + + public fun process(elements: Iterable) { + val first = elements.firstOrNull() + if (first == null) return + + val visitor = Visitor(first.getContainingFile() as JetFile) + for (element in elements) { + element.accept(visitor) + } + } + + private class Visitor(val file: JetFile) : JetVisitorVoid() { + private var modificationCount = file.getManager()!!.getModificationTracker().getModificationCount() + private val resolveSession : CancelableResolveSession = AnalyzerFacadeWithCache.getLazyResolveSessionForFile(file) + get(){ + val currentModificationCount = file.getManager()!!.getModificationTracker().getModificationCount() + if (currentModificationCount != modificationCount) { + $resolveSession = AnalyzerFacadeWithCache.getLazyResolveSessionForFile(file) + modificationCount = currentModificationCount + } + return $resolveSession + } + + override fun visitJetElement(element : JetElement) { + element.acceptChildren(this) + } + + override fun visitUserType(userType: JetUserType) { + val resultElement = processType(userType) + resultElement.acceptChildren(this) + } + + private fun processType(userType: JetUserType): PsiElement { + if (userType.getQualifier() == null) return userType + + val bindingContext = resolveSession.resolveToElement(userType) + val target = bindingContext.get(BindingContext.REFERENCE_TARGET, userType.getReferenceExpression()) + if (target == null) return userType + // references to nested classes should be shortened when visiting qualifier + if (target.getContainingDeclaration() is ClassDescriptor) return userType + + val typeReference = PsiTreeUtil.getParentOfType(userType, javaClass())!! + val scope = bindingContext.get(BindingContext.TYPE_RESOLUTION_SCOPE, typeReference)!! + val name = target.getName() + val targetByName = scope.getClassifier(name) ?: scope.getPackage(name) + if (target == targetByName) { + return shortenType(userType) + } + else if (targetByName == null) { + addImportIfNeeded(target) + return shortenType(userType) + } + else { + // leave FQ name + return userType + } + } + + private fun shortenType(userType: JetUserType): JetUserType { + val referenceExpression = userType.getReferenceExpression() + if (referenceExpression == null) return userType + val typeArgumentList = userType.getTypeArgumentList() + val text = referenceExpression.getText() + (if (typeArgumentList != null) typeArgumentList.getText() else "") + val newUserType = JetPsiFactory.createType(userType.getProject(), text).getTypeElement() as JetUserType + return userType.replace(newUserType) as JetUserType + } + + override fun visitDotQualifiedExpression(expression: JetDotQualifiedExpression) { + val resultElement = processDotQualifiedExpression(expression) + resultElement.acceptChildren(this) + } + + private fun processDotQualifiedExpression(qualifiedExpression: JetDotQualifiedExpression): PsiElement { + val selectorExpression = qualifiedExpression.getSelectorExpression() + if (selectorExpression is JetCallExpression) { + val calleeExpression = selectorExpression.getCalleeExpression() + if (calleeExpression is JetReferenceExpression) { + val targetClass = instantiatedClass(calleeExpression) + if (targetClass != null) { + return shortenIfPossible(qualifiedExpression, targetClass) + } + } + } + else if (selectorExpression is JetReferenceExpression) { + val bindingContext = resolveSession.resolveToElement(selectorExpression) + val target = bindingContext.get(BindingContext.REFERENCE_TARGET, selectorExpression) + if (target is ClassDescriptor || target is PackageViewDescriptor) { //TODO: should we ever add imports to real packages? + return shortenIfPossible(qualifiedExpression, target) + } + } + return qualifiedExpression + } + + private fun instantiatedClass(calleeExpression: JetReferenceExpression): ClassDescriptor? { + val bindingContext = resolveSession.resolveToElement(calleeExpression) + val target = bindingContext.get(BindingContext.REFERENCE_TARGET, calleeExpression) + if (target != null) { + if (target is ConstructorDescriptor) { + return target.getContainingDeclaration() + } + } + else { + val targets = bindingContext.get(BindingContext.AMBIGUOUS_REFERENCE_TARGET, calleeExpression) + if (targets != null && !targets.isEmpty()) { + var targetClass: ClassDescriptor? = null + for (descriptor in targets) { + if (descriptor is ConstructorDescriptor) { + val classDescriptor = descriptor.getContainingDeclaration().getOriginal() as ClassDescriptor + if (targetClass == null || targetClass == classDescriptor) { + targetClass = classDescriptor + continue + } + } + return null + } + return targetClass + } + } + return null + } + + private fun shortenIfPossible(qualifiedExpression: JetDotQualifiedExpression, targetClassOrPackage: DeclarationDescriptor): PsiElement { + // references to nested classes should be shortened when visiting qualifier + if (targetClassOrPackage.getContainingDeclaration() is ClassDescriptor) return qualifiedExpression + + var bindingContext = resolveSession.resolveToElement(qualifiedExpression) + + val referenceExpression = referenceExpression(qualifiedExpression.getSelectorExpression()!!) + val resolveBefore = resolveState(referenceExpression, bindingContext) + + val copy = qualifiedExpression.copy() + + val selectorExpression = qualifiedExpression.getSelectorExpression()!! + val newExpression = qualifiedExpression.replace(selectorExpression) as JetExpression + val newReferenceExpression = referenceExpression(newExpression) + + bindingContext = resolveSession.resolveToElement(newReferenceExpression) + val resolveAfter = resolveState(newReferenceExpression, bindingContext) + if (resolveAfter != null) { + if (resolveBefore == resolveAfter) return newExpression + return newExpression.replace(copy) // revert shortening + } + + addImportIfNeeded(targetClassOrPackage) + return newExpression + } + + private fun resolveState(referenceExpression: JetReferenceExpression, bindingContext: BindingContext): Any? { + val target = bindingContext.get(BindingContext.REFERENCE_TARGET, referenceExpression) + if (target != null) return DescriptorRenderer.TEXT.render(target.getOriginal()) + + val targets = bindingContext.get(BindingContext.AMBIGUOUS_REFERENCE_TARGET, referenceExpression) + if (targets != null) return HashSet(targets.map{DescriptorRenderer.TEXT.render(it!!)}) + + return null + } + + //TODO: do we need this "IfNeeded" check? + private fun addImportIfNeeded(descriptor : DeclarationDescriptor) { + ImportInsertHelper.addImportDirectiveIfNeeded(DescriptorUtils.getFqNameSafe(descriptor), file) + } + } + + private fun referenceExpression(selectorExpression: JetExpression) = if (selectorExpression is JetCallExpression) + selectorExpression.getCalleeExpression() as JetReferenceExpression + else + selectorExpression as JetReferenceExpression +} + +//TODO: how about such function in stdlib? +fun Iterable.firstOrNull() : T? { + val iterator = this.iterator() + return if (iterator.hasNext()) iterator.next() else null +} + diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/MoveDeclarationsOutHelper.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/MoveDeclarationsOutHelper.java index dcc65a8c9c6..3631a352300 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/MoveDeclarationsOutHelper.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/surroundWith/MoveDeclarationsOutHelper.java @@ -30,7 +30,7 @@ import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.plugin.codeInsight.CodeInsightUtils; -import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening; +import org.jetbrains.jet.plugin.codeInsight.ShortenReferences; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.renderer.DescriptorRenderer; @@ -86,7 +86,7 @@ public class MoveDeclarationsOutHelper { dummyFirstStatement.delete(); } - ReferenceToClassesShortening.compactReferenceToClasses(propertiesDeclarations); + ShortenReferences.instance$.process(propertiesDeclarations); return PsiUtilCore.toPsiElementArray(resultStatements); } diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/ReconstructTypeInCastOrIsAction.java b/idea/src/org/jetbrains/jet/plugin/intentions/ReconstructTypeInCastOrIsAction.java index 332f2ff5ab6..eb93db5f9cb 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/ReconstructTypeInCastOrIsAction.java +++ b/idea/src/org/jetbrains/jet/plugin/intentions/ReconstructTypeInCastOrIsAction.java @@ -26,7 +26,7 @@ import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.plugin.JetBundle; -import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening; +import org.jetbrains.jet.plugin.codeInsight.ShortenReferences; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.renderer.DescriptorRenderer; @@ -47,7 +47,7 @@ public class ReconstructTypeInCastOrIsAction extends PsiElementBaseIntentionActi JetType type = getReconstructedType(typeRef); JetTypeReference newType = JetPsiFactory.createType(project, DescriptorRenderer.SOURCE_CODE.renderType(type)); JetTypeReference replaced = (JetTypeReference) typeRef.replace(newType); - ReferenceToClassesShortening.compactReferenceToClasses(Collections.singletonList(replaced)); + ShortenReferences.instance$.process(replaced); } @Override diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java b/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java index c936587dc1e..8d2abccf6c6 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java +++ b/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java @@ -45,7 +45,7 @@ import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.TypeUtils; import org.jetbrains.jet.plugin.JetBundle; -import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening; +import org.jetbrains.jet.plugin.codeInsight.ShortenReferences; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.renderer.DescriptorRenderer; @@ -257,7 +257,7 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction { manager.startTemplate(editor, builder.buildInlineTemplate(), new TemplateEditingAdapter() { @Override public void templateFinished(Template template, boolean brokenOff) { - ReferenceToClassesShortening.compactReferenceToClasses(Collections.singletonList(namedDeclaration)); + ShortenReferences.instance$.process(getTypeRef(namedDeclaration)); } }); } diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/declarations/DeclarationUtils.java b/idea/src/org/jetbrains/jet/plugin/intentions/declarations/DeclarationUtils.java index 49c7c9d9bc9..e0e53c40b61 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/declarations/DeclarationUtils.java +++ b/idea/src/org/jetbrains/jet/plugin/intentions/declarations/DeclarationUtils.java @@ -28,7 +28,7 @@ import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lexer.JetTokens; -import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening; +import org.jetbrains.jet.plugin.codeInsight.ShortenReferences; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.plugin.util.JetPsiMatcher; import org.jetbrains.jet.renderer.DescriptorRenderer; @@ -128,7 +128,7 @@ public class DeclarationUtils { ); if (inferredType != null) { - ReferenceToClassesShortening.compactReferenceToClasses(Collections.singletonList(property.getTypeRef())); + ShortenReferences.instance$.process(property.getTypeRef()); } return newInitializer; diff --git a/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java b/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java index f17e9d6eeae..7fd34aac512 100644 --- a/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java +++ b/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java @@ -85,6 +85,7 @@ public final class AnalyzerFacadeWithCache { return cancelableResolveSession.resolveToElement(jetElement); } + @NotNull public static CancelableResolveSession getLazyResolveSessionForFile(@NotNull JetFile file) { Project project = file.getProject(); DeclarationsCacheProvider provider = KotlinCacheManager.getInstance(project).getRegisteredProvider(TargetPlatformDetector.getPlatform(file)); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/CreateFunctionFromUsageFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/CreateFunctionFromUsageFix.java index a639f12e832..82eb30a0437 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/CreateFunctionFromUsageFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/CreateFunctionFromUsageFix.java @@ -64,7 +64,7 @@ import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.plugin.JetBundle; import org.jetbrains.jet.plugin.codeInsight.DescriptorToDeclarationUtil; -import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening; +import org.jetbrains.jet.plugin.codeInsight.ShortenReferences; import org.jetbrains.jet.plugin.presentation.JetClassPresenter; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.plugin.refactoring.JetNameSuggester; @@ -801,7 +801,7 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase { } }); - ReferenceToClassesShortening.compactReferenceToClasses(typeRefsToShorten); + ShortenReferences.instance$.process(typeRefsToShorten); } }); } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java index 4a8dd86f1a6..90769340fa8 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ImportInsertHelper.java @@ -170,15 +170,4 @@ public class ImportInsertHelper { return true; } - - public static ClassDescriptor getTopLevelClass(ClassDescriptor classDescriptor) { - while (true) { - DeclarationDescriptor parent = classDescriptor.getContainingDeclaration(); - if (parent instanceof ClassDescriptor) { - classDescriptor = (ClassDescriptor) parent; - } else { - return classDescriptor; - } - } - } } diff --git a/idea/src/org/jetbrains/jet/plugin/refactoring/inline/KotlinInlineValHandler.java b/idea/src/org/jetbrains/jet/plugin/refactoring/inline/KotlinInlineValHandler.java index dd43888f40a..df63571aac9 100644 --- a/idea/src/org/jetbrains/jet/plugin/refactoring/inline/KotlinInlineValHandler.java +++ b/idea/src/org/jetbrains/jet/plugin/refactoring/inline/KotlinInlineValHandler.java @@ -59,7 +59,7 @@ import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.plugin.JetLanguage; -import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening; +import org.jetbrains.jet.plugin.codeInsight.ShortenReferences; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.plugin.project.CancelableResolveSession; import org.jetbrains.jet.renderer.DescriptorRenderer; @@ -292,7 +292,7 @@ public class KotlinInlineValHandler extends InlineActionHandler { functionLiteral.addAfter(whitespaceToAdd, openBraceElement); } } - ReferenceToClassesShortening.compactReferenceToClasses(functionLiteralExpression.getValueParameters()); + ShortenReferences.instance$.process(functionLiteralExpression.getValueParameters()); } } @@ -332,7 +332,7 @@ public class KotlinInlineValHandler extends InlineActionHandler { for (JetCallExpression call : callsToAddArguments) { call.addAfter(JetPsiFactory.createTypeArguments(containingFile.getProject(), "<" + typeArguments + ">"), call.getCalleeExpression()); - ReferenceToClassesShortening.compactReferenceToClasses(Arrays.asList(call.getTypeArgumentList())); + ShortenReferences.instance$.process(call.getTypeArgumentList()); } } diff --git a/idea/src/org/jetbrains/jet/plugin/refactoring/introduceVariable/JetIntroduceVariableHandler.java b/idea/src/org/jetbrains/jet/plugin/refactoring/introduceVariable/JetIntroduceVariableHandler.java index fb796e30a22..1e0990cbfed 100644 --- a/idea/src/org/jetbrains/jet/plugin/refactoring/introduceVariable/JetIntroduceVariableHandler.java +++ b/idea/src/org/jetbrains/jet/plugin/refactoring/introduceVariable/JetIntroduceVariableHandler.java @@ -49,7 +49,7 @@ import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.plugin.codeInsight.CodeInsightUtils; -import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening; +import org.jetbrains.jet.plugin.codeInsight.ShortenReferences; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.plugin.refactoring.*; import org.jetbrains.jet.renderer.DescriptorRenderer; @@ -388,7 +388,7 @@ public class JetIntroduceVariableHandler extends JetIntroduceHandlerBase { } propertyRef.set(property); if (noTypeInference) { - ReferenceToClassesShortening.compactReferenceToClasses(Collections.singletonList(property)); + ShortenReferences.instance$.process(property); } } }; diff --git a/idea/testData/shortenRefs/JavaStaticMethod.kt b/idea/testData/shortenRefs/JavaStaticMethod.kt new file mode 100644 index 00000000000..941b935d420 --- /dev/null +++ b/idea/testData/shortenRefs/JavaStaticMethod.kt @@ -0,0 +1 @@ +val x = java.util.Collections.copy() diff --git a/idea/testData/shortenRefs/JavaStaticMethod.kt.after b/idea/testData/shortenRefs/JavaStaticMethod.kt.after new file mode 100644 index 00000000000..11f758feef3 --- /dev/null +++ b/idea/testData/shortenRefs/JavaStaticMethod.kt.after @@ -0,0 +1,3 @@ +import java.util.Collections + +val x = Collections.copy() diff --git a/idea/testData/shortenRefs/constructor/Ambiguous.kt b/idea/testData/shortenRefs/constructor/Ambiguous.kt new file mode 100644 index 00000000000..1e1bfd9bc78 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/Ambiguous.kt @@ -0,0 +1,3 @@ +class A { + val x = java.io.File() +} diff --git a/idea/testData/shortenRefs/constructor/Ambiguous.kt.after b/idea/testData/shortenRefs/constructor/Ambiguous.kt.after new file mode 100644 index 00000000000..bdd677e8814 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/Ambiguous.kt.after @@ -0,0 +1,5 @@ +import java.io.File + +class A { + val x = File() +} diff --git a/idea/testData/shortenRefs/constructor/GenericType.kt b/idea/testData/shortenRefs/constructor/GenericType.kt new file mode 100644 index 00000000000..2165ddc6b9b --- /dev/null +++ b/idea/testData/shortenRefs/constructor/GenericType.kt @@ -0,0 +1,3 @@ +class A { + val x = java.util.HashMap, String>() +} diff --git a/idea/testData/shortenRefs/constructor/GenericType.kt.after b/idea/testData/shortenRefs/constructor/GenericType.kt.after new file mode 100644 index 00000000000..045ced61558 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/GenericType.kt.after @@ -0,0 +1,7 @@ +import java.util.HashMap +import java.util.ArrayList +import java.io.File + +class A { + val x = HashMap, String>() +} diff --git a/idea/testData/shortenRefs/constructor/LeaveQualified.kt b/idea/testData/shortenRefs/constructor/LeaveQualified.kt new file mode 100644 index 00000000000..4b4c644b931 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/LeaveQualified.kt @@ -0,0 +1,3 @@ +import java.util.Date + +val x = java.sql.Date(1) diff --git a/idea/testData/shortenRefs/constructor/LeaveQualified.kt.after b/idea/testData/shortenRefs/constructor/LeaveQualified.kt.after new file mode 100644 index 00000000000..1c28b5801d8 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/LeaveQualified.kt.after @@ -0,0 +1,4 @@ +import java.util.Date +import java.sql + +val x = sql.Date(1) diff --git a/idea/testData/shortenRefs/constructor/LeaveQualified1.kt b/idea/testData/shortenRefs/constructor/LeaveQualified1.kt new file mode 100644 index 00000000000..bc59e5a5387 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/LeaveQualified1.kt @@ -0,0 +1,5 @@ +fun File(): String = "" + +class A { + val x = java.io.File() +} diff --git a/idea/testData/shortenRefs/constructor/LeaveQualified1.kt.after b/idea/testData/shortenRefs/constructor/LeaveQualified1.kt.after new file mode 100644 index 00000000000..649e81a40d2 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/LeaveQualified1.kt.after @@ -0,0 +1,5 @@ +fun File(): String = "" + +class A { + val x = java.io.File() +} diff --git a/idea/testData/shortenRefs/constructor/LeaveQualified2.kt b/idea/testData/shortenRefs/constructor/LeaveQualified2.kt new file mode 100644 index 00000000000..811eda4a8b2 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/LeaveQualified2.kt @@ -0,0 +1,5 @@ +fun Random(): Int = 1 + +class A { + val x = java.util.Random() +} diff --git a/idea/testData/shortenRefs/constructor/LeaveQualified2.kt.after b/idea/testData/shortenRefs/constructor/LeaveQualified2.kt.after new file mode 100644 index 00000000000..fb504edad84 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/LeaveQualified2.kt.after @@ -0,0 +1,5 @@ +fun Random(): Int = 1 + +class A { + val x = java.util.Random() +} diff --git a/idea/testData/shortenRefs/constructor/LeaveQualified3.kt b/idea/testData/shortenRefs/constructor/LeaveQualified3.kt new file mode 100644 index 00000000000..e443de1415f --- /dev/null +++ b/idea/testData/shortenRefs/constructor/LeaveQualified3.kt @@ -0,0 +1,4 @@ +fun foo(){ + val Random = {} + val x = java.util.Random() +} diff --git a/idea/testData/shortenRefs/constructor/LeaveQualified3.kt.after b/idea/testData/shortenRefs/constructor/LeaveQualified3.kt.after new file mode 100644 index 00000000000..45ec568b883 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/LeaveQualified3.kt.after @@ -0,0 +1,4 @@ +fun foo(){ + val Random = {} + val x = java.util.Random() +} diff --git a/idea/testData/shortenRefs/constructor/LeaveQualified4.kt.after b/idea/testData/shortenRefs/constructor/LeaveQualified4.kt.after new file mode 100644 index 00000000000..e6232b2307e --- /dev/null +++ b/idea/testData/shortenRefs/constructor/LeaveQualified4.kt.after @@ -0,0 +1,6 @@ +import java.util.Random + +fun foo(){ + val Random = 1 + val x = Random() +} diff --git a/idea/testData/shortenRefs/constructor/LeaveQualified4.kt.todo b/idea/testData/shortenRefs/constructor/LeaveQualified4.kt.todo new file mode 100644 index 00000000000..6b42365c770 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/LeaveQualified4.kt.todo @@ -0,0 +1,4 @@ +fun foo(){ + val Random = 1 + val x = java.util.Random() +} diff --git a/idea/testData/shortenRefs/constructor/LeaveQualified5.kt b/idea/testData/shortenRefs/constructor/LeaveQualified5.kt new file mode 100644 index 00000000000..263a2e70dc5 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/LeaveQualified5.kt @@ -0,0 +1,6 @@ +fun File(p: Int): String = "" +fun File(p: String): String = "" + +class A { + val x = java.io.File() +} diff --git a/idea/testData/shortenRefs/constructor/LeaveQualified5.kt.after b/idea/testData/shortenRefs/constructor/LeaveQualified5.kt.after new file mode 100644 index 00000000000..362cabe8006 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/LeaveQualified5.kt.after @@ -0,0 +1,6 @@ +fun File(p: Int): String = "" +fun File(p: String): String = "" + +class A { + val x = java.io.File() +} diff --git a/idea/testData/shortenRefs/constructor/NestedClass.kt b/idea/testData/shortenRefs/constructor/NestedClass.kt new file mode 100644 index 00000000000..f0dc2a0156e --- /dev/null +++ b/idea/testData/shortenRefs/constructor/NestedClass.kt @@ -0,0 +1 @@ +val x = java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock() diff --git a/idea/testData/shortenRefs/constructor/NestedClass.kt.after b/idea/testData/shortenRefs/constructor/NestedClass.kt.after new file mode 100644 index 00000000000..c8a6408dde1 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/NestedClass.kt.after @@ -0,0 +1,3 @@ +import java.util.concurrent.locks.ReentrantReadWriteLock + +val x = ReentrantReadWriteLock.WriteLock() diff --git a/idea/testData/shortenRefs/constructor/NoImportNeeded.kt b/idea/testData/shortenRefs/constructor/NoImportNeeded.kt new file mode 100644 index 00000000000..498fd67a269 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/NoImportNeeded.kt @@ -0,0 +1,3 @@ +import java.io.File + +val f = java.io.File() diff --git a/idea/testData/shortenRefs/constructor/NoImportNeeded.kt.after b/idea/testData/shortenRefs/constructor/NoImportNeeded.kt.after new file mode 100644 index 00000000000..a383be74da3 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/NoImportNeeded.kt.after @@ -0,0 +1,3 @@ +import java.io.File + +val f = File() diff --git a/idea/testData/shortenRefs/constructor/NoImportNeeded2.kt b/idea/testData/shortenRefs/constructor/NoImportNeeded2.kt new file mode 100644 index 00000000000..3f0f00428cd --- /dev/null +++ b/idea/testData/shortenRefs/constructor/NoImportNeeded2.kt @@ -0,0 +1,3 @@ +package java.io + +val f = java.io.File() \ No newline at end of file diff --git a/idea/testData/shortenRefs/constructor/NoImportNeeded2.kt.after b/idea/testData/shortenRefs/constructor/NoImportNeeded2.kt.after new file mode 100644 index 00000000000..0d1a83da2e8 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/NoImportNeeded2.kt.after @@ -0,0 +1,3 @@ +package java.io + +val f = File() \ No newline at end of file diff --git a/idea/testData/shortenRefs/constructor/NoImportNeeded3.kt b/idea/testData/shortenRefs/constructor/NoImportNeeded3.kt new file mode 100644 index 00000000000..4968634e891 --- /dev/null +++ b/idea/testData/shortenRefs/constructor/NoImportNeeded3.kt @@ -0,0 +1,3 @@ +import java.io.* + +val f = java.io.File() diff --git a/idea/testData/shortenRefs/constructor/NoImportNeeded3.kt.after b/idea/testData/shortenRefs/constructor/NoImportNeeded3.kt.after new file mode 100644 index 00000000000..b717d10514f --- /dev/null +++ b/idea/testData/shortenRefs/constructor/NoImportNeeded3.kt.after @@ -0,0 +1,3 @@ +import java.io.* + +val f = File() diff --git a/idea/testData/shortenRefs/type/FunctionType.kt b/idea/testData/shortenRefs/type/FunctionType.kt new file mode 100644 index 00000000000..1a3d207148a --- /dev/null +++ b/idea/testData/shortenRefs/type/FunctionType.kt @@ -0,0 +1,3 @@ +class A { + var x: ((java.io.File, java.util.ArrayList) -> java.awt.Container)? = null +} diff --git a/idea/testData/shortenRefs/type/FunctionType.kt.after b/idea/testData/shortenRefs/type/FunctionType.kt.after new file mode 100644 index 00000000000..77bf7832303 --- /dev/null +++ b/idea/testData/shortenRefs/type/FunctionType.kt.after @@ -0,0 +1,7 @@ +import java.io.File +import java.util.ArrayList +import java.awt.Container + +class A { + var x: ((File, ArrayList) -> Container)? = null +} diff --git a/idea/testData/shortenRefs/type/GenericType.kt b/idea/testData/shortenRefs/type/GenericType.kt new file mode 100644 index 00000000000..2056c5535a0 --- /dev/null +++ b/idea/testData/shortenRefs/type/GenericType.kt @@ -0,0 +1,3 @@ +class A { + val x: java.util.HashMap, String> +} diff --git a/idea/testData/shortenRefs/type/GenericType.kt.after b/idea/testData/shortenRefs/type/GenericType.kt.after new file mode 100644 index 00000000000..c25ded9e2f1 --- /dev/null +++ b/idea/testData/shortenRefs/type/GenericType.kt.after @@ -0,0 +1,7 @@ +import java.util.HashMap +import java.util.ArrayList +import java.io.File + +class A { + val x: HashMap, String> +} diff --git a/idea/testData/shortenRefs/type/GenericType2.kt b/idea/testData/shortenRefs/type/GenericType2.kt new file mode 100644 index 00000000000..a3c75489787 --- /dev/null +++ b/idea/testData/shortenRefs/type/GenericType2.kt @@ -0,0 +1,6 @@ +import java.util.HashMap +import java.util.List + +fun f(){ + foo(java.util.HashMap>()) +} diff --git a/idea/testData/shortenRefs/type/GenericType2.kt.after b/idea/testData/shortenRefs/type/GenericType2.kt.after new file mode 100644 index 00000000000..e29d47b9499 --- /dev/null +++ b/idea/testData/shortenRefs/type/GenericType2.kt.after @@ -0,0 +1,6 @@ +import java.util.HashMap +import java.util.List + +fun f(){ + foo(HashMap>()) +} diff --git a/idea/testData/shortenRefs/type/LeaveQualified.kt b/idea/testData/shortenRefs/type/LeaveQualified.kt new file mode 100644 index 00000000000..3b653d9a5eb --- /dev/null +++ b/idea/testData/shortenRefs/type/LeaveQualified.kt @@ -0,0 +1,3 @@ +import java.util.Date + +class A : java.sql.Date diff --git a/idea/testData/shortenRefs/type/LeaveQualified.kt.after b/idea/testData/shortenRefs/type/LeaveQualified.kt.after new file mode 100644 index 00000000000..db9aa1c7774 --- /dev/null +++ b/idea/testData/shortenRefs/type/LeaveQualified.kt.after @@ -0,0 +1,4 @@ +import java.util.Date +import java.sql + +class A : sql.Date diff --git a/idea/testData/shortenRefs/type/NestedClass.kt b/idea/testData/shortenRefs/type/NestedClass.kt new file mode 100644 index 00000000000..3f880241e52 --- /dev/null +++ b/idea/testData/shortenRefs/type/NestedClass.kt @@ -0,0 +1 @@ +class A : java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock diff --git a/idea/testData/shortenRefs/type/NestedClass.kt.after b/idea/testData/shortenRefs/type/NestedClass.kt.after new file mode 100644 index 00000000000..f2d92222f13 --- /dev/null +++ b/idea/testData/shortenRefs/type/NestedClass.kt.after @@ -0,0 +1,3 @@ +import java.util.concurrent.locks.ReentrantReadWriteLock + +class A : ReentrantReadWriteLock.WriteLock diff --git a/idea/testData/shortenRefs/type/NoImportNeeded.kt b/idea/testData/shortenRefs/type/NoImportNeeded.kt new file mode 100644 index 00000000000..78711bb0018 --- /dev/null +++ b/idea/testData/shortenRefs/type/NoImportNeeded.kt @@ -0,0 +1,3 @@ +import java.io.File + +class A : java.io.File diff --git a/idea/testData/shortenRefs/type/NoImportNeeded.kt.after b/idea/testData/shortenRefs/type/NoImportNeeded.kt.after new file mode 100644 index 00000000000..aa6b19e8380 --- /dev/null +++ b/idea/testData/shortenRefs/type/NoImportNeeded.kt.after @@ -0,0 +1,3 @@ +import java.io.File + +class A : File diff --git a/idea/testData/shortenRefs/type/NoImportNeeded2.kt b/idea/testData/shortenRefs/type/NoImportNeeded2.kt new file mode 100644 index 00000000000..83ed6b9bdab --- /dev/null +++ b/idea/testData/shortenRefs/type/NoImportNeeded2.kt @@ -0,0 +1,3 @@ +package java.io + +class A : java.io.File \ No newline at end of file diff --git a/idea/testData/shortenRefs/type/NoImportNeeded2.kt.after b/idea/testData/shortenRefs/type/NoImportNeeded2.kt.after new file mode 100644 index 00000000000..dacfc8161c2 --- /dev/null +++ b/idea/testData/shortenRefs/type/NoImportNeeded2.kt.after @@ -0,0 +1,3 @@ +package java.io + +class A : File \ No newline at end of file diff --git a/idea/testData/shortenRefs/type/NullableType.kt b/idea/testData/shortenRefs/type/NullableType.kt new file mode 100644 index 00000000000..49df700535b --- /dev/null +++ b/idea/testData/shortenRefs/type/NullableType.kt @@ -0,0 +1,3 @@ +class A { + val x: java.io.File? +} diff --git a/idea/testData/shortenRefs/type/NullableType.kt.after b/idea/testData/shortenRefs/type/NullableType.kt.after new file mode 100644 index 00000000000..d6a793b3563 --- /dev/null +++ b/idea/testData/shortenRefs/type/NullableType.kt.after @@ -0,0 +1,5 @@ +import java.io.File + +class A { + val x: File? +} diff --git a/idea/testData/shortenRefs/type/SameClassTwice.kt b/idea/testData/shortenRefs/type/SameClassTwice.kt new file mode 100644 index 00000000000..ce9007022c6 --- /dev/null +++ b/idea/testData/shortenRefs/type/SameClassTwice.kt @@ -0,0 +1,3 @@ +class A { + val x: java.util.ArrayList> +} diff --git a/idea/testData/shortenRefs/type/SameClassTwice.kt.after b/idea/testData/shortenRefs/type/SameClassTwice.kt.after new file mode 100644 index 00000000000..cb3c2d496d9 --- /dev/null +++ b/idea/testData/shortenRefs/type/SameClassTwice.kt.after @@ -0,0 +1,6 @@ +import java.util.ArrayList +import java.io.File + +class A { + val x: ArrayList> +} diff --git a/idea/testData/shortenRefs/type/SimpleAddImport.kt b/idea/testData/shortenRefs/type/SimpleAddImport.kt new file mode 100644 index 00000000000..0c69d1ffcb9 --- /dev/null +++ b/idea/testData/shortenRefs/type/SimpleAddImport.kt @@ -0,0 +1 @@ +class A : java.io.File diff --git a/idea/testData/shortenRefs/type/SimpleAddImport.kt.after b/idea/testData/shortenRefs/type/SimpleAddImport.kt.after new file mode 100644 index 00000000000..aa6b19e8380 --- /dev/null +++ b/idea/testData/shortenRefs/type/SimpleAddImport.kt.after @@ -0,0 +1,3 @@ +import java.io.File + +class A : File diff --git a/idea/testData/shortenRefs/type/TwoClassesSameNames.kt b/idea/testData/shortenRefs/type/TwoClassesSameNames.kt new file mode 100644 index 00000000000..c3d0ed3aa7d --- /dev/null +++ b/idea/testData/shortenRefs/type/TwoClassesSameNames.kt @@ -0,0 +1,3 @@ +class A { + val x: java.util.HashMap +} diff --git a/idea/testData/shortenRefs/type/TwoClassesSameNames.kt.after b/idea/testData/shortenRefs/type/TwoClassesSameNames.kt.after new file mode 100644 index 00000000000..887bf4f5d9d --- /dev/null +++ b/idea/testData/shortenRefs/type/TwoClassesSameNames.kt.after @@ -0,0 +1,7 @@ +import java.util.HashMap +import java.util.Date +import java.sql + +class A { + val x: HashMap +} diff --git a/idea/tests/org/jetbrains/jet/shortenRefs/AbstractShortenRefsTest.kt b/idea/tests/org/jetbrains/jet/shortenRefs/AbstractShortenRefsTest.kt new file mode 100644 index 00000000000..28687fbbdfc --- /dev/null +++ b/idea/tests/org/jetbrains/jet/shortenRefs/AbstractShortenRefsTest.kt @@ -0,0 +1,51 @@ +package org.jetbrains.jet.shortenRefs + +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase +import org.jetbrains.jet.InTextDirectivesUtils +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor +import org.jetbrains.jet.lang.psi.JetSimpleNameExpression +import org.jetbrains.jet.lang.psi.JetExpression +import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache +import org.jetbrains.jet.lang.psi.JetFile +import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils +import org.jetbrains.jet.lang.resolve.name.FqName +import org.jetbrains.jet.plugin.project.TargetPlatform +import org.jetbrains.jet.plugin.JetWithJdkAndRuntimeLightProjectDescriptor +import org.jetbrains.jet.lang.resolve.name.Name +import com.intellij.util.containers.Predicate +import org.jetbrains.jet.lang.descriptors.ClassDescriptor +import com.intellij.openapi.application.Application +import com.intellij.openapi.application.ApplicationManager +import org.jetbrains.jet.plugin.PluginTestCaseBase +import java.io.File +import com.intellij.openapi.command.CommandProcessor +import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.jet.lang.psi.JetReferenceExpression +import com.intellij.psi.PsiElement +import org.jetbrains.jet.plugin.codeInsight.ShortenReferences +import org.jetbrains.jet.lang.psi.JetElement +import org.jetbrains.jet.JetTestCaseBuilder + +abstract class AbstractShortenRefsTest : LightCodeInsightFixtureTestCase() { + override fun getTestDataPath() = JetTestCaseBuilder.getHomeDirectory() + override fun getProjectDescriptor() = JetWithJdkAndRuntimeLightProjectDescriptor.INSTANCE + + protected fun doTest(testPath: String) { + val fixture = myFixture!! + fixture.configureByFile(testPath) + + val file = fixture.getFile() as JetFile + val selectionModel = fixture.getEditor()!!.getSelectionModel() + if (!selectionModel.hasSelection()) error("No selection in input file") + val element = PsiTreeUtil.findElementOfClassAtRange(file, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), javaClass())!! + + CommandProcessor.getInstance()!!.executeCommand(getProject(), { + ApplicationManager.getApplication()!!.runWriteAction { + ShortenReferences.process(element) + } + }, null, null) + selectionModel.removeSelection() + + fixture.checkResultByFile(testPath + ".after") + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/shortenRefs/ShortenRefsTestGenerated.java b/idea/tests/org/jetbrains/jet/shortenRefs/ShortenRefsTestGenerated.java new file mode 100644 index 00000000000..abd11013060 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/shortenRefs/ShortenRefsTestGenerated.java @@ -0,0 +1,178 @@ +/* + * 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.shortenRefs; + +import junit.framework.Assert; +import junit.framework.Test; +import junit.framework.TestSuite; + +import java.io.File; +import java.util.regex.Pattern; +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.test.InnerTestClasses; +import org.jetbrains.jet.test.TestMetadata; + +import org.jetbrains.jet.shortenRefs.AbstractShortenRefsTest; + +/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("idea/testData/shortenRefs") +@InnerTestClasses({ShortenRefsTestGenerated.Constructor.class, ShortenRefsTestGenerated.Type.class}) +public class ShortenRefsTestGenerated extends AbstractShortenRefsTest { + public void testAllFilesPresentInShortenRefs() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/shortenRefs"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("JavaStaticMethod.kt") + public void testJavaStaticMethod() throws Exception { + doTest("idea/testData/shortenRefs/JavaStaticMethod.kt"); + } + + @TestMetadata("idea/testData/shortenRefs/constructor") + public static class Constructor extends AbstractShortenRefsTest { + public void testAllFilesPresentInConstructor() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/shortenRefs/constructor"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("Ambiguous.kt") + public void testAmbiguous() throws Exception { + doTest("idea/testData/shortenRefs/constructor/Ambiguous.kt"); + } + + @TestMetadata("GenericType.kt") + public void testGenericType() throws Exception { + doTest("idea/testData/shortenRefs/constructor/GenericType.kt"); + } + + @TestMetadata("LeaveQualified.kt") + public void testLeaveQualified() throws Exception { + doTest("idea/testData/shortenRefs/constructor/LeaveQualified.kt"); + } + + @TestMetadata("LeaveQualified1.kt") + public void testLeaveQualified1() throws Exception { + doTest("idea/testData/shortenRefs/constructor/LeaveQualified1.kt"); + } + + @TestMetadata("LeaveQualified2.kt") + public void testLeaveQualified2() throws Exception { + doTest("idea/testData/shortenRefs/constructor/LeaveQualified2.kt"); + } + + @TestMetadata("LeaveQualified3.kt") + public void testLeaveQualified3() throws Exception { + doTest("idea/testData/shortenRefs/constructor/LeaveQualified3.kt"); + } + + @TestMetadata("LeaveQualified5.kt") + public void testLeaveQualified5() throws Exception { + doTest("idea/testData/shortenRefs/constructor/LeaveQualified5.kt"); + } + + @TestMetadata("NestedClass.kt") + public void testNestedClass() throws Exception { + doTest("idea/testData/shortenRefs/constructor/NestedClass.kt"); + } + + @TestMetadata("NoImportNeeded.kt") + public void testNoImportNeeded() throws Exception { + doTest("idea/testData/shortenRefs/constructor/NoImportNeeded.kt"); + } + + @TestMetadata("NoImportNeeded2.kt") + public void testNoImportNeeded2() throws Exception { + doTest("idea/testData/shortenRefs/constructor/NoImportNeeded2.kt"); + } + + @TestMetadata("NoImportNeeded3.kt") + public void testNoImportNeeded3() throws Exception { + doTest("idea/testData/shortenRefs/constructor/NoImportNeeded3.kt"); + } + + } + + @TestMetadata("idea/testData/shortenRefs/type") + public static class Type extends AbstractShortenRefsTest { + public void testAllFilesPresentInType() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/shortenRefs/type"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("FunctionType.kt") + public void testFunctionType() throws Exception { + doTest("idea/testData/shortenRefs/type/FunctionType.kt"); + } + + @TestMetadata("GenericType.kt") + public void testGenericType() throws Exception { + doTest("idea/testData/shortenRefs/type/GenericType.kt"); + } + + @TestMetadata("GenericType2.kt") + public void testGenericType2() throws Exception { + doTest("idea/testData/shortenRefs/type/GenericType2.kt"); + } + + @TestMetadata("LeaveQualified.kt") + public void testLeaveQualified() throws Exception { + doTest("idea/testData/shortenRefs/type/LeaveQualified.kt"); + } + + @TestMetadata("NestedClass.kt") + public void testNestedClass() throws Exception { + doTest("idea/testData/shortenRefs/type/NestedClass.kt"); + } + + @TestMetadata("NoImportNeeded.kt") + public void testNoImportNeeded() throws Exception { + doTest("idea/testData/shortenRefs/type/NoImportNeeded.kt"); + } + + @TestMetadata("NoImportNeeded2.kt") + public void testNoImportNeeded2() throws Exception { + doTest("idea/testData/shortenRefs/type/NoImportNeeded2.kt"); + } + + @TestMetadata("NullableType.kt") + public void testNullableType() throws Exception { + doTest("idea/testData/shortenRefs/type/NullableType.kt"); + } + + @TestMetadata("SameClassTwice.kt") + public void testSameClassTwice() throws Exception { + doTest("idea/testData/shortenRefs/type/SameClassTwice.kt"); + } + + @TestMetadata("SimpleAddImport.kt") + public void testSimpleAddImport() throws Exception { + doTest("idea/testData/shortenRefs/type/SimpleAddImport.kt"); + } + + @TestMetadata("TwoClassesSameNames.kt") + public void testTwoClassesSameNames() throws Exception { + doTest("idea/testData/shortenRefs/type/TwoClassesSameNames.kt"); + } + + } + + public static Test suite() { + TestSuite suite = new TestSuite("ShortenRefsTestGenerated"); + suite.addTestSuite(ShortenRefsTestGenerated.class); + suite.addTestSuite(Constructor.class); + suite.addTestSuite(Type.class); + return suite; + } +}