-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add create Pest dialog support
- Loading branch information
Oliver Nybroe
committed
May 31, 2023
1 parent
d7cebbb
commit 1ead810
Showing
8 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
|
||
## Unreleased | ||
|
||
### Added | ||
- Added pest file creation support | ||
|
||
## 1.9.3 - 2023-05-31 | ||
|
||
### Fixed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/kotlin/com/pestphp/pest/templates/PestConfigNewFileAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
#parse("PHP File Header.php") | ||
|
||
it('#[[$END$]]#', function () { | ||
|
||
}); |
6 changes: 6 additions & 0 deletions
6
src/main/resources/fileTemplates/internal/Pest Scoped Dataset.php.ft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
6
src/main/resources/fileTemplates/internal/Pest Shared Dataset.php.ft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
#parse("PHP File Header.php") | ||
|
||
dataset('#[[$DATASET_NAME$]]#', [ | ||
'#[[$END$]]#' | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
#parse("PHP File Header.php") | ||
|
||
test('#[[$END$]]#', function () { | ||
|
||
}); |