Bläddra i källkod

Modify for use simply quick class

Etheram68 5 år sedan
förälder
incheckning
d27f2992e3
3 ändrade filer med 105 tillägg och 8 borttagningar
  1. 42 4
      src/basicInput.ts
  2. 4 4
      src/extension.ts
  3. 59 0
      src/template.ts

+ 42 - 4
src/basicInput.ts

@@ -11,7 +11,7 @@
 /* ************************************************************************** */
 
 import * as vscode from 'vscode';
-import { getTemplate } from './template';
+import { getTemplate, getTemplateFull } from './template';
 
 export async function createQuickClass() {
 	let name = await vscode.window.showInputBox({
@@ -39,7 +39,7 @@ export async function createQuickClass() {
 
 		vscode.workspace.applyEdit(wsedit);
 
-		getTemplate(name, filePathCpp, filePathHpp )
+		getTemplate( name, filePathCpp, filePathHpp )
 	}
 }
 
@@ -55,8 +55,46 @@ export async function createClass() {
 			}
 		}
 	});
+
+	let value1: string | undefined;
+	let value2: string | undefined;
+	let value3: string | undefined;
+	let valueArray = [];
+	let typeArray = [];
+
+	for ( ; value1 !== 'exit' || !value1; )
+	{
+		value1 = await vscode.window.showInputBox({
+			prompt: "Enter your Type Name, (Press 'exit' for leave.)",
+			placeHolder: "Type",
+			validateInput: (text: string): string | undefined => {
+				if (!text){
+					return 'You must enter a Type or Press \'exit\' if finish';
+				} else {
+					return undefined;
+				}
+			}
+		})
+		if (value1 != 'exit')
+			value2 = await vscode.window.showInputBox({
+				prompt: "Enter your variable Name",
+				placeHolder: "Variable Name",
+				validateInput: (text: string): string | undefined => {
+					if (!text){
+						return 'You must enter a Variable';
+					} else {
+						return undefined;
+					}
+				}
+			})
+		if (value1 !== 'exit')
+		{
+			typeArray.push(value1?.toLowerCase());
+			valueArray.push(value2?.toLowerCase());
+		}
+	}
 	if (name !== undefined){
-		name = name.charAt(0).toUpperCase() + name.slice(1).toLowerCase
+		name = name.charAt(0).toUpperCase() + name.slice(1).toLowerCase()
 
 		const wsedit = new vscode.WorkspaceEdit();
 		const wsPath = vscode.workspace.workspaceFolders![0].uri.fsPath;
@@ -69,6 +107,6 @@ export async function createClass() {
 
 		vscode.workspace.applyEdit(wsedit);
 
-		getTemplate(name, filePathCpp, filePathHpp )
+		getTemplateFull( name, filePathCpp, filePathHpp, valueArray, typeArray )
 	}
 }

+ 4 - 4
src/extension.ts

@@ -15,7 +15,7 @@ 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> } = {
+		/*const options: { [key: string]: (context: ExtensionContext) => Promise<void> } = {
 			createQuickClass,
 			createClass,
 		};
@@ -23,12 +23,12 @@ export function activate(context: ExtensionContext) {
 		quickPick.items = Object.keys(options).map(label => ({ label }));
 		quickPick.onDidChangeSelection(selection => {
 			if (selection[0]) {
-				options[selection[0].label](context)
-					.catch(console.error);
+				options[selection[0].label](context).catch(console.error);
 			}
 		});
 		quickPick.onDidHide(() => quickPick.dispose());
-		quickPick.show();
+		quickPick.show();*/
+		createQuickClass();
 	}));
 }
 

+ 59 - 0
src/template.ts

@@ -59,3 +59,62 @@ export const getTemplate = ( name: string, filePathCpp: vscode.Uri , filePathHpp
 
 		vscode.window.showInformationMessage("Files : " + name + ".class.cpp, " + name + ".class.hpp created !");
 }
+
+
+export const getTemplateFull = ( name: string, filePathCpp: vscode.Uri, filePathHpp: vscode.Uri, valueArray: Array<string | undefined>, typeArray: Array<string | undefined> ) => {
+	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";
+
+	for (var i = 0; i < valueArray.length; i++)
+	{
+		classhpp += "		";
+		if (typeArray[i] == 'string')
+			classhpp += "std::" + typeArray[i];
+		else
+			classhpp += typeArray[i];
+		classhpp += "		_" + valueArray[i] + ";\n";
+	}
+
+		classhpp += "};\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 !");
+}