Skip to content

Commit

Permalink
feat(vscode): support sealed classes
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Aug 6, 2023
1 parent eb41c06 commit e12d1ef
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 36 deletions.
12 changes: 11 additions & 1 deletion extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
"default": true,
"description": "Whether to create a bloc directory when creating a new bloc."
},
"bloc.newBlocTemplate.useSealedClasses": {
"type": "boolean",
"default": true,
"description": "Whether to use sealed classes when creating a new bloc."
},
"bloc.newCubitTemplate.type": {
"type": "string",
"default": "auto",
Expand All @@ -84,6 +89,11 @@
"type": "boolean",
"default": true,
"description": "Whether to create a cubit directory when creating a new cubit."
},
"bloc.newCubitTemplate.useSealedClasses": {
"type": "boolean",
"default": true,
"description": "Whether to use sealed classes when creating a new cubit."
}
}
}
Expand All @@ -96,7 +106,7 @@
},
{
"command": "extension.new-cubit",
"title": "Cubit: New Cubit",
"title": "Bloc: New Cubit",
"icon": "assets/logo.png"
},
{
Expand Down
28 changes: 21 additions & 7 deletions extensions/vscode/src/commands/new-bloc.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,22 @@ async function generateBlocCode(
if (!existsSync(blocDirectoryPath)) {
await createDirectory(blocDirectoryPath);
}

const useSealedClasses = workspace
.getConfiguration("bloc")
.get<boolean>("newBlocTemplate.useSealedClasses", true);
await Promise.all([
createBlocEventTemplate(blocName, blocDirectoryPath, type),
createBlocStateTemplate(blocName, blocDirectoryPath, type),
createBlocEventTemplate(
blocName,
blocDirectoryPath,
type,
useSealedClasses
),
createBlocStateTemplate(
blocName,
blocDirectoryPath,
type,
useSealedClasses
),
createBlocTemplate(blocName, blocDirectoryPath, type),
]);
}
Expand All @@ -109,7 +121,8 @@ function createDirectory(targetDirectory: string): Promise<void> {
function createBlocEventTemplate(
blocName: string,
targetDirectory: string,
type: BlocType
type: BlocType,
useSealedClasses: boolean
) {
const snakeCaseBlocName = changeCase.snakeCase(blocName);
const targetPath = `${targetDirectory}/${snakeCaseBlocName}_event.dart`;
Expand All @@ -119,7 +132,7 @@ function createBlocEventTemplate(
return new Promise<void>(async (resolve, reject) => {
writeFile(
targetPath,
getBlocEventTemplate(blocName, type),
getBlocEventTemplate(blocName, type, useSealedClasses),
"utf8",
(error) => {
if (error) {
Expand All @@ -135,7 +148,8 @@ function createBlocEventTemplate(
function createBlocStateTemplate(
blocName: string,
targetDirectory: string,
type: BlocType
type: BlocType,
useSealedClasses: boolean
) {
const snakeCaseBlocName = changeCase.snakeCase(blocName);
const targetPath = `${targetDirectory}/${snakeCaseBlocName}_state.dart`;
Expand All @@ -145,7 +159,7 @@ function createBlocStateTemplate(
return new Promise<void>(async (resolve, reject) => {
writeFile(
targetPath,
getBlocStateTemplate(blocName, type),
getBlocStateTemplate(blocName, type, useSealedClasses),
"utf8",
(error) => {
if (error) {
Expand Down
10 changes: 7 additions & 3 deletions extensions/vscode/src/commands/new-cubit.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ async function generateCubitCode(
await createDirectory(cubitDirectoryPath);
}

const useSealedClasses = workspace
.getConfiguration("bloc")
.get<boolean>("newCubitTemplate.useSealedClasses", true);
await Promise.all([
createCubitStateTemplate(cubitName, cubitDirectoryPath, type),
createCubitStateTemplate(cubitName, cubitDirectoryPath, type, useSealedClasses),
createCubitTemplate(cubitName, cubitDirectoryPath, type),
]);
}
Expand All @@ -104,7 +107,8 @@ function createDirectory(targetDirectory: string): Promise<void> {
function createCubitStateTemplate(
cubitName: string,
targetDirectory: string,
type: BlocType
type: BlocType,
useSealedClasses: boolean
) {
const snakeCaseCubitName = changeCase.snakeCase(cubitName);
const targetPath = `${targetDirectory}/${snakeCaseCubitName}_state.dart`;
Expand All @@ -114,7 +118,7 @@ function createCubitStateTemplate(
return new Promise<void>(async (resolve, reject) => {
writeFile(
targetPath,
getCubitStateTemplate(cubitName, type),
getCubitStateTemplate(cubitName, type, useSealedClasses),
"utf8",
(error) => {
if (error) {
Expand Down
26 changes: 19 additions & 7 deletions extensions/vscode/src/templates/bloc-event.template.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import * as changeCase from "change-case";
import { BlocType } from "../utils";

export function getBlocEventTemplate(blocName: string, type: BlocType): string {
export function getBlocEventTemplate(
blocName: string,
type: BlocType,
useSealedClasses: boolean
): string {
switch (type) {
case BlocType.Freezed:
return getFreezedBlocEvent(blocName);
case BlocType.Equatable:
return getEquatableBlocEventTemplate(blocName);
return getEquatableBlocEventTemplate(blocName, useSealedClasses);
default:
return getDefaultBlocEventTemplate(blocName);
return getDefaultBlocEventTemplate(blocName, useSealedClasses);
}
}

function getEquatableBlocEventTemplate(blocName: string): string {
function getEquatableBlocEventTemplate(
blocName: string,
useSealedClasses: boolean
): string {
const classPrefix = useSealedClasses ? "sealed" : "abstract";
const pascalCaseBlocName = changeCase.pascalCase(blocName);
const snakeCaseBlocName = changeCase.snakeCase(blocName);
return `part of '${snakeCaseBlocName}_bloc.dart';
abstract class ${pascalCaseBlocName}Event extends Equatable {
${classPrefix} class ${pascalCaseBlocName}Event extends Equatable {
const ${pascalCaseBlocName}Event();
@override
Expand All @@ -26,13 +34,17 @@ abstract class ${pascalCaseBlocName}Event extends Equatable {
`;
}

function getDefaultBlocEventTemplate(blocName: string): string {
function getDefaultBlocEventTemplate(
blocName: string,
useSealedClasses: boolean
): string {
const classPrefix = useSealedClasses ? "sealed" : "abstract";
const pascalCaseBlocName = changeCase.pascalCase(blocName);
const snakeCaseBlocName = changeCase.snakeCase(blocName);
return `part of '${snakeCaseBlocName}_bloc.dart';
@immutable
abstract class ${pascalCaseBlocName}Event {}
${classPrefix} class ${pascalCaseBlocName}Event {}
`;
}

Expand Down
32 changes: 23 additions & 9 deletions extensions/vscode/src/templates/bloc-state.template.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,56 @@
import * as changeCase from "change-case";
import { BlocType } from "../utils";

export function getBlocStateTemplate(blocName: string, type: BlocType): string {
export function getBlocStateTemplate(
blocName: string,
type: BlocType,
useSealedClasses: boolean
): string {
switch (type) {
case BlocType.Freezed:
return getFreezedBlocStateTemplate(blocName);
case BlocType.Equatable:
return getEquatableBlocStateTemplate(blocName);
return getEquatableBlocStateTemplate(blocName, useSealedClasses);
default:
return getDefaultBlocStateTemplate(blocName);
return getDefaultBlocStateTemplate(blocName, useSealedClasses);
}
}

function getEquatableBlocStateTemplate(blocName: string): string {
function getEquatableBlocStateTemplate(
blocName: string,
useSealedClasses: boolean
): string {
const classPrefix = useSealedClasses ? "sealed" : "abstract";
const subclassPrefix = useSealedClasses ? "final " : "";
const pascalCaseBlocName = changeCase.pascalCase(blocName);
const snakeCaseBlocName = changeCase.snakeCase(blocName);
return `part of '${snakeCaseBlocName}_bloc.dart';
abstract class ${pascalCaseBlocName}State extends Equatable {
${classPrefix} class ${pascalCaseBlocName}State extends Equatable {
const ${pascalCaseBlocName}State();
@override
List<Object> get props => [];
}
class ${pascalCaseBlocName}Initial extends ${pascalCaseBlocName}State {}
${subclassPrefix}class ${pascalCaseBlocName}Initial extends ${pascalCaseBlocName}State {}
`;
}

function getDefaultBlocStateTemplate(blocName: string): string {
function getDefaultBlocStateTemplate(
blocName: string,
useSealedClasses: boolean
): string {
const classPrefix = useSealedClasses ? "sealed" : "abstract";
const subclassPrefix = useSealedClasses ? "final " : "";
const pascalCaseBlocName = changeCase.pascalCase(blocName);
const snakeCaseBlocName = changeCase.snakeCase(blocName);
return `part of '${snakeCaseBlocName}_bloc.dart';
@immutable
abstract class ${pascalCaseBlocName}State {}
${classPrefix} class ${pascalCaseBlocName}State {}
class ${pascalCaseBlocName}Initial extends ${pascalCaseBlocName}State {}
${subclassPrefix}class ${pascalCaseBlocName}Initial extends ${pascalCaseBlocName}State {}
`;
}

Expand Down
29 changes: 20 additions & 9 deletions extensions/vscode/src/templates/cubit-state.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,54 @@ import { BlocType } from "../utils";

export function getCubitStateTemplate(
cubitName: string,
type: BlocType
type: BlocType,
useSealedClasses: boolean
): string {
switch (type) {
case BlocType.Freezed:
return getFreezedCubitStateTemplate(cubitName);
case BlocType.Equatable:
return getEquatableCubitStateTemplate(cubitName);
return getEquatableCubitStateTemplate(cubitName, useSealedClasses);
default:
return getDefaultCubitStateTemplate(cubitName);
return getDefaultCubitStateTemplate(cubitName, useSealedClasses);
}
}

function getEquatableCubitStateTemplate(cubitName: string): string {
function getEquatableCubitStateTemplate(
cubitName: string,
useSealedClasses: boolean
): string {
const classPrefix = useSealedClasses ? "sealed" : "abstract";
const subclassPrefix = useSealedClasses ? "final " : "";
const pascalCaseCubitName = changeCase.pascalCase(cubitName);
const snakeCaseCubitName = changeCase.snakeCase(cubitName);
return `part of '${snakeCaseCubitName}_cubit.dart';
abstract class ${pascalCaseCubitName}State extends Equatable {
${classPrefix} class ${pascalCaseCubitName}State extends Equatable {
const ${pascalCaseCubitName}State();
@override
List<Object> get props => [];
}
class ${pascalCaseCubitName}Initial extends ${pascalCaseCubitName}State {}
${subclassPrefix}class ${pascalCaseCubitName}Initial extends ${pascalCaseCubitName}State {}
`;
}

function getDefaultCubitStateTemplate(cubitName: string): string {
function getDefaultCubitStateTemplate(
cubitName: string,
useSealedClasses: boolean
): string {
const classPrefix = useSealedClasses ? "sealed" : "abstract";
const subclassPrefix = useSealedClasses ? "final " : "";
const pascalCaseCubitName = changeCase.pascalCase(cubitName);
const snakeCaseCubitName = changeCase.snakeCase(cubitName);
return `part of '${snakeCaseCubitName}_cubit.dart';
@immutable
abstract class ${pascalCaseCubitName}State {}
${classPrefix} class ${pascalCaseCubitName}State {}
class ${pascalCaseCubitName}Initial extends ${pascalCaseCubitName}State {}
${subclassPrefix}class ${pascalCaseCubitName}Initial extends ${pascalCaseCubitName}State {}
`;
}

Expand Down

0 comments on commit e12d1ef

Please sign in to comment.