-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerator.cs
More file actions
123 lines (102 loc) · 3.29 KB
/
CodeGenerator.cs
File metadata and controls
123 lines (102 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EnvDTE;
namespace CodeNavigator
{
abstract class CodeGenerator
{
//instance must be create via method create
protected CodeGenerator() { }
//--- public ---
/// <summary>
/// Create an instance
/// </summary>
/// <param name="language">the language ("cpp", "cs", TBD)</param>
/// <returns></returns>
internal static CodeGenerator Create(String language)
{
if (language.Equals("cpp"))
return new CodeGeneratorCpp();
else if (language.Equals("cs"))
return new CodeGeneratorCs();
else
return null;
}
//--- protected ---
protected CodeTemplate AddCodeTemplate(String name)
{
CodeTemplate templ = new CodeTemplate(name);
_codeTemplatesNewClass.Add(templ);
return templ;
}
//--- private ---
internal ProjectItem AddProjectItem(ProjectItems items, String path, String content)
{
try
{
//TODO: check if item already exists
TextWriter tw = new StreamWriter(path);
tw.WriteLine(content);
tw.Close();
}
catch (Exception /*ex*/)
{
//MessageBox.Show(ex.Message);
return null;
}
//try to add to project
try
{
return items.AddFromFile(path);
}
catch { }
//return existing
try
{
return items.Item(path);
}
catch { }
return null;
}
internal abstract CodeItem CreateCodeItem(ProjectItem srcItem);
internal void FormatDocument(ProjectItem item)
{
Window w = item.Open();
w.Activate();
try
{
//does not work with VS 2010
item.DTE.ExecuteCommand("Edit.FormatDocument", "");
w.Document.Save();
//do not close -> we leave it open since they were just created
//w.Close();
return;
}
catch (Exception e)
{
String err = e.ToString();
}
//format failed -> try work-around
try
{
TextDocument doc = (TextDocument)item.Document.Object(String.Empty);
EditPoint start = doc.StartPoint.CreateEditPoint();
EditPoint end = doc.EndPoint.CreateEditPoint();
start.SmartFormat(end);
w.Document.Save();
}
catch (Exception e)
{
String err = e.ToString();
}
}
internal List<CodeTemplate> GetNewClassTemplates() { return _codeTemplatesNewClass; }
internal CodeTemplate GetClassDefTemplate() { return _codeTemplateClassDef; }
private List<CodeTemplate> _codeTemplatesNewClass = new List<CodeTemplate>();
protected CodeTemplate _codeTemplateClassDef = null;
}
}