Skip to content

Commit

Permalink
Support multiple widgets in the same file - each gets its own file in…
Browse files Browse the repository at this point in the history
… widgetbook
  • Loading branch information
FirentisTFW committed Nov 16, 2024
1 parent d9ca442 commit 02d35ef
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 54 deletions.
48 changes: 24 additions & 24 deletions src/commands/generate_entries_for_directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from "vscode";
import { FileContentGeneratorFactory } from "../generators/file_content/factory";
import { PathGeneratorFactory } from "../generators/path/factory";
import { CustomKnobsProvider } from "../providers/custom_knobs_provider";
import { parseTextToClass } from "../util/dart_class_parser";
import { parseTextToClasses } from "../util/dart_class_parser";
import {
createDirectoryIfNotExists,
writeWidgetbookEntry,
Expand All @@ -27,32 +27,32 @@ async function generateWidgetbookEntriesForDirectory(
);
const fileContentString = new TextDecoder().decode(fileContent);

const clazz = parseTextToClass(fileContentString);
const classes = parseTextToClasses(fileContentString);

if (!clazz) return;
for (const clazz of classes) {
const pathGenerator = PathGeneratorFactory.create();
const customKnobsFilePath = pathGenerator.prepareCustomKnobsFilePath(
uri.path
);
const customKnobs = await new CustomKnobsProvider().getCustomKnobs(
customKnobsFilePath
);
const fileContentGenerator = FileContentGeneratorFactory.create(
clazz,
customKnobs
);

const pathGenerator = PathGeneratorFactory.create();
const customKnobsFilePath = pathGenerator.prepareCustomKnobsFilePath(
uri.path
);
const customKnobs = await new CustomKnobsProvider().getCustomKnobs(
customKnobsFilePath
);
const fileContentGenerator = FileContentGeneratorFactory.create(
clazz,
customKnobs
);
await createDirectoryIfNotExists(
pathGenerator.prepareWidgetbookWidgetsDirectoryPath(filePath)
);

await createDirectoryIfNotExists(
pathGenerator.prepareWidgetbookWidgetsDirectoryPath(filePath)
);

await writeWidgetbookEntry(
clazz,
filePath,
pathGenerator,
fileContentGenerator
);
await writeWidgetbookEntry(
clazz,
filePath,
pathGenerator,
fileContentGenerator
);
}
} else if (fileType === vscode.FileType.Directory) {
const subdirectoryPath = path.join(directoryPath, fileName);

Expand Down
56 changes: 28 additions & 28 deletions src/commands/generate_entry_for_widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from "vscode";
import { FileContentGeneratorFactory } from "../generators/file_content/factory";
import { PathGeneratorFactory } from "../generators/path/factory";
import { CustomKnobsProvider } from "../providers/custom_knobs_provider";
import { parseTextToClass } from "../util/dart_class_parser";
import { parseTextToClasses } from "../util/dart_class_parser";
import {
createDirectoryIfNotExists,
writeWidgetbookEntry,
Expand All @@ -19,33 +19,33 @@ async function generateWidgetbookEntryForWidgetInScope(): Promise<void> {
.slice(currentLineIndex)
.join("\n");

const clazz = parseTextToClass(fileContentFromCurrentLine);

if (!clazz) return;

const pathGenerator = PathGeneratorFactory.create();
const customKnobsFilePath = pathGenerator.prepareCustomKnobsFilePath(
activeEditor.document.uri.path
);
const customKnobs = await new CustomKnobsProvider().getCustomKnobs(
customKnobsFilePath
);
const fileContentGenerator = FileContentGeneratorFactory.create(
clazz,
customKnobs
);
const filePath = activeEditor.document.fileName;

await createDirectoryIfNotExists(
pathGenerator.prepareWidgetbookWidgetsDirectoryPath(filePath)
);

await writeWidgetbookEntry(
clazz,
filePath,
pathGenerator,
fileContentGenerator
);
const classes = parseTextToClasses(fileContentFromCurrentLine);

for (const clazz of classes) {
const pathGenerator = PathGeneratorFactory.create();
const customKnobsFilePath = pathGenerator.prepareCustomKnobsFilePath(
activeEditor.document.uri.path
);
const customKnobs = await new CustomKnobsProvider().getCustomKnobs(
customKnobsFilePath
);
const fileContentGenerator = FileContentGeneratorFactory.create(
clazz,
customKnobs
);
const filePath = activeEditor.document.fileName;

await createDirectoryIfNotExists(
pathGenerator.prepareWidgetbookWidgetsDirectoryPath(filePath)
);

await writeWidgetbookEntry(
clazz,
filePath,
pathGenerator,
fileContentGenerator
);
}
}

export { generateWidgetbookEntryForWidgetInScope };
28 changes: 26 additions & 2 deletions src/util/dart_class_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,35 @@ import {
DartClassField,
} from "../data/dart_class";

function parseTextToClass(text: string): DartClass | null {
function parseTextToClasses(text: string): Array<DartClass> {
const lines = text.split("\n").filter((line) => line !== "");

const classes: Array<DartClass> = [];

let i = 0;
while (i < lines.length) {
const line = lines[i];
if (doesLineContainClassDeclaration(line, lines[i + 1])) {
const endLineIndex =
lines.slice(i).findIndex((line) => line.startsWith("}")) + i;

const classLines = lines.slice(i, endLineIndex);
const clazz = parseTextToClass(classLines);
if (clazz) classes.push(clazz);
i = endLineIndex + 1;
} else {
i++;
}
}

return classes;
}

function parseTextToClass(lines: Array<string>): DartClass | null {
const className = parseLinesToClassName(lines);

if (className === "") return null;
// Discard private widgets.
if (className === "" || className.startsWith("_")) return null;

const classFields = parseLinesToClassFields(lines);
const constructors = parseLinesToConstructors(lines, className, classFields);
Expand Down Expand Up @@ -264,4 +287,5 @@ export {
parseLinesToConstructor,
parseLinesToConstructors,
parseTextToClass,
parseTextToClasses,
};

0 comments on commit 02d35ef

Please sign in to comment.