Search in sources :

Example 1 with CreateStepDefinitionFileModel

use of org.jetbrains.plugins.cucumber.inspections.model.CreateStepDefinitionFileModel in project intellij-plugins by JetBrains.

the class CucumberCreateStepFixBase method askUserForFilePath.

@Nullable
private static CreateStepDefinitionFileModel askUserForFilePath(@NotNull final GherkinStep step) {
    final InputValidator validator = new InputValidator() {

        public boolean checkInput(final String filePath) {
            return !StringUtil.isEmpty(filePath);
        }

        public boolean canClose(final String fileName) {
            return true;
        }
    };
    Map<BDDFrameworkType, String> supportedFileTypesAndDefaultFileNames = new HashMap<>();
    Map<BDDFrameworkType, PsiDirectory> fileTypeToDefaultDirectoryMap = new HashMap<>();
    for (CucumberJvmExtensionPoint e : Extensions.getExtensions(CucumberJvmExtensionPoint.EP_NAME)) {
        if (e instanceof OptionalStepDefinitionExtensionPoint) {
            // Skip if framework file creation support is optional
            if (!((OptionalStepDefinitionExtensionPoint) e).participateInStepDefinitionCreation(step)) {
                continue;
            }
        }
        supportedFileTypesAndDefaultFileNames.put(e.getStepFileType(), e.getStepDefinitionCreator().getDefaultStepFileName(step));
        fileTypeToDefaultDirectoryMap.put(e.getStepFileType(), e.getStepDefinitionCreator().getDefaultStepDefinitionFolder(step));
    }
    CreateStepDefinitionFileModel model = new CreateStepDefinitionFileModel(step.getProject(), supportedFileTypesAndDefaultFileNames, fileTypeToDefaultDirectoryMap);
    CreateStepDefinitionFileDialog createStepDefinitionFileDialog = new CreateStepDefinitionFileDialog(step.getProject(), model, validator);
    if (createStepDefinitionFileDialog.showAndGet()) {
        return model;
    } else {
        return null;
    }
}
Also used : InputValidator(com.intellij.openapi.ui.InputValidator) HashMap(com.intellij.util.containers.HashMap) PsiDirectory(com.intellij.psi.PsiDirectory) CreateStepDefinitionFileModel(org.jetbrains.plugins.cucumber.inspections.model.CreateStepDefinitionFileModel) CreateStepDefinitionFileDialog(org.jetbrains.plugins.cucumber.inspections.ui.CreateStepDefinitionFileDialog) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with CreateStepDefinitionFileModel

use of org.jetbrains.plugins.cucumber.inspections.model.CreateStepDefinitionFileModel in project intellij-plugins by JetBrains.

the class CucumberCreateStepFixBase method createStepDefinitionFile.

private static void createStepDefinitionFile(final GherkinStep step) {
    final PsiFile featureFile = step.getContainingFile();
    assert featureFile != null;
    final CreateStepDefinitionFileModel model = askUserForFilePath(step);
    if (model == null) {
        return;
    }
    String filePath = FileUtil.toSystemDependentName(model.getFilePath());
    final BDDFrameworkType frameworkType = model.getSelectedFileType();
    // show error if file already exists
    Project project = step.getProject();
    if (LocalFileSystem.getInstance().findFileByPath(filePath) == null) {
        final String parentDirPath = model.getDirectory().getVirtualFile().getPath();
        ApplicationManager.getApplication().invokeLater(() -> CommandProcessor.getInstance().executeCommand(project, () -> {
            try {
                VirtualFile parentDir = VfsUtil.createDirectories(parentDirPath);
                PsiDirectory parentPsiDir = PsiManager.getInstance(project).findDirectory(parentDir);
                assert parentPsiDir != null;
                PsiFile newFile = CucumberStepsIndex.getInstance(project).createStepDefinitionFile(model.getDirectory(), model.getFileName(), frameworkType);
                Pair<PsiFile, BDDFrameworkType> pair = Pair.create(newFile, frameworkType);
                createStepDefinition(step, pair);
            } catch (IOException e) {
                LOG.error(e);
            }
        }, CucumberBundle.message("cucumber.quick.fix.create.step.command.name.create"), null));
    } else {
        Messages.showErrorDialog(project, CucumberBundle.message("cucumber.quick.fix.create.step.error.already.exist.msg", filePath), CucumberBundle.message("cucumber.quick.fix.create.step.file.name.title"));
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) CreateStepDefinitionFileModel(org.jetbrains.plugins.cucumber.inspections.model.CreateStepDefinitionFileModel) IOException(java.io.IOException) Pair(com.intellij.openapi.util.Pair)

Aggregations

PsiDirectory (com.intellij.psi.PsiDirectory)2 CreateStepDefinitionFileModel (org.jetbrains.plugins.cucumber.inspections.model.CreateStepDefinitionFileModel)2 Project (com.intellij.openapi.project.Project)1 InputValidator (com.intellij.openapi.ui.InputValidator)1 Pair (com.intellij.openapi.util.Pair)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 HashMap (com.intellij.util.containers.HashMap)1 IOException (java.io.IOException)1 Nullable (org.jetbrains.annotations.Nullable)1 CreateStepDefinitionFileDialog (org.jetbrains.plugins.cucumber.inspections.ui.CreateStepDefinitionFileDialog)1