|
| 1 | +/* |
| 2 | +Copyright 2016 Colin Girling |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +var doc = dte.ActiveDocument; |
| 18 | + |
| 19 | +function Insert(str) |
| 20 | +{ |
| 21 | + doc.Selection.StartOfLine(); |
| 22 | + doc.Selection.Insert(str, 1); |
| 23 | +} |
| 24 | + |
| 25 | +function InsertLine(str) |
| 26 | +{ |
| 27 | + doc.Selection.StartOfLine(); |
| 28 | + doc.Selection.Insert(str, 1); |
| 29 | + doc.Selection.NewLine(); |
| 30 | +} |
| 31 | + |
| 32 | +function InserBlanktLine() |
| 33 | +{ |
| 34 | + doc.Selection.NewLine(); |
| 35 | +} |
| 36 | + |
| 37 | +function ImplementEmptyFunction(func, add_blank) |
| 38 | +{ |
| 39 | + InsertLine(func); |
| 40 | + InsertLine("{"); |
| 41 | + InsertLine("}"); |
| 42 | + if (add_blank) |
| 43 | + InserBlanktLine(); |
| 44 | +} |
| 45 | + |
| 46 | +function CreateClass(class_name, indent) |
| 47 | +{ |
| 48 | + InsertLine("class " + class_name); |
| 49 | + InsertLine("{"); |
| 50 | + InsertLine("public:"); |
| 51 | + InsertLine(indent + class_name + "();"); |
| 52 | + InsertLine(indent + class_name + "(" + class_name + " const&);"); |
| 53 | + InsertLine(indent + "~" + class_name + "();"); |
| 54 | + InserBlanktLine(); |
| 55 | + InsertLine(indent + class_name + "& operator =(" + class_name + " const&);"); |
| 56 | + InsertLine("};"); |
| 57 | +} |
| 58 | + |
| 59 | +function ImplementClass(class_name, indent) |
| 60 | +{ |
| 61 | + var class_member = class_name + "::"; |
| 62 | + |
| 63 | + InsertLine("#include \"" + class_name + ".hpp\""); |
| 64 | + InserBlanktLine(); |
| 65 | + ImplementEmptyFunction(class_member + class_name + "()", true); |
| 66 | + ImplementEmptyFunction(class_member + class_name + "(" + class_name + " const&)", true); |
| 67 | + ImplementEmptyFunction(class_member + "~" + class_name + "()", true); |
| 68 | + ImplementEmptyFunction(class_name + "& " + class_member + "operator =(" + class_name + " const&)", false); |
| 69 | +} |
| 70 | + |
| 71 | +function SanitizeClassName(class_name) |
| 72 | +{ |
| 73 | + // Remove single line comment characters // and any preceding spaces. |
| 74 | + // E.g. |
| 75 | + // before: " // MyClass" |
| 76 | + // after: "MyClass" |
| 77 | + |
| 78 | + while (class_name.charAt(0) == " ") |
| 79 | + class_name = class_name.substr(1); |
| 80 | + if (class_name.length > 2 && class_name.substr(0, 2) == "//") |
| 81 | + class_name = class_name.substr(2); |
| 82 | + while (class_name.charAt(0) == " ") |
| 83 | + class_name = class_name.substr(1); |
| 84 | + |
| 85 | + return class_name; |
| 86 | +} |
| 87 | + |
| 88 | +function DocumentTextToClass() |
| 89 | +{ |
| 90 | + // Prepare undo. |
| 91 | + if (dte.UndoContext.IsOpen) |
| 92 | + dte.UndoContext.Close(); |
| 93 | + dte.UndoContext.Open("Insert Header Guard"); |
| 94 | + |
| 95 | + var indent = " "; |
| 96 | + var TextSelection = doc.Selection; |
| 97 | + |
| 98 | + // Get the class name from the current line. |
| 99 | + // Any leading single line comment is removed from the class name. |
| 100 | + TextSelection.StartOfLine(); |
| 101 | + TextSelection.EndOfLine(true); |
| 102 | + var class_name = SanitizeClassName(TextSelection.Text); |
| 103 | + |
| 104 | + dte.ItemOperations.NewFile("General\\Text File", class_name + ".hpp"); |
| 105 | + doc = dte.ActiveDocument; |
| 106 | + |
| 107 | + // Create the class declaration within the new .hpp file. |
| 108 | + CreateClass(class_name, indent); |
| 109 | + |
| 110 | + dte.ItemOperations.NewFile("General\\Text File", class_name + ".cpp"); |
| 111 | + doc = dte.ActiveDocument; |
| 112 | + |
| 113 | + // Create the class implementation within the new .cpp file. |
| 114 | + ImplementClass(class_name, indent); |
| 115 | + |
| 116 | + dte.UndoContext.Close(); |
| 117 | +} |
| 118 | + |
| 119 | +// Takes the first word on the line and uses this as the class name. |
| 120 | +// then created a new .hpp and .cpp with the class defined and implemented. |
| 121 | +DocumentTextToClass(); |
0 commit comments