Datei: NDOPackage/Commands/AddAccessorVb.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 AddAccessorVb |
37 | ····{ |
38 | ········TextDocument textDoc; |
39 | ········Document document; |
40 | |
41 | ········public AddAccessorVb(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 | ················Regex regex = new Regex(@"<[^\>]+\>"); |
60 | ················Match match = regex.Match(original); |
61 | ················string attrtext; |
62 | ················if (match.Success) |
63 | ················{ |
64 | ····················attrtext = match.Value; |
65 | ················} |
66 | ················else |
67 | ················{ |
68 | ····················textDoc.Selection.LineUp(false, 1); |
69 | ····················textDoc.Selection.SelectLine(); |
70 | ····················match = regex.Match(textDoc.Selection.Text); |
71 | ····················attrtext = match.Value; |
72 | ················} |
73 | ················bool hasAttribute = match.Success; |
74 | ················textDoc.Selection.CharRight(false, 1); |
75 | ················textDoc.Selection.LineDown(false, 1); |
76 | |
77 | //················int i = 0; |
78 | //················string bl = string.Empty; |
79 | //················while (char.IsWhiteSpace(original[i])) |
80 | //····················bl += original[i++]; |
81 | ················TabProperty tp = TabProperties.Instance.VBasic; |
82 | ················string bl = tp.Indent; |
83 | |
84 | ················string selLine = original.Trim(); |
85 | ················regex = new Regex(@"(Dim|Private)\s([^\s]+)\sAs\s(New\s|)([^\s^(]+)(\s*|)(\(Of\s*([^\s]+)\)|)", RegexOptions.IgnoreCase); |
86 | ················match = regex.Match(selLine); |
87 | ················if (!match.Success) |
88 | ················{ |
89 | ····················MessageBox.Show("Please select a private member variable declaration"); |
90 | ····················return; |
91 | ················} |
92 | ················string typeStr = match.Groups[4].Value; |
93 | ················string bigname; |
94 | ················string name = match.Groups[2].Value; |
95 | ················string genericParameter = match.Groups[6].Value; |
96 | ················string genericArgumentType = match.Groups[7].Value; |
97 | ················ |
98 | ················bool hasPrefix = false; |
99 | |
100 | ················if (name.StartsWith("_")) |
101 | ················{ |
102 | ····················bigname = name.Substring(1); |
103 | ····················hasPrefix = true; |
104 | ················} |
105 | ················else if (name.StartsWith("m_")) |
106 | ················{ |
107 | ····················bigname = name.Substring(2); |
108 | ····················hasPrefix = true; |
109 | ················} |
110 | ················else |
111 | ····················bigname = name; |
112 | |
113 | ················if (!hasPrefix) |
114 | ····················bigname = "P" + bigname; |
115 | ················else |
116 | ····················bigname = bigname.Substring(0, 1).ToUpper() + bigname.Substring(1); |
117 | |
118 | ················bool isContainer = (typeStr == "IList" || typeStr == "List" || typeStr == "ArrayList" || typeStr == "NDOArrayList" || typeStr == "NDOGenericList"); |
119 | ················isContainer = hasAttribute && isContainer; |
120 | ················ |
121 | ················bool isGenericList = genericParameter != string.Empty && genericArgumentType != string.Empty; |
122 | ················if (isGenericList) |
123 | ····················typeStr += genericParameter; |
124 | |
125 | ················if (isContainer) |
126 | ················{ |
127 | ····················attrtext = attrtext.Trim(); |
128 | ····················string elementTyp = null; |
129 | ····················if (!isGenericList) |
130 | ························elementTyp = GetElementTyp(attrtext); |
131 | ····················else |
132 | ························elementTyp = genericArgumentType; |
133 | ····················string relationName = GetRelationName(attrtext); |
134 | |
135 | ····················if (relationName == null) |
136 | ····················{ |
137 | ························int p = elementTyp.LastIndexOf("."); |
138 | ························relationName = elementTyp.Substring(p + 1); |
139 | ····················} |
140 | |
141 | ····················bool isComposite = (attrtext.IndexOf("RelationInfo.Composite") > -1); |
142 | ····················string parameter = elementTyp.Substring(0, 1).ToLower(); |
143 | ····················result = string.Empty; |
144 | ····················if (isComposite) |
145 | ····················{ |
146 | ························result += bl + "Public Function New" + relationName + "() As " + elementTyp + "\n"; |
147 | ························result += bl + "\tDim " + parameter + " As " + elementTyp + " = New " + elementTyp + "\n"; |
148 | ························result += bl + "\t" + name + ".Add(" + parameter + ")\n"; |
149 | ························result += bl + "\t" + "return " + parameter + "\n"; |
150 | ························result += bl + "End Function\n"; |
151 | ····················} |
152 | ····················else |
153 | ····················{ |
154 | ························result += bl + "Public Sub Add" + relationName + "(" + parameter + " As " + elementTyp + ")\n"; |
155 | ························result += bl + "\t" + name + ".Add(" + parameter + ")\n"; |
156 | ························result += bl + "End Sub\n"; |
157 | ····················} |
158 | ····················result += bl + "Public Sub Remove" + relationName + "(" + parameter + " As " + elementTyp + ")\n"; |
159 | ····················result += bl + "\tIf " + name + ".Contains(" + parameter + ") Then\n"; |
160 | ····················result += bl + "\t\t" + name + ".Remove(" + parameter + ")\n"; |
161 | ····················result += bl + "\tEnd If\n"; |
162 | ····················result += bl + "End Sub\n"; |
163 | ····················textDoc.Selection.Insert(result, (int)vsInsertFlags.vsInsertFlagsInsertAtStart); |
164 | ················} |
165 | ················else // wir haben es mit einer Elementbeziehung bzw. einem Feld zu tun |
166 | ················{ |
167 | ····················ConfigurationOptions options = new ConfigurationOptions(document.ProjectItem.ContainingProject); |
168 | ····················if (options.GenerateChangeEvents) |
169 | ····················{ |
170 | ························genChangeEvent = true; |
171 | ························result = bl + "Public Event " + bigname + "Changed As EventHandler\n"; |
172 | ························textDoc.Selection.Insert(result, (int)vsInsertFlags.vsInsertFlagsInsertAtStart); |
173 | ····················} |
174 | ················} |
175 | |
176 | ················string ilistType = isGenericList ? "IEnumerable " + genericParameter : "IEnumerable" ;···································· |
177 | |
178 | ················result = string.Empty; |
179 | ················if (isContainer) |
180 | ····················result += bl + "Public Property " + bigname + "() As " + ilistType + "\n"; |
181 | ················else |
182 | ····················result += bl + "Public Property " + bigname + "() As " + typeStr + "\n"; |
183 | ················result += bl + "\tGet\n"; |
184 | ················result += bl + "\t\tReturn " + name + "\n"; |
185 | ················result += bl + "\tEnd Get\n"; |
186 | ················if (isContainer) |
187 | ····················result += bl + "\tSet(ByVal Value As " + ilistType +")\n"; |
188 | ················else |
189 | ····················result += bl + "\tSet(ByVal Value As " + typeStr + ")\n"; |
190 | ················if (isContainer) |
191 | ····················if (!isGenericList) |
192 | ························result += bl + "\t\t" + name + " = New ArrayList(CType(Value, ICollection))\n"; |
193 | ····················else |
194 | ························result += bl + "\t\t" + name + " = Value.ToList()\n"; |
195 | ················else |
196 | ····················result += bl + "\t\t" + name + " = Value\n"; |
197 | ················if (genChangeEvent) |
198 | ····················result += bl + "\t\tRaiseEvent " + bigname + "Changed(Me, EventArgs.Empty)\n"; |
199 | ················result += bl + "\tEnd Set\n"; |
200 | ················result += bl + "End Property\n"; |
201 | ················textDoc.Selection.Insert(result, (int)vsInsertFlags.vsInsertFlagsInsertAtStart); |
202 | ············}················ |
203 | ············catch (Exception e) |
204 | ············{ |
205 | ················MessageBox.Show(e.Message, "Add Accessor Add-in"); |
206 | ············} |
207 | ········} |
208 | |
209 | ········private string GetElementTyp(string attrtext) |
210 | ········{ |
211 | ············Regex regex = new Regex(@"<NDORelation\(GetType\(([^\)]+)"); |
212 | ············Match match = regex.Match(attrtext); |
213 | ············if (match.Success) |
214 | ············{ |
215 | ················return match.Groups[1].Value; |
216 | ············} |
217 | ············return null; |
218 | ········} |
219 | |
220 | ········private string GetRelationName(string attrtext) |
221 | ········{ |
222 | ············string result; |
223 | ············if (attrtext.IndexOf(@"NDORelation") == -1) |
224 | ················return null; |
225 | |
226 | ············Regex regex = new Regex(@"("")([^""]+)("")"); |
227 | ············Match match = regex.Match(attrtext); |
228 | ············if (match.Success) // wir haben einen Relationsnamen |
229 | ············{ |
230 | ················result = match.Groups[2].Value; |
231 | ················if (char.IsLower(result[0])) |
232 | ····················result = result.Substring(0, 1).ToUpper() + result.Substring(1); |
233 | ················return result; |
234 | ············} |
235 | ············return null; |
236 | ········} |
237 | |
238 | ····} |
239 | } |
240 |
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 System.IO; |
25 | using MessageBox = System. Windows. Forms. MessageBox; |
26 | using EnvDTE; |
27 | using System.Text.RegularExpressions; |
28 | |
29 | #pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread |
30 | |
31 | namespace NDOVsPackage.Commands |
32 | { |
33 | ····/// <summary> |
34 | ····/// Zusammenfassung für AddAccessorCs. |
35 | ····/// </summary> |
36 | ····internal class AddAccessorVb |
37 | ····{ |
38 | ········TextDocument textDoc; |
39 | ········Document document; |
40 | |
41 | ········public AddAccessorVb(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 | ················Regex regex = new Regex(@"<[^\>]+\>"); |
60 | ················Match match = regex.Match(original); |
61 | ················string attrtext; |
62 | ················if (match.Success) |
63 | ················{ |
64 | ····················attrtext = match.Value; |
65 | ················} |
66 | ················else |
67 | ················{ |
68 | ····················textDoc.Selection.LineUp(false, 1); |
69 | ····················textDoc.Selection.SelectLine(); |
70 | ····················match = regex.Match(textDoc.Selection.Text); |
71 | ····················attrtext = match.Value; |
72 | ················} |
73 | ················bool hasAttribute = match.Success; |
74 | ················textDoc.Selection.CharRight(false, 1); |
75 | ················textDoc.Selection.LineDown(false, 1); |
76 | |
77 | //················int i = 0; |
78 | //················string bl = string.Empty; |
79 | //················while (char.IsWhiteSpace(original[i])) |
80 | //····················bl += original[i++]; |
81 | ················TabProperty tp = TabProperties.Instance.VBasic; |
82 | ················string bl = tp.Indent; |
83 | |
84 | ················string selLine = original.Trim(); |
85 | ················regex = new Regex(@"(Dim|Private)\s([^\s]+)\sAs\s(New\s|)([^\s^(]+)(\s*|)(\(Of\s*([^\s]+)\)|)", RegexOptions.IgnoreCase); |
86 | ················match = regex.Match(selLine); |
87 | ················if (!match.Success) |
88 | ················{ |
89 | ····················MessageBox.Show("Please select a private member variable declaration"); |
90 | ····················return; |
91 | ················} |
92 | ················string typeStr = match.Groups[4].Value; |
93 | ················string bigname; |
94 | ················string name = match.Groups[2].Value; |
95 | ················string genericParameter = match.Groups[6].Value; |
96 | ················string genericArgumentType = match.Groups[7].Value; |
97 | ················ |
98 | ················bool hasPrefix = false; |
99 | |
100 | ················if (name.StartsWith("_")) |
101 | ················{ |
102 | ····················bigname = name.Substring(1); |
103 | ····················hasPrefix = true; |
104 | ················} |
105 | ················else if (name.StartsWith("m_")) |
106 | ················{ |
107 | ····················bigname = name.Substring(2); |
108 | ····················hasPrefix = true; |
109 | ················} |
110 | ················else |
111 | ····················bigname = name; |
112 | |
113 | ················if (!hasPrefix) |
114 | ····················bigname = "P" + bigname; |
115 | ················else |
116 | ····················bigname = bigname.Substring(0, 1).ToUpper() + bigname.Substring(1); |
117 | |
118 | ················bool isContainer = (typeStr == "IList" || typeStr == "List" || typeStr == "ArrayList" || typeStr == "NDOArrayList" || typeStr == "NDOGenericList"); |
119 | ················isContainer = hasAttribute && isContainer; |
120 | ················ |
121 | ················bool isGenericList = genericParameter != string.Empty && genericArgumentType != string.Empty; |
122 | ················if (isGenericList) |
123 | ····················typeStr += genericParameter; |
124 | |
125 | ················if (isContainer) |
126 | ················{ |
127 | ····················attrtext = attrtext.Trim(); |
128 | ····················string elementTyp = null; |
129 | ····················if (!isGenericList) |
130 | ························elementTyp = GetElementTyp(attrtext); |
131 | ····················else |
132 | ························elementTyp = genericArgumentType; |
133 | ····················string relationName = GetRelationName(attrtext); |
134 | |
135 | ····················if (relationName == null) |
136 | ····················{ |
137 | ························int p = elementTyp.LastIndexOf("."); |
138 | ························relationName = elementTyp.Substring(p + 1); |
139 | ····················} |
140 | |
141 | ····················bool isComposite = (attrtext.IndexOf("RelationInfo.Composite") > -1); |
142 | ····················string parameter = elementTyp.Substring(0, 1).ToLower(); |
143 | ····················result = string.Empty; |
144 | ····················if (isComposite) |
145 | ····················{ |
146 | ························result += bl + "Public Function New" + relationName + "() As " + elementTyp + "\n"; |
147 | ························result += bl + "\tDim " + parameter + " As " + elementTyp + " = New " + elementTyp + "\n"; |
148 | ························result += bl + "\t" + name + ".Add(" + parameter + ")\n"; |
149 | ························result += bl + "\t" + "return " + parameter + "\n"; |
150 | ························result += bl + "End Function\n"; |
151 | ····················} |
152 | ····················else |
153 | ····················{ |
154 | ························result += bl + "Public Sub Add" + relationName + "(" + parameter + " As " + elementTyp + ")\n"; |
155 | ························result += bl + "\t" + name + ".Add(" + parameter + ")\n"; |
156 | ························result += bl + "End Sub\n"; |
157 | ····················} |
158 | ····················result += bl + "Public Sub Remove" + relationName + "(" + parameter + " As " + elementTyp + ")\n"; |
159 | ····················result += bl + "\tIf " + name + ".Contains(" + parameter + ") Then\n"; |
160 | ····················result += bl + "\t\t" + name + ".Remove(" + parameter + ")\n"; |
161 | ····················result += bl + "\tEnd If\n"; |
162 | ····················result += bl + "End Sub\n"; |
163 | ····················textDoc.Selection.Insert(result, (int)vsInsertFlags.vsInsertFlagsInsertAtStart); |
164 | ················} |
165 | ················else // wir haben es mit einer Elementbeziehung bzw. einem Feld zu tun |
166 | ················{ |
167 | ····················ConfigurationOptions options = new ConfigurationOptions(document.ProjectItem.ContainingProject); |
168 | ····················if (options.GenerateChangeEvents) |
169 | ····················{ |
170 | ························genChangeEvent = true; |
171 | ························result = bl + "Public Event " + bigname + "Changed As EventHandler\n"; |
172 | ························textDoc.Selection.Insert(result, (int)vsInsertFlags.vsInsertFlagsInsertAtStart); |
173 | ····················} |
174 | ················} |
175 | |
176 | ················string ilistType = isGenericList ? "IEnumerable " + genericParameter : "IEnumerable" ;···································· |
177 | |
178 | ················result = string.Empty; |
179 | ················if (isContainer) |
180 | ····················result += bl + "Public Property " + bigname + "() As " + ilistType + "\n"; |
181 | ················else |
182 | ····················result += bl + "Public Property " + bigname + "() As " + typeStr + "\n"; |
183 | ················result += bl + "\tGet\n"; |
184 | ················result += bl + "\t\tReturn " + name + "\n"; |
185 | ················result += bl + "\tEnd Get\n"; |
186 | ················if (isContainer) |
187 | ····················result += bl + "\tSet(ByVal Value As " + ilistType +")\n"; |
188 | ················else |
189 | ····················result += bl + "\tSet(ByVal Value As " + typeStr + ")\n"; |
190 | ················if (isContainer) |
191 | ····················if (!isGenericList) |
192 | ························result += bl + "\t\t" + name + " = New ArrayList(CType(Value, ICollection))\n"; |
193 | ····················else |
194 | ························result += bl + "\t\t" + name + " = Value.ToList()\n"; |
195 | ················else |
196 | ····················result += bl + "\t\t" + name + " = Value\n"; |
197 | ················if (genChangeEvent) |
198 | ····················result += bl + "\t\tRaiseEvent " + bigname + "Changed(Me, EventArgs.Empty)\n"; |
199 | ················result += bl + "\tEnd Set\n"; |
200 | ················result += bl + "End Property\n"; |
201 | ················textDoc.Selection.Insert(result, (int)vsInsertFlags.vsInsertFlagsInsertAtStart); |
202 | ············}················ |
203 | ············catch (Exception e) |
204 | ············{ |
205 | ················MessageBox.Show(e.Message, "Add Accessor Add-in"); |
206 | ············} |
207 | ········} |
208 | |
209 | ········private string GetElementTyp(string attrtext) |
210 | ········{ |
211 | ············Regex regex = new Regex(@"<NDORelation\(GetType\(([^\)]+)"); |
212 | ············Match match = regex.Match(attrtext); |
213 | ············if (match.Success) |
214 | ············{ |
215 | ················return match.Groups[1].Value; |
216 | ············} |
217 | ············return null; |
218 | ········} |
219 | |
220 | ········private string GetRelationName(string attrtext) |
221 | ········{ |
222 | ············string result; |
223 | ············if (attrtext.IndexOf(@"NDORelation") == -1) |
224 | ················return null; |
225 | |
226 | ············Regex regex = new Regex(@"("")([^""]+)("")"); |
227 | ············Match match = regex.Match(attrtext); |
228 | ············if (match.Success) // wir haben einen Relationsnamen |
229 | ············{ |
230 | ················result = match.Groups[2].Value; |
231 | ················if (char.IsLower(result[0])) |
232 | ····················result = result.Substring(0, 1).ToUpper() + result.Substring(1); |
233 | ················return result; |
234 | ············} |
235 | ············return null; |
236 | ········} |
237 | |
238 | ····} |
239 | } |
240 | #pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread |
241 |