Skip to content

Commit

Permalink
feat: Add create Pest dialog support
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Nybroe committed May 31, 2023
1 parent d7cebbb commit 1ead810
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

## Unreleased

### Added
- Added pest file creation support

## 1.9.3 - 2023-05-31

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pluginGroup = com.pestphp
pluginName = PEST PHP
pluginVersion = 1.9.3
pluginVersion = 1.10.0-EAP.1

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.pestphp.pest.templates

import com.intellij.ide.actions.CreateFileFromTemplateAction
import com.intellij.ide.actions.CreateFileFromTemplateDialog
import com.intellij.ide.fileTemplates.FileTemplate
import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFile
import com.pestphp.pest.PestIcons


class PestConfigNewFileAction :
CreateFileFromTemplateAction() {

override fun isAvailable(dataContext: DataContext): Boolean {
val view = LangDataKeys.IDE_VIEW.getData(dataContext)
var psiDir: PsiDirectory? = null
if (view != null) {
val directories = view.directories
if (directories.size == 1) {
psiDir = directories[0]
}
}

if (psiDir == null || !psiDir.isValid) {
return false
}

val virtualDir = psiDir.virtualFile
if (!virtualDir.isValid || !virtualDir.isDirectory) {
return false
}

return virtualDir.path.contains("tests")
}

override fun buildDialog(project: Project, directory: PsiDirectory, builder: CreateFileFromTemplateDialog.Builder) {
builder
.setTitle("Create Pest Test File")
.addKind("It", PestIcons.FILE, "Pest It")
.addKind("Test", PestIcons.FILE, "Pest Test")
.addKind("Shared dataset", PestIcons.DATASET_FILE, "Pest Shared Dataset")
.addKind("Scoped dataset", PestIcons.DATASET_FILE, "Pest Scoped Dataset")
}

override fun getActionName(directory: PsiDirectory?, newName: String, templateName: String?): String {
return "Pest Test"
}

override fun createFileFromTemplate(name: String?, template: FileTemplate, dir: PsiDirectory): PsiFile {
if (template.name == "Pest Shared Dataset") {
// find parent directory named "tests"
var parentDir = dir
while (parentDir.name != "tests") {
parentDir = parentDir.parentDirectory ?: break
}

val datasetDir = parentDir.findSubdirectory("Datasets")
?: parentDir.createSubdirectory("Datasets")

// Check if first character is lowercase in name
var newName = name
if (name!![0].isLowerCase()) {
newName = name.replaceFirstChar { it.uppercase() }
}

return createFileFromTemplate(
newName,
template,
datasetDir,
defaultTemplateProperty,
true,
mapOf("DATASET_NAME" to name.replaceFirstChar { it.lowercase() })
)!!
}

if (template.name == "Pest Scoped Dataset") {
return createFileFromTemplate(
"Datasets",
template,
dir,
defaultTemplateProperty,
true,
mapOf("DATASET_NAME" to name!!.replaceFirstChar { it.lowercase() })
)!!
}

var testName = name
if (!name!!.endsWith("test", true)) {
testName = "${name}Test"
}

return super.createFileFromTemplate(testName, template, dir)
}
}
11 changes: 10 additions & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
description="Whether custom expectation file generation should happen."
key="pestphp.custom-expectations"
/>
<internalFileTemplate name="Pest It"/>
<internalFileTemplate name="Pest Test"/>
<internalFileTemplate name="Pest Scoped Dataset"/>
<internalFileTemplate name="Pest Shared Dataset"/>
</extensions>

<projectListeners>
Expand All @@ -134,6 +138,11 @@
</extensions>

<actions>
<!-- Add your actions here -->
<action id="Pest.New.File"
class="com.pestphp.pest.templates.PestConfigNewFileAction"
icon="/file.svg"
text="Pest Test">
<add-to-group group-id="NewGroup" anchor="before" relative-to-action="NewFromTemplate"/>
</action>
</actions>
</idea-plugin>
6 changes: 6 additions & 0 deletions src/main/resources/fileTemplates/internal/Pest It.php.ft
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
#parse("PHP File Header.php")

it('#[[$END$]]#', function () {

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
#parse("PHP File Header.php")

dataset('#[[$DATASET_NAME$]]#', [
'#[[$END$]]#'
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
#parse("PHP File Header.php")

dataset('#[[$DATASET_NAME$]]#', [
'#[[$END$]]#'
]);
6 changes: 6 additions & 0 deletions src/main/resources/fileTemplates/internal/Pest Test.php.ft
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
#parse("PHP File Header.php")

test('#[[$END$]]#', function () {

});

0 comments on commit 1ead810

Please sign in to comment.