11import * as vscode from 'vscode' ;
2- import { generateDartModel } from '../utils/dartGenUtils ' ;
2+ import { generateSingleModel } from '../utils/singleGenUtils ' ;
33
44export function registerGenerateModelCommand ( context : vscode . ExtensionContext ) {
5- return vscode . commands . registerCommand ( 'neuma-api-dart.generateModel' , async ( ) => {
6- const jsonInput = await vscode . window . showInputBox ( {
7- placeHolder : 'Paste your JSON object here' ,
8- prompt : 'This should be a valid JSON object (e.g. {"name": "John"})' ,
9- validateInput : text => {
10- try {
11- JSON . parse ( text ) ;
12- return null ;
13- } catch ( e ) {
14- return 'Invalid JSON' ;
15- }
16- }
17- } ) ;
18-
19- if ( ! jsonInput ) return ;
20-
21- const modelType = await vscode . window . showQuickPick ( [ 'Request' , 'Response' ] , {
22- placeHolder : 'Select model type'
23- } ) ;
24-
25- if ( ! modelType ) return ;
26-
27- const classNameBase = await vscode . window . showInputBox ( {
28- placeHolder : 'Enter base class name (e.g. UserProfile, LoginAuth, ProductDetails)' ,
29- prompt : 'This will create the model class and organize it in a folder structure. For example:\n• "UserProfile" creates models/user_profile/user_profile_request.dart\n• "LoginAuth" creates models/login_auth/login_auth_response.dart\nUse PascalCase - it will be converted to snake_case for folders and files.'
30- } ) ;
31-
32- if ( ! classNameBase ) return ;
33-
34- const finalClassName = `${ classNameBase } ${ modelType } ` ;
35-
36- // Get extension configuration
37- const config = vscode . workspace . getConfiguration ( 'neuma-api-dart' ) ;
38- const baseFolder = config . get < string > ( 'defaultBaseFolder' , 'lib/models' ) ;
39- const generateSubfolders = config . get < boolean > ( 'generateSubfolders' , true ) ;
40-
41- // Convert PascalCase to snake_case for folder and file names (Dart convention)
42- const folderName = classNameBase . replace ( / ( [ A - Z ] ) / g, ( match , letter , index ) => {
43- return index === 0 ? letter . toLowerCase ( ) : '_' + letter . toLowerCase ( ) ;
44- } ) ;
45-
46- const fileName = `${ folderName } _${ modelType . toLowerCase ( ) } .dart` ;
47-
48- // Build path based on subfolder setting
49- const relativePath = generateSubfolders
50- ? `${ baseFolder } /${ folderName } /${ fileName } `
51- : `${ baseFolder } /${ fileName } ` ;
52-
53- const json = JSON . parse ( jsonInput ) ;
54-
55- // Create model generation options from config
56- const modelOptions = {
57- nullSafety : config . get < string > ( 'nullSafety' , 'auto' ) ,
58- generateJsonAnnotation : config . get < boolean > ( 'generateJsonAnnotation' , true ) ,
59- generateFromJson : config . get < boolean > ( 'generateFromJson' , true ) ,
60- generateToJson : config . get < boolean > ( 'generateToJson' , true ) ,
61- generateCopyWith : config . get < boolean > ( 'generateCopyWith' , false ) ,
62- generateEquatable : config . get < boolean > ( 'generateEquatable' , false ) ,
63- generateToString : config . get < boolean > ( 'generateToString' , false ) ,
64- useFreezed : config . get < boolean > ( 'useFreezed' , false ) ,
65- fieldCase : config . get < string > ( 'fieldCase' , 'camelCase' ) ,
66- addPartStatement : config . get < boolean > ( 'addPartStatement' , true )
67- } ;
68-
69- const dartCode = generateDartModel ( json , finalClassName , new Set ( Object . keys ( modelOptions ) ) ) ;
70-
71- // Get the workspace folder
72- const workspaceFolder = vscode . workspace . workspaceFolders ?. [ 0 ] ;
73-
74- if ( ! workspaceFolder ) {
75- // No workspace - fallback to untitled document
76- vscode . window . showWarningMessage ( 'No workspace folder found. Opening as untitled document.' ) ;
77- const doc = await vscode . workspace . openTextDocument ( {
78- content : dartCode ,
79- language : 'dart'
80- } ) ;
81- await vscode . window . showTextDocument ( doc ) ;
82- return ;
83- }
84-
85- try {
86- // Create the full file path
87- const fullPath = vscode . Uri . joinPath ( workspaceFolder . uri , relativePath ) ;
88-
89- // Create directories if they don't exist
90- const dirPath = generateSubfolders
91- ? vscode . Uri . joinPath ( workspaceFolder . uri , `${ baseFolder } /${ folderName } ` )
92- : vscode . Uri . joinPath ( workspaceFolder . uri , baseFolder ) ;
93- await vscode . workspace . fs . createDirectory ( dirPath ) ;
94-
95- // Write the file
96- const encoder = new TextEncoder ( ) ;
97- await vscode . workspace . fs . writeFile ( fullPath , encoder . encode ( dartCode ) ) ;
98-
99- // Open the created file
100- const doc = await vscode . workspace . openTextDocument ( fullPath ) ;
101- await vscode . window . showTextDocument ( doc ) ;
102-
103- vscode . window . showInformationMessage ( `Model created at: ${ relativePath } ` ) ;
104-
105- } catch ( error ) {
106- vscode . window . showErrorMessage ( `Failed to create file: ${ error } ` ) ;
107-
108- // Fallback: open in untitled document
109- const doc = await vscode . workspace . openTextDocument ( {
110- content : dartCode ,
111- language : 'dart'
112- } ) ;
113- vscode . window . showTextDocument ( doc ) ;
114- }
115- } ) ;
5+ return vscode . commands . registerCommand ( 'neuma-api-dart.generateModel' , generateSingleModel ) ;
1166}
0 commit comments