Skip to content

Commit

Permalink
feat(intellij): add freezed support (#3537)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinneff authored Dec 6, 2022
1 parent c353afb commit acad4ed
Show file tree
Hide file tree
Showing 39 changed files with 251 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bloc.intellij_generator_plugin.action;

public enum BlocTemplateType {
BASIC,
EQUATABLE,
FREEZED
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class GenerateBlocAction : AnAction(), GenerateBlocDialog.Listener {
dialog.show()
}

override fun onGenerateBlocClicked(blocName: String?, shouldUseEquatable: Boolean) {
override fun onGenerateBlocClicked(blocName: String?, blocTemplateType: BlocTemplateType) {
blocName?.let { name ->
val generators = BlocGeneratorFactory.getBlocGenerators(name, shouldUseEquatable)
val generators = BlocGeneratorFactory.getBlocGenerators(name, blocTemplateType)
generate(generators)
}
}
Expand All @@ -39,12 +39,12 @@ class GenerateBlocAction : AnAction(), GenerateBlocDialog.Listener {
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
)
}
}
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,42 @@
<toolTipText value="Bloc name goes here (e.g. Login)"/>
</properties>
</component>
<component id="7d19b" class="javax.swing.JCheckBox" binding="useEquatableCheckbox">
<component id="9e8d8" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
<text value="Name:"/>
</properties>
</component>
<component id="9e8d8" class="javax.swing.JLabel">
<component id="29a5e" class="javax.swing.JComboBox" binding="style">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Name:"/>
<model>
<item value="Basic"/>
<item value="Equatable"/>
<item value="Freezed"/>
</model>
</properties>
</component>
<component id="b8c83" class="javax.swing.JLabel">
<component id="ed03a" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Use Equatable:"/>
<text value="Style"/>
</properties>
</component>
</children>
</grid>
<buttonGroups>
<group name="radionButtonGroup">
<member id="bb6d1"/>
<member id="317bf"/>
<member id="2b62e"/>
<member id="900f4"/>
</group>
</buttonGroups>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class GenerateBlocDialog extends DialogWrapper {

private final Listener listener;
private JTextField blocNameTextField;
private JCheckBox useEquatableCheckbox;
private JPanel contentPanel;
private JComboBox<String> style;

public GenerateBlocDialog(final Listener listener) {
super(null);
Expand All @@ -27,7 +27,16 @@ protected JComponent createCenterPanel() {
@Override
protected void doOKAction() {
super.doOKAction();
this.listener.onGenerateBlocClicked(blocNameTextField.getText(), useEquatableCheckbox.isSelected());
BlocTemplateType blocTemplateType;
final String selectedStyle = style.getSelectedItem().toString();
if (selectedStyle == "Equatable") {
blocTemplateType = BlocTemplateType.EQUATABLE;
} else if (selectedStyle == "Freezed") {
blocTemplateType = BlocTemplateType.FREEZED;
} else {
blocTemplateType = BlocTemplateType.BASIC;
}
this.listener.onGenerateBlocClicked(blocNameTextField.getText(), blocTemplateType);
}

@Nullable
Expand All @@ -37,6 +46,6 @@ public JComponent getPreferredFocusedComponent() {
}

public interface Listener {
void onGenerateBlocClicked(String blocName, boolean shouldUseEquatable);
void onGenerateBlocClicked(String blocName, BlocTemplateType blocTemplateType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class GenerateCubitAction : AnAction(), GenerateBlocDialog.Listener {
dialog.show()
}

override fun onGenerateBlocClicked(name: String?, useEquatable: Boolean) {
name?.let { name ->
val generators = CubitGeneratorFactory.getCubitGenerators(name, useEquatable)
override fun onGenerateBlocClicked(name: String?, blocTemplateType: BlocTemplateType) {
name?.let {
val generators = CubitGeneratorFactory.getCubitGenerators(it, blocTemplateType)
generate(generators)
}
}
Expand All @@ -39,12 +39,12 @@ 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
)
}
}
Expand All @@ -58,7 +58,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)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.bloc.intellij_generator_plugin.generator

import com.bloc.intellij_generator_plugin.action.BlocTemplateType
import com.fleshgrinder.extensions.kotlin.toLowerSnakeCase
import com.fleshgrinder.extensions.kotlin.toUpperCamelCase
import com.google.common.io.CharStreams
import com.fleshgrinder.extensions.kotlin.*
import org.apache.commons.lang.text.StrSubstitutor
import java.io.InputStreamReader
import java.lang.RuntimeException

abstract class BlocGenerator(private val name: String,
useEquatable: Boolean,
blocTemplateType: BlocTemplateType,
templateName: String) {

private val TEMPLATE_BLOC_PASCAL_CASE = "bloc_pascal_case"
Expand All @@ -18,11 +19,15 @@ 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 = if (useEquatable) "bloc_with_equatable" else "bloc_without_equatable"
val templateFolder = when (blocTemplateType) {
BlocTemplateType.BASIC -> "bloc_basic"
BlocTemplateType.EQUATABLE -> "bloc_equatable"
BlocTemplateType.FREEZED -> "bloc_freezed"
}
val resource = "/templates/$templateFolder/$templateName.dart.template"
val resourceAsStream = BlocGenerator::class.java.getResourceAsStream(resource)
templateString = CharStreams.toString(InputStreamReader(resourceAsStream, Charsets.UTF_8))
Expand All @@ -34,7 +39,7 @@ abstract class BlocGenerator(private val name: String,
abstract fun fileName(): String

fun generate(): String {
val substitutor = StrSubstitutor(templateValues)
val substitutor = StrSubstitutor(templateValues, "{{", "}}", '\\')
return substitutor.replace(templateString)
}

Expand All @@ -43,4 +48,4 @@ abstract class BlocGenerator(private val name: String,
fun snakeCase() = name.toLowerSnakeCase().replace("_bloc", "")

fun fileExtension() = "dart"
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.bloc.intellij_generator_plugin.generator

import com.bloc.intellij_generator_plugin.action.BlocTemplateType
import com.bloc.intellij_generator_plugin.generator.components.BlocEventGenerator
import com.bloc.intellij_generator_plugin.generator.components.BlocGenerator
import com.bloc.intellij_generator_plugin.generator.components.BlocStateGenerator

object BlocGeneratorFactory {
fun getBlocGenerators(name: String, useEquatable: Boolean): List<com.bloc.intellij_generator_plugin.generator.BlocGenerator> {
val bloc = BlocGenerator(name, useEquatable)
val event = BlocEventGenerator(name, useEquatable)
val state = BlocStateGenerator(name, useEquatable)
fun getBlocGenerators(name: String, blocTemplateType: BlocTemplateType): List<com.bloc.intellij_generator_plugin.generator.BlocGenerator> {
val bloc = BlocGenerator(name, blocTemplateType)
val event = BlocEventGenerator(name, blocTemplateType)
val state = BlocStateGenerator(name, blocTemplateType)
return listOf(bloc, event, state)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.bloc.intellij_generator_plugin.generator

import com.bloc.intellij_generator_plugin.action.BlocTemplateType
import com.fleshgrinder.extensions.kotlin.toLowerSnakeCase
import com.fleshgrinder.extensions.kotlin.toUpperCamelCase
import com.google.common.io.CharStreams
import com.fleshgrinder.extensions.kotlin.*
import org.apache.commons.lang.text.StrSubstitutor
import java.io.InputStreamReader
import java.lang.RuntimeException

abstract class CubitGenerator(private val name: String,
useEquatable: Boolean,
templateName: String) {
blocTemplateType: BlocTemplateType,
templateName: String) {

private val TEMPLATE_CUBIT_PASCAL_CASE = "cubit_pascal_case"
private val TEMPLATE_CUBIT_SNAKE_CASE = "cubit_snake_case"
Expand All @@ -18,11 +19,15 @@ 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 = if (useEquatable) "cubit_with_equatable" else "cubit_without_equatable"
val templateFolder = when (blocTemplateType) {
BlocTemplateType.BASIC -> "cubit_basic"
BlocTemplateType.EQUATABLE -> "cubit_equatable"
BlocTemplateType.FREEZED -> "cubit_freezed"
}
val resource = "/templates/$templateFolder/$templateName.dart.template"
val resourceAsStream = CubitGenerator::class.java.getResourceAsStream(resource)
templateString = CharStreams.toString(InputStreamReader(resourceAsStream, Charsets.UTF_8))
Expand All @@ -34,7 +39,7 @@ abstract class CubitGenerator(private val name: String,
abstract fun fileName(): String

fun generate(): String {
val substitutor = StrSubstitutor(templateValues)
val substitutor = StrSubstitutor(templateValues, "{{", "}}", '\\')
return substitutor.replace(templateString)
}

Expand All @@ -43,4 +48,4 @@ abstract class CubitGenerator(private val name: String,
fun snakeCase() = name.toLowerSnakeCase().replace("_cubit", "")

fun fileExtension() = "dart"
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.bloc.intellij_generator_plugin.generator

import com.bloc.intellij_generator_plugin.action.BlocTemplateType
import com.bloc.intellij_generator_plugin.generator.components.CubitGenerator
import com.bloc.intellij_generator_plugin.generator.components.CubitStateGenerator

object CubitGeneratorFactory {
fun getCubitGenerators(name: String, useEquatable: Boolean): List<com.bloc.intellij_generator_plugin.generator.CubitGenerator> {
val cubit = CubitGenerator(name, useEquatable)
val state = CubitStateGenerator(name, useEquatable)
fun getCubitGenerators(name: String, blocTemplateType: BlocTemplateType): List<com.bloc.intellij_generator_plugin.generator.CubitGenerator> {
val cubit = CubitGenerator(name, blocTemplateType)
val state = CubitStateGenerator(name, blocTemplateType)
return listOf(cubit, state)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.bloc.intellij_generator_plugin.generator.components

import com.bloc.intellij_generator_plugin.action.BlocTemplateType
import com.bloc.intellij_generator_plugin.generator.BlocGenerator

class BlocEventGenerator(
blocName: String,
blocShouldUseEquatable: Boolean
) : BlocGenerator(blocName, blocShouldUseEquatable, templateName = "bloc_event") {
blocName: String,
blocTemplateType: BlocTemplateType
) : BlocGenerator(blocName, blocTemplateType, templateName = "bloc_event") {

override fun fileName() = "${snakeCase()}_event.${fileExtension()}"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.bloc.intellij_generator_plugin.generator.components

import com.bloc.intellij_generator_plugin.action.BlocTemplateType
import com.bloc.intellij_generator_plugin.generator.BlocGenerator

class BlocGenerator(
name: String,
useEquatable: Boolean
) : BlocGenerator(name, useEquatable, templateName = "bloc") {
name: String,
blocTemplateType: BlocTemplateType
) : BlocGenerator(name, blocTemplateType, templateName = "bloc") {
override fun fileName() = "${snakeCase()}_bloc.${fileExtension()}"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.bloc.intellij_generator_plugin.generator.components

import com.bloc.intellij_generator_plugin.action.BlocTemplateType
import com.bloc.intellij_generator_plugin.generator.BlocGenerator

class BlocStateGenerator(
name: String,
useEquatable: Boolean
) : BlocGenerator(name, useEquatable, templateName = "bloc_state") {
name: String,
blocTemplateType: BlocTemplateType
) : BlocGenerator(name, blocTemplateType, templateName = "bloc_state") {
override fun fileName() = "${snakeCase()}_state.${fileExtension()}"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.bloc.intellij_generator_plugin.generator.components

import com.bloc.intellij_generator_plugin.action.BlocTemplateType
import com.bloc.intellij_generator_plugin.generator.CubitGenerator

class CubitGenerator(
name: String,
useEquatable: Boolean
) : CubitGenerator(name, useEquatable, templateName = "cubit") {
name: String,
blocTemplateType: BlocTemplateType
) : CubitGenerator(name, blocTemplateType, templateName = "cubit") {
override fun fileName() = "${snakeCase()}_cubit.${fileExtension()}"
}
}
Loading

0 comments on commit acad4ed

Please sign in to comment.