Generate test support: ask everything before write action to avoid AWT events inside
This commit is contained in:
+9
-8
@@ -149,9 +149,6 @@ abstract class KotlinGenerateTestSupportActionBase(
|
|||||||
private fun doGenerate(editor: Editor, file: PsiFile, klass: KtClassOrObject, framework: TestFramework) {
|
private fun doGenerate(editor: Editor, file: PsiFile, klass: KtClassOrObject, framework: TestFramework) {
|
||||||
val project = file.project
|
val project = file.project
|
||||||
val commandName = "Generate test function"
|
val commandName = "Generate test function"
|
||||||
project.executeWriteCommand(commandName) {
|
|
||||||
try {
|
|
||||||
PsiDocumentManager.getInstance(project).commitAllDocuments()
|
|
||||||
|
|
||||||
val fileTemplateDescriptor = methodKind.getFileTemplateDescriptor(framework)
|
val fileTemplateDescriptor = methodKind.getFileTemplateDescriptor(framework)
|
||||||
val fileTemplate = FileTemplateManager.getInstance(project).getCodeTemplate(fileTemplateDescriptor.fileName)
|
val fileTemplate = FileTemplateManager.getInstance(project).getCodeTemplate(fileTemplateDescriptor.fileName)
|
||||||
@@ -161,21 +158,25 @@ abstract class KotlinGenerateTestSupportActionBase(
|
|||||||
name = if (templateText.contains("test$NAME_VAR")) "Name" else "name"
|
name = if (templateText.contains("test$NAME_VAR")) "Name" else "name"
|
||||||
if (!ApplicationManager.getApplication().isUnitTestMode) {
|
if (!ApplicationManager.getApplication().isUnitTestMode) {
|
||||||
name = Messages.showInputDialog("Choose test name: ", commandName, null, name, NAME_VALIDATOR)
|
name = Messages.showInputDialog("Choose test name: ", commandName, null, name, NAME_VALIDATOR)
|
||||||
?: return@executeWriteCommand
|
?: return
|
||||||
}
|
}
|
||||||
|
|
||||||
templateText = fileTemplate.text.replace(NAME_VAR, DUMMY_NAME)
|
templateText = fileTemplate.text.replace(NAME_VAR, DUMMY_NAME)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
project.executeWriteCommand(commandName) {
|
||||||
|
try {
|
||||||
|
PsiDocumentManager.getInstance(project).commitAllDocuments()
|
||||||
|
|
||||||
val factory = PsiElementFactory.SERVICE.getInstance(project)
|
val factory = PsiElementFactory.SERVICE.getInstance(project)
|
||||||
val psiMethod = factory.createMethodFromText(templateText, null)
|
val psiMethod = factory.createMethodFromText(templateText, null)
|
||||||
psiMethod.throwsList.referenceElements.forEach { it.delete() }
|
psiMethod.throwsList.referenceElements.forEach { it.delete() }
|
||||||
var function = psiMethod.j2k() as? KtNamedFunction
|
var function = psiMethod.j2k() as? KtNamedFunction ?: run {
|
||||||
if (function == null) {
|
|
||||||
HintManager.getInstance().showErrorHint(editor, "Couldn't convert Java template to Kotlin")
|
HintManager.getInstance().showErrorHint(editor, "Couldn't convert Java template to Kotlin")
|
||||||
return@executeWriteCommand
|
return@executeWriteCommand
|
||||||
}
|
}
|
||||||
if (name != null) {
|
name?.let {
|
||||||
function = substituteNewName(function, name)
|
function = substituteNewName(function, it)
|
||||||
}
|
}
|
||||||
val functionInPlace = insertMember(editor, klass, function)
|
val functionInPlace = insertMember(editor, klass, function)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user