Java Code Examples for com.jetbrains.python.psi.PyStringLiteralExpression

The following examples show how to use com.jetbrains.python.psi.PyStringLiteralExpression. These examples are extracted from open source projects. 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
@NotNull
private List<PsiReference> getFileReferences(@NotNull PyStringLiteralExpression expression) {
  final Optional<VirtualFile> buildRoot = PantsUtil.findBuildRoot(expression.getContainingFile());
  if (!buildRoot.isPresent()) {
    return Collections.emptyList();
  }

  final PartialTargetAddress address = PartialTargetAddress.parse(expression.getStringValue());
  final List<PsiReference> result = new ArrayList<>();

  result.addAll(createPathSegments(expression, address.normalizedPath));

  if (address.explicitTarget != null) {
    result.add(createTargetSegmentReference(expression, address));
  }
  return result;
}
 
Example 2
@NotNull
private PantsVirtualFileReference createPathSegmentReference(
  @NotNull PyStringLiteralExpression expression,
  @NotNull String path,
  int prevIndex,
  int endIndex
) {
  final TextRange range = TextRange.create(
    expression.valueOffsetToTextOffset(prevIndex),
    expression.valueOffsetToTextOffset(endIndex)
  );
  return new PantsVirtualFileReference(
    myStringLiteralExpression,
    range,
    path.substring(prevIndex, endIndex),
    path.substring(0, endIndex)
  );
}
 
Example 3
private void visitCall(PyCallExpression expression) {
    if(PythonPsiHelper.checkGetMethodCall(expression)
            && expression.getArgumentList() != null
            && expression.getArgumentList().getArguments().length > 0
            && expression.getArgumentList().getArguments()[0] instanceof PyStringLiteralExpression) {
        PyStringLiteralExpression stringLiteral = (PyStringLiteralExpression) expression.getArgumentList().getArguments()[0];

        collectedItems.add(new KeyUsagePsiElement(stringLiteral.getStringValue(), stringLiteral));
    }
}
 
Example 4
private void visitIndex(PySubscriptionExpression expression) {
    if(PythonPsiHelper.checkIndexCall(expression) &&
            expression.getIndexExpression() instanceof PyStringLiteralExpression) {

        PyStringLiteralExpression stringLiteral = (PyStringLiteralExpression) expression.getIndexExpression();

        collectedItems.add(new KeyUsagePsiElement(stringLiteral.getStringValue(), stringLiteral));
    }
}
 
Example 5
private boolean isDependenciesString(@NotNull CompletionParameters parameters) {
  PsiElement position = parameters.getPosition();
  PsiElement stringLiteral = position.getParent();
  if (!(stringLiteral instanceof PyStringLiteralExpression)) return false;
  if (stringLiteral.getParent() == null) return false;
  PsiElement dependencies = stringLiteral.getParent().getParent();
  return dependencies != null && dependencies.getText().startsWith("dependencies");
}
 
Example 6
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
  final PyStringLiteralExpression stringLiteral = PsiTreeUtil.getParentOfType(element, PyStringLiteralExpression.class, false);
  final PsiElement parent = stringLiteral != null ? stringLiteral.getParent() : null;
  if (!(parent instanceof PyListLiteralExpression)) {
    return PsiReference.EMPTY_ARRAY;
  }
  final PsiElement parentParent = parent.getParent();
  if (parentParent instanceof PyKeywordArgument &&
      "dependencies".equalsIgnoreCase(((PyKeywordArgument)parentParent).getKeyword())) {
    return getReferences(stringLiteral);
  }
  return PsiReference.EMPTY_ARRAY;
}
 
Example 7
@NotNull
private List<PsiReference> createPathSegments(@NotNull PyStringLiteralExpression expression, @NotNull String path) {
  final List<PsiReference> result = new ArrayList<>();
  int prevIndex = 0;
  for (int i = 0; i < path.length(); ++i) {
    if (path.charAt(i) != '/') continue;

    result.add(
      createPathSegmentReference(expression, path, prevIndex, i)
    );
    prevIndex = i + 1;
  }

  return result;
}
 
Example 8
@NotNull
private PantsTargetReference createTargetSegmentReference(
  @NotNull PyStringLiteralExpression expression,
  @NotNull PartialTargetAddress address
) {
  final TextRange range = TextRange.create(
    expression.valueOffsetToTextOffset(address.startOfExplicitTarget()),
    expression.valueOffsetToTextOffset(address.valueLength)
  );
  return new PantsTargetReference(
    myStringLiteralExpression,
    range, address.explicitTarget, address.normalizedPath
  );
}
 
Example 9
@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
  registrar.registerReferenceProvider(
    psiElement(PyStringLiteralExpression.class),
    new PantsTargetPathReferenceProvider()
  );
}
 
Example 10
Source Project: intellij-pants-plugin   Source File: PantsPsiUtil.java    License: 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 11
@NotNull
private PsiReference[] getReferences(PyStringLiteralExpression expression) {
  final List<PsiReference> references = new PantsTargetReferenceSet(expression).getReferences();
  return ArrayUtil.toObjectArray(references, PsiReference.class);
}
 
Example 12
public PantsTargetReferenceSet(@NotNull PyStringLiteralExpression element) {
  myStringLiteralExpression = element;
}