Datei: NDOPackage/CodeGenHelper.cs

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