com.jetbrains.python.psi.PyStringLiteralExpression Java Examples

The following examples show how to use com.jetbrains.python.psi.PyStringLiteralExpression. 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: PantsTargetReferenceSet.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
@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
Source File: PantsTargetReferenceSet.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
@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
Source File: PythonEnvironmentCallsVisitor.java    From idea-php-dotenv-plugin with MIT License 5 votes vote down vote up
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
Source File: PythonEnvironmentCallsVisitor.java    From idea-php-dotenv-plugin with MIT License 5 votes vote down vote up
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
Source File: PantsCompletionContributor.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
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
Source File: PantsTargetPathReferenceProvider.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@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
Source File: PantsTargetReferenceSet.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@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
Source File: PantsTargetReferenceSet.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@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
Source File: PantsReferenceContributor.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
  registrar.registerReferenceProvider(
    psiElement(PyStringLiteralExpression.class),
    new PantsTargetPathReferenceProvider()
  );
}
 
Example #10
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 #11
Source File: PantsTargetPathReferenceProvider.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
@NotNull
private PsiReference[] getReferences(PyStringLiteralExpression expression) {
  final List<PsiReference> references = new PantsTargetReferenceSet(expression).getReferences();
  return ArrayUtil.toObjectArray(references, PsiReference.class);
}
 
Example #12
Source File: PantsTargetReferenceSet.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
public PantsTargetReferenceSet(@NotNull PyStringLiteralExpression element) {
  myStringLiteralExpression = element;
}