Completion for top-level functions from class and jar files - import quick fix

This commit is contained in:
Nikolay Krasko
2012-03-11 16:53:30 +04:00
parent fba492c492
commit efece992eb
7 changed files with 119 additions and 49 deletions
@@ -155,6 +155,7 @@ public class JetShortNamesCache extends PsiShortNamesCache {
return functionNames;
}
// TODO: Make it work for properties
@NotNull
public Collection<FunctionDescriptor> getTopLevelFunctionDescriptorsByName(
@NotNull String name,
@@ -201,10 +202,6 @@ public class JetShortNamesCache extends PsiShortNamesCache {
return WholeProjectAnalyzerFacade.analyzeProjectWithCache(project, scope);
}
public Collection<JetNamedFunction> getTopLevelFunctionsByName(@NotNull String name, @NotNull GlobalSearchScope scope) {
return JetShortFunctionNameIndex.getInstance().get(name, project, scope);
}
/**
* Get jet extensions top-level function names. Method is allowed to give invalid names - all result should be
* checked with getAllJetExtensionFunctionsByName().
@@ -17,7 +17,6 @@
package org.jetbrains.jet.plugin.quickfix;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
@@ -41,9 +40,9 @@ import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.plugin.JetFileType;
@@ -84,30 +83,24 @@ public class ImportClassAndFunFix extends JetHintAction<JetSimpleNameExpression>
final ArrayList<String> result = new ArrayList<String>();
result.addAll(getClassNames(referenceName, file.getProject()));
result.addAll(getJetTopLevelFunctions(referenceName, file.getProject()));
result.addAll(getJetTopLevelFunctions(referenceName, element, file.getProject()));
result.addAll(getJetExtensionFunctions(referenceName, element, file.getProject()));
return result;
}
private static Collection<String> getJetTopLevelFunctions(@NotNull String referenceName, @NotNull Project project) {
final Collection<JetNamedFunction> namedFunctions =
JetCacheManager.getInstance(project).getNamesCache().getTopLevelFunctionsByName(
referenceName, GlobalSearchScope.allScope(project));
private static Collection<String> getJetTopLevelFunctions(@NotNull String referenceName, JetSimpleNameExpression expression, @NotNull Project project) {
JetShortNamesCache namesCache = JetCacheManager.getInstance(project).getNamesCache();
Collection<FunctionDescriptor> topLevelFunctions = namesCache.getTopLevelFunctionDescriptorsByName(
referenceName,
expression,
GlobalSearchScope.allScope(project));
final Collection<String> nullableNames =
Collections2.transform(Lists.newArrayList(namedFunctions), new Function<JetNamedFunction, String>() {
@Nullable
@Override
public String apply(@Nullable JetNamedFunction jetFunction) {
return jetFunction != null ? jetFunction.getQualifiedName() : null;
}
});
return Collections2.filter(nullableNames, new Predicate<String>() {
return Collections2.transform(topLevelFunctions, new Function<DeclarationDescriptor, String>() {
@Override
public boolean apply(@Nullable String fqn) {
return fqn != null && !fqn.isEmpty();
public String apply(@Nullable DeclarationDescriptor declarationDescriptor) {
assert declarationDescriptor != null;
return DescriptorUtils.getFQName(declarationDescriptor);
}
});
}
@@ -0,0 +1,8 @@
// "Import Class" "true"
package some
import kotlin.util.measureTimeNano
fun testFun() {
measureTimeNano()
}
@@ -0,0 +1,6 @@
// "Import Class" "true"
package some
fun testFun() {
<caret>measureTimeNano()
}
@@ -24,11 +24,9 @@ import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import org.apache.commons.lang.SystemUtils;
import org.jetbrains.jet.plugin.JetWithJdkAndRuntimeLightProjectDescriptor;
import org.jetbrains.jet.plugin.PluginTestCaseBase;
import org.jetbrains.jet.testing.ConfigRuntimeUtil;
/**
* @author Nikolay.Krasko
@@ -54,7 +52,7 @@ public abstract class JetCompletionTestBase extends LightCompletionTestCase {
try {
if (withKotlinRuntime) {
configureWithKotlinRuntime();
ConfigRuntimeUtil.configureKotlinRuntime(getModule(), getFullJavaJDK());
}
Integer completionTime = completionUtils.getExecutionTime(fileText);
@@ -76,7 +74,7 @@ public abstract class JetCompletionTestBase extends LightCompletionTestCase {
}
finally {
if (withKotlinRuntime) {
unConfigureKotlinRuntime();
ConfigRuntimeUtil.unConfigureKotlinRuntime(getModule(), getProjectJDK());
}
}
@@ -90,26 +88,6 @@ public abstract class JetCompletionTestBase extends LightCompletionTestCase {
return (testName.startsWith("Smart")) ? CompletionType.SMART : CompletionType.BASIC;
}
protected static void configureWithKotlinRuntime() {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(getModule());
final ModifiableRootModel rootModel = rootManager.getModifiableModel();
rootModel.setSdk(getFullJavaJDK());
JetWithJdkAndRuntimeLightProjectDescriptor.INSTANCE.configureModule(getModule(), rootModel, null);
rootModel.commit();
}
protected void unConfigureKotlinRuntime() {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(getModule());
final ModifiableRootModel rootModel = rootManager.getModifiableModel();
rootModel.setSdk(getProjectJDK());
JetWithJdkAndRuntimeLightProjectDescriptor.unConfigureModule(rootModel);
rootModel.commit();
}
@Override
protected Sdk getProjectJDK() {
return PluginTestCaseBase.jdkFromIdeaHome();
@@ -18,12 +18,15 @@ package org.jetbrains.jet.plugin.quickfix;
import com.google.common.collect.Lists;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixTestCase;
import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.projectRoots.Sdk;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.lang.SystemUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestCaseBuilder;
import org.jetbrains.jet.plugin.PluginTestCaseBase;
import org.jetbrains.jet.testing.ConfigRuntimeUtil;
import java.io.File;
import java.io.FilenameFilter;
@@ -35,16 +38,19 @@ import java.util.List;
/**
* @author svtk
*/
@SuppressWarnings("JUnitTestCaseWithNoTests")
public class JetQuickFixTest extends LightQuickFixTestCase {
private final String dataPath;
private final String name;
private static FilenameFilter quickFixTestsFilter;
@SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors")
public JetQuickFixTest(String dataPath, String name) {
this.dataPath = dataPath;
this.name = name;
}
@SuppressWarnings("UnusedDeclaration")
private static void setFilter() {
final ArrayList<String> appropriateDirs = Lists.newArrayList("classImport", "expressions");
quickFixTestsFilter = new FilenameFilter() {
@@ -97,7 +103,19 @@ public class JetQuickFixTest extends LightQuickFixTestCase {
@Override
protected void runTest() throws Throwable {
doSingleTest(name.substring("before".length()) + ".kt");
boolean isWithRuntime = name.endsWith("Runtime");
if (isWithRuntime) {
ConfigRuntimeUtil.configureKotlinRuntime(getModule(), getFullJavaJDK());
}
try {
doSingleTest(name.substring("before".length()) + ".kt");
} finally {
if (isWithRuntime) {
ConfigRuntimeUtil.unConfigureKotlinRuntime(getModule(), getProjectJDK());
}
}
}
@Override
@@ -114,4 +132,8 @@ public class JetQuickFixTest extends LightQuickFixTestCase {
protected Sdk getProjectJDK() {
return PluginTestCaseBase.jdkFromIdeaHome();
}
protected static Sdk getFullJavaJDK() {
return JavaSdk.getInstance().createJdk("JDK", SystemUtils.getJavaHome().getAbsolutePath());
}
}
@@ -0,0 +1,66 @@
/*
* Copyright 2000-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.testing;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import org.jetbrains.jet.plugin.JetWithJdkAndRuntimeLightProjectDescriptor;
/**
* Helper for configuring kotlin runtime in tested project.
*
* @author Nikolay Krasko
*/
public class ConfigRuntimeUtil {
private ConfigRuntimeUtil() {
}
public static void configureKotlinRuntime(final Module module, final Sdk sdk) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
final ModifiableRootModel rootModel = rootManager.getModifiableModel();
rootModel.setSdk(sdk);
JetWithJdkAndRuntimeLightProjectDescriptor.INSTANCE.configureModule(module, rootModel, null);
rootModel.commit();
}
});
}
public static void unConfigureKotlinRuntime(final Module module, final Sdk sdk) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
final ModifiableRootModel rootModel = rootManager.getModifiableModel();
rootModel.setSdk(sdk);
JetWithJdkAndRuntimeLightProjectDescriptor.unConfigureModule(rootModel);
rootModel.commit();
}
});
}
}