Datei: NDOPackage/Commands/AddAccessorCs.cs

Last Commit (946ad0e)
1 //
2 // Copyright (c) 2002-2019 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System. IO;
25 using System.Windows.Forms;
26 using EnvDTE;
27 using EnvDTE80;
28 using Microsoft.VisualStudio.CommandBars;
29 using System.Text.RegularExpressions;
30
 
 
31 namespace NDOVsPackage.Commands
32 {
33 ····/// <summary>
34 ····/// Zusammenfassung für AddAccessorCs.
35 ····/// </summary>
36 ····internal class AddAccessorCs
37 ····{
38 ········TextDocument textDoc;
39 ········Document document;
40
41 ········public AddAccessorCs(TextDocument textDoc, Document document)
42 ········{
43 ············this.document = document;
44 ············this.textDoc = textDoc;
45 ········}
46
47 ········public void DoIt()
48 ········{
49 ············bool genChangeEvent = false;
50
51 ············try
52 ············{
53 ················string result;
54 ················int textLine = textDoc.Selection.TopLine;
55 ················if (textLine != textDoc.Selection.BottomLine)
56 ····················return;
57 ················textDoc.Selection.SelectLine();
58 ················string original = textDoc.Selection.Text;
59 ················textDoc.Selection.LineUp(false, 1);
60 ················textDoc.Selection.SelectLine();
61 ················string attrtext = textDoc.Selection.Text;
62 ················textDoc.Selection.CharRight(false, 1);
63 ················textDoc.Selection.LineDown(false, 1);
64
65 ················int i = 0;
66 ················string bl = string.Empty;
67 ················while (char.IsWhiteSpace(original[i]))
68 ····················bl += original[i++];
69 ················string selLine = original.Trim();
70 ················Regex regex = new Regex(@"(private[\s]*|)([^\s]+(\s*<[^\s]+|))\s+([^\s^;^=]+)\s*(=|;)");
71 ················Match match = regex.Match(selLine);
72 ················if (!match.Success)
73 ················{
74 ····················MessageBox.Show("Please select a private member variable declaration");
75 ····················return;
76 ················}
77 ················string typeStr = match.Groups[2].Value;
78 ················string bigname;
79 ················string name = match.Groups[4].Value;
80 ················if (name.StartsWith("_"))
81 ····················bigname = name.Substring(1);
82 ················else if (name.StartsWith("m_"))
83 ····················bigname = name.Substring(2);
84 ················else
85 ····················bigname = name;
86 ················bigname = bigname.Substring(0, 1).ToUpper() + bigname.Substring(1);
87
88 ················string genericTypeStr = string.Empty;
89 ················string genericArgumentType = string.Empty;
90 ················string genericParameter = string.Empty;
91 ················regex = new Regex(@"([^\s]+)\s*<([^>]+)>");
92 ················match = regex.Match(typeStr);
93 ················if (match.Success)
94 ················{
95 ····················genericTypeStr = match.Groups[1].Value;
96 ····················genericArgumentType = match.Groups[2].Value;
97 ····················genericParameter = "<" + genericArgumentType + '>';
98 ················}
99
100 ················bool isContainer = ····(typeStr == "IList" || typeStr == "ArrayList");
101 ················if (genericTypeStr != string.Empty)
102 ················{
103 ····················isContainer = isContainer || (genericTypeStr == "IList" || genericTypeStr == "List");
104 ················}
105
106 ················bool isGenericList = genericTypeStr != string.Empty && isContainer;
107
108 //················bool isIList = (typeStr == "IList" || genericTypeStr == "IList");
109
110 ················if (isContainer)
111 ················{
112 ····················attrtext = attrtext.Trim();
113 ····················string elementTyp = null;
114 ····················if (!isGenericList)
115 ························elementTyp = GetElementTyp(attrtext);
116 ····················else
117 ························elementTyp = genericArgumentType;
118
119 ····················string relationName = GetRelationName(attrtext);
120
121 ····················if (elementTyp == null)
122 ····················{
123 ························isContainer = false;
124 ····················}
125 ····················else
126 ····················{
127
128 ························if (relationName == null)
129 ························{
130 ····························int p = elementTyp.LastIndexOf(".");
131 ····························relationName = elementTyp.Substring(p + 1);
132 ························}
133
134 ························bool isComposite = (attrtext.IndexOf("RelationInfo.Composite") > -1);
135 ························string parameter = elementTyp.Substring(0,1).ToLower();
136 ························result = string.Empty;
137 ························if (isComposite)
138 ························{
139 ····························result += bl + "public " + elementTyp + " New" + relationName + "()\n";
140 ····························result += bl + "{\n";
141 ····························result += bl + "\t" + elementTyp + " " + parameter + " = new " + elementTyp + "();\n";
142 ····························result += bl + "\tthis." + name + ".Add(" + parameter + ");\n";
143 ····························result += bl + "\t" + "return " + parameter + ";\n";
144 ····························result += bl + "}\n";
145 ························}
146 ························else
147 ························{
148 ····························result += bl + "public void Add" + relationName + "(" + elementTyp + " " + parameter + ")\n";
149 ····························result += bl + "{\n";
150 ····························result += bl + "\tthis." + name + ".Add(" + parameter + ");\n";
151 ····························result += bl + "}\n";
152 ························}
153 ························result += bl + "public void Remove" + relationName + "(" + elementTyp + " " + parameter + ")\n";
154 ························result += bl + "{\n";
155 ························result += bl + "\tif (this." + name + ".Contains(" + parameter + "))\n";
156 ························result += bl + "\t\tthis." + name + ".Remove(" + parameter + ");\n";
157 ························result += bl + "}\n";
158 ························textDoc.Selection.Insert(result, (int)vsInsertFlags.vsInsertFlagsInsertAtStart);
159 ····················}
160 ················}
161 ················if (!isContainer) // isContainer may change in the if case, so we test it again
162 ················{
163 ····················ConfigurationOptions options = new ConfigurationOptions(document.ProjectItem.ContainingProject);
164 ····················if (options.GenerateChangeEvents)
165 ····················{
166 ························genChangeEvent = true;
167 ························result = bl + "public event EventHandler " + bigname + "Changed;\n";
168 ························textDoc.Selection.Insert(result, (int)vsInsertFlags.vsInsertFlagsInsertAtStart);
169 ····················}
170 ················}
171
172 ················result = string.Empty;
173
174 ················string ilistType = null;
175 ················if (isGenericList)
176 ················{
177 ····················ilistType = "IEnumerable" + genericParameter;
178 ················}
179 ················else
180 ····················ilistType = "IEnumerable";
181
182 ················if (isContainer)
183 ····················result += bl + "public " + ilistType + " " + bigname + '\n';
184 ················else
185 ····················result += bl + "public " + typeStr + " " + bigname + '\n';
186 ················result += bl + "{\n";
187
188 ················result += bl + "\tget { return this." + name + "; }\n";
189
190 ················if (genChangeEvent)··// Set Accessor in mehreren Zeilen
191 ················{
192 ····················result += bl + "\tset\n";
193 ····················result += bl + "\t{\n";
194 ····················if (isContainer)
195 ····················{
196 ························if (!isGenericList)
197 ····························result += bl + "\t\tthis." + name + " = new ArrayList( (ICollection)value );\n";
198 ························else
199 ····························result += bl + "\t\tthis." + name + " = value.ToList();\n";
200 ····················}
201 ····················else
202 ····················{
203 ························result += bl + "\t\tthis." + name + " = value;\n";
204 ····················}
205 ····················result += bl + "\t\tif (" + bigname + "Changed != null)\n";
206 ····················result += bl + "\t\t\t" + bigname + "Changed(this, EventArgs.Empty);\n";
207 ····················result += bl +"\t}\n";
208 ················}
209 ················else··// Accessor in einer Zeile
210 ················{
211 ····················if (isContainer)
212 ························if (!isGenericList)
213 ····························result += bl + "\tset { this." + name + " = new ArrayList( (ICollection)value ); }\n";
214 ························else
215 ····························result += bl + "\tset { this." + name + " = value.ToList(); }\n";
216 ····················else
217 ························result += bl + "\tset { this." + name + " = value; }\n";
218 ················}
219
220 ················result += bl + "}\n";
221 ················TabProperty tp = TabProperties.Instance.CSharp;
222 ················if (tp.UseSpaces)
223 ····················result = result.Replace("\t", tp.Indent);
224 ················textDoc.Selection.Insert(result, (int)vsInsertFlags.vsInsertFlagsInsertAtStart);
225 ············}················
226 ············catch (Exception e)
227 ············{
228 ················MessageBox.Show(e.Message, "Add Accessor Add-in");
229 ············}
230 ········}
231
232 ········private string GetElementTyp(string attrtext)
233 ········{
234 ············Regex regex = new Regex(@"\[\s*NDORelation\s*\(\s*typeof\s*\(\s*([^\s^\)]+)");
235 ············Match match = regex.Match(attrtext);
236 ············if (match.Success)
237 ············{
238 ················return match.Groups[1].Value;
239 ············}
240 ············return null;
241 ········}
242
243 ········private string GetRelationName(string attrtext)
244 ········{
245 ············string result;
246 ············Regex regex = new Regex(@"\[\s*NDORelation");
247 ············Match match = regex.Match(attrtext);
248 ············if (!match.Success)
249 ················return null;
250 ············regex = new Regex(@"("")([^""]+)("")");
251 ············match = regex.Match(attrtext);
252 ············if (match.Success) // wir haben einen Relationsnamen
253 ············{
254 ················result = match.Groups[2].Value;
255 ················if (char.IsLower(result[0]))
256 ····················result = result.Substring(0, 1).ToUpper() + result.Substring(1);
257 ················return result;
258 ············}
259 ············return null;
260 ········}
261
262 ····}
263 }
 
 
264
New Commit (33e9857)
1 //
2 // Copyright (c) 2002-2019 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using MessageBox = System. Windows. Forms. MessageBox;
 
25 using EnvDTE;
 
 
26 using System.Text.RegularExpressions;
27
28 #pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
29
30 namespace NDOVsPackage.Commands
31 {
32 ····/// <summary>
33 ····/// Zusammenfassung für AddAccessorCs.
34 ····/// </summary>
35 ····internal class AddAccessorCs
36 ····{
37 ········TextDocument textDoc;
38 ········Document document;
39
40 ········public AddAccessorCs(TextDocument textDoc, Document document)
41 ········{
42 ············this.document = document;
43 ············this.textDoc = textDoc;
44 ········}
45
46 ········public void DoIt()
47 ········{
48 ············bool genChangeEvent = false;
49
50 ············try
51 ············{
52 ················string result;
53 ················int textLine = textDoc.Selection.TopLine;
54 ················if (textLine != textDoc.Selection.BottomLine)
55 ····················return;
56 ················textDoc.Selection.SelectLine();
57 ················string original = textDoc.Selection.Text;
58 ················textDoc.Selection.LineUp(false, 1);
59 ················textDoc.Selection.SelectLine();
60 ················string attrtext = textDoc.Selection.Text;
61 ················textDoc.Selection.CharRight(false, 1);
62 ················textDoc.Selection.LineDown(false, 1);
63
64 ················int i = 0;
65 ················string bl = string.Empty;
66 ················while (char.IsWhiteSpace(original[i]))
67 ····················bl += original[i++];
68 ················string selLine = original.Trim();
69 ················Regex regex = new Regex(@"(private[\s]*|)([^\s]+(\s*<[^\s]+|))\s+([^\s^;^=]+)\s*(=|;)");
70 ················Match match = regex.Match(selLine);
71 ················if (!match.Success)
72 ················{
73 ····················MessageBox.Show("Please select a private member variable declaration");
74 ····················return;
75 ················}
76 ················string typeStr = match.Groups[2].Value;
77 ················string bigname;
78 ················string name = match.Groups[4].Value;
79 ················if (name.StartsWith("_"))
80 ····················bigname = name.Substring(1);
81 ················else if (name.StartsWith("m_"))
82 ····················bigname = name.Substring(2);
83 ················else
84 ····················bigname = name;
85 ················bigname = bigname.Substring(0, 1).ToUpper() + bigname.Substring(1);
86
87 ················string genericTypeStr = string.Empty;
88 ················string genericArgumentType = string.Empty;
89 ················string genericParameter = string.Empty;
90 ················regex = new Regex(@"([^\s]+)\s*<([^>]+)>");
91 ················match = regex.Match(typeStr);
92 ················if (match.Success)
93 ················{
94 ····················genericTypeStr = match.Groups[1].Value;
95 ····················genericArgumentType = match.Groups[2].Value;
96 ····················genericParameter = "<" + genericArgumentType + '>';
97 ················}
98
99 ················bool isContainer = ····(typeStr == "IList" || typeStr == "ArrayList");
100 ················if (genericTypeStr != string.Empty)
101 ················{
102 ····················isContainer = isContainer || (genericTypeStr == "IList" || genericTypeStr == "List");
103 ················}
104
105 ················bool isGenericList = genericTypeStr != string.Empty && isContainer;
106
107 //················bool isIList = (typeStr == "IList" || genericTypeStr == "IList");
108
109 ················if (isContainer)
110 ················{
111 ····················attrtext = attrtext.Trim();
112 ····················string elementTyp = null;
113 ····················if (!isGenericList)
114 ························elementTyp = GetElementTyp(attrtext);
115 ····················else
116 ························elementTyp = genericArgumentType;
117
118 ····················string relationName = GetRelationName(attrtext);
119
120 ····················if (elementTyp == null)
121 ····················{
122 ························isContainer = false;
123 ····················}
124 ····················else
125 ····················{
126
127 ························if (relationName == null)
128 ························{
129 ····························int p = elementTyp.LastIndexOf(".");
130 ····························relationName = elementTyp.Substring(p + 1);
131 ························}
132
133 ························bool isComposite = (attrtext.IndexOf("RelationInfo.Composite") > -1);
134 ························string parameter = elementTyp.Substring(0,1).ToLower();
135 ························result = string.Empty;
136 ························if (isComposite)
137 ························{
138 ····························result += bl + "public " + elementTyp + " New" + relationName + "()\n";
139 ····························result += bl + "{\n";
140 ····························result += bl + "\t" + elementTyp + " " + parameter + " = new " + elementTyp + "();\n";
141 ····························result += bl + "\tthis." + name + ".Add(" + parameter + ");\n";
142 ····························result += bl + "\t" + "return " + parameter + ";\n";
143 ····························result += bl + "}\n";
144 ························}
145 ························else
146 ························{
147 ····························result += bl + "public void Add" + relationName + "(" + elementTyp + " " + parameter + ")\n";
148 ····························result += bl + "{\n";
149 ····························result += bl + "\tthis." + name + ".Add(" + parameter + ");\n";
150 ····························result += bl + "}\n";
151 ························}
152 ························result += bl + "public void Remove" + relationName + "(" + elementTyp + " " + parameter + ")\n";
153 ························result += bl + "{\n";
154 ························result += bl + "\tif (this." + name + ".Contains(" + parameter + "))\n";
155 ························result += bl + "\t\tthis." + name + ".Remove(" + parameter + ");\n";
156 ························result += bl + "}\n";
157 ························textDoc.Selection.Insert(result, (int)vsInsertFlags.vsInsertFlagsInsertAtStart);
158 ····················}
159 ················}
160 ················if (!isContainer) // isContainer may change in the if case, so we test it again
161 ················{
162 ····················ConfigurationOptions options = new ConfigurationOptions(document.ProjectItem.ContainingProject);
163 ····················if (options.GenerateChangeEvents)
164 ····················{
165 ························genChangeEvent = true;
166 ························result = bl + "public event EventHandler " + bigname + "Changed;\n";
167 ························textDoc.Selection.Insert(result, (int)vsInsertFlags.vsInsertFlagsInsertAtStart);
168 ····················}
169 ················}
170
171 ················result = string.Empty;
172
173 ················string ilistType = null;
174 ················if (isGenericList)
175 ················{
176 ····················ilistType = "IEnumerable" + genericParameter;
177 ················}
178 ················else
179 ····················ilistType = "IEnumerable";
180
181 ················if (isContainer)
182 ····················result += bl + "public " + ilistType + " " + bigname + '\n';
183 ················else
184 ····················result += bl + "public " + typeStr + " " + bigname + '\n';
185 ················result += bl + "{\n";
186
187 ················result += bl + "\tget { return this." + name + "; }\n";
188
189 ················if (genChangeEvent)··// Set Accessor in mehreren Zeilen
190 ················{
191 ····················result += bl + "\tset\n";
192 ····················result += bl + "\t{\n";
193 ····················if (isContainer)
194 ····················{
195 ························if (!isGenericList)
196 ····························result += bl + "\t\tthis." + name + " = new ArrayList( (ICollection)value );\n";
197 ························else
198 ····························result += bl + "\t\tthis." + name + " = value.ToList();\n";
199 ····················}
200 ····················else
201 ····················{
202 ························result += bl + "\t\tthis." + name + " = value;\n";
203 ····················}
204 ····················result += bl + "\t\tif (" + bigname + "Changed != null)\n";
205 ····················result += bl + "\t\t\t" + bigname + "Changed(this, EventArgs.Empty);\n";
206 ····················result += bl +"\t}\n";
207 ················}
208 ················else··// Accessor in einer Zeile
209 ················{
210 ····················if (isContainer)
211 ························if (!isGenericList)
212 ····························result += bl + "\tset { this." + name + " = new ArrayList( (ICollection)value ); }\n";
213 ························else
214 ····························result += bl + "\tset { this." + name + " = value.ToList(); }\n";
215 ····················else
216 ························result += bl + "\tset { this." + name + " = value; }\n";
217 ················}
218
219 ················result += bl + "}\n";
220 ················TabProperty tp = TabProperties.Instance.CSharp;
221 ················if (tp.UseSpaces)
222 ····················result = result.Replace("\t", tp.Indent);
223 ················textDoc.Selection.Insert(result, (int)vsInsertFlags.vsInsertFlagsInsertAtStart);
224 ············}················
225 ············catch (Exception e)
226 ············{
227 ················MessageBox.Show(e.Message, "Add Accessor Add-in");
228 ············}
229 ········}
230
231 ········private string GetElementTyp(string attrtext)
232 ········{
233 ············Regex regex = new Regex(@"\[\s*NDORelation\s*\(\s*typeof\s*\(\s*([^\s^\)]+)");
234 ············Match match = regex.Match(attrtext);
235 ············if (match.Success)
236 ············{
237 ················return match.Groups[1].Value;
238 ············}
239 ············return null;
240 ········}
241
242 ········private string GetRelationName(string attrtext)
243 ········{
244 ············string result;
245 ············Regex regex = new Regex(@"\[\s*NDORelation");
246 ············Match match = regex.Match(attrtext);
247 ············if (!match.Success)
248 ················return null;
249 ············regex = new Regex(@"("")([^""]+)("")");
250 ············match = regex.Match(attrtext);
251 ············if (match.Success) // wir haben einen Relationsnamen
252 ············{
253 ················result = match.Groups[2].Value;
254 ················if (char.IsLower(result[0]))
255 ····················result = result.Substring(0, 1).ToUpper() + result.Substring(1);
256 ················return result;
257 ············}
258 ············return null;
259 ········}
260
261 ····}
262 }
263
264 #pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
265