Pārlūkot izejas kodu

Add canonical class for template

Etheram68 5 gadi atpakaļ
vecāks
revīzija
f2f39d3c6f
5 mainītis faili ar 85 papildinājumiem un 29 dzēšanām
  1. 6 0
      README.md
  2. 1 1
      package.json
  3. 23 14
      src/basicInput.ts
  4. 16 14
      src/extension.ts
  5. 39 0
      src/template.ts

+ 6 - 0
README.md

@@ -33,12 +33,18 @@ Enter the Class Name
  src="https://raw.githubusercontent.com/Etheram68/Header-Cpp-Class/master/tuto2.gif"
  width=680>
 
+ ### Create file Cpp and Hpp with Header
+  Enter on the box "MyTemplateName.tpp"
+
 ## Issues
 
 To report a bug or ask for a feature, please open a [Github issue](https://github.com/Etheram68/Header-Cpp-Class/issues).
 
 ## Release Notes
 
+## 0.3.0
+Add canonical Class for template (file extension .tpp)
+
 ### 0.2.8
 Modify Template
 

+ 1 - 1
package.json

@@ -2,7 +2,7 @@
 	"name": "canonicalclasscpp",
 	"displayName": "42 Canonical Class CPP",
 	"description": "This extension is to automatically create a canonical hpp and cpp file",
-	"version": "0.2.8",
+	"version": "0.3.0",
 	"publisher": "frfreyCanonicalClassCPP",
 	"icon": "42.png",
 	"repository": {

+ 23 - 14
src/basicInput.ts

@@ -13,22 +13,10 @@
 import * as vscode from 'vscode';
 import * as fs from 'fs';
 
-import { getTemplate, getTemplateFull } from './template';
+import { getTemplate, getTemplateFull, getTemplateTpp } from './template';
 import { getHeadercpp, getHeaderhpp } from './header';
 
-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;
-			}
-		}
-	});
+export async function createQuickClass(name: string | undefined) {
 
 	if (name !== undefined){
 		name = name.charAt(0).toUpperCase() + name.slice(1)
@@ -51,6 +39,27 @@ export async function createQuickClass() {
 		await getHeaderhpp( command, filePathHpp );
 	}
 }
+
+export async function createTemplateFile(name: string | undefined){
+	if (name !== undefined){
+		name = name.charAt(0).toUpperCase() + name.slice(1);
+
+		name = name.substr(0, name.length - 4);
+
+		const wsedit = new vscode.WorkspaceEdit();
+		const wsPath = vscode.workspace.workspaceFolders![0].uri.fsPath;
+
+		const filePathTpp = vscode.Uri.file(wsPath + '/' + name + ".tpp");
+
+		let command = (vscode.workspace.getConfiguration().get('headercppclass.headerId') as string).trim();
+
+		wsedit.createFile(filePathTpp, { ignoreIfExists: true });
+		vscode.workspace.applyEdit(wsedit);
+		
+		getTemplateTpp( name, filePathTpp );
+		await getHeadercpp( command, filePathTpp );
+	}
+}
 /*
 export async function createClass() {
 	let name = await vscode.window.showInputBox({

+ 16 - 14
src/extension.ts

@@ -11,24 +11,26 @@
 /* ************************************************************************** */
 
 import { window, commands, ExtensionContext } from 'vscode';
-import { createQuickClass } from './basicInput';
+import { createQuickClass, createTemplateFile } from './basicInput';
+import * as vscode from 'vscode';
 
 export function activate(context: ExtensionContext) {
-	context.subscriptions.push(commands.registerCommand('canonicalclass.makeclass', () => {
-		/*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);
+	context.subscriptions.push(commands.registerCommand('canonicalclass.makeclass', async () => {
+		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;
+				}
 			}
 		});
-		quickPick.onDidHide(() => quickPick.dispose());
-		quickPick.show();*/
-		createQuickClass();
+		if (name !== undefined && name.slice(name.length - 4) == ".tpp")
+			createTemplateFile(name);
+		else
+			createQuickClass(name);
 	}));
 }
 

+ 39 - 0
src/template.ts

@@ -68,6 +68,45 @@ export const getTemplate = ( name: string | undefined, filePathCpp: vscode.Uri ,
 	}
 }
 
+export const getTemplateTpp = ( name: string | undefined, filePathTpp: vscode.Uri ) => {
+	if (name)
+	{
+		let len = name.length;
+		len += 19;
+		len = 80 - len - 1
+		let star = "*"
+		for (; len > 0; len--)
+		{
+			star += "*"
+		}
+
+		let classtpp =	"#ifndef " + name.toUpperCase() + "_TPP\n" +
+						"# define " + name.toUpperCase() + "_TPP\n\n" +
+						"# include <iostream>\n" +
+						"# include <string>\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			//if ( this != &rhs )\n" +
+						"			//{\n				//this->_value = rhs.getValue();\n			//}\n" +
+						"			return *this;\n		}\n\n" +
+						"\n\n" +
+						"	private:\n\n" +
+						"};\n\n" +
+						"#endif /* *" + star + " " + name.toUpperCase() + "_TPP */";
+
+
+		fs.writeFile(filePathTpp.fsPath, classtpp, function (err: any) { if (err) return console.log(err); });
+
+		vscode.window.showInformationMessage("Files : " + name + ".tpp created !");
+	}
+}
+
 
 export const getTemplateFull = ( name: string, filePathCpp: vscode.Uri, filePathHpp: vscode.Uri, valueArray: Array<string | undefined>, typeArray: Array<string | undefined> ) => {
 	let len = name.length;