com.jetbrains.python.psi.PyReferenceExpression Java Examples

The following examples show how to use com.jetbrains.python.psi.PyReferenceExpression. 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: PantsTargetIndex.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
public static List<PsiElement> resolveTargetByName(@Nls String name, @NotNull Project project, GlobalSearchScope scope) {
  final PsiManager psiManager = PsiManager.getInstance(project);
  final ArrayList<PsiElement> result = new ArrayList<>();
  final Collection<VirtualFile> containingFiles = FileBasedIndex.getInstance().getContainingFiles(NAME, name, scope);
  for (VirtualFile virtualFile : containingFiles) {
    final PsiFile psiFile = psiManager.findFile(virtualFile);
    if (psiFile instanceof PyFile) {
      final PyReferenceExpression referenceExpression = PantsPsiUtil.findTargetDefinitions((PyFile)psiFile).get(name);
      final PsiPolyVariantReference reference = referenceExpression != null ? referenceExpression.getReference() : null;
      final PsiElement definition = reference != null ? reference.resolve() : null;
      if (definition != null) {
        result.add(definition);
      } else if (referenceExpression != null) {
        // at least something
        result.add(referenceExpression);
      }
    }
  }
  return result;
}
 
Example #2
Source File: PantsTargetIndex.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
@Override
@NotNull
public Map<String, Void> map(@NotNull final FileContent inputData) {
  final PsiFile psiFile = inputData.getPsiFile();
  if (psiFile instanceof PyFile) {
    final Map<String, PyReferenceExpression> targetDefinitions = PantsPsiUtil.findTargetDefinitions((PyFile)psiFile);
    return ContainerUtil.newMapFromKeys(
      targetDefinitions.keySet().iterator(),
      new Convertor<String, Void>() {
        @Override
        public Void convert(String o) {
          return null;
        }
      }
    );
  }
  return Collections.emptyMap();
}
 
Example #3
Source File: PantsCompletionContributor.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
public PantsCompletionContributor() {
  extend(
    CompletionType.BASIC,
    psiElement().withParent(PyReferenceExpression.class),
    new TargetTypeCompletionProvider()
  );

  extend(
    CompletionType.BASIC,
    psiElement(),
    new AbsoluteAddressesCompletionProvider()
  );
}
 
Example #4
Source File: PantsPsiUtil.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
public static Map<String, PyReferenceExpression> findTargetDefinitions(@NotNull PyFile pyFile) {
  final PyFunction buildFileAliases = pyFile.findTopLevelFunction("build_file_aliases");
  final PyStatement[] statements =
    buildFileAliases != null ? buildFileAliases.getStatementList().getStatements() : PyStatement.EMPTY_ARRAY;
  final Map<String, PyReferenceExpression> result = new HashMap<>();
  for (PyStatement statement : statements) {
    if (!(statement instanceof PyReturnStatement)) {
      continue;
    }
    final PyExpression returnExpression = ((PyReturnStatement)statement).getExpression();
    if (!(returnExpression instanceof PyCallExpression)) {
      continue;
    }
    final PyArgumentList argumentList = ((PyCallExpression)returnExpression).getArgumentList();
    final Collection<PyKeywordArgument> targetDefinitions = PsiTreeUtil.findChildrenOfType(argumentList, PyKeywordArgument.class);
    for (PyKeywordArgument targets : targetDefinitions) {
      final PyExpression targetsExpression = targets != null ? targets.getValueExpression() : null;
      if (targetsExpression instanceof PyDictLiteralExpression) {
        for (PyKeyValueExpression keyValueExpression : ((PyDictLiteralExpression)targetsExpression).getElements()) {
          final PyExpression keyExpression = keyValueExpression.getKey();
          final PyExpression valueExpression = keyValueExpression.getValue();
          if (keyExpression instanceof PyStringLiteralExpression) {
            result.put(
              ((PyStringLiteralExpression)keyExpression).getStringValue(),
              valueExpression instanceof PyReferenceExpression ? (PyReferenceExpression)valueExpression : null
            );
          }
        }
      }
    }
  }
  return result;
}
 
Example #5
Source File: PantsPsiUtilTest.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
public void testAliases() {
  final PsiFile psiFile = myFixture.configureByFile("aliases.py");
  assertTrue(psiFile instanceof PyFile);
  final Map<String, PyReferenceExpression> definitions = PantsPsiUtil.findTargetDefinitions((PyFile)psiFile);
  assertContainsElements(
    definitions.keySet(),
    Arrays.asList("annotation_processor", "jar_library", "scala_library", "scalac_plugin", "rglobs")
  );
}
 
Example #6
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"));
}