Sfoglia il codice sorgente

Add multi choice for creet function

Etheram68 5 anni fa
parent
commit
5dda822f9f
3 ha cambiato i file con 171 aggiunte e 81 eliminazioni
  1. 74 0
      src/basicInput.ts
  2. 36 81
      src/extension.ts
  3. 61 0
      src/template.ts

+ 74 - 0
src/basicInput.ts

@@ -0,0 +1,74 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   basicInput.ts                                      :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: frfrey <frfrey@student.42lyon.fr>          +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/07/22 10:21:45 by frfrey            #+#    #+#             */
+/*   Updated: 2020/07/22 10:21:45 by frfrey           ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+import * as vscode from 'vscode';
+import { getTemplate } from './template';
+
+export async function createQuickClass() {
+	let name = await vscode.window.showInputBox({
+		prompt: "Enter your Class Name",
+		placeHolder: "ClassName",
+		validateInput: (text: string): string | undefined => {
+			if (!text){
+				return 'You must enter a name';
+			} else {
+				return undefined;
+			}
+		}
+	});
+	if (name !== undefined){
+		name = name.charAt(0).toUpperCase() + name.slice(1).toLowerCase()
+
+		const wsedit = new vscode.WorkspaceEdit();
+		const wsPath = vscode.workspace.workspaceFolders![0].uri.fsPath;
+
+		const filePathCpp = vscode.Uri.file(wsPath + '/' + name + '.Class' + '.cpp');
+		const filePathHpp = vscode.Uri.file(wsPath + '/' + name + '.Class' + '.hpp');
+
+		wsedit.createFile(filePathCpp, { ignoreIfExists: true });
+		wsedit.createFile(filePathHpp, { ignoreIfExists: true });
+
+		vscode.workspace.applyEdit(wsedit);
+
+		getTemplate(name, filePathCpp, filePathHpp )
+	}
+}
+
+export async function createClass() {
+	let name = await vscode.window.showInputBox({
+		prompt: "Enter your Class Name",
+		placeHolder: "ClassName",
+		validateInput: (text: string): string | undefined => {
+			if (!text){
+				return 'You must enter a name';
+			} else {
+				return undefined;
+			}
+		}
+	});
+	if (name !== undefined){
+		name = name.charAt(0).toUpperCase() + name.slice(1).toLowerCase
+
+		const wsedit = new vscode.WorkspaceEdit();
+		const wsPath = vscode.workspace.workspaceFolders![0].uri.fsPath;
+
+		const filePathCpp = vscode.Uri.file(wsPath + '/' + name + '.Class' + '.cpp');
+		const filePathHpp = vscode.Uri.file(wsPath + '/' + name + '.Class' + '.hpp');
+
+		wsedit.createFile(filePathCpp, { ignoreIfExists: true });
+		wsedit.createFile(filePathHpp, { ignoreIfExists: true });
+
+		vscode.workspace.applyEdit(wsedit);
+
+		getTemplate(name, filePathCpp, filePathHpp )
+	}
+}

+ 36 - 81
src/extension.ts

@@ -1,81 +1,36 @@
-import * as vscode from 'vscode';
-import * as fs from 'fs';
-
-
-export function activate(context: vscode.ExtensionContext) {
-
-
-	let disposable = vscode.commands.registerCommand('canonicalclass.makeclass', async function (){
-		let name = await vscode.window.showInputBox({
-			prompt: "Enter your Class Name",
-			validateInput: (text: string): string | undefined => {
-				if (!text){
-					return 'You must enter a name';
-				} else {
-					return undefined;
-				}
-			}
-		})
-		if (name !== undefined){
-			name = name.charAt(0).toUpperCase() + name.slice(1)
-
-			const wsedit = new vscode.WorkspaceEdit();
-			const wsPath = vscode.workspace.workspaceFolders![0].uri.fsPath;
-
-			const filePathCpp = vscode.Uri.file(wsPath + '/' + name + '.Class' + '.cpp');
-			const filePathHpp = vscode.Uri.file(wsPath + '/' + name + '.Class' + '.hpp');
-
-			wsedit.createFile(filePathCpp, { ignoreIfExists: true });
-			wsedit.createFile(filePathHpp, { ignoreIfExists: true });
-
-			vscode.workspace.applyEdit(wsedit);
-
-			let len = name.length;
-			len += 23;
-			len = 80 - len - 1
-			let star = "*"
-			for (; len > 0; len--)
-			{
-				star += "*"
-			}
-
-			let classcpp =  "#include \"" + name + ".Class" + ".hpp\"\n\n" +
-							name + "::" + name + "() {}\n\n" +
-							name + "::" + name + "( const " + name + " & object ) {}\n\n" +
-							name + "::~" + name + "()\n{\n	std::cout << \"Destructor called\" << std::endl;\n}\n\n" +
-							name + " &		" + name + "::operator=( " + name + " const & rhs )\n{\n	//if ( this != &rhs )\n" +
-							"		//this->_value = rhs.getValue();\n" +
-							"	return *this;\n}\n\n" +
-							"std::ostream &		operator<<( std::ostream & o, " + name + " const & i )\n" +
-							"{\n	//o << \"Value = \" << i.getValue();\n	return o;\n}\n\n" +
-							"\n/* ************************************************************************** */";
-
-			let classhpp =	"#ifndef " + name.toUpperCase() + "_CLASS" + "_HPP\n" +
-							"# define " + name.toUpperCase() + "_CLASS" + "_HPP\n\n" +
-							"# include <iostream>\n\n" +
-							"class " + name + "\n{\n" +
-							"\n" +
-							"	public:\n" +
-							"\n" +
-							"		" + name + "();\n" +
-							"		" + name + "( " + name + " const & src );\n" +
-							"		~" + name + "();\n" +
-							"\n" +
-							"		" + name + " &		operator=( " + name + " const & rhs );"+
-							"\n\n" +
-							"	private:\n" +
-							"};\n\n" +
-							"std::ostream &		operator<<( std::ostream & o, " + name + " const & i );"+
-							"\n\n" +
-							"#endif /* *" + star + " " + name.toUpperCase() + "_CLASS_H */";
-			fs.writeFile(filePathCpp.fsPath, classcpp, function (err: any) { if (err) return console.log(err); });
-			fs.writeFile(filePathHpp.fsPath, classhpp, function (err: any) { if (err) return console.log(err); });
-			vscode.window.showInformationMessage("Files : " + name + ".class.cpp, " + name + ".class.hpp created !");
-		}
-	});
-
-	context.subscriptions.push(disposable);
-}
-
-// this method is called when your extension is deactivated
-export function deactivate() {}
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   extension.ts                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: frfrey <frfrey@student.42lyon.fr>          +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/07/22 10:02:46 by frfrey            #+#    #+#             */
+/*   Updated: 2020/07/22 10:02:46 by frfrey           ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+import { window, commands, ExtensionContext } from 'vscode';
+import { createQuickClass, createClass } from './basicInput';
+
+export function activate(context: ExtensionContext) {
+	context.subscriptions.push(commands.registerCommand('canonicalclass.makeclass', async () => {
+		const options: { [key: string]: (context: ExtensionContext) => Promise<void> } = {
+			createQuickClass,
+			createClass,
+		};
+		const quickPick = window.createQuickPick();
+		quickPick.items = Object.keys(options).map(label => ({ label }));
+		quickPick.onDidChangeSelection(selection => {
+			if (selection[0]) {
+				options[selection[0].label](context)
+					.catch(console.error);
+			}
+		});
+		quickPick.onDidHide(() => quickPick.dispose());
+		quickPick.show();
+	}));
+}
+
+// this method is called when your extension is deactivated
+export function deactivate() {}

+ 61 - 0
src/template.ts

@@ -0,0 +1,61 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   template.ts                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: frfrey <frfrey@student.42lyon.fr>          +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/07/22 10:41:27 by frfrey            #+#    #+#             */
+/*   Updated: 2020/07/22 10:41:27 by frfrey           ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+import * as vscode from 'vscode';
+import * as fs from 'fs';
+
+export const getTemplate = ( name: string, filePathCpp: vscode.Uri , filePathHpp: vscode.Uri  ) => {
+		let len = name.length;
+		len += 23;
+		len = 80 - len - 1
+		let star = "*"
+		for (; len > 0; len--)
+		{
+			star += "*"
+		}
+
+		let classcpp =  "#include \"" + name + ".Class" + ".hpp\"\n\n" +
+						name + "::" + name + "()\n{\n}\n\n" +
+						name + "::" + name + "( const " + name + " & object )\n{\n}\n\n" +
+						name + "::~" + name + "()\n{\n	std::cout << \"Destructor called\" << std::endl;\n}\n\n" +
+						name + " &		" + name + "::operator=( " + name + " const & rhs )\n{\n	//if ( this != &rhs )\n" +
+						"		//this->_value = rhs.getValue();\n" +
+						"	return *this;\n}\n\n" +
+						"std::ostream &		operator<<( std::ostream & o, " + name + " const & i )\n" +
+						"{\n	//o << \"Value = \" << i.getValue();\n	return o;\n}\n\n" +
+						"\n/* ************************************************************************** */";
+
+		let classhpp =	"#ifndef " + name.toUpperCase() + "_CLASS" + "_HPP\n" +
+						"# define " + name.toUpperCase() + "_CLASS" + "_HPP\n\n" +
+						"# include <iostream>\n\n" +
+						"class " + name + "\n{\n" +
+						"\n" +
+						"	public:\n" +
+						"\n" +
+						"		" + name + "();\n" +
+						"		" + name + "( " + name + " const & src );\n" +
+						"		~" + name + "();\n" +
+						"\n" +
+						"		" + name + " &		operator=( " + name + " const & rhs );"+
+						"\n\n" +
+						"	private:\n" +
+						"};\n\n" +
+						"std::ostream &		operator<<( std::ostream & o, " + name + " const & i );"+
+						"\n\n" +
+						"#endif /* *" + star + " " + name.toUpperCase() + "_CLASS_H */";
+
+
+		fs.writeFile(filePathCpp.fsPath, classcpp, function (err: any) { if (err) return console.log(err); });
+		fs.writeFile(filePathHpp.fsPath, classhpp, function (err: any) { if (err) return console.log(err); });
+
+		vscode.window.showInformationMessage("Files : " + name + ".class.cpp, " + name + ".class.hpp created !");
+}