use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PythonStringUtil method intersect.
@Nullable
public static String intersect(String fullName, String elementStringValue) {
QualifiedName fullQName = QualifiedName.fromDottedString(fullName);
QualifiedName stringQName = QualifiedName.fromDottedString(elementStringValue);
String[] s1 = stringQName.getComponents().toArray(new String[stringQName.getComponentCount()]);
String[] s2 = fullQName.getComponents().toArray(new String[fullQName.getComponentCount()]);
for (int i = s1.length - 1; i >= 0; i--) {
boolean flag = true;
if (i > s2.length - 1) {
continue;
}
for (int j = 0; j <= i; j++) {
if (!s1[i - j].equals(s2[s2.length - j - 1])) {
flag = false;
break;
}
}
if (flag) {
StringBuilder res = new StringBuilder("");
for (int j = 0; j <= i; j++) {
if (j > 0) {
res.append(".");
}
res.append(s1[j]);
}
return res.toString();
}
}
return null;
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyMoveFileHandler method updateRelativeImportsInModule.
private static void updateRelativeImportsInModule(@NotNull PyFile module) {
final String originalLocation = module.getUserData(ORIGINAL_FILE_LOCATION);
if (originalLocation == null) {
return;
}
//module.putUserData(ORIGINAL_FILE_LOCATION, null);
for (PyFromImportStatement statement : module.getFromImports()) {
if (!canBeRelative(statement)) {
continue;
}
final int relativeLevel = Math.max(statement.getRelativeLevel(), 1);
final PsiFileSystemItem sourceElement = resolveRelativeImportFromModuleLocation(statement.getManager(), originalLocation, statement.getImportSource(), relativeLevel);
if (sourceElement == null) {
continue;
}
final QualifiedName newName = QualifiedNameFinder.findShortestImportableQName(sourceElement);
replaceRelativeImportSourceWithQualifiedExpression(statement, newName);
}
for (PyImportElement importElement : module.getImportTargets()) {
final PyReferenceExpression referenceExpr = importElement.getImportReferenceExpression();
if (!canBeRelative(importElement) || referenceExpr == null) {
continue;
}
final PsiFileSystemItem resolved = resolveRelativeImportFromModuleLocation(importElement.getManager(), originalLocation, referenceExpr, 1);
if (resolved == null) {
continue;
}
final QualifiedName newName = QualifiedNameFinder.findShortestImportableQName(resolved);
replaceWithQualifiedExpression(referenceExpr, newName);
final QualifiedName oldQualifiedName = referenceExpr.asQualifiedName();
if (!Comparing.equal(oldQualifiedName, newName)) {
final ScopeOwner scopeOwner = ScopeUtil.getScopeOwner(importElement);
if (scopeOwner == null) {
continue;
}
scopeOwner.accept(new PyRecursiveElementVisitor() {
@Override
public void visitPyReferenceExpression(PyReferenceExpression node) {
if (Comparing.equal(node.asQualifiedName(), oldQualifiedName)) {
replaceWithQualifiedExpression(node, newName);
} else {
super.visitPyReferenceExpression(node);
}
}
});
}
}
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyUserSkeletonsUtil method getUserSkeletonForModuleQName.
@Nullable
public static PyFile getUserSkeletonForModuleQName(@NotNull String qName, @NotNull PsiElement foothold) {
final Sdk sdk = PythonSdkType.findPythonSdk(foothold);
if (sdk != null) {
final Project project = foothold.getProject();
final PythonSdkPathCache cache = PythonSdkPathCache.getInstance(project, sdk);
final QualifiedName cacheQName = QualifiedName.fromDottedString(USER_SKELETONS_DIR + "." + qName);
final List<PsiElement> results = cache.get(cacheQName);
if (results != null) {
final PsiElement element = results.isEmpty() ? null : results.get(0);
if (element instanceof PyFile) {
return (PyFile) element;
}
}
final VirtualFile directory = getUserSkeletonsDirectory();
if (directory != null) {
final PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(directory);
PsiElement fileSkeleton = PyResolveImportUtil.resolveModuleAt(QualifiedName.fromDottedString(qName), psiDirectory, PyResolveImportUtil.fromFoothold(foothold)).stream().findFirst().orElse(null);
if (fileSkeleton instanceof PsiDirectory) {
fileSkeleton = PyUtil.getPackageElement((PsiDirectory) fileSkeleton, foothold);
}
if (fileSkeleton instanceof PyFile) {
cache.put(cacheQName, Collections.singletonList(fileSkeleton));
return (PyFile) fileSkeleton;
}
}
cache.put(cacheQName, Collections.<PsiElement>emptyList());
}
return null;
}
Aggregations