From e522b226d40aaea5377e8b28ae1ce0f771781e47 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Tue, 12 Mar 2024 23:59:25 -0500 Subject: [PATCH] feat(intellij): generate sealed classes (#4104) --- .../action/GenerateBlocAction.kt | 18 +++++++++--------- .../action/GenerateBlocDialog.java | 7 ++++--- .../action/GenerateCubitAction.kt | 11 ++++------- .../action/GenerateEquatablePropsAction.kt | 12 +++++------- .../generator/BlocGenerator.kt | 16 +++++++++------- .../generator/BlocGeneratorFactory.kt | 5 ++++- .../generator/CubitGenerator.kt | 16 +++++++++------- .../generator/CubitGeneratorFactory.kt | 5 ++++- .../templates/bloc_basic/bloc.dart.template | 2 -- .../bloc_basic/bloc_event.dart.template | 2 +- .../bloc_basic/bloc_state.dart.template | 4 ++-- .../bloc_equatable/bloc.dart.template | 2 -- .../bloc_equatable/bloc_event.dart.template | 2 +- .../bloc_equatable/bloc_state.dart.template | 4 ++-- .../cubit_basic/cubit_state.dart.template | 4 ++-- .../cubit_equatable/cubit_state.dart.template | 4 ++-- 16 files changed, 58 insertions(+), 56 deletions(-) diff --git a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateBlocAction.kt b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateBlocAction.kt index 9b0dc5f7590..4fdbc7f1cd1 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateBlocAction.kt +++ b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateBlocAction.kt @@ -18,7 +18,10 @@ class GenerateBlocAction : AnAction(), GenerateBlocDialog.Listener { dialog.show() } - override fun onGenerateBlocClicked(blocName: String?, blocTemplateType: BlocTemplateType) { + override fun onGenerateBlocClicked( + blocName: String?, + blocTemplateType: BlocTemplateType, + ) { blocName?.let { name -> val generators = BlocGeneratorFactory.getBlocGenerators(name, blocTemplateType) generate(generators) @@ -33,18 +36,15 @@ class GenerateBlocAction : AnAction(), GenerateBlocDialog.Listener { } } - protected fun generate(mainSourceGenerators: List) { + private fun generate(mainSourceGenerators: List) { val project = CommonDataKeys.PROJECT.getData(dataContext) val view = LangDataKeys.IDE_VIEW.getData(dataContext) val directory = view?.orChooseDirectory ApplicationManager.getApplication().runWriteAction { CommandProcessor.getInstance().executeCommand( - project, - { - mainSourceGenerators.forEach { createSourceFile(project!!, it, directory!!) } - }, - "Generate a new Bloc", - null + project, { + mainSourceGenerators.forEach { createSourceFile(project!!, it, directory!!) } + }, "Generate a new Bloc", null ) } } @@ -58,7 +58,7 @@ class GenerateBlocAction : AnAction(), GenerateBlocDialog.Listener { return } val psiFile = PsiFileFactory.getInstance(project) - .createFileFromText(fileName, JavaLanguage.INSTANCE, generator.generate()) + .createFileFromText(fileName, JavaLanguage.INSTANCE, generator.generate()) directory.add(psiFile) } } diff --git a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateBlocDialog.java b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateBlocDialog.java index 16fb4d8cde9..679392e14ab 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateBlocDialog.java +++ b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateBlocDialog.java @@ -4,6 +4,7 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; +import java.util.Objects; public class GenerateBlocDialog extends DialogWrapper { @@ -28,10 +29,10 @@ protected JComponent createCenterPanel() { protected void doOKAction() { super.doOKAction(); BlocTemplateType blocTemplateType; - final String selectedStyle = style.getSelectedItem().toString(); - if (selectedStyle == "Equatable") { + final String selectedStyle = Objects.requireNonNull(style.getSelectedItem()).toString(); + if (Objects.equals(selectedStyle, "Equatable")) { blocTemplateType = BlocTemplateType.EQUATABLE; - } else if (selectedStyle == "Freezed") { + } else if (Objects.equals(selectedStyle, "Freezed")) { blocTemplateType = BlocTemplateType.FREEZED; } else { blocTemplateType = BlocTemplateType.BASIC; diff --git a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateCubitAction.kt b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateCubitAction.kt index 97c862f4794..5edd083e14f 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateCubitAction.kt +++ b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateCubitAction.kt @@ -39,12 +39,9 @@ class GenerateCubitAction : AnAction(), GenerateBlocDialog.Listener { val directory = view?.orChooseDirectory ApplicationManager.getApplication().runWriteAction { CommandProcessor.getInstance().executeCommand( - project, - { - mainSourceGenerators.forEach { createSourceFile(project!!, it, directory!!) } - }, - "Generate a new Cubit", - null + project, { + mainSourceGenerators.forEach { createSourceFile(project!!, it, directory!!) } + }, "Generate a new Cubit", null ) } } @@ -58,7 +55,7 @@ class GenerateCubitAction : AnAction(), GenerateBlocDialog.Listener { return } val psiFile = PsiFileFactory.getInstance(project) - .createFileFromText(fileName, JavaLanguage.INSTANCE, generator.generate()) + .createFileFromText(fileName, JavaLanguage.INSTANCE, generator.generate()) directory.add(psiFile) } } diff --git a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateEquatablePropsAction.kt b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateEquatablePropsAction.kt index 5ad91fb3106..f20c6d926cb 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateEquatablePropsAction.kt +++ b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/action/GenerateEquatablePropsAction.kt @@ -18,13 +18,11 @@ class GenerateEquatablePropsAction : AnAction() { override fun update(event: AnActionEvent) { super.update(event) - val action = event?.presentation; - if (action != null) { - val editor = event.getRequiredData(CommonDataKeys.EDITOR) - val project = event.project ?: return - val currentFile = PsiUtilBase.getPsiFileInEditor(editor, project) - action.isEnabledAndVisible = currentFile?.name?.endsWith(".dart") == true - } + val action = event.presentation; + val editor = event.getRequiredData(CommonDataKeys.EDITOR) + val project = event.project ?: return + val currentFile = PsiUtilBase.getPsiFileInEditor(editor, project) + action.isEnabledAndVisible = currentFile?.name?.endsWith(".dart") == true } override fun actionPerformed(event: AnActionEvent) { diff --git a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/BlocGenerator.kt b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/BlocGenerator.kt index bd58aba9643..4bcec14e881 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/BlocGenerator.kt +++ b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/BlocGenerator.kt @@ -7,9 +7,11 @@ import com.google.common.io.CharStreams import org.apache.commons.lang.text.StrSubstitutor import java.io.InputStreamReader -abstract class BlocGenerator(private val name: String, - blocTemplateType: BlocTemplateType, - templateName: String) { +abstract class BlocGenerator( + private val name: String, + blocTemplateType: BlocTemplateType, + templateName: String +) { private val TEMPLATE_BLOC_PASCAL_CASE = "bloc_pascal_case" private val TEMPLATE_BLOC_SNAKE_CASE = "bloc_snake_case" @@ -19,8 +21,8 @@ abstract class BlocGenerator(private val name: String, init { templateValues = mutableMapOf( - TEMPLATE_BLOC_PASCAL_CASE to pascalCase(), - TEMPLATE_BLOC_SNAKE_CASE to snakeCase() + TEMPLATE_BLOC_PASCAL_CASE to pascalCase(), + TEMPLATE_BLOC_SNAKE_CASE to snakeCase() ) try { val templateFolder = when (blocTemplateType) { @@ -30,7 +32,7 @@ abstract class BlocGenerator(private val name: String, } val resource = "/templates/$templateFolder/$templateName.dart.template" val resourceAsStream = BlocGenerator::class.java.getResourceAsStream(resource) - templateString = CharStreams.toString(InputStreamReader(resourceAsStream, Charsets.UTF_8)) + templateString = CharStreams.toString(InputStreamReader(resourceAsStream!!, Charsets.UTF_8)) } catch (e: Exception) { throw RuntimeException(e) } @@ -43,7 +45,7 @@ abstract class BlocGenerator(private val name: String, return substitutor.replace(templateString) } - fun pascalCase(): String = name.toUpperCamelCase().replace("Bloc", "") + private fun pascalCase(): String = name.toUpperCamelCase().replace("Bloc", "") fun snakeCase() = name.toLowerSnakeCase().replace("_bloc", "") diff --git a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/BlocGeneratorFactory.kt b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/BlocGeneratorFactory.kt index b8636e5fb7e..c4c890a4d24 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/BlocGeneratorFactory.kt +++ b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/BlocGeneratorFactory.kt @@ -6,7 +6,10 @@ import com.bloc.intellij_generator_plugin.generator.components.BlocGenerator import com.bloc.intellij_generator_plugin.generator.components.BlocStateGenerator object BlocGeneratorFactory { - fun getBlocGenerators(name: String, blocTemplateType: BlocTemplateType): List { + fun getBlocGenerators( + name: String, + blocTemplateType: BlocTemplateType, + ): List { val bloc = BlocGenerator(name, blocTemplateType) val event = BlocEventGenerator(name, blocTemplateType) val state = BlocStateGenerator(name, blocTemplateType) diff --git a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/CubitGenerator.kt b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/CubitGenerator.kt index 4fbe13e8bca..5d50ddae1a5 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/CubitGenerator.kt +++ b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/CubitGenerator.kt @@ -7,9 +7,11 @@ import com.google.common.io.CharStreams import org.apache.commons.lang.text.StrSubstitutor import java.io.InputStreamReader -abstract class CubitGenerator(private val name: String, - blocTemplateType: BlocTemplateType, - templateName: String) { +abstract class CubitGenerator( + private val name: String, + blocTemplateType: BlocTemplateType, + templateName: String +) { private val TEMPLATE_CUBIT_PASCAL_CASE = "cubit_pascal_case" private val TEMPLATE_CUBIT_SNAKE_CASE = "cubit_snake_case" @@ -19,8 +21,8 @@ abstract class CubitGenerator(private val name: String, init { templateValues = mutableMapOf( - TEMPLATE_CUBIT_PASCAL_CASE to pascalCase(), - TEMPLATE_CUBIT_SNAKE_CASE to snakeCase() + TEMPLATE_CUBIT_PASCAL_CASE to pascalCase(), + TEMPLATE_CUBIT_SNAKE_CASE to snakeCase() ) try { val templateFolder = when (blocTemplateType) { @@ -30,7 +32,7 @@ abstract class CubitGenerator(private val name: String, } val resource = "/templates/$templateFolder/$templateName.dart.template" val resourceAsStream = CubitGenerator::class.java.getResourceAsStream(resource) - templateString = CharStreams.toString(InputStreamReader(resourceAsStream, Charsets.UTF_8)) + templateString = CharStreams.toString(InputStreamReader(resourceAsStream!!, Charsets.UTF_8)) } catch (e: Exception) { throw RuntimeException(e) } @@ -43,7 +45,7 @@ abstract class CubitGenerator(private val name: String, return substitutor.replace(templateString) } - fun pascalCase(): String = name.toUpperCamelCase().replace("Cubit", "") + private fun pascalCase(): String = name.toUpperCamelCase().replace("Cubit", "") fun snakeCase() = name.toLowerSnakeCase().replace("_cubit", "") diff --git a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/CubitGeneratorFactory.kt b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/CubitGeneratorFactory.kt index c62990b18d4..045eb389a79 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/CubitGeneratorFactory.kt +++ b/extensions/intellij/intellij_generator_plugin/src/main/java/com/bloc/intellij_generator_plugin/generator/CubitGeneratorFactory.kt @@ -5,7 +5,10 @@ import com.bloc.intellij_generator_plugin.generator.components.CubitGenerator import com.bloc.intellij_generator_plugin.generator.components.CubitStateGenerator object CubitGeneratorFactory { - fun getCubitGenerators(name: String, blocTemplateType: BlocTemplateType): List { + fun getCubitGenerators( + name: String, + blocTemplateType: BlocTemplateType, + ): List { val cubit = CubitGenerator(name, blocTemplateType) val state = CubitStateGenerator(name, blocTemplateType) return listOf(cubit, state) diff --git a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_basic/bloc.dart.template b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_basic/bloc.dart.template index 9130667bad2..3429d6b07a2 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_basic/bloc.dart.template +++ b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_basic/bloc.dart.template @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:bloc/bloc.dart'; import 'package:meta/meta.dart'; diff --git a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_basic/bloc_event.dart.template b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_basic/bloc_event.dart.template index 460a75613f7..b757edcaf89 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_basic/bloc_event.dart.template +++ b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_basic/bloc_event.dart.template @@ -1,4 +1,4 @@ part of '{{bloc_snake_case}}_bloc.dart'; @immutable -abstract class {{bloc_pascal_case}}Event {} +sealed class {{bloc_pascal_case}}Event {} diff --git a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_basic/bloc_state.dart.template b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_basic/bloc_state.dart.template index 220eda95c1e..46353e6bc8f 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_basic/bloc_state.dart.template +++ b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_basic/bloc_state.dart.template @@ -1,6 +1,6 @@ part of '{{bloc_snake_case}}_bloc.dart'; @immutable -abstract class {{bloc_pascal_case}}State {} +sealed class {{bloc_pascal_case}}State {} -class {{bloc_pascal_case}}Initial extends {{bloc_pascal_case}}State {} +final class {{bloc_pascal_case}}Initial extends {{bloc_pascal_case}}State {} diff --git a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_equatable/bloc.dart.template b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_equatable/bloc.dart.template index 9304c8f6169..1e0748d8ca0 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_equatable/bloc.dart.template +++ b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_equatable/bloc.dart.template @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; diff --git a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_equatable/bloc_event.dart.template b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_equatable/bloc_event.dart.template index ed23ddbefd3..6cd03bc8139 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_equatable/bloc_event.dart.template +++ b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_equatable/bloc_event.dart.template @@ -1,5 +1,5 @@ part of '{{bloc_snake_case}}_bloc.dart'; -abstract class {{bloc_pascal_case}}Event extends Equatable { +sealed class {{bloc_pascal_case}}Event extends Equatable { const {{bloc_pascal_case}}Event(); } diff --git a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_equatable/bloc_state.dart.template b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_equatable/bloc_state.dart.template index 3b456ce9b18..81898676111 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_equatable/bloc_state.dart.template +++ b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/bloc_equatable/bloc_state.dart.template @@ -1,10 +1,10 @@ part of '{{bloc_snake_case}}_bloc.dart'; -abstract class {{bloc_pascal_case}}State extends Equatable { +sealed class {{bloc_pascal_case}}State extends Equatable { const {{bloc_pascal_case}}State(); } -class {{bloc_pascal_case}}Initial extends {{bloc_pascal_case}}State { +final class {{bloc_pascal_case}}Initial extends {{bloc_pascal_case}}State { @override List get props => []; } diff --git a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/cubit_basic/cubit_state.dart.template b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/cubit_basic/cubit_state.dart.template index 85d06c7e5d8..964c6e1dabb 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/cubit_basic/cubit_state.dart.template +++ b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/cubit_basic/cubit_state.dart.template @@ -1,6 +1,6 @@ part of '{{cubit_snake_case}}_cubit.dart'; @immutable -abstract class {{cubit_pascal_case}}State {} +sealed class {{cubit_pascal_case}}State {} -class {{cubit_pascal_case}}Initial extends {{cubit_pascal_case}}State {} +final class {{cubit_pascal_case}}Initial extends {{cubit_pascal_case}}State {} diff --git a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/cubit_equatable/cubit_state.dart.template b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/cubit_equatable/cubit_state.dart.template index ccac690567d..71ca3ea60e8 100644 --- a/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/cubit_equatable/cubit_state.dart.template +++ b/extensions/intellij/intellij_generator_plugin/src/main/resources/templates/cubit_equatable/cubit_state.dart.template @@ -1,10 +1,10 @@ part of '{{cubit_snake_case}}_cubit.dart'; -abstract class {{cubit_pascal_case}}State extends Equatable { +sealed class {{cubit_pascal_case}}State extends Equatable { const {{cubit_pascal_case}}State(); } -class {{cubit_pascal_case}}Initial extends {{cubit_pascal_case}}State { +final class {{cubit_pascal_case}}Initial extends {{cubit_pascal_case}}State { @override List get props => []; }