Datei: NDOPackage/Commands/AddRelation.cs

Last Commit (bff399f)
1 //
2 // Copyright (c) 2002-2022 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 using EnvDTE;
23 using System.IO;
24 using System.Text;
25 using System.Windows.Forms;
26 using MessageBox = System.Windows.Forms.MessageBox;
27
28 namespace NDOVsPackage.Commands
29 {
30
31 ····[Command(PackageGuids.guidNDOPackageCmdSetString, PackageIds.cmdidAddRelation)]
32 ····internal sealed class AddRelation : BaseCommand<AddRelation>
33 ····{
34 ········protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
35 ········{
36 ············await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
37 ············Document document;
38
39 ············document = ApplicationObject.VisualStudioApplication.ActiveDocument;
40 ············if (document == null)
41 ················return;
42
43 ············TextDocument textDoc = (TextDocument) document.Object( "TextDocument" );
44 ············if (textDoc == null)
45 ················return;
46
47 ············StringBuilder sbResult = new System.Text.StringBuilder( 500 );
48
49 ············RelationDialog dlg = new RelationDialog();
50 ············DialogResult result = dlg.ShowDialog();
51 ············if (result == DialogResult.Cancel)
52 ················return;
53
54 ············string openBracket;
55 ············string closeBracket;
56 ············string typeOf;
57
58 ············textDoc.Selection.StartOfLine( vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, false );
59
60
61 ············string listLeft = null;
62 ············string listRight = null;
63 ············string startGeneric = null;
64 ············string endGeneric = null;
65 ············string spc = null;
66
67 ············bool isVC;
68 ············if (isVC = ( Path.GetExtension( document.FullName ).ToLower() == ".cs" ))
69 ············{
70 ················openBracket = "[";
71 ················closeBracket = "]";
72 ················typeOf = "typeof";
73 ················spc = "\t\t";
74 ················startGeneric = "<";
75 ················endGeneric = ">";
76 ············}
77 ············else
78 ············{
79 ················openBracket = "<";
80 ················closeBracket = "> _";
81 ················typeOf = "GetType";
82 ················spc = "\t";
83 ················startGeneric = "(Of ";
84 ················endGeneric = ")";
85 ············}
86
87
88 ············if (dlg.List)
89 ············{
90 ················if (!dlg.UseGenerics)
91 ················{
92 ····················switch (dlg.ListType)
93 ····················{
94 ························case ListType.IList:
95 ····························listLeft = "IList";
96 ····························listRight = "ArrayList";
97 ····························break;
98 ························default:
99 ····························listLeft = listRight = "ArrayList";
100 ····························break;
101 ····················}
102 ················}
103 ················else
104 ················{
105 ····················switch (dlg.ListType)
106 ····················{
107 ························case ListType.IList:
108 ····························listLeft = "IList" + startGeneric + dlg.Type + endGeneric;
109 ····························listRight = "List" + startGeneric + dlg.Type + endGeneric;
110 ····························break;
111 ························default:
112 ····························listLeft = listRight = "List" + startGeneric + dlg.Type + endGeneric;
113 ····························break;
114 ····················}
115 ················}
116 ············}
117
118
119 ············try
120 ············{
121 ················sbResult.Append( spc + openBracket + "NDORelation" );
122 ················if (( dlg.List && !dlg.UseGenerics ) || dlg.Composite || dlg.RelationName.Trim() != string.Empty)
123 ················{
124 ····················bool needsComma = false;
125 ····················sbResult.Append( "(" );
126 ····················if (dlg.List && !dlg.UseGenerics)
127 ····················{
128 ························sbResult.Append( typeOf + '(' + dlg.Type + ')' );
129 ························needsComma = true;
130 ····················}
131 ····················if (dlg.Composite)
132 ····················{
133 ························if (needsComma)
134 ····························sbResult.Append( ", " );
135 ························sbResult.Append( "RelationInfo.Composite" );
136 ························needsComma = true;
137 ····················}
138 ····················if (dlg.RelationName.Trim() != string.Empty)
139 ····················{
140 ························if (needsComma)
141 ····························sbResult.Append( ", " );
142 ························sbResult.Append( "\"" + dlg.RelationName + "\"" );
143 ····················}
144 ····················sbResult.Append( ")" );
145
146 ················}
147 ················sbResult.Append( closeBracket + '\n' );
148
149 ················if (isVC)
150 ················{
151 ····················if (dlg.List)
152 ························sbResult.Append( spc + listLeft + " " );
153 ····················else
154 ························sbResult.Append( spc + dlg.Type + " " );
155 ····················sbResult.Append( dlg.FieldName );
156 ····················if (dlg.List)
157 ····················{
158 ························sbResult.Append( " = new " + listRight );
159 ························sbResult.Append( "()" );
160 ····················}
161
162 ····················sbResult.Append( ";\n" );
163 ················}
164 ················else
165 ················{
166 ····················sbResult.Append( spc + "Dim " );
167 ····················sbResult.Append( dlg.FieldName );
168 ····················sbResult.Append( " As " );
169 ····················if (dlg.List)
170 ························sbResult.Append( listLeft );
171 ····················else
172 ························sbResult.Append( dlg.Type );
173 ····················if (dlg.List)
174 ····················{
175 ························sbResult.Append( " = new " + listRight );
176 ························sbResult.Append( "()" );
177 ····················}
178 ····················sbResult.Append( "\n" );
179 ················}
180
181 ················textDoc.Selection.Insert( sbResult.ToString(), (int) vsInsertFlags.vsInsertFlagsInsertAtStart );
182 ············}
183 ············catch (Exception ex)
184 ············{
185 ················MessageBox.Show( ex.Message, "Add Relation Add-in" );
186 ············}
187
188 ········}
189
190 ········protected override void BeforeQueryStatus(EventArgs e)
191 ········{
192 ············ThreadHelper.JoinableTaskFactory.Run(async () =>
193 ············{
194 ················var active = await VS.Documents.GetActiveDocumentViewAsync();
195 var enabled = active. FilePath. EndsWith( ". cs") || active. FilePath. EndsWith( ". vb") ;
196 ················await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
197 ················Command.Enabled = enabled;
198 ············});
199 ········}
200 ····}
201 }
202
New Commit (90ec57c)
1 //
2 // Copyright (c) 2002-2022 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 using EnvDTE;
23 using System.IO;
24 using System.Text;
25 using System.Windows.Forms;
26 using MessageBox = System.Windows.Forms.MessageBox;
27
28 namespace NDOVsPackage.Commands
29 {
30
31 ····[Command(PackageGuids.guidNDOPackageCmdSetString, PackageIds.cmdidAddRelation)]
32 ····internal sealed class AddRelation : BaseCommand<AddRelation>
33 ····{
34 ········protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
35 ········{
36 ············await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
37 ············Document document;
38
39 ············document = ApplicationObject.VisualStudioApplication.ActiveDocument;
40 ············if (document == null)
41 ················return;
42
43 ············TextDocument textDoc = (TextDocument) document.Object( "TextDocument" );
44 ············if (textDoc == null)
45 ················return;
46
47 ············StringBuilder sbResult = new System.Text.StringBuilder( 500 );
48
49 ············RelationDialog dlg = new RelationDialog();
50 ············DialogResult result = dlg.ShowDialog();
51 ············if (result == DialogResult.Cancel)
52 ················return;
53
54 ············string openBracket;
55 ············string closeBracket;
56 ············string typeOf;
57
58 ············textDoc.Selection.StartOfLine( vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, false );
59
60
61 ············string listLeft = null;
62 ············string listRight = null;
63 ············string startGeneric = null;
64 ············string endGeneric = null;
65 ············string spc = null;
66
67 ············bool isVC;
68 ············if (isVC = ( Path.GetExtension( document.FullName ).ToLower() == ".cs" ))
69 ············{
70 ················openBracket = "[";
71 ················closeBracket = "]";
72 ················typeOf = "typeof";
73 ················spc = "\t\t";
74 ················startGeneric = "<";
75 ················endGeneric = ">";
76 ············}
77 ············else
78 ············{
79 ················openBracket = "<";
80 ················closeBracket = "> _";
81 ················typeOf = "GetType";
82 ················spc = "\t";
83 ················startGeneric = "(Of ";
84 ················endGeneric = ")";
85 ············}
86
87
88 ············if (dlg.List)
89 ············{
90 ················if (!dlg.UseGenerics)
91 ················{
92 ····················switch (dlg.ListType)
93 ····················{
94 ························case ListType.IList:
95 ····························listLeft = "IList";
96 ····························listRight = "ArrayList";
97 ····························break;
98 ························default:
99 ····························listLeft = listRight = "ArrayList";
100 ····························break;
101 ····················}
102 ················}
103 ················else
104 ················{
105 ····················switch (dlg.ListType)
106 ····················{
107 ························case ListType.IList:
108 ····························listLeft = "IList" + startGeneric + dlg.Type + endGeneric;
109 ····························listRight = "List" + startGeneric + dlg.Type + endGeneric;
110 ····························break;
111 ························default:
112 ····························listLeft = listRight = "List" + startGeneric + dlg.Type + endGeneric;
113 ····························break;
114 ····················}
115 ················}
116 ············}
117
118
119 ············try
120 ············{
121 ················sbResult.Append( spc + openBracket + "NDORelation" );
122 ················if (( dlg.List && !dlg.UseGenerics ) || dlg.Composite || dlg.RelationName.Trim() != string.Empty)
123 ················{
124 ····················bool needsComma = false;
125 ····················sbResult.Append( "(" );
126 ····················if (dlg.List && !dlg.UseGenerics)
127 ····················{
128 ························sbResult.Append( typeOf + '(' + dlg.Type + ')' );
129 ························needsComma = true;
130 ····················}
131 ····················if (dlg.Composite)
132 ····················{
133 ························if (needsComma)
134 ····························sbResult.Append( ", " );
135 ························sbResult.Append( "RelationInfo.Composite" );
136 ························needsComma = true;
137 ····················}
138 ····················if (dlg.RelationName.Trim() != string.Empty)
139 ····················{
140 ························if (needsComma)
141 ····························sbResult.Append( ", " );
142 ························sbResult.Append( "\"" + dlg.RelationName + "\"" );
143 ····················}
144 ····················sbResult.Append( ")" );
145
146 ················}
147 ················sbResult.Append( closeBracket + '\n' );
148
149 ················if (isVC)
150 ················{
151 ····················if (dlg.List)
152 ························sbResult.Append( spc + listLeft + " " );
153 ····················else
154 ························sbResult.Append( spc + dlg.Type + " " );
155 ····················sbResult.Append( dlg.FieldName );
156 ····················if (dlg.List)
157 ····················{
158 ························sbResult.Append( " = new " + listRight );
159 ························sbResult.Append( "()" );
160 ····················}
161
162 ····················sbResult.Append( ";\n" );
163 ················}
164 ················else
165 ················{
166 ····················sbResult.Append( spc + "Dim " );
167 ····················sbResult.Append( dlg.FieldName );
168 ····················sbResult.Append( " As " );
169 ····················if (dlg.List)
170 ························sbResult.Append( listLeft );
171 ····················else
172 ························sbResult.Append( dlg.Type );
173 ····················if (dlg.List)
174 ····················{
175 ························sbResult.Append( " = new " + listRight );
176 ························sbResult.Append( "()" );
177 ····················}
178 ····················sbResult.Append( "\n" );
179 ················}
180
181 ················textDoc.Selection.Insert( sbResult.ToString(), (int) vsInsertFlags.vsInsertFlagsInsertAtStart );
182 ············}
183 ············catch (Exception ex)
184 ············{
185 ················MessageBox.Show( ex.Message, "Add Relation Add-in" );
186 ············}
187
188 ········}
189
190 ········protected override void BeforeQueryStatus(EventArgs e)
191 ········{
192 ············ThreadHelper.JoinableTaskFactory.Run(async () =>
193 ············{
194 ················var active = await VS.Documents.GetActiveDocumentViewAsync();
195 var enabled = active != null && ( active. FilePath. EndsWith( ". cs") || active. FilePath. EndsWith( ". vb") ) ;
196 ················await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
197 ················Command.Enabled = enabled;
198 ············});
199 ········}
200 ····}
201 }
202