com.intellij.psi.util.QualifiedName Java Examples

The following examples show how to use com.intellij.psi.util.QualifiedName. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlazePyOutsideModuleImportResolver.java    From intellij with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #2
Source File: CSharpCreateFromTemplateHandler.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: AbstractPyImportResolverStrategy.java    From intellij with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #4
Source File: AbstractPyImportResolverStrategy.java    From intellij with Apache License 2.0 6 votes vote down vote up
@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());
    }
  }
}
 
Example #5
Source File: PythonPsiHelper.java    From idea-php-dotenv-plugin with MIT License 5 votes vote down vote up
/**
 * 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__");
}
 
Example #6
Source File: QualifiedNameTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testQ2() {
  QualifiedName qualifiedName = QualifiedName.fromDottedString("parent1");
  QualifiedName parent = qualifiedName.getParent();

  assertNotNull(parent);
  assertEquals(parent, QualifiedName.fromDottedString(""));
}
 
Example #7
Source File: QualifiedNameTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testQ1() {
  QualifiedName qualifiedName = QualifiedName.fromDottedString("parent1.parent2.parent3");
  QualifiedName parent = qualifiedName.getParent();

  assertNotNull(parent);
  assertEquals(parent, QualifiedName.fromDottedString("parent1.parent2"));
}
 
Example #8
Source File: AbstractPyImportResolverStrategy.java    From intellij with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #9
Source File: BazelPyImportResolverStrategy.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
protected QualifiedName toImportString(ArtifactLocation source) {
  if (source.isGenerated() || !source.getRelativePath().endsWith(".py")) {
    return null;
  }
  return fromRelativePath(source.getRelativePath());
}
 
Example #10
Source File: BazelPyImportResolverStrategy.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public PsiElement resolveToWorkspaceSource(
    QualifiedName name, PyQualifiedNameResolveContext context) {
  String relativePath = name.join(File.separator);
  return BlazePyResolverUtils.resolvePath(context, relativePath);
}
 
Example #11
Source File: BazelPyGenfilesImportResolverStrategy.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
protected QualifiedName toImportString(ArtifactLocation source) {
  if (source.isExternal() || source.isSource() || !source.getRelativePath().endsWith(".py")) {
    return null;
  }
  return fromRelativePath(source.getRelativePath());
}
 
Example #12
Source File: BazelPyGenfilesImportResolverStrategy.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public PsiElement resolveToWorkspaceSource(
    QualifiedName name, PyQualifiedNameResolveContext context) {
  String relativePath = name.join(File.separator);
  return BlazePyResolverUtils.resolveGenfilesPath(context, relativePath);
}
 
Example #13
Source File: BlazePyOutsideModuleImportResolver.java    From intellij with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #14
Source File: PySourcesIndex.java    From intellij with Apache License 2.0 4 votes vote down vote up
PySourcesIndex(
    ImmutableSetMultimap<String, QualifiedName> shortNames,
    ImmutableMap<QualifiedName, PsiElementProvider> sourceMap) {
  this.shortNames = shortNames;
  this.sourceMap = sourceMap;
}
 
Example #15
Source File: PyImportResolverStrategy.java    From intellij with Apache License 2.0 4 votes vote down vote up
/** Find a python source corresponding to the given name, available during the last blaze sync. */
@Nullable
PsiElement resolveFromSyncData(QualifiedName name, PyQualifiedNameResolveContext context);
 
Example #16
Source File: AbstractPyImportResolverStrategy.java    From intellij with Apache License 2.0 4 votes vote down vote up
/** Maps a blaze artifact to the import string used to reference it. */
@Nullable
abstract QualifiedName toImportString(ArtifactLocation source);
 
Example #17
Source File: AbstractPyImportResolverStrategy.java    From intellij with Apache License 2.0 4 votes vote down vote up
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));
}
 
Example #18
Source File: PythonPsiHelper.java    From idea-php-dotenv-plugin with MIT License 4 votes vote down vote up
/**
 * 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"));
}
 
Example #19
Source File: QualifiedNameTest.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void testQ3() {
  QualifiedName qualifiedName = QualifiedName.fromDottedString("");
  QualifiedName parent = qualifiedName.getParent();

  assertNull(parent);
}
 
Example #20
Source File: QualifiedNameTest.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void testQ4() {
  assertEquals(QualifiedName.fromComponents(), QualifiedName.fromDottedString(""));
  assertEquals(QualifiedName.ROOT, QualifiedName.fromDottedString(""));
  assertEquals(QualifiedName.ROOT, QualifiedName.fromComponents());
}
 
Example #21
Source File: PyImportResolverStrategy.java    From intellij with Apache License 2.0 2 votes vote down vote up
/**
 * 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);