Datei: NDOPackage/CodeGenHelper.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.Threading.Tasks; |
25 | using System.IO; |
26 | using System.Windows.Forms; |
27 | using Microsoft.VisualStudio.CommandBars; |
28 | using Community.VisualStudio.Toolkit; |
29 | using Microsoft.VisualStudio.Shell.Interop; |
30 | |
31 | namespace NDOVsPackage |
32 | { |
33 | |
34 | ····internal class CodeGenHelper |
35 | ····{ |
36 | |
37 | ········public static bool IsCsProject(Project project) |
38 | ········{ |
39 | ············project.GetItemInfo(out IVsHierarchy hierarchy, out _, out _); |
40 | ············return hierarchy.IsCapabilityMatch("CSharp") || hierarchy.IsCapabilityMatch("VB"); |
41 | ········} |
42 | |
43 | ········public static bool IsVbProject(Project project) |
44 | ········{ |
45 | ············project.GetItemInfo(out IVsHierarchy hierarchy, out _, out _); |
46 | ············return hierarchy.IsCapabilityMatch("VB"); |
47 | ········} |
48 | |
49 | |
50 | ········public static bool IsCsOrVbProject(Project project) |
51 | ········{ |
52 | ············project.GetItemInfo(out IVsHierarchy hierarchy, out _, out _); |
53 | ············return hierarchy.IsCapabilityMatch("CSharp") || hierarchy.IsCapabilityMatch("VB"); |
54 | ········} |
55 | |
56 | ········public static bool IsCsOrVbProject( EnvDTE.Project project ) |
57 | ········{ |
58 | #pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread |
59 | ············return project.Kind == ProjectKind.prjKindVBProject || project.Kind == ProjectKind.prjKindCSharpProject; |
60 | #pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread |
61 | ········} |
62 | |
63 | ········public static bool IsCsProject( EnvDTE.Project project ) |
64 | ········{ |
65 | #pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread |
66 | ············return project.Kind == ProjectKind.prjKindCSharpProject; |
67 | #pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread |
68 | ········} |
69 | |
70 | ········public static bool IsVbProject( EnvDTE.Project project ) |
71 | ········{ |
72 | #pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread |
73 | ············return project.Kind == ProjectKind.prjKindVBProject; |
74 | #pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread |
75 | ········} |
76 | |
77 | |
78 | ········public static ProjectItem FindProjectItemByName( EnvDTE.Project prj, string strItemNameWithFileExtension ) |
79 | ········{ |
80 | ············if (prj.ProjectItems.Count < 1) |
81 | ················throw new Exception( string.Format( "Project Item {0} not found. No project items in project {1}", strItemNameWithFileExtension, prj.Name ) ); |
82 | ············if (strItemNameWithFileExtension == null) |
83 | ················return prj.ProjectItems.Item( 1 ); |
84 | ············foreach (ProjectItem pri in prj.ProjectItems) |
85 | ················if (pri.Name == strItemNameWithFileExtension) |
86 | ····················return pri; |
87 | ············return null; |
88 | ········} |
89 | |
90 | ········public static TextDocument ActivateAndGetTextDocument( EnvDTE.Project prj, string strProjectItem ) |
91 | ········{ |
92 | ············ProjectItem pri = FindProjectItemByName( prj, strProjectItem ); |
93 | ············if (pri == null) |
94 | ················return null; |
95 | |
96 | ············// we need to ensure that the item is open since we would not |
97 | ············// be able to get a text document otherwise |
98 | if ( !pri. get_IsOpen( Constants. vsViewKindCode ) ) |
99 | pri. Open( Constants. vsViewKindCode ) ; |
100 | ············Document doc = pri.Document; |
101 | ············doc.Activate(); |
102 | ············TextDocument txdoc = doc.DTE.ActiveDocument.Object( "TextDocument" ) as TextDocument; |
103 | ············return txdoc; |
104 | ········} |
105 | |
106 | ········public static async Task<DocumentView> ActivateTextDocumentAsync( Project prj, string fileName ) |
107 | ········{ |
108 | ············var documentView = await VS.Documents.OpenAsync( fileName ); |
109 | ············return documentView; |
110 | ········} |
111 | |
112 | ····} |
113 | } |
114 | |
115 |
New Commit (33e9857)
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 | |
23 | using System; |
24 | using System.Threading.Tasks; |
25 | using Microsoft.VisualStudio.Shell.Interop; |
26 | using Project = Community.VisualStudio.Toolkit.Project; |
27 | using EnvDTE; |
28 | |
29 | #pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread |
30 | namespace NDOVsPackage |
31 | { |
32 | |
33 | ····internal class CodeGenHelper |
34 | ····{ |
35 | |
36 | ········public static bool IsCsProject(Project project) |
37 | ········{ |
38 | ············project.GetItemInfo(out IVsHierarchy hierarchy, out _, out _); |
39 | ············return hierarchy.IsCapabilityMatch("CSharp") || hierarchy.IsCapabilityMatch("VB"); |
40 | ········} |
41 | |
42 | ········public static bool IsVbProject(Project project) |
43 | ········{ |
44 | ············project.GetItemInfo(out IVsHierarchy hierarchy, out _, out _); |
45 | ············return hierarchy.IsCapabilityMatch("VB"); |
46 | ········} |
47 | |
48 | |
49 | ········public static bool IsCsOrVbProject(Project project) |
50 | ········{ |
51 | ············project.GetItemInfo(out IVsHierarchy hierarchy, out _, out _); |
52 | ············return hierarchy.IsCapabilityMatch("CSharp") || hierarchy.IsCapabilityMatch("VB"); |
53 | ········} |
54 | |
55 | ········public static bool IsCsOrVbProject( EnvDTE.Project project ) |
56 | ········{ |
57 | ············return project.Kind == ProjectKind.prjKindVBProject || project.Kind == ProjectKind.prjKindCSharpProject; |
58 | ········} |
59 | |
60 | ········public static bool IsCsProject( EnvDTE.Project project ) |
61 | ········{ |
62 | ············return project.Kind == ProjectKind.prjKindCSharpProject; |
63 | ········} |
64 | |
65 | ········public static bool IsVbProject( EnvDTE.Project project ) |
66 | ········{ |
67 | ············return project.Kind == ProjectKind.prjKindVBProject; |
68 | ········} |
69 | |
70 | |
71 | ········public static ProjectItem FindProjectItemByName( EnvDTE.Project prj, string strItemNameWithFileExtension ) |
72 | ········{ |
73 | ············if (prj.ProjectItems.Count < 1) |
74 | ················throw new Exception( string.Format( "Project Item {0} not found. No project items in project {1}", strItemNameWithFileExtension, prj.Name ) ); |
75 | ············if (strItemNameWithFileExtension == null) |
76 | ················return prj.ProjectItems.Item( 1 ); |
77 | ············foreach (ProjectItem pri in prj.ProjectItems) |
78 | ················if (pri.Name == strItemNameWithFileExtension) |
79 | ····················return pri; |
80 | ············return null; |
81 | ········} |
82 | |
83 | ········public static TextDocument ActivateAndGetTextDocument( EnvDTE.Project prj, string strProjectItem ) |
84 | ········{ |
85 | ············ProjectItem pri = FindProjectItemByName( prj, strProjectItem ); |
86 | ············if (pri == null) |
87 | ················return null; |
88 | |
89 | ············// we need to ensure that the item is open since we would not |
90 | ············// be able to get a text document otherwise |
91 | if ( !pri. get_IsOpen( EnvDTE. Constants. vsViewKindCode ) ) |
92 | pri. Open( EnvDTE. Constants. vsViewKindCode ) ; |
93 | ············Document doc = pri.Document; |
94 | ············doc.Activate(); |
95 | ············TextDocument txdoc = doc.DTE.ActiveDocument.Object( "TextDocument" ) as TextDocument; |
96 | ············return txdoc; |
97 | ········} |
98 | |
99 | ········public static async Task<DocumentView> ActivateTextDocumentAsync( Project prj, string fileName ) |
100 | ········{ |
101 | ············var documentView = await VS.Documents.OpenAsync( fileName ); |
102 | ············return documentView; |
103 | ········} |
104 | |
105 | ····} |
106 | } |
107 | |
108 | #pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread |
109 | |
110 | ············TextDocument txdoc = doc.DTE.ActiveDocument.Object( "TextDocument" ) as TextDocument; |
111 | ············return txdoc; |
112 | ········} |
113 | |
114 | ········public static async Task<DocumentView> ActivateTextDocumentAsync( Project prj, string fileName ) |
115 | ········{ |
116 | ············var documentView = await VS.Documents.OpenAsync( fileName ); |
117 | ············return documentView; |
118 | ········} |
119 | |
120 | ····} |
121 | } |
122 | |
123 | |
124 | ············return documentView; |
125 | ········} |
126 | |
127 | ····} |
128 | } |
129 | |
130 |