use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyUserSkeletonsUtil method getUserSkeletonForFile.
@Nullable
private static PyFile getUserSkeletonForFile(@NotNull PyFile file) {
final Boolean hasSkeleton = file.getUserData(HAS_SKELETON);
if (hasSkeleton != null && !hasSkeleton) {
return null;
}
final VirtualFile moduleVirtualFile = file.getVirtualFile();
if (moduleVirtualFile != null) {
String moduleName = QualifiedNameFinder.findShortestImportableName(file, moduleVirtualFile);
if (moduleName != null) {
// TODO: Delete user-skeletons altogether, meanwhile disabled user-skeletons for modules already covered by PyTypeShed
if (PyTypeShed.INSTANCE.getWHITE_LIST().contains(moduleName)) {
return null;
}
final QualifiedName qName = QualifiedName.fromDottedString(moduleName);
for (PyCanonicalPathProvider provider : Extensions.getExtensions(PyCanonicalPathProvider.EP_NAME)) {
final QualifiedName restored = provider.getCanonicalPath(qName, null);
if (restored != null) {
moduleName = restored.toString();
}
}
final PyFile skeletonFile = getUserSkeletonForModuleQName(moduleName, file);
file.putUserData(HAS_SKELETON, skeletonFile != null);
return skeletonFile;
}
}
return null;
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class AddImportHelper method getSortNames.
@NotNull
private static List<String> getSortNames(@NotNull PyImportStatementBase importStatement) {
final List<String> result = new ArrayList<>();
final PyFromImportStatement fromImport = as(importStatement, PyFromImportStatement.class);
if (fromImport != null) {
// because of that relative imports go to the end of an import block
result.add(StringUtil.repeatSymbol('.', fromImport.getRelativeLevel()));
final QualifiedName source = fromImport.getImportSourceQName();
result.add(Objects.toString(source, ""));
if (fromImport.isStarImport()) {
result.add("*");
}
} else {
// fake relative level
result.add("");
}
for (PyImportElement importElement : importStatement.getImportElements()) {
final QualifiedName qualifiedName = importElement.getImportedQName();
result.add(Objects.toString(qualifiedName, ""));
result.add(StringUtil.notNullize(importElement.getAsName()));
}
return result;
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class AddImportHelper method addImportStatement.
/**
* Adds an import statement, if it doesn't exist yet, presumably below all other initial imports in the file.
*
* @param file where to operate
* @param name which to import (qualified is OK)
* @param asName optional name for 'as' clause
* @param anchor place where the imported name was used. It will be used to determine proper block where new import should be inserted,
* e.g. inside conditional block or try/except statement. Also if anchor is another import statement, new import statement
* will be inserted right after it.
* @return whether import statement was actually added
*/
public static boolean addImportStatement(@NotNull PsiFile file, @NotNull String name, @Nullable String asName, @Nullable ImportPriority priority, @Nullable PsiElement anchor) {
if (!(file instanceof PyFile)) {
return false;
}
final List<PyImportElement> existingImports = ((PyFile) file).getImportTargets();
for (PyImportElement element : existingImports) {
final QualifiedName qName = element.getImportedQName();
if (qName != null && name.equals(qName.toString())) {
if ((asName != null && asName.equals(element.getAsName())) || (asName == null && element.getAsName() == null)) {
return false;
}
}
}
final PyElementGenerator generator = PyElementGenerator.getInstance(file.getProject());
final LanguageLevel languageLevel = LanguageLevel.forElement(file);
final PyImportStatement importNodeToInsert = generator.createImportStatement(languageLevel, name, asName);
final PyImportStatementBase importStatement = PsiTreeUtil.getParentOfType(anchor, PyImportStatementBase.class, false);
final PsiElement insertParent = importStatement != null && importStatement.getContainingFile() == file ? importStatement.getParent() : file;
try {
if (anchor instanceof PyImportStatementBase) {
insertParent.addAfter(importNodeToInsert, anchor);
} else {
insertParent.addBefore(importNodeToInsert, getInsertPosition(insertParent, importNodeToInsert, priority));
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return true;
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PythonImportUtils method addSymbolImportCandidates.
private static void addSymbolImportCandidates(PyElement node, String refText, @Nullable String asName, AutoImportQuickFix fix, Set<String> seenCandidateNames, PsiFile existingImportFile) {
Project project = node.getProject();
List<PsiElement> symbols = new ArrayList<>();
symbols.addAll(PyClassNameIndex.find(refText, project, true));
GlobalSearchScope scope = PyProjectScopeBuilder.excludeSdkTestsScope(node);
if (!isQualifier(node)) {
symbols.addAll(PyFunctionNameIndex.find(refText, project, scope));
}
symbols.addAll(PyVariableNameIndex.find(refText, project, scope));
if (isPossibleModuleReference(node)) {
symbols.addAll(findImportableModules(node.getContainingFile(), refText, project, scope));
}
if (!symbols.isEmpty()) {
for (PsiElement symbol : symbols) {
if (isIndexableTopLevel(symbol)) {
// we only want top-level symbols
PsiFileSystemItem srcfile = symbol instanceof PsiFileSystemItem ? ((PsiFileSystemItem) symbol).getParent() : symbol.getContainingFile();
if (srcfile != null && isAcceptableForImport(node, existingImportFile, srcfile)) {
QualifiedName importPath = QualifiedNameFinder.findCanonicalImportPath(symbol, node);
if (importPath == null) {
continue;
}
if (symbol instanceof PsiFileSystemItem) {
importPath = importPath.removeTail(1);
}
final String symbolImportQName = importPath.append(refText).toString();
if (!seenCandidateNames.contains(symbolImportQName)) {
// a new, valid hit
fix.addImport(symbol, srcfile, importPath, asName);
seenCandidateNames.add(symbolImportQName);
}
}
}
}
}
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class CompatibilityVisitor method visitPyImportStatement.
@Override
public void visitPyImportStatement(PyImportStatement node) {
super.visitPyImportStatement(node);
final PyIfStatement ifParent = PsiTreeUtil.getParentOfType(node, PyIfStatement.class);
if (ifParent != null)
return;
for (PyImportElement importElement : node.getImportElements()) {
final QualifiedName qName = importElement.getImportedQName();
if (qName != null) {
if (qName.matches("builtins")) {
registerForAllMatchingVersions(level -> !level.isPy3K(), " not have module builtins", node, new ReplaceBuiltinsQuickFix());
} else if (qName.matches("__builtin__")) {
registerForAllMatchingVersions(LanguageLevel::isPy3K, " not have module __builtin__", node, new ReplaceBuiltinsQuickFix());
}
}
}
}
Aggregations