use of com.jetbrains.python.psi.PyStatement in project intellij-community by JetBrains.
the class PyTestConfigurationProducer method setupConfigurationFromContext.
@Override
protected boolean setupConfigurationFromContext(AbstractPythonLegacyTestRunConfiguration<PyTestRunConfiguration> configuration, ConfigurationContext context, Ref<PsiElement> sourceElement) {
final PsiElement element = sourceElement.get();
final Module module = ModuleUtilCore.findModuleForPsiElement(element);
if (!(configuration instanceof PyTestRunConfiguration)) {
return false;
}
if (module == null) {
return false;
}
if (!(TestRunnerService.getInstance(module).getProjectConfiguration().equals(PythonTestConfigurationsModel.PY_TEST_NAME))) {
return false;
}
final PsiFileSystemItem file = element instanceof PsiDirectory ? (PsiDirectory) element : element.getContainingFile();
if (file == null) {
return false;
}
final VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile == null) {
return false;
}
if (file instanceof PyFile || file instanceof PsiDirectory) {
final List<PyStatement> testCases = PyTestUtil.getPyTestCasesFromFile(file, TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile()));
if (testCases.isEmpty()) {
return false;
}
} else {
return false;
}
final Sdk sdk = PythonSdkType.findPythonSdk(context.getModule());
if (sdk == null) {
return false;
}
configuration.setUseModuleSdk(true);
configuration.setModule(ModuleUtilCore.findModuleForPsiElement(element));
((PyTestRunConfiguration) configuration).setTestToRun(virtualFile.getPath());
final String keywords = getKeywords(element, sdk);
if (keywords != null) {
((PyTestRunConfiguration) configuration).useKeyword(true);
((PyTestRunConfiguration) configuration).setKeywords(keywords);
configuration.setName("py.test in " + keywords);
} else {
configuration.setName("py.test in " + file.getName());
}
return true;
}
use of com.jetbrains.python.psi.PyStatement in project intellij-community by JetBrains.
the class PyTestConfigurationProducer method isConfigurationFromContext.
@Override
public boolean isConfigurationFromContext(AbstractPythonLegacyTestRunConfiguration configuration, ConfigurationContext context) {
final Location location = context.getLocation();
if (location == null)
return false;
if (!(configuration instanceof PyTestRunConfiguration))
return false;
final PsiElement element = location.getPsiElement();
final PsiFileSystemItem file = element instanceof PsiDirectory ? (PsiDirectory) element : element.getContainingFile();
if (file == null)
return false;
final VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile == null)
return false;
if (file instanceof PyFile || file instanceof PsiDirectory) {
final List<PyStatement> testCases = PyTestUtil.getPyTestCasesFromFile(file, TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile()));
if (testCases.isEmpty())
return false;
} else {
return false;
}
final Sdk sdk = PythonSdkType.findPythonSdk(context.getModule());
if (sdk == null)
return false;
final String keywords = getKeywords(element, sdk);
final String scriptName = ((PyTestRunConfiguration) configuration).getTestToRun();
final String workingDirectory = configuration.getWorkingDirectory();
final String path = virtualFile.getPath();
final boolean isTestFileEquals = scriptName.equals(path) || path.equals(new File(workingDirectory, scriptName).getAbsolutePath());
final String configurationKeywords = ((PyTestRunConfiguration) configuration).getKeywords();
return isTestFileEquals && (configurationKeywords.equals(keywords) || StringUtil.isEmptyOrSpaces(((PyTestRunConfiguration) configuration).getKeywords()) && keywords == null);
}
use of com.jetbrains.python.psi.PyStatement in project intellij-community by JetBrains.
the class PySmartEnterProcessor method collectAllElements.
private static void collectAllElements(final PsiElement element, @NotNull final List<PsiElement> result, boolean recurse) {
result.add(0, element);
if (doNotStep(element)) {
if (!recurse) {
return;
}
recurse = false;
}
final PsiElement[] children = element.getChildren();
for (final PsiElement child : children) {
if (element instanceof PyStatement && child instanceof PyStatement) {
continue;
}
collectAllElements(child, result, recurse);
}
}
use of com.jetbrains.python.psi.PyStatement in project intellij-community by JetBrains.
the class PyCodeBlockProvider method getCodeBlockRange.
@Nullable
@Override
public TextRange getCodeBlockRange(Editor editor, PsiFile psiFile) {
int caretOffset = editor.getCaretModel().getOffset();
PsiElement element = psiFile.findElementAt(caretOffset);
if (element == null) {
return null;
}
while (caretOffset > 0 && element instanceof PsiWhiteSpace) {
caretOffset--;
element = psiFile.findElementAt(caretOffset);
}
PyStatement statement = PsiTreeUtil.getParentOfType(element, PyStatement.class);
if (statement != null) {
PyStatementList statementList = PsiTreeUtil.findChildOfType(statement, PyStatementList.class);
// that statement list
if (statementList == null) {
statementList = PsiTreeUtil.getParentOfType(statement, PyStatementList.class);
if (statementList != null) {
statement = PsiTreeUtil.getParentOfType(statementList, PyStatement.class);
}
}
if (statement != null) {
// if we're in the beginning of the statement already, pressing Ctrl-[ again should move the caret one statement higher
final int statementStart = statement.getTextRange().getStartOffset();
int statementEnd = statement.getTextRange().getEndOffset();
while (statementEnd > statementStart && psiFile.findElementAt(statementEnd) instanceof PsiWhiteSpace) {
statementEnd--;
}
if (caretOffset == statementStart || caretOffset == statementEnd) {
final PyStatement statementAbove = PsiTreeUtil.getParentOfType(statement, PyStatement.class);
if (statementAbove != null) {
if (caretOffset == statementStart) {
return new TextRange(statementAbove.getTextRange().getStartOffset(), statementEnd);
} else {
return new TextRange(statementStart, statementAbove.getTextRange().getEndOffset());
}
}
}
return statement.getTextRange();
}
}
return null;
}
use of com.jetbrains.python.psi.PyStatement in project intellij-community by JetBrains.
the class PyStatementListImpl method deleteChildInternal.
@Override
public void deleteChildInternal(@NotNull ASTNode child) {
final PsiElement childElement = child.getPsi();
if (childElement instanceof PyStatement && getStatements().length == 1) {
childElement.replace(PyElementGenerator.getInstance(getProject()).createPassStatement());
return;
}
super.deleteChildInternal(child);
}
Aggregations