|
| 1 | +// The module 'vscode' contains the VS Code extensibility API |
| 2 | +// Import the module and reference it with the alias vscode in your code below |
| 3 | +import * as vscode from 'vscode'; |
| 4 | +import * as path from 'path'; |
| 5 | +import * as fs from 'fs'; |
| 6 | + |
| 7 | +// this method is called when your extension is activated |
| 8 | +// your extension is activated the very first time the command is executed |
| 9 | +export function activate(context: vscode.ExtensionContext) { |
| 10 | + |
| 11 | + // Use the console to output diagnostic information (console.log) and errors (console.error) |
| 12 | + // This line of code will only be executed once when your extension is activated |
| 13 | + console.log('Congratulations, your extension "flutter-stateful-widget-generator" is now active!'); |
| 14 | + |
| 15 | + // The command has been defined in the package.json file |
| 16 | + // Now provide the implementation of the command with registerCommand |
| 17 | + // The commandId parameter must match the command field in package.json |
| 18 | + let disposable = vscode.commands.registerCommand('extension.create', () => { |
| 19 | + // The code you place here will be executed every time your command is executed |
| 20 | + |
| 21 | + // Display a message box to the user |
| 22 | + var options: vscode.InputBoxOptions = { |
| 23 | + prompt: "Name the widget", |
| 24 | + placeHolder: "ex: MyWidget" |
| 25 | + }; |
| 26 | + vscode.window.showInputBox(options).then((x) => { |
| 27 | + if (x !== undefined && String(x) !== '') { |
| 28 | + var dialogoptions: vscode.OpenDialogOptions = { |
| 29 | + canSelectFiles: false, |
| 30 | + canSelectFolders: true, |
| 31 | + canSelectMany: false, |
| 32 | + openLabel: 'Create here' |
| 33 | + }; |
| 34 | + vscode.window.showOpenDialog(dialogoptions).then((y) => { |
| 35 | + if (y !== undefined && String(y) !== '') { |
| 36 | + var folderPath = y[0]; |
| 37 | + var widgetName = String(x); |
| 38 | + var i: number; |
| 39 | + var fileName = ''; |
| 40 | + for (i = 0; i < x.length; i++) { |
| 41 | + var t = x[i]; |
| 42 | + if (i === 0) { |
| 43 | + t = x[i].toLocaleLowerCase(); |
| 44 | + } |
| 45 | + fileName += t; |
| 46 | + } |
| 47 | + fileName += '.dart'; |
| 48 | + var fullPath = path.join(folderPath.toString(), fileName); |
| 49 | + var theu = vscode.Uri.parse(fullPath); |
| 50 | + fs.exists(theu.fsPath, (exists) => { |
| 51 | + if (exists) { |
| 52 | + vscode.window.showErrorMessage(`Widget Generator: File Exists -> ${fileName} `); |
| 53 | + } else { |
| 54 | + var content = |
| 55 | + ` |
| 56 | +import 'package:flutter/material.dart'; |
| 57 | +
|
| 58 | +class ${widgetName} extends StatefulWidget { |
| 59 | + @override |
| 60 | + ${widgetName}View createState() { |
| 61 | + return ${widgetName}View(); |
| 62 | + } |
| 63 | +} |
| 64 | +
|
| 65 | +class ${widgetName}View extends ${widgetName}State { |
| 66 | + @override |
| 67 | + Widget build(BuildContext context) { |
| 68 | + // TODO: implement build |
| 69 | + return Text('${widgetName}'); |
| 70 | + } |
| 71 | +} |
| 72 | +
|
| 73 | +abstract class ${widgetName}State extends State<${widgetName}> { |
| 74 | + // TODO: implement state |
| 75 | +} |
| 76 | +`; |
| 77 | + |
| 78 | + fs.writeFile(theu.fsPath, content, function (err) { |
| 79 | + if (err) { |
| 80 | + console.log(err); |
| 81 | + } |
| 82 | + else { |
| 83 | + console.log('Write operation complete.'); |
| 84 | + vscode.workspace.openTextDocument(theu.fsPath).then(doc => { |
| 85 | + vscode.window.showTextDocument(doc); |
| 86 | + }); |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + context.subscriptions.push(disposable); |
| 98 | +} |
| 99 | + |
| 100 | +// this method is called when your extension is deactivated |
| 101 | +export function deactivate() { } |
0 commit comments