下面列出了怎么用com.intellij.psi.util.QualifiedName的API类实例代码及写法,或者点击链接到github查看源代码。
@Nullable
private static PsiElement resolveModuleAt(
@Nullable PsiDirectory directory,
QualifiedName qualifiedName,
PyQualifiedNameResolveContext context) {
if (directory == null || !directory.isValid()) {
return null;
}
PsiElement seeker = directory;
for (String name : qualifiedName.getComponents()) {
if (name == null) {
return null;
}
seeker =
ResolveImportUtil.resolveChild(
seeker, name, context.getFootholdFile(), false, true, false);
}
return seeker;
}
@Override
public final void addImportCandidates(
PsiReference reference, String name, AutoImportQuickFix quickFix) {
Project project = reference.getElement().getProject();
PySourcesIndex index = getSourcesIndex(project);
if (index == null) {
return;
}
PsiManager psiManager = PsiManager.getInstance(project);
for (QualifiedName candidate : index.shortNames.get(name)) {
PsiElementProvider resolver = index.sourceMap.get(candidate);
if (resolver == null) {
continue;
}
PsiElement psi = PyUtil.turnDirIntoInit(resolver.get(psiManager));
if (psi == null) {
continue;
}
PsiFile file = psi.getContainingFile();
if (file != null) {
quickFix.addImport(psi, file, candidate.removeLastComponent());
}
}
}
@SuppressWarnings("unused")
private PySourcesIndex buildSourcesIndex(Project project, BlazeProjectData projectData) {
ImmutableSetMultimap.Builder<String, QualifiedName> shortNames = ImmutableSetMultimap.builder();
Map<QualifiedName, PsiElementProvider> map = new HashMap<>();
ArtifactLocationDecoder decoder = projectData.getArtifactLocationDecoder();
for (TargetIdeInfo target : projectData.getTargetMap().targets()) {
for (ArtifactLocation source : getPySources(target)) {
QualifiedName name = toImportString(source);
if (name == null || name.getLastComponent() == null) {
continue;
}
shortNames.put(name.getLastComponent(), name);
PsiElementProvider psiProvider = psiProviderFromArtifact(project, decoder, source);
map.put(name, psiProvider);
if (includeParentDirectory(source)) {
map.put(name.removeTail(1), PsiElementProvider.getParent(psiProvider));
}
}
}
return new PySourcesIndex(shortNames.build(), ImmutableMap.copyOf(map));
}
@Nullable
private static String formatNamespace(@Nullable String original)
{
if(StringUtil.isEmpty(original))
{
return null;
}
QualifiedName qualifiedName = QualifiedName.fromDottedString(original);
List<String> components = qualifiedName.getComponents();
List<String> newComponents = new ArrayList<>(components.size());
for(String component : components)
{
if(CSharpNameSuggesterUtil.isKeyword(component))
{
newComponents.add("@" + component);
}
else
{
newComponents.add(component);
}
}
return String.join(".", newComponents);
}
/**
* Checks os.environ[""] call
* @param subscription checking element
* @return true if
*/
static boolean checkIndexCall(PySubscriptionExpression subscription) {
QualifiedName qualifiedName = subscription.asQualifiedName();
if(qualifiedName == null) {
return false;
}
String name = qualifiedName.toString();
return name != null && name.equals("os.environ.__getitem__");
}
@Nullable
@Override
public PsiElement resolveImportReference(
QualifiedName name, PyQualifiedNameResolveContext context, boolean withRoots) {
Project project = context.getProject();
if (!Blaze.isBlazeProject(project)) {
return null;
}
if (context.getModule() != null) {
// the file is associated with a module, so this import resolver is not necessary.
return null;
}
if (context.getFoothold() == null) {
// we're not resolving in the context of a specific py file, so this hack is unnecessary.
return null;
}
Sdk projectSdk = ProjectRootManager.getInstance(project).getProjectSdk();
if (projectSdk != null && projectSdk.getSdkType() instanceof PythonSdkType) {
// if this is a python workspace type, imports in external files are already resolved by the
// python plugin.
return null;
}
Sdk pythonSdk = PySdkUtils.getPythonSdk(context.getProject());
if (pythonSdk == null) {
return null;
}
for (VirtualFile root : pythonSdk.getRootProvider().getFiles(OrderRootType.CLASSES)) {
if (!root.isValid() || !root.isDirectory()) {
continue;
}
PsiElement element =
resolveModuleAt(context.getPsiManager().findDirectory(root), name, context);
if (element != null) {
return element;
}
}
return null;
}
@Nullable
@Override
public PsiElement resolveToWorkspaceSource(
QualifiedName name, PyQualifiedNameResolveContext context) {
String relativePath = name.join(File.separator);
return BlazePyResolverUtils.resolveGenfilesPath(context, relativePath);
}
@Nullable
@Override
protected QualifiedName toImportString(ArtifactLocation source) {
if (source.isExternal() || source.isSource() || !source.getRelativePath().endsWith(".py")) {
return null;
}
return fromRelativePath(source.getRelativePath());
}
@Nullable
@Override
public PsiElement resolveToWorkspaceSource(
QualifiedName name, PyQualifiedNameResolveContext context) {
String relativePath = name.join(File.separator);
return BlazePyResolverUtils.resolvePath(context, relativePath);
}
@Nullable
@Override
protected QualifiedName toImportString(ArtifactLocation source) {
if (source.isGenerated() || !source.getRelativePath().endsWith(".py")) {
return null;
}
return fromRelativePath(source.getRelativePath());
}
@Nullable
@Override
public final PsiElement resolveFromSyncData(
QualifiedName name, PyQualifiedNameResolveContext context) {
PySourcesIndex index = getSourcesIndex(context.getProject());
if (index == null) {
return null;
}
PsiElementProvider resolver = index.sourceMap.get(name);
return resolver != null ? resolver.get(context.getPsiManager()) : null;
}
public void testQ1() {
QualifiedName qualifiedName = QualifiedName.fromDottedString("parent1.parent2.parent3");
QualifiedName parent = qualifiedName.getParent();
assertNotNull(parent);
assertEquals(parent, QualifiedName.fromDottedString("parent1.parent2"));
}
public void testQ2() {
QualifiedName qualifiedName = QualifiedName.fromDottedString("parent1");
QualifiedName parent = qualifiedName.getParent();
assertNotNull(parent);
assertEquals(parent, QualifiedName.fromDottedString(""));
}
/**
* Checks os.environ.get("") call
* @param callExpression checking element
* @return true if
*/
static boolean checkGetMethodCall(PyCallExpression callExpression) {
PyExpression callee = callExpression.getCallee();
if(!(callee instanceof PyReferenceExpression)) {
return false;
}
QualifiedName qualifiedName = ((PyReferenceExpression) (callee)).asQualifiedName();
if(qualifiedName == null) {
return false;
}
String name = qualifiedName.toString();
return name != null && (name.equals("os.environ.get") || name.equals("os.getenv"));
}
/** Find a python source corresponding to the given name, available during the last blaze sync. */
@Nullable
PsiElement resolveFromSyncData(QualifiedName name, PyQualifiedNameResolveContext context);
PySourcesIndex(
ImmutableSetMultimap<String, QualifiedName> shortNames,
ImmutableMap<QualifiedName, PsiElementProvider> sourceMap) {
this.shortNames = shortNames;
this.sourceMap = sourceMap;
}
/** Maps a blaze artifact to the import string used to reference it. */
@Nullable
abstract QualifiedName toImportString(ArtifactLocation source);
static QualifiedName fromRelativePath(String relativePath) {
relativePath = StringUtil.trimEnd(relativePath, File.separator + PyNames.INIT_DOT_PY);
relativePath = StringUtil.trimExtensions(relativePath);
return QualifiedName.fromComponents(StringUtil.split(relativePath, File.separator));
}
public void testQ3() {
QualifiedName qualifiedName = QualifiedName.fromDottedString("");
QualifiedName parent = qualifiedName.getParent();
assertNull(parent);
}
public void testQ4() {
assertEquals(QualifiedName.fromComponents(), QualifiedName.fromDottedString(""));
assertEquals(QualifiedName.ROOT, QualifiedName.fromDottedString(""));
assertEquals(QualifiedName.ROOT, QualifiedName.fromComponents());
}
/**
* Find a python source somewhere in the Blaze workspace, corresponding to the given import
* string. Not limited to .blazeproject source roots.
*/
@Nullable
PsiElement resolveToWorkspaceSource(QualifiedName name, PyQualifiedNameResolveContext context);