Insert import on applying top level function completion
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package org.jetbrains.jet.lang.descriptors;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Nikolay Krasko
|
||||
*/
|
||||
public final class DescriptorsUtils {
|
||||
|
||||
private DescriptorsUtils() {
|
||||
}
|
||||
|
||||
public static String getFQName(@NotNull NamespaceDescriptor namespaceDescriptor) {
|
||||
|
||||
NamespaceDescriptor tempNamespace = namespaceDescriptor;
|
||||
String fqn = tempNamespace.getName();
|
||||
|
||||
while (tempNamespace.getContainingDeclaration() instanceof NamespaceDescriptor) {
|
||||
tempNamespace = (NamespaceDescriptor) tempNamespace.getContainingDeclaration();
|
||||
|
||||
assert tempNamespace != null;
|
||||
fqn = tempNamespace.getName() + "." + fqn;
|
||||
}
|
||||
|
||||
return fqn;
|
||||
}
|
||||
|
||||
public static String getFQName(@NotNull NamedFunctionDescriptor functionDescriptor) {
|
||||
if (functionDescriptor.getContainingDeclaration() instanceof NamespaceDescriptor) {
|
||||
final String namespaceFQN = getFQName((NamespaceDescriptor) functionDescriptor.getContainingDeclaration());
|
||||
return !namespaceFQN.isEmpty() ? namespaceFQN + "." + functionDescriptor.getName() : functionDescriptor.getName();
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Currently supported only for top level functions");
|
||||
}
|
||||
|
||||
public static boolean isTopLevelFunction(@NotNull NamedFunctionDescriptor functionDescriptor) {
|
||||
return functionDescriptor.getContainingDeclaration() instanceof NamespaceDescriptor;
|
||||
}
|
||||
}
|
||||
@@ -107,17 +107,6 @@ public class JetPsiUtil {
|
||||
}
|
||||
}
|
||||
|
||||
// public static String getFQName(JetNamespace jetNamespace) {
|
||||
// JetNamespace parent = PsiTreeUtil.getParentOfType(jetNamespace, JetNamespace.class);
|
||||
// if (parent != null) {
|
||||
// String parentFQName = getFQName(parent);
|
||||
// if (parentFQName.length() > 0) {
|
||||
// return parentFQName + "." + getFQName(jetNamespace.getNamespaceHeader());
|
||||
// }
|
||||
// }
|
||||
// return getFQName(jetNamespace.getNamespaceHeader()); // TODO: Must include module root namespace
|
||||
// }
|
||||
|
||||
private static String getFQName(JetNamespaceHeader header) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (JetSimpleNameExpression nameExpression : header.getParentNamespaceNames()) {
|
||||
|
||||
@@ -88,11 +88,14 @@ public class AnalyzingUtils {
|
||||
@NotNull JetControlFlowDataTraceFactory flowDataTraceFactory,
|
||||
@NotNull BindingTraceContext bindingTraceContext,
|
||||
@NotNull JetSemanticServices semanticServices) {
|
||||
|
||||
JetScope libraryScope = semanticServices.getStandardLibrary().getLibraryScope();
|
||||
ModuleDescriptor owner = new ModuleDescriptor("<module>");
|
||||
|
||||
final WritableScope scope = new WritableScopeImpl(JetScope.EMPTY, owner, new TraceBasedRedeclarationHandler(bindingTraceContext)).setDebugName("Root scope in analyzeNamespace");
|
||||
// configuration.addImports(project, semanticServices, bindingTraceContext, scope);
|
||||
final WritableScope scope = new WritableScopeImpl(
|
||||
JetScope.EMPTY, owner,
|
||||
new TraceBasedRedeclarationHandler(bindingTraceContext)).setDebugName("Root scope in analyzeNamespace");
|
||||
|
||||
scope.importScope(libraryScope);
|
||||
scope.changeLockLevel(WritableScope.LockLevel.BOTH);
|
||||
|
||||
@@ -136,6 +139,7 @@ public class AnalyzingUtils {
|
||||
throw new IllegalStateException("Must be guaranteed not to happen by the parser");
|
||||
}
|
||||
}, files, filesToAnalyzeCompletely, flowDataTraceFactory, configuration);
|
||||
|
||||
return bindingTraceContext.getBindingContext();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.jetbrains.jet.util;
|
||||
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Common methods for working with qualified names strings.
|
||||
*
|
||||
* @author Nikolay Krasko
|
||||
*/
|
||||
public final class QualifiedNamesUtil {
|
||||
|
||||
public static boolean isSubpackageOf(@NotNull final String subpackageName, @NotNull String packageName) {
|
||||
return subpackageName.equals(packageName) ||
|
||||
(subpackageName.startsWith(packageName) && subpackageName.charAt(packageName.length()) == '.');
|
||||
}
|
||||
|
||||
public static boolean isShortNameForFQN(@NotNull final String name, @NotNull final String fqn) {
|
||||
return fqn.equals(name) ||
|
||||
(fqn.endsWith(name) && fqn.charAt(fqn.length() - name.length() - 1) == '.');
|
||||
}
|
||||
|
||||
public static boolean isOneSegmentFQN(@NotNull final String fqn) {
|
||||
if (fqn.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return fqn.indexOf('.') < 0;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String fqnToShortName(@NotNull String fqName) {
|
||||
int lastDotIndex = fqName.lastIndexOf('.');
|
||||
|
||||
if (lastDotIndex != -1) {
|
||||
return fqName.substring(lastDotIndex + 1, fqName.length());
|
||||
}
|
||||
|
||||
return fqName;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String withoutLastSegment(@NotNull String fqn) {
|
||||
final int lastDotIndex = fqn.lastIndexOf('.');
|
||||
if (lastDotIndex > 0) {
|
||||
return fqn.substring(0, lastDotIndex);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String combine(@Nullable String first, @NotNull String second) {
|
||||
if (StringUtil.isEmpty(first)) return second;
|
||||
return first + "." + second;
|
||||
}
|
||||
|
||||
// private static String subPackageName(String packageName, String packageFQN) {
|
||||
// if (!isInPackage(packageName, packageFQN)) {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// int nextDotIndex = packageFQN.indexOf('.', (packageName + ".").length());
|
||||
//
|
||||
// if (nextDotIndex != -1) {
|
||||
// return packageFQN.substring((packageName + ".").length(), nextDotIndex);
|
||||
// }
|
||||
//
|
||||
// return packageFQN.substring((packageName + ".").length());
|
||||
// }
|
||||
}
|
||||
@@ -4,7 +4,6 @@
|
||||
package org.jetbrains.jet.asJava;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.*;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElementFinder;
|
||||
@@ -17,6 +16,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.codegen.JetTypeMapper;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
|
||||
import org.jetbrains.jet.util.QualifiedNamesUtil;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -74,7 +74,7 @@ public class JavaElementFinder extends PsiElementFinder {
|
||||
for (JetFile file : filesInScope) {
|
||||
final String packageName = JetPsiUtil.getFQName(file);
|
||||
if (packageName != null && qualifiedName.startsWith(packageName)) {
|
||||
if (qualifiedName.equals(fqn(packageName, JvmAbi.PACKAGE_CLASS))) {
|
||||
if (qualifiedName.equals(QualifiedNamesUtil.combine(packageName, JvmAbi.PACKAGE_CLASS))) {
|
||||
answer.add(new JetLightClass(psiManager, file, qualifiedName));
|
||||
}
|
||||
else {
|
||||
@@ -91,7 +91,7 @@ public class JavaElementFinder extends PsiElementFinder {
|
||||
if (declaration instanceof JetClassOrObject) {
|
||||
String localName = getLocalName(declaration);
|
||||
if (localName != null) {
|
||||
String fqn = fqn(containerFqn, localName);
|
||||
String fqn = QualifiedNamesUtil.combine(containerFqn, localName);
|
||||
if (qualifiedName.equals(fqn)) {
|
||||
answer.add(new JetLightClass(psiManager, file, qualifiedName));
|
||||
}
|
||||
@@ -136,11 +136,6 @@ public class JavaElementFinder extends PsiElementFinder {
|
||||
return answer;
|
||||
}
|
||||
|
||||
private static String fqn(String packageName, String className) {
|
||||
if (StringUtil.isEmpty(packageName)) return className;
|
||||
return packageName + "." + className;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiPackage findPackage(@NotNull String qualifiedName) {
|
||||
final List<JetFile> psiFiles = collectProjectJetFiles(project, GlobalSearchScope.allScope(project));
|
||||
@@ -163,7 +158,10 @@ public class JavaElementFinder extends PsiElementFinder {
|
||||
|
||||
for (JetFile psiFile : psiFiles) {
|
||||
String jetRootNamespace = JetPsiUtil.getFQName(psiFile);
|
||||
if (isInPackage(psiPackage.getQualifiedName(), jetRootNamespace)) {
|
||||
|
||||
if (QualifiedNamesUtil.isSubpackageOf(jetRootNamespace, psiPackage.getQualifiedName())) {
|
||||
|
||||
// TODO: wrong package here
|
||||
answer.add(new JetLightPackage(psiFile.getManager(), jetRootNamespace));
|
||||
}
|
||||
}
|
||||
@@ -179,10 +177,10 @@ public class JavaElementFinder extends PsiElementFinder {
|
||||
String packageFQN = psiPackage.getQualifiedName();
|
||||
for (JetFile file : filesInScope) {
|
||||
if (packageFQN.equals(JetPsiUtil.getFQName(file))) {
|
||||
answer.add(new JetLightClass(psiManager, file, fqn(packageFQN, JvmAbi.PACKAGE_CLASS)));
|
||||
answer.add(new JetLightClass(psiManager, file, QualifiedNamesUtil.combine(packageFQN, JvmAbi.PACKAGE_CLASS)));
|
||||
for (JetDeclaration declaration : file.getDeclarations()) {
|
||||
if (declaration instanceof JetClassOrObject) {
|
||||
answer.add(new JetLightClass(psiManager, file, fqn(packageFQN, getLocalName(declaration))));
|
||||
answer.add(new JetLightClass(psiManager, file, QualifiedNamesUtil.combine(packageFQN, getLocalName(declaration))));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -191,24 +189,6 @@ public class JavaElementFinder extends PsiElementFinder {
|
||||
return answer.toArray(new PsiClass[answer.size()]);
|
||||
}
|
||||
|
||||
private static boolean isInPackage(String packageName, String fqn) {
|
||||
return fqn.startsWith(packageName + ".");
|
||||
}
|
||||
|
||||
private static String subPackageName(String packageName, String packageFQN) {
|
||||
if (!isInPackage(packageName, packageFQN)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int nextDotIndex = packageFQN.indexOf('.', (packageName + ".").length());
|
||||
|
||||
if (nextDotIndex != -1) {
|
||||
return packageFQN.substring((packageName + ".").length(), nextDotIndex);
|
||||
}
|
||||
|
||||
return packageFQN.substring((packageName + ".").length());
|
||||
}
|
||||
|
||||
private synchronized void invalidateJetFilesCache() {
|
||||
jetFiles.clear();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user