Skip to content

Commit deb6471

Browse files
committed
Change DTE to DTE2
Add Parser for 2022 and 2025
1 parent ffc2cfa commit deb6471

File tree

4 files changed

+54
-145
lines changed

4 files changed

+54
-145
lines changed

src/SQLScriptsExplorer.Addin/Infrastructure/DocumentManager.cs

Lines changed: 43 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using EnvDTE;
2+
using EnvDTE80;
23
using Microsoft.SqlServer.TransactSql.ScriptDom;
3-
using Microsoft.VisualStudio;
4-
using Microsoft.VisualStudio.OLE.Interop;
54
using Microsoft.VisualStudio.Shell;
6-
using Microsoft.VisualStudio.Shell.Interop;
75
using SQLScriptsExplorer.Addin.Repository;
86
using SQLScriptsExplorer.Addin.Repository.Interfaces;
97
using System;
@@ -15,41 +13,40 @@ namespace SQLScriptsExplorer.Addin.Infrastructure
1513
{
1614
public static class DocumentManager
1715
{
16+
private static DTE2 DTE
17+
{
18+
get
19+
{
20+
ThreadHelper.ThrowIfNotOnUIThread();
21+
return Package.GetGlobalService(typeof(DTE)) as DTE2;
22+
}
23+
}
24+
1825
public static void OpenTemplate(string fileName, string fileFullPath)
1926
{
2027
try
2128
{
22-
// TODO: This needs to open in a new window, not edit the current one.
2329
ThreadHelper.ThrowIfNotOnUIThread();
2430

25-
if (!File.Exists(fileFullPath))
31+
if (File.Exists(fileFullPath))
2632
{
27-
throw new FileNotFoundException($"File {fileFullPath} doesn't exist!");
28-
}
33+
string fileContent = File.ReadAllText(fileFullPath);
34+
var docName = Path.GetFileNameWithoutExtension(fileName);
35+
var docExtension = Path.GetExtension(fileName);
2936

30-
var openDoc = Package.GetGlobalService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
31-
if (openDoc == null)
32-
{
33-
throw new InvalidOperationException("Could not get OpenDocument service.");
34-
}
35-
36-
Guid logicalView = VSConstants.LOGVIEWID_Primary;
37-
IVsUIHierarchy hierarchy;
38-
uint itemId;
39-
IVsWindowFrame windowFrame;
40-
Microsoft.VisualStudio.OLE.Interop.IServiceProvider docServiceProvider;
37+
var fileDocument = DTE.ItemOperations.NewFile(@"General\Text File", $"{docName}_Copy.{docExtension}").Document;
4138

42-
int hr = openDoc.OpenDocumentViaProject(
43-
fileFullPath,
44-
ref logicalView,
45-
out docServiceProvider,
46-
out hierarchy,
47-
out itemId,
48-
out windowFrame);
39+
TextSelection textSelection = fileDocument.Selection as TextSelection;
40+
textSelection.SelectAll();
41+
textSelection.Text = string.Empty;
42+
textSelection.Insert(fileContent);
43+
textSelection.StartOfDocument();
4944

50-
if (ErrorHandler.Succeeded(hr) && windowFrame != null)
45+
fileDocument.Save();
46+
}
47+
else
5148
{
52-
windowFrame.Show();
49+
throw new Exception($"File {fileFullPath} doesn't exist!");
5350
}
5451
}
5552
catch (Exception ex)
@@ -62,36 +59,13 @@ public static void EditTemplate(string fileName, string fileFullPath)
6259
{
6360
try
6461
{
65-
ThreadHelper.ThrowIfNotOnUIThread();
66-
67-
if (!File.Exists(fileFullPath))
68-
{
69-
throw new FileNotFoundException($"File {fileFullPath} doesn't exist!");
70-
}
71-
72-
var openDoc = Package.GetGlobalService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
73-
if (openDoc == null)
62+
if (File.Exists(fileFullPath))
7463
{
75-
throw new InvalidOperationException("Could not get OpenDocument service.");
64+
DTE.ItemOperations.OpenFile(fileFullPath);
7665
}
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)
66+
else
9367
{
94-
windowFrame.Show();
68+
throw new Exception($"File {fileFullPath} doesn't exist!");
9569
}
9670
}
9771
catch (Exception ex)
@@ -102,64 +76,14 @@ public static void EditTemplate(string fileName, string fileFullPath)
10276

10377
public static void ExecuteTemplate(string fileName, string fileFullPath)
10478
{
105-
// TODO: This is opening the document, however it's not executing the query (automatically removing the first line instead).
79+
string CMD_QUERY_EXECUTE = "Query.Execute";
10680

10781
try
10882
{
109-
ThreadHelper.ThrowIfNotOnUIThread();
110-
111-
if (!File.Exists(fileFullPath))
83+
// Ensure the document we are executing is the document we have opened by checking its name
84+
if (DTE.ActiveDocument != null && DTE.ActiveDocument.ProjectItem.Name.Equals(fileName))
11285
{
113-
throw new FileNotFoundException($"File {fileFullPath} doesn't exist!");
114-
}
115-
116-
var openDoc = Package.GetGlobalService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
117-
if (openDoc == null)
118-
{
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}");
86+
DTE.ExecuteCommand(CMD_QUERY_EXECUTE);
16387
}
16488
}
16589
catch (Exception ex)
@@ -168,29 +92,13 @@ public static void ExecuteTemplate(string fileName, string fileFullPath)
16892
}
16993
}
17094

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-
18395
public static void FormatSelection()
18496
{
18597
try
18698
{
187-
ThreadHelper.ThrowIfNotOnUIThread();
188-
189-
DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE;
190-
191-
if (dte.ActiveDocument != null)
99+
if (DTE.ActiveDocument != null)
192100
{
193-
TextSelection selection = (TextSelection)dte.ActiveDocument.Selection;
101+
TextSelection selection = (TextSelection)DTE.ActiveDocument.Selection;
194102

195103
// Format whole text: selection.SelectAll();
196104
string selectedText = selection.Text;
@@ -242,7 +150,15 @@ private static string FormatSelectionUsingSQLServer(string code)
242150

243151
private static Tuple<TSqlParser, SqlScriptGenerator> GetSQLParser(string targetVersion)
244152
{
245-
if (targetVersion == "SQL Server 2019")
153+
if (targetVersion == "SQL Server 2025")
154+
{
155+
return new Tuple<TSqlParser, SqlScriptGenerator>(new TSql170Parser(false), new Sql170ScriptGenerator());
156+
}
157+
else if (targetVersion == "SQL Server 2022")
158+
{
159+
return new Tuple<TSqlParser, SqlScriptGenerator>(new TSql160Parser(false), new Sql160ScriptGenerator());
160+
}
161+
else if (targetVersion == "SQL Server 2019")
246162
{
247163
return new Tuple<TSqlParser, SqlScriptGenerator>(new TSql150Parser(false), new Sql150ScriptGenerator());
248164
}

src/SQLScriptsExplorer.Addin/SQLScriptsExplorer.Addin.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
</ItemGroup>-->
131131
<ItemGroup>
132132
<PackageReference Include="Microsoft.SqlServer.TransactSql.ScriptDom">
133-
<Version>150.4897.1</Version>
133+
<Version>170.64.0</Version>
134134
</PackageReference>
135135
<PackageReference Include="Microsoft.VisualStudio.CoreUtility">
136136
<Version>15.0.26606</Version>

src/SQLScriptsExplorer.Addin/frmSettings.Designer.cs

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/SQLScriptsExplorer.Addin/frmSettings.resx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,4 @@
126126
<metadata name="Valid.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
127127
<value>True</value>
128128
</metadata>
129-
<metadata name="Alias.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
130-
<value>True</value>
131-
</metadata>
132-
<metadata name="FolderPath.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
133-
<value>True</value>
134-
</metadata>
135-
<metadata name="Valid.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
136-
<value>True</value>
137-
</metadata>
138129
</root>

0 commit comments

Comments
 (0)