KT-1850 Fixed: Redundant imports are not removed when classes are used only by FQ name
OptimizeImportsMultiFileTest added
This commit is contained in:
@@ -138,6 +138,20 @@ public class JetImportOptimizer implements ImportOptimizer {
|
|||||||
element.acceptChildren(this);
|
element.acceptChildren(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitUserType(JetUserType type) {
|
||||||
|
if (type.getQualifier() == null) {
|
||||||
|
super.visitUserType(type);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
JetTypeArgumentList argumentList = type.getTypeArgumentList();
|
||||||
|
if (argumentList != null) {
|
||||||
|
super.visitTypeArgumentList(argumentList);
|
||||||
|
}
|
||||||
|
visitUserType(type.getQualifier());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitReferenceExpression(JetReferenceExpression expression) {
|
public void visitReferenceExpression(JetReferenceExpression expression) {
|
||||||
if (PsiTreeUtil.getParentOfType(expression, JetImportDirective.class) == null &&
|
if (PsiTreeUtil.getParentOfType(expression, JetImportDirective.class) == null &&
|
||||||
@@ -179,6 +193,26 @@ public class JetImportOptimizer implements ImportOptimizer {
|
|||||||
if (element instanceof JetFile) {
|
if (element instanceof JetFile) {
|
||||||
return JetPsiUtil.getFQName((JetFile) element);
|
return JetPsiUtil.getFQName((JetFile) element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (element instanceof JetSimpleNameExpression) {
|
||||||
|
JetNamespaceHeader namespaceHeader = PsiTreeUtil.getParentOfType(element, JetNamespaceHeader.class);
|
||||||
|
if (namespaceHeader != null) {
|
||||||
|
List<JetSimpleNameExpression> simpleNameExpressions = PsiTreeUtil.getChildrenOfTypeAsList(namespaceHeader, JetSimpleNameExpression.class);
|
||||||
|
FqName fqName = null;
|
||||||
|
for (JetSimpleNameExpression nameExpression : simpleNameExpressions) {
|
||||||
|
Name referencedName = nameExpression.getReferencedNameAsName();
|
||||||
|
assert referencedName != null;
|
||||||
|
if (fqName == null) {
|
||||||
|
fqName = new FqName(referencedName.getName());
|
||||||
|
} else {
|
||||||
|
fqName = QualifiedNamesUtil.combine(fqName, referencedName);
|
||||||
|
}
|
||||||
|
if (nameExpression.equals(element)) {
|
||||||
|
return fqName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (element instanceof JetNamedDeclaration) {
|
if (element instanceof JetNamedDeclaration) {
|
||||||
return JetPsiUtil.getFQName((JetNamedDeclaration) element);
|
return JetPsiUtil.getFQName((JetNamedDeclaration) element);
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import java
|
||||||
|
import java.util
|
||||||
|
import java.util.Map
|
||||||
|
import java.util.List
|
||||||
|
import java.util.Map.Entry
|
||||||
|
|
||||||
|
var x : java.util.Map.Entry<java.util.List<String>, String>? = null
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
var x : java.util.Map.Entry<java.util.List<String>, String>? = null
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import java
|
||||||
|
import java.util
|
||||||
|
import java.util.Map
|
||||||
|
import java.util.List
|
||||||
|
import java.util.Map.Entry
|
||||||
|
|
||||||
|
var x : Map.Entry<List<String>, String>? = null
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import java.util.List
|
||||||
|
import java.util.Map
|
||||||
|
|
||||||
|
var x : Map.Entry<List<String>, String>? = null
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import java
|
||||||
|
import java.util
|
||||||
|
import java.util.Map
|
||||||
|
import java.util.Map.Entry
|
||||||
|
|
||||||
|
var x : util.Map.Entry<util.List<String>, String>? = null
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import java.util
|
||||||
|
|
||||||
|
var x : util.Map.Entry<util.List<String>, String>? = null
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import test1.test2
|
||||||
|
|
||||||
|
val s: test2.test3.Class1? = null
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package test1.test2.test3
|
||||||
|
|
||||||
|
public class Class1 {
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import test1
|
||||||
|
import test1.test2
|
||||||
|
import test1.test2.test3
|
||||||
|
import test1.test2.test3.Class1
|
||||||
|
|
||||||
|
val s: test2.test3.Class1? = null
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2012 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.importOptimizer;
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.CodeInsightTestCase;
|
||||||
|
import com.intellij.openapi.command.CommandProcessor;
|
||||||
|
import com.intellij.openapi.command.UndoConfirmationPolicy;
|
||||||
|
import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||||
|
import org.jetbrains.jet.plugin.editor.importOptimizer.JetImportOptimizer;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Natalia Ukhorskaya
|
||||||
|
*/
|
||||||
|
public class OptimizeImportsMultiFileTest extends CodeInsightTestCase {
|
||||||
|
|
||||||
|
public void testKotlinPackage() throws Exception {
|
||||||
|
doTest(getTestName(false) + "/main.kt", getTestName(false) + "/kotlinClass.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void doTest(String... fileNames) throws Exception {
|
||||||
|
configureByFiles(null, fileNames);
|
||||||
|
invokeFormatFile();
|
||||||
|
checkResultByFile(checkFileName(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String checkFileName() {
|
||||||
|
return getTestName(false) + "/" + getTestName(false) + "_after.kt";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getTestDataPath() {
|
||||||
|
return new File(PluginTestCaseBase.getTestDataPathBase(), "/editor/optimizeImports/multifile").getPath() +
|
||||||
|
File.separator;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void invokeFormatFile() {
|
||||||
|
CommandProcessor.getInstance().executeCommand(
|
||||||
|
getProject(), new JetImportOptimizer().processFile(getFile()),
|
||||||
|
"Optimize Imports", null, UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -56,6 +56,18 @@ public class OptimizeImportsTest extends LightCodeInsightTestCase {
|
|||||||
doTest();
|
doTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testKt1850FullQualified() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testKt1850InnerClass() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testPartiallyQualified() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
public void doTest() throws Exception {
|
public void doTest() throws Exception {
|
||||||
configureByFile(fileName());
|
configureByFile(fileName());
|
||||||
invokeFormatFile();
|
invokeFormatFile();
|
||||||
|
|||||||
Reference in New Issue
Block a user