From 02d35ef42b706dd1ee7e0165da8d3db444e9c99b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jakubowski?= Date: Sat, 16 Nov 2024 11:03:19 +0100 Subject: [PATCH] Support multiple widgets in the same file - each gets its own file in widgetbook --- .../generate_entries_for_directory.ts | 48 ++++++++-------- src/commands/generate_entry_for_widget.ts | 56 +++++++++---------- src/util/dart_class_parser.ts | 28 +++++++++- 3 files changed, 78 insertions(+), 54 deletions(-) diff --git a/src/commands/generate_entries_for_directory.ts b/src/commands/generate_entries_for_directory.ts index f459c97..edcb5ea 100644 --- a/src/commands/generate_entries_for_directory.ts +++ b/src/commands/generate_entries_for_directory.ts @@ -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, @@ -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); diff --git a/src/commands/generate_entry_for_widget.ts b/src/commands/generate_entry_for_widget.ts index ca8f494..b37b8f0 100644 --- a/src/commands/generate_entry_for_widget.ts +++ b/src/commands/generate_entry_for_widget.ts @@ -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, @@ -19,33 +19,33 @@ async function generateWidgetbookEntryForWidgetInScope(): Promise { .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 }; diff --git a/src/util/dart_class_parser.ts b/src/util/dart_class_parser.ts index 5ae8c7f..4f4c7dc 100644 --- a/src/util/dart_class_parser.ts +++ b/src/util/dart_class_parser.ts @@ -6,12 +6,35 @@ import { DartClassField, } from "../data/dart_class"; -function parseTextToClass(text: string): DartClass | null { +function parseTextToClasses(text: string): Array { const lines = text.split("\n").filter((line) => line !== ""); + const classes: Array = []; + + 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): 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); @@ -264,4 +287,5 @@ export { parseLinesToConstructor, parseLinesToConstructors, parseTextToClass, + parseTextToClasses, };