11using EnvDTE ;
22using Microsoft . SqlServer . TransactSql . ScriptDom ;
3+ using Microsoft . VisualStudio ;
4+ using Microsoft . VisualStudio . OLE . Interop ;
35using Microsoft . VisualStudio . Shell ;
6+ using Microsoft . VisualStudio . Shell . Interop ;
47using SQLScriptsExplorer . Addin . Repository ;
58using SQLScriptsExplorer . Addin . Repository . Interfaces ;
69using System ;
@@ -16,26 +19,37 @@ public static void OpenTemplate(string fileName, string fileFullPath)
1619 {
1720 try
1821 {
22+ // TODO: This needs to open in a new window, not edit the current one.
1923 ThreadHelper . ThrowIfNotOnUIThread ( ) ;
2024
21- if ( File . Exists ( fileFullPath ) )
25+ if ( ! File . Exists ( fileFullPath ) )
2226 {
23- string fileContent = File . ReadAllText ( fileFullPath ) ;
27+ throw new FileNotFoundException ( $ "File { fileFullPath } doesn't exist!") ;
28+ }
2429
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+ }
2735
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 ;
3341
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 )
3751 {
38- throw new Exception ( $ "File { fileFullPath } doesn't exist!" ) ;
52+ windowFrame . Show ( ) ;
3953 }
4054 }
4155 catch ( Exception ex )
@@ -50,14 +64,34 @@ public static void EditTemplate(string fileName, string fileFullPath)
5064 {
5165 ThreadHelper . ThrowIfNotOnUIThread ( ) ;
5266
53- if ( File . Exists ( fileFullPath ) )
67+ if ( ! File . Exists ( fileFullPath ) )
5468 {
55- DTE dte = Package . GetGlobalService ( typeof ( DTE ) ) as DTE ;
56- dte . ItemOperations . OpenFile ( fileFullPath ) ;
69+ throw new FileNotFoundException ( $ "File { fileFullPath } doesn't exist!") ;
5770 }
58- else
71+
72+ var openDoc = Package . GetGlobalService ( typeof ( SVsUIShellOpenDocument ) ) as IVsUIShellOpenDocument ;
73+ if ( openDoc == null )
5974 {
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 ( ) ;
6195 }
6296 }
6397 catch ( Exception ex )
@@ -66,20 +100,66 @@ public static void EditTemplate(string fileName, string fileFullPath)
66100 }
67101 }
68102
69- public static void ExecuteTemplate ( string fileName , string fileFullPath , bool confirmScriptExecution )
103+ public static void ExecuteTemplate ( string fileName , string fileFullPath )
70104 {
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).
72106
73107 try
74108 {
75109 ThreadHelper . ThrowIfNotOnUIThread ( ) ;
76110
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+ }
78115
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 )
81118 {
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 } ") ;
83163 }
84164 }
85165 catch ( Exception ex )
@@ -88,6 +168,18 @@ public static void ExecuteTemplate(string fileName, string fileFullPath, bool co
88168 }
89169 }
90170
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+
91183 public static void FormatSelection ( )
92184 {
93185 try
0 commit comments