use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyFileImpl method getPresentation.
@Override
public ItemPresentation getPresentation() {
return new ItemPresentation() {
@Override
public String getPresentableText() {
return getModuleName(PyFileImpl.this);
}
@Override
public String getLocationString() {
final String name = getLocationName();
return name != null ? "(" + name + ")" : null;
}
@Override
public Icon getIcon(final boolean open) {
if (PyUtil.isPackage(PyFileImpl.this)) {
return AllIcons.Modules.SourceFolder;
}
return PyFileImpl.this.getIcon(0);
}
@NotNull
private String getModuleName(@NotNull PyFile file) {
if (PyUtil.isPackage(file)) {
final PsiDirectory dir = file.getContainingDirectory();
if (dir != null) {
return dir.getName();
}
}
return FileUtil.getNameWithoutExtension(file.getName());
}
@Nullable
private String getLocationName() {
final QualifiedName name = QualifiedNameFinder.findShortestImportableQName(PyFileImpl.this);
if (name != null) {
final QualifiedName prefix = name.removeTail(1);
if (prefix.getComponentCount() > 0) {
return prefix.toString();
}
}
final String relativePath = getRelativeContainerPath();
if (relativePath != null) {
return relativePath;
}
final PsiDirectory psiDirectory = getParent();
if (psiDirectory != null) {
return psiDirectory.getVirtualFile().getPresentableUrl();
}
return null;
}
@Nullable
private String getRelativeContainerPath() {
final PsiDirectory psiDirectory = getParent();
if (psiDirectory != null) {
final VirtualFile virtualFile = getVirtualFile();
if (virtualFile != null) {
final VirtualFile root = ProjectFileIndex.SERVICE.getInstance(getProject()).getContentRootForFile(virtualFile);
if (root != null) {
final VirtualFile parent = virtualFile.getParent();
final VirtualFile rootParent = root.getParent();
if (rootParent != null && parent != null) {
return VfsUtilCore.getRelativePath(parent, rootParent, File.separatorChar);
}
}
}
}
return null;
}
};
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyFromImportStatementImpl method getImportSourceQName.
public QualifiedName getImportSourceQName() {
final PyFromImportStatementStub stub = getStub();
if (stub != null) {
final QualifiedName qName = stub.getImportSourceQName();
if (qName != null && qName.getComponentCount() == 0) {
// relative import only: from .. import the_name
return null;
}
return qName;
}
final PyReferenceExpression importSource = getImportSource();
if (importSource == null) {
return null;
}
return importSource.asQualifiedName();
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyFromImportStatementImpl method resolveImplicitSubModule.
/**
* The statement 'from pkg1.m1 import ...' makes 'm1' available as a local name in the package 'pkg1'.
*
* http://stackoverflow.com/questions/6048786/from-module-import-in-init-py-makes-module-name-visible
*/
@Nullable
private PsiElement resolveImplicitSubModule() {
final QualifiedName importSourceQName = getImportSourceQName();
if (importSourceQName != null) {
final String name = importSourceQName.getLastComponent();
final PsiFile file = getContainingFile();
if (name != null && PyUtil.isPackage(file)) {
final PsiElement resolvedImportSource = PyUtil.turnInitIntoDir(resolveImportSource());
if (resolvedImportSource != null && resolvedImportSource.getParent() == file.getContainingDirectory()) {
return resolvedImportSource;
}
}
}
return null;
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyQualifiedReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
PyExpression qualifier = myElement.getQualifier();
if (qualifier != null) {
qualifier = CompletionUtil.getOriginalOrSelf(qualifier);
}
if (qualifier == null) {
return EMPTY_ARRAY;
}
final PyQualifiedExpression element = CompletionUtil.getOriginalOrSelf(myElement);
PyType qualifierType = TypeEvalContext.codeCompletion(element.getProject(), element.getContainingFile()).getType(qualifier);
ProcessingContext ctx = new ProcessingContext();
final Set<String> namesAlready = new HashSet<>();
ctx.put(PyType.CTX_NAMES, namesAlready);
final Collection<Object> variants = new ArrayList<>();
if (qualifierType != null) {
Collections.addAll(variants, getVariantFromHasAttr(qualifier));
if (qualifierType instanceof PyStructuralType && ((PyStructuralType) qualifierType).isInferredFromUsages()) {
final PyClassType guessedType = guessClassTypeByName();
if (guessedType != null) {
Collections.addAll(variants, getTypeCompletionVariants(myElement, guessedType));
}
}
if (qualifier instanceof PyQualifiedExpression) {
final PyQualifiedExpression qualifierExpression = (PyQualifiedExpression) qualifier;
final QualifiedName qualifiedName = qualifierExpression.asQualifiedName();
if (qualifiedName == null) {
return variants.toArray();
}
final Collection<PyExpression> attrs = collectAssignedAttributes(qualifiedName, qualifier);
for (PyExpression ex : attrs) {
final String name = ex.getName();
if (name != null && name.endsWith(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED)) {
continue;
}
if (ex instanceof PsiNamedElement && qualifierType instanceof PyClassType && name != null) {
variants.add(LookupElementBuilder.createWithSmartPointer(name, ex).withTypeText(qualifierType.getName()).withIcon(PlatformIcons.FIELD_ICON));
}
if (ex instanceof PyReferenceExpression) {
PyReferenceExpression refExpr = (PyReferenceExpression) ex;
namesAlready.add(refExpr.getReferencedName());
} else if (ex instanceof PyTargetExpression) {
PyTargetExpression targetExpr = (PyTargetExpression) ex;
namesAlready.add(targetExpr.getName());
}
}
Collections.addAll(variants, qualifierType.getCompletionVariants(element.getName(), element, ctx));
return variants.toArray();
} else {
return qualifierType.getCompletionVariants(element.getName(), element, ctx);
}
} else {
final PyClassType guessedType = guessClassTypeByName();
if (guessedType != null) {
Collections.addAll(variants, getTypeCompletionVariants(myElement, guessedType));
}
if (qualifier instanceof PyReferenceExpression) {
Collections.addAll(variants, collectSeenMembers(qualifier.getText()));
}
return variants.toArray();
}
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyImportStatementImpl method resolveImplicitSubModule.
/**
* The statement 'import pkg1.m1' makes 'm1' available as a local name in the package 'pkg1'.
*
* http://stackoverflow.com/questions/6048786/from-module-import-in-init-py-makes-module-name-visible
*/
@Nullable
private PsiElement resolveImplicitSubModule() {
final PyImportElement[] elements = getImportElements();
if (elements.length == 1) {
final PyImportElement element = elements[0];
final QualifiedName importedQName = element.getImportedQName();
final PsiFile file = element.getContainingFile();
if (file != null) {
if (importedQName != null && importedQName.getComponentCount() > 1 && PyUtil.isPackage(file)) {
final QualifiedName packageQName = importedQName.removeLastComponent();
final PsiElement resolvedImport = PyUtil.turnDirIntoInit(ResolveImportUtil.resolveImportElement(element, packageQName));
if (resolvedImport == file) {
return element.resolve();
}
}
}
}
return null;
}
Aggregations