Datei: NDOPackage/Commands/AddAccessorCs.cs

Last Commit (aa458ff)
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
New Commit (4962638)
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 => 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