1
1
using EnvDTE ;
2
2
using Microsoft . SqlServer . TransactSql . ScriptDom ;
3
+ using Microsoft . VisualStudio ;
4
+ using Microsoft . VisualStudio . OLE . Interop ;
3
5
using Microsoft . VisualStudio . Shell ;
6
+ using Microsoft . VisualStudio . Shell . Interop ;
4
7
using SQLScriptsExplorer . Addin . Repository ;
5
8
using SQLScriptsExplorer . Addin . Repository . Interfaces ;
6
9
using System ;
@@ -16,26 +19,37 @@ public static void OpenTemplate(string fileName, string fileFullPath)
16
19
{
17
20
try
18
21
{
22
+ // TODO: This needs to open in a new window, not edit the current one.
19
23
ThreadHelper . ThrowIfNotOnUIThread ( ) ;
20
24
21
- if ( File . Exists ( fileFullPath ) )
25
+ if ( ! File . Exists ( fileFullPath ) )
22
26
{
23
- string fileContent = File . ReadAllText ( fileFullPath ) ;
27
+ throw new FileNotFoundException ( $ "File { fileFullPath } doesn't exist!") ;
28
+ }
24
29
25
- DTE dte = Package . GetGlobalService ( typeof ( DTE ) ) as DTE ;
26
- var fileDocument = dte . ItemOperations . NewFile ( @"General\Text File" , fileName ) . Document ;
30
+ var openDoc = Package . GetGlobalService ( typeof ( SVsUIShellOpenDocument ) ) as IVsUIShellOpenDocument ;
31
+ if ( openDoc == null )
32
+ {
33
+ throw new InvalidOperationException ( "Could not get OpenDocument service." ) ;
34
+ }
27
35
28
- TextSelection textSelection = fileDocument . Selection as TextSelection ;
29
- textSelection . SelectAll ( ) ;
30
- textSelection . Text = string . Empty ;
31
- textSelection . Insert ( fileContent ) ;
32
- textSelection . StartOfDocument ( ) ;
36
+ Guid logicalView = VSConstants . LOGVIEWID_Primary ;
37
+ IVsUIHierarchy hierarchy ;
38
+ uint itemId ;
39
+ IVsWindowFrame windowFrame ;
40
+ Microsoft . VisualStudio . OLE . Interop . IServiceProvider docServiceProvider ;
33
41
34
- fileDocument . Save ( ) ;
35
- }
36
- else
42
+ int hr = openDoc . OpenDocumentViaProject (
43
+ fileFullPath ,
44
+ ref logicalView ,
45
+ out docServiceProvider ,
46
+ out hierarchy ,
47
+ out itemId ,
48
+ out windowFrame ) ;
49
+
50
+ if ( ErrorHandler . Succeeded ( hr ) && windowFrame != null )
37
51
{
38
- throw new Exception ( $ "File { fileFullPath } doesn't exist!" ) ;
52
+ windowFrame . Show ( ) ;
39
53
}
40
54
}
41
55
catch ( Exception ex )
@@ -50,14 +64,34 @@ public static void EditTemplate(string fileName, string fileFullPath)
50
64
{
51
65
ThreadHelper . ThrowIfNotOnUIThread ( ) ;
52
66
53
- if ( File . Exists ( fileFullPath ) )
67
+ if ( ! File . Exists ( fileFullPath ) )
54
68
{
55
- DTE dte = Package . GetGlobalService ( typeof ( DTE ) ) as DTE ;
56
- dte . ItemOperations . OpenFile ( fileFullPath ) ;
69
+ throw new FileNotFoundException ( $ "File { fileFullPath } doesn't exist!") ;
57
70
}
58
- else
71
+
72
+ var openDoc = Package . GetGlobalService ( typeof ( SVsUIShellOpenDocument ) ) as IVsUIShellOpenDocument ;
73
+ if ( openDoc == null )
59
74
{
60
- throw new Exception ( $ "File { fileFullPath } doesn't exist!") ;
75
+ throw new InvalidOperationException ( "Could not get OpenDocument service." ) ;
76
+ }
77
+
78
+ Guid logicalView = VSConstants . LOGVIEWID_Primary ;
79
+ IVsUIHierarchy hierarchy ;
80
+ uint itemId ;
81
+ IVsWindowFrame windowFrame ;
82
+ Microsoft . VisualStudio . OLE . Interop . IServiceProvider docServiceProvider ;
83
+
84
+ int hr = openDoc . OpenDocumentViaProject (
85
+ fileFullPath ,
86
+ ref logicalView ,
87
+ out docServiceProvider ,
88
+ out hierarchy ,
89
+ out itemId ,
90
+ out windowFrame ) ;
91
+
92
+ if ( ErrorHandler . Succeeded ( hr ) && windowFrame != null )
93
+ {
94
+ windowFrame . Show ( ) ;
61
95
}
62
96
}
63
97
catch ( Exception ex )
@@ -66,20 +100,66 @@ public static void EditTemplate(string fileName, string fileFullPath)
66
100
}
67
101
}
68
102
69
- public static void ExecuteTemplate ( string fileName , string fileFullPath , bool confirmScriptExecution )
103
+ public static void ExecuteTemplate ( string fileName , string fileFullPath )
70
104
{
71
- string CMD_QUERY_EXECUTE = "Query.Execute" ;
105
+ // TODO: This is opening the document, however it's not executing the query (automatically removing the first line instead).
72
106
73
107
try
74
108
{
75
109
ThreadHelper . ThrowIfNotOnUIThread ( ) ;
76
110
77
- DTE dte = Package . GetGlobalService ( typeof ( DTE ) ) as DTE ;
111
+ if ( ! File . Exists ( fileFullPath ) )
112
+ {
113
+ throw new FileNotFoundException ( $ "File { fileFullPath } doesn't exist!") ;
114
+ }
78
115
79
- // Ensure the document we are executing is the document we have opened by checking its name
80
- if ( dte . ActiveDocument != null && dte . ActiveDocument . ProjectItem . Name . Equals ( fileName ) )
116
+ var openDoc = Package . GetGlobalService ( typeof ( SVsUIShellOpenDocument ) ) as IVsUIShellOpenDocument ;
117
+ if ( openDoc == null )
81
118
{
82
- dte . ExecuteCommand ( CMD_QUERY_EXECUTE ) ;
119
+ throw new InvalidOperationException ( "Could not get OpenDocument service." ) ;
120
+ }
121
+
122
+ Guid logicalView = VSConstants . LOGVIEWID_Primary ;
123
+ IVsUIHierarchy hierarchy ;
124
+ uint itemId ;
125
+ IVsWindowFrame windowFrame ;
126
+ Microsoft . VisualStudio . OLE . Interop . IServiceProvider docServiceProvider ;
127
+
128
+ int hr = openDoc . OpenDocumentViaProject (
129
+ fileFullPath ,
130
+ ref logicalView ,
131
+ out docServiceProvider ,
132
+ out hierarchy ,
133
+ out itemId ,
134
+ out windowFrame ) ;
135
+
136
+ if ( ErrorHandler . Succeeded ( hr ) && windowFrame != null )
137
+ {
138
+ windowFrame . Show ( ) ;
139
+
140
+ // Now, get the command interface from the window frame
141
+ var commandTarget = GetCommandTarget ( windowFrame ) ;
142
+ if ( commandTarget != null )
143
+ {
144
+ // Execute the Query.Execute command
145
+ Guid cmdGroup = new Guid ( "5EFC7975-14BC-11CF-9B2B-00AA00573819" ) ;
146
+ uint cmdId = 16 ; // Command ID for Query.Execute in SSMS
147
+
148
+ commandTarget . Exec (
149
+ ref cmdGroup ,
150
+ cmdId ,
151
+ ( uint ) OLECMDEXECOPT . OLECMDEXECOPT_DONTPROMPTUSER ,
152
+ IntPtr . Zero ,
153
+ IntPtr . Zero ) ;
154
+ }
155
+ else
156
+ {
157
+ throw new InvalidOperationException ( "Could not get command target to execute the query." ) ;
158
+ }
159
+ }
160
+ else
161
+ {
162
+ throw new InvalidOperationException ( $ "Failed to open document: { fileFullPath } ") ;
83
163
}
84
164
}
85
165
catch ( Exception ex )
@@ -88,6 +168,18 @@ public static void ExecuteTemplate(string fileName, string fileFullPath, bool co
88
168
}
89
169
}
90
170
171
+ private static IOleCommandTarget GetCommandTarget ( IVsWindowFrame windowFrame )
172
+ {
173
+ ThreadHelper . ThrowIfNotOnUIThread ( ) ;
174
+
175
+ object docView ;
176
+ if ( ErrorHandler . Succeeded ( windowFrame . GetProperty ( ( int ) __VSFPROPID . VSFPROPID_DocView , out docView ) ) && docView != null )
177
+ {
178
+ return docView as IOleCommandTarget ;
179
+ }
180
+ return null ;
181
+ }
182
+
91
183
public static void FormatSelection ( )
92
184
{
93
185
try
0 commit comments