KT-3620 Don't auto-import js.* and remove in optimize imports
KT-3620 Fixed
This commit is contained in:
@@ -143,6 +143,16 @@ public final class QualifiedNamesUtil {
|
||||
return isImported(alreadyImported, newImport.fqnPart());
|
||||
}
|
||||
|
||||
public static boolean isImported(@NotNull Iterable<ImportPath> imports, @NotNull ImportPath newImport) {
|
||||
for (ImportPath alreadyImported : imports) {
|
||||
if (isImported(alreadyImported, newImport)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isValidJavaFqName(@Nullable String qualifiedName) {
|
||||
if (qualifiedName == null) return false;
|
||||
|
||||
|
||||
@@ -37,9 +37,11 @@ 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.JetPluginUtil;
|
||||
import org.jetbrains.jet.plugin.framework.KotlinFrameworkDetector;
|
||||
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
|
||||
import org.jetbrains.jet.plugin.references.JetPsiReference;
|
||||
import org.jetbrains.jet.util.QualifiedNamesUtil;
|
||||
import org.jetbrains.k2js.analyze.JsConfiguration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -145,7 +147,7 @@ public class ImportInsertHelper {
|
||||
/**
|
||||
* Check that import is useless.
|
||||
*/
|
||||
private static boolean isImportedByDefault(@NotNull ImportPath importPath, @NotNull FqName filePackageFqn) {
|
||||
private static boolean isImportedByDefault(@NotNull ImportPath importPath, @NotNull JetFile jetFile) {
|
||||
if (importPath.fqnPart().isRoot()) {
|
||||
return true;
|
||||
}
|
||||
@@ -157,33 +159,31 @@ public class ImportInsertHelper {
|
||||
}
|
||||
|
||||
// There's no need to import a declaration from the package of current file
|
||||
if (filePackageFqn.equals(importPath.fqnPart().parent())) {
|
||||
if (JetPsiUtil.getFQName(jetFile).equals(importPath.fqnPart().parent())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (isImportedWithKotlinDefault(importPath)) return true;
|
||||
if (isImportedWithJavaDefault(importPath)) return true;
|
||||
|
||||
return false;
|
||||
if (KotlinFrameworkDetector.isJsKotlinModule(jetFile)) {
|
||||
return isImportedWithJsDefault(importPath);
|
||||
}
|
||||
else {
|
||||
return isImportedWithJavaDefault(importPath);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isImportedWithJavaDefault(ImportPath importPath) {
|
||||
for (ImportPath defaultJavaImport : JavaBridgeConfiguration.DEFAULT_JAVA_IMPORTS) {
|
||||
if (QualifiedNamesUtil.isImported(defaultJavaImport, importPath)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return QualifiedNamesUtil.isImported(JavaBridgeConfiguration.DEFAULT_JAVA_IMPORTS, importPath);
|
||||
}
|
||||
|
||||
public static boolean isImportedWithJsDefault(ImportPath importPath) {
|
||||
return QualifiedNamesUtil.isImported(JsConfiguration.DEFAULT_IMPORT_PATHS, importPath);
|
||||
}
|
||||
|
||||
public static boolean isImportedWithKotlinDefault(ImportPath importPath) {
|
||||
for (ImportPath defaultJetImport : DefaultModuleConfiguration.DEFAULT_JET_IMPORTS) {
|
||||
if (QualifiedNamesUtil.isImported(defaultJetImport, importPath)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return QualifiedNamesUtil.isImported(DefaultModuleConfiguration.DEFAULT_JET_IMPORTS, importPath);
|
||||
}
|
||||
|
||||
public static boolean doNeedImport(@NotNull ImportPath importPath, @NotNull JetFile file) {
|
||||
@@ -196,7 +196,7 @@ public class ImportInsertHelper {
|
||||
importPath = new ImportPath(withoutJavaRoot, importPath.isAllUnder(), importPath.getAlias());
|
||||
}
|
||||
|
||||
if (isImportedByDefault(importPath, JetPsiUtil.getFQName(file))) {
|
||||
if (isImportedByDefault(importPath, file)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
fun main(args: Array<String>) {
|
||||
println<caret>
|
||||
}
|
||||
|
||||
// For KT-3620: Don't auto-import js.* and remove in `optimize imports`
|
||||
// JS
|
||||
@@ -0,0 +1,6 @@
|
||||
fun main(args: Array<String>) {
|
||||
println()
|
||||
}
|
||||
|
||||
// For KT-3620: Don't auto-import js.* and remove in `optimize imports`
|
||||
// JS
|
||||
@@ -0,0 +1,8 @@
|
||||
import js.Json
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val a: Json? = null
|
||||
}
|
||||
|
||||
// For KT-3620 Don't auto-import js.* and remove in `optimize imports`
|
||||
// JS
|
||||
@@ -0,0 +1,6 @@
|
||||
fun main(args: Array<String>) {
|
||||
val a: Json? = null
|
||||
}
|
||||
|
||||
// For KT-3620 Don't auto-import js.* and remove in `optimize imports`
|
||||
// JS
|
||||
@@ -22,35 +22,19 @@ import com.intellij.codeInsight.lookup.LookupElementPresentation;
|
||||
import com.intellij.codeInsight.lookup.LookupEvent;
|
||||
import com.intellij.codeInsight.lookup.LookupManager;
|
||||
import com.intellij.codeInsight.lookup.impl.LookupImpl;
|
||||
import com.intellij.ide.startup.impl.StartupManagerImpl;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.startup.StartupManager;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.plugin.JetLightProjectDescriptor;
|
||||
import org.jetbrains.jet.plugin.JetLightCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||
import org.jetbrains.jet.plugin.formatter.JetCodeStyleSettings;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class CompletionHandlerTest extends LightCodeInsightFixtureTestCase {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
((StartupManagerImpl) StartupManager.getInstance(getProject())).runPostStartupActivities();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected LightProjectDescriptor getProjectDescriptor() {
|
||||
return JetLightProjectDescriptor.INSTANCE;
|
||||
}
|
||||
|
||||
public class CompletionHandlerTest extends JetLightCodeInsightFixtureTestCase {
|
||||
public void testClassCompletionImport() {
|
||||
doTest(CompletionType.BASIC, 2, "SortedSet", null, '\n');
|
||||
}
|
||||
@@ -59,6 +43,10 @@ public class CompletionHandlerTest extends LightCodeInsightFixtureTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testDoNotInsertDefaultJsImports() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testDoNotInsertImportIfResolvedIntoJavaConstructor() {
|
||||
doTest();
|
||||
}
|
||||
@@ -88,9 +76,9 @@ public class CompletionHandlerTest extends LightCodeInsightFixtureTestCase {
|
||||
}
|
||||
|
||||
public void testSingleBrackets() {
|
||||
myFixture.configureByFile(getBeforeFileName());
|
||||
myFixture.configureByFile(fileName());
|
||||
myFixture.type('(');
|
||||
myFixture.checkResultByFile(getAfterFileName());
|
||||
myFixture.checkResultByFile(afterFileName());
|
||||
}
|
||||
|
||||
public void testExistingSingleBrackets() {
|
||||
@@ -106,9 +94,9 @@ public class CompletionHandlerTest extends LightCodeInsightFixtureTestCase {
|
||||
}
|
||||
|
||||
public void testInsertFunctionWithBothParentheses() {
|
||||
myFixture.configureByFile(getBeforeFileName());
|
||||
myFixture.configureByFile(fileName());
|
||||
myFixture.type("test()");
|
||||
myFixture.checkResultByFile(getAfterFileName());
|
||||
myFixture.checkResultByFile(afterFileName());
|
||||
}
|
||||
|
||||
public void testInsertImportOnTab() {
|
||||
@@ -144,7 +132,7 @@ public class CompletionHandlerTest extends LightCodeInsightFixtureTestCase {
|
||||
}
|
||||
|
||||
public void doTest(CompletionType type, int time, @Nullable String lookupString, @Nullable String tailText, char completionChar) {
|
||||
myFixture.configureByFile(getBeforeFileName());
|
||||
myFixture.configureByFile(fileName());
|
||||
|
||||
if (lookupString != null || tailText != null) {
|
||||
myFixture.complete(type, time);
|
||||
@@ -158,7 +146,7 @@ public class CompletionHandlerTest extends LightCodeInsightFixtureTestCase {
|
||||
forceCompleteFirst(type, time);
|
||||
}
|
||||
|
||||
myFixture.checkResultByFile(getAfterFileName());
|
||||
myFixture.checkResultByFile(afterFileName());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -203,11 +191,7 @@ public class CompletionHandlerTest extends LightCodeInsightFixtureTestCase {
|
||||
return foundElement;
|
||||
}
|
||||
|
||||
protected String getBeforeFileName() {
|
||||
return getTestName(false) + ".kt";
|
||||
}
|
||||
|
||||
protected String getAfterFileName() {
|
||||
protected String afterFileName() {
|
||||
return getTestName(false) + ".kt.after";
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,9 @@ public abstract class JetLightCodeInsightFixtureTestCase extends LightCodeInsigh
|
||||
if (InTextDirectivesUtils.isDirectiveDefined(fileText, "RUNTIME")) {
|
||||
projectDescriptor = JetWithJdkAndRuntimeLightProjectDescriptor.INSTANCE;
|
||||
}
|
||||
else if (InTextDirectivesUtils.isDirectiveDefined(fileText, "JS")) {
|
||||
projectDescriptor = JetStdJSProjectDescriptor.INSTANCE;
|
||||
}
|
||||
else {
|
||||
projectDescriptor = JetLightProjectDescriptor.INSTANCE;
|
||||
}
|
||||
@@ -56,5 +59,7 @@ public abstract class JetLightCodeInsightFixtureTestCase extends LightCodeInsigh
|
||||
return projectDescriptor;
|
||||
}
|
||||
|
||||
protected abstract String fileName();
|
||||
protected String fileName() {
|
||||
return getTestName(false) + ".kt";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,10 @@ public class OptimizeImportsTest extends JetLightCodeInsightFixtureTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testDefaultJsImports() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testRemoveImportsIfGeneral() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
@@ -85,11 +89,6 @@ public class OptimizeImportsTest extends JetLightCodeInsightFixtureTestCase {
|
||||
File.separator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String fileName() {
|
||||
return getTestName(false) + ".kt";
|
||||
}
|
||||
|
||||
public String checkFileName() {
|
||||
return getTestName(false) + "_after.kt";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user