com.jetbrains.python.psi.PyExpression Java Examples

The following examples show how to use com.jetbrains.python.psi.PyExpression. 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: PyTestContextProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
private static PyExpression[] getParameterizedDecoratorArgumentList(PyDecorator decorator) {
  PyArgumentList parameterizedArgumentList = decorator.getArgumentList();
  if (parameterizedArgumentList == null) {
    return null;
  }

  PyExpression[] arguments = parameterizedArgumentList.getArguments();
  if (arguments.length == 1 && arguments[0] instanceof PyListLiteralExpression) {
    PyListLiteralExpression literalList = (PyListLiteralExpression) arguments[0];
    arguments = literalList.getElements();
  }

  if (arguments.length == 0) {
    return null;
  }
  return arguments;
}
 
Example #2
Source File: PyTestContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private static String getTestFilterForParameters(String testBase, PyDecorator decorator) {
  PyExpression[] arguments = getParameterizedDecoratorArgumentList(decorator);
  if (arguments == null) {
    return null;
  }

  ArrayList<String> parameterizedFilters = new ArrayList<>();
  for (int i = 0; i < arguments.length; ++i) {

    parameterizedFilters.add(testBase + i);
  }

  return Joiner.on(" ").join(parameterizedFilters);
}
 
Example #3
Source File: PantsCompletionContributor.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
private boolean isTopLevelExpression(@NotNull CompletionParameters parameters) {
  PsiElement position = parameters.getPosition();
  PsiElement expression = position.getParent();
  if (!(expression instanceof PyExpression)) return false;
  PsiElement statement = expression.getParent();
  if (!(statement instanceof PyStatement)) return false;
  PsiElement file = statement.getParent();
  return file instanceof PyFile;
}
 
Example #4
Source File: PantsPsiUtil.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Pair<String, PyCallExpression> findTarget(@NotNull PyExpressionStatement statement) {
  final PyCallExpression expression = PsiTreeUtil.findChildOfType(statement, PyCallExpression.class);
  final PyExpression callee = expression != null ? expression.getCallee() : null;
  final PyArgumentList argumentList = expression != null ? expression.getArgumentList() : null;
  final PyKeywordArgument nameArgument = argumentList != null ? argumentList.getKeywordArgument("name") : null;
  final PyExpression valueExpression = nameArgument != null ? nameArgument.getValueExpression() : null;
  if (valueExpression != null && callee != null) {
    return Pair.create(unquoteString(valueExpression.getText()), expression);
  }
  return null;
}
 
Example #5
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 #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"));
}