use of com.intellij.psi.PsiDirectory in project intellij-community by JetBrains.
the class CreateTestAction method invoke.
@Override
public void invoke(@NotNull final Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
final PyFunction srcFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class);
final PyClass srcClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
if (srcClass == null && srcFunction == null)
return;
final PsiDirectory dir = element.getContainingFile().getContainingDirectory();
final CreateTestDialog d = new CreateTestDialog(project);
if (srcClass != null) {
d.setClassName("Test" + StringUtil.capitalize(srcClass.getName()));
d.setFileName("test_" + StringUtil.decapitalize(srcClass.getName()) + ".py");
if (dir != null)
d.setTargetDir(dir.getVirtualFile().getPath());
if (srcFunction != null) {
d.methodsSize(1);
d.addMethod("test_" + srcFunction.getName(), 0);
} else {
final List<PyFunction> methods = Lists.newArrayList();
srcClass.visitMethods(pyFunction -> {
if (pyFunction.getName() != null && !pyFunction.getName().startsWith("__"))
methods.add(pyFunction);
return true;
}, false, null);
d.methodsSize(methods.size());
int i = 0;
for (PyFunction f : methods) {
d.addMethod("test_" + f.getName(), i);
++i;
}
}
} else {
d.setClassName("Test" + StringUtil.capitalize(srcFunction.getName()));
d.setFileName("test_" + StringUtil.decapitalize(srcFunction.getName()) + ".py");
if (dir != null)
d.setTargetDir(dir.getVirtualFile().getPath());
d.methodsSize(1);
d.addMethod("test_" + srcFunction.getName(), 0);
}
if (!d.showAndGet()) {
return;
}
CommandProcessor.getInstance().executeCommand(project, () -> {
PsiFile e = PyTestCreator.generateTestAndNavigate(project, d);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitAllDocuments();
}, CodeInsightBundle.message("intention.create.test"), this);
}
use of com.intellij.psi.PsiDirectory in project intellij-community by JetBrains.
the class PyToxConfigurationProducer method setupConfigurationFromContext.
@Override
protected boolean setupConfigurationFromContext(final PyToxConfiguration configuration, final ConfigurationContext context, final Ref<PsiElement> sourceElement) {
final PsiFile file = sourceElement.get().getContainingFile();
if (file == null) {
return false;
}
final String envName = PyToxTestLocator.getEnvNameFromElement(file);
if (envName != null) {
configuration.setRunOnlyEnvs(envName);
configuration.setName(String.format("Tox: run on %s", envName));
return true;
}
final PsiDirectory directory = file.getContainingDirectory();
if (directory == null) {
return false;
}
configuration.setWorkingDirectory(directory.getVirtualFile().getCanonicalPath());
if (TOX_FILE_NAME.equals(file.getName())) {
configuration.setName("Tox");
return true;
}
return false;
}
use of com.intellij.psi.PsiDirectory 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.intellij.psi.PsiDirectory 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.intellij.psi.PsiDirectory in project intellij-community by JetBrains.
the class PropertiesImplUtil method createByUrl.
@Nullable
public static ResourceBundle createByUrl(@NotNull final String url, @NotNull final Project project) {
final int idx = url.lastIndexOf('/');
if (idx == -1)
return null;
final String baseDirectoryName = url.substring(0, idx);
final String baseName = url.substring(idx + 1);
final VirtualFile baseDirectoryVirtualFile = VirtualFileManager.getInstance().findFileByUrl(baseDirectoryName);
if (baseDirectoryVirtualFile == null) {
return null;
}
final PsiDirectory baseDirectory = PsiManager.getInstance(project).findDirectory(baseDirectoryVirtualFile);
if (baseDirectory == null) {
return null;
}
final ResourceBundleManager bundleBaseNameManager = ResourceBundleManager.getInstance(project);
for (PsiFile file : baseDirectory.getFiles()) {
final PropertiesFile propertiesFile = getPropertiesFile(file);
if (propertiesFile == null)
continue;
final String currBaseName = bundleBaseNameManager.getBaseName(file);
if (currBaseName.equals(baseName)) {
return getResourceBundle(propertiesFile);
}
}
return null;
}
Aggregations