KT-1051 Java interoperability completion - fix java subpackages
This commit is contained in:
@@ -15,12 +15,12 @@ public final class QualifiedNamesUtil {
|
|||||||
return subpackageName.equals(packageName) ||
|
return subpackageName.equals(packageName) ||
|
||||||
(subpackageName.startsWith(packageName) && subpackageName.charAt(packageName.length()) == '.');
|
(subpackageName.startsWith(packageName) && subpackageName.charAt(packageName.length()) == '.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isShortNameForFQN(@NotNull final String name, @NotNull final String fqn) {
|
public static boolean isShortNameForFQN(@NotNull final String name, @NotNull final String fqn) {
|
||||||
return fqn.equals(name) ||
|
return fqn.equals(name) ||
|
||||||
(fqn.endsWith(name) && fqn.charAt(fqn.length() - name.length() - 1) == '.');
|
(fqn.endsWith(name) && fqn.charAt(fqn.length() - name.length() - 1) == '.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isOneSegmentFQN(@NotNull final String fqn) {
|
public static boolean isOneSegmentFQN(@NotNull final String fqn) {
|
||||||
if (fqn.isEmpty()) {
|
if (fqn.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
@@ -30,14 +30,20 @@ public final class QualifiedNamesUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static String fqnToShortName(@NotNull String fqName) {
|
public static String fqnToShortName(@NotNull String fqn) {
|
||||||
int lastDotIndex = fqName.lastIndexOf('.');
|
return getLastSegment(fqn);
|
||||||
|
}
|
||||||
|
|
||||||
if (lastDotIndex != -1) {
|
@NotNull
|
||||||
return fqName.substring(lastDotIndex + 1, fqName.length());
|
public static String getLastSegment(@NotNull String fqn) {
|
||||||
}
|
int lastDotIndex = fqn.lastIndexOf('.');
|
||||||
|
return (lastDotIndex != -1) ? fqn.substring(lastDotIndex + 1, fqn.length()) : fqn;
|
||||||
|
}
|
||||||
|
|
||||||
return fqName;
|
@NotNull
|
||||||
|
public static String getFirstSegment(@NotNull String fqn) {
|
||||||
|
int dotIndex = fqn.indexOf('.');
|
||||||
|
return (dotIndex != -1) ? fqn.substring(0, dotIndex) : fqn;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -55,17 +61,43 @@ public final class QualifiedNamesUtil {
|
|||||||
return first + "." + second;
|
return first + "." + second;
|
||||||
}
|
}
|
||||||
|
|
||||||
// private static String subPackageName(String packageName, String packageFQN) {
|
/**
|
||||||
// if (!isInPackage(packageName, packageFQN)) {
|
* Get tail part of the full fqn by subtracting head part.
|
||||||
// return null;
|
*
|
||||||
// }
|
* @param headFQN
|
||||||
//
|
* @param fullFQN
|
||||||
// int nextDotIndex = packageFQN.indexOf('.', (packageName + ".").length());
|
* @return tail fqn. If first part is not a begging of the full fqn, fullFQN will be returned.
|
||||||
//
|
*/
|
||||||
// if (nextDotIndex != -1) {
|
@NotNull
|
||||||
// return packageFQN.substring((packageName + ".").length(), nextDotIndex);
|
public static String tail(@NotNull String headFQN, @NotNull String fullFQN) {
|
||||||
// }
|
if (!isSubpackageOf(fullFQN, headFQN)) {
|
||||||
//
|
return fullFQN;
|
||||||
// return packageFQN.substring((packageName + ".").length());
|
}
|
||||||
// }
|
|
||||||
}
|
return fullFQN.equals(headFQN) ?
|
||||||
|
"" :
|
||||||
|
fullFQN.substring(headFQN.length() + 1); // (headFQN + '.').length
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add one segment of nesting to given qualified name according to the full qualified name.
|
||||||
|
*
|
||||||
|
* @param fqn
|
||||||
|
* @param fullFQN
|
||||||
|
* @return qualified name with one more segment or null if fqn is not head part of fullFQN or there's no additional segment.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public static String plusOneSegment(String fqn, String fullFQN) {
|
||||||
|
if (!isSubpackageOf(fullFQN, fqn)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String nextSegment = getFirstSegment(tail(fqn, fullFQN));
|
||||||
|
|
||||||
|
if (isOneSegmentFQN(nextSegment)) {
|
||||||
|
return combine(fqn, nextSegment);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,6 @@ import com.intellij.psi.PsiClass;
|
|||||||
import com.intellij.psi.PsiElementFinder;
|
import com.intellij.psi.PsiElementFinder;
|
||||||
import com.intellij.psi.PsiManager;
|
import com.intellij.psi.PsiManager;
|
||||||
import com.intellij.psi.PsiPackage;
|
import com.intellij.psi.PsiPackage;
|
||||||
import com.intellij.psi.impl.file.PsiPackageImpl;
|
|
||||||
import com.intellij.psi.search.GlobalSearchScope;
|
import com.intellij.psi.search.GlobalSearchScope;
|
||||||
import com.intellij.util.SmartList;
|
import com.intellij.util.SmartList;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -142,7 +141,7 @@ public class JavaElementFinder extends PsiElementFinder {
|
|||||||
|
|
||||||
for (JetFile psiFile : psiFiles) {
|
for (JetFile psiFile : psiFiles) {
|
||||||
if (JetPsiUtil.getFQName(psiFile).startsWith(qualifiedName)) {
|
if (JetPsiUtil.getFQName(psiFile).startsWith(qualifiedName)) {
|
||||||
return new PsiPackageImpl(psiFile.getManager(), qualifiedName);
|
return new JetLightPackage(psiManager, qualifiedName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,10 +158,9 @@ public class JavaElementFinder extends PsiElementFinder {
|
|||||||
for (JetFile psiFile : psiFiles) {
|
for (JetFile psiFile : psiFiles) {
|
||||||
String jetRootNamespace = JetPsiUtil.getFQName(psiFile);
|
String jetRootNamespace = JetPsiUtil.getFQName(psiFile);
|
||||||
|
|
||||||
if (QualifiedNamesUtil.isSubpackageOf(jetRootNamespace, psiPackage.getQualifiedName())) {
|
final String subPackageFQN = QualifiedNamesUtil.plusOneSegment(psiPackage.getQualifiedName(), jetRootNamespace);
|
||||||
|
if (subPackageFQN != null) {
|
||||||
// TODO: wrong package here
|
answer.add(new JetLightPackage(psiManager, subPackageFQN));
|
||||||
answer.add(new JetLightPackage(psiFile.getManager(), jetRootNamespace));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
|
|||||||
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
|
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
|
||||||
import org.jetbrains.jet.lang.resolve.java.JetJavaMirrorMarker;
|
import org.jetbrains.jet.lang.resolve.java.JetJavaMirrorMarker;
|
||||||
import org.jetbrains.jet.plugin.JetLanguage;
|
import org.jetbrains.jet.plugin.JetLanguage;
|
||||||
|
import org.jetbrains.jet.util.QualifiedNamesUtil;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -55,8 +56,7 @@ public class JetLightClass extends AbstractLightClass implements JetJavaMirrorMa
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
int idx = qualifiedName.lastIndexOf('.');
|
return QualifiedNamesUtil.fqnToShortName(qualifiedName);
|
||||||
return idx > 0 ? qualifiedName.substring(idx + 1) : qualifiedName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -72,6 +72,7 @@ public class JetLightClass extends AbstractLightClass implements JetJavaMirrorMa
|
|||||||
@Override
|
@Override
|
||||||
public PsiClass getDelegate() {
|
public PsiClass getDelegate() {
|
||||||
if (delegate == null) {
|
if (delegate == null) {
|
||||||
|
// TODO: What is the reason for this?
|
||||||
delegate = findClass(qualifiedName, getStub());
|
delegate = findClass(qualifiedName, getStub());
|
||||||
if (delegate == null) {
|
if (delegate == null) {
|
||||||
delegate = findClass(qualifiedName, getStub());
|
delegate = findClass(qualifiedName, getStub());
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package org.jetbrains.jet.asJava;
|
package org.jetbrains.jet.asJava;
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiElement;
|
||||||
import com.intellij.psi.PsiManager;
|
import com.intellij.psi.PsiManager;
|
||||||
import com.intellij.psi.impl.file.PsiPackageImpl;
|
import com.intellij.psi.impl.file.PsiPackageImpl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* TODO: make more accurate wrapper
|
||||||
|
*
|
||||||
* @author Nikolay Krasko
|
* @author Nikolay Krasko
|
||||||
*/
|
*/
|
||||||
public class JetLightPackage extends PsiPackageImpl {
|
public class JetLightPackage extends PsiPackageImpl {
|
||||||
@@ -11,8 +14,14 @@ public class JetLightPackage extends PsiPackageImpl {
|
|||||||
super(manager, qualifiedName);
|
super(manager, qualifiedName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PsiElement copy() {
|
||||||
|
return new JetLightPackage(getManager(), getQualifiedName());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isValid() {
|
public boolean isValid() {
|
||||||
return true;
|
// TODO: invalidate properly
|
||||||
|
return super.isValid();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,4 +4,8 @@ public class Testing {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// EXIST: jet
|
// EXIST: jet1
|
||||||
|
// ABSENT: jet2
|
||||||
|
|
||||||
|
// Only two proposals expected
|
||||||
|
// NUMBER: 1
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package testing.jet
|
package testing.jet1.jet2
|
||||||
|
|
||||||
fun somefun() {
|
fun somefun() {
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user