Skip to content

Commit c39c031

Browse files
Phase 2: Wired up Extensibility commands to CodeConversion logic
1 parent b0481d1 commit c39c031

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

Vsix/CodeConverterPackage.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public sealed class CodeConverterPackage : AsyncPackage
8686
public const string ConvertableSolutionMenuVisibilityGuid = "8e7192d0-28b7-4fe7-8d84-82c1db98d459";
8787

8888
internal Cancellation PackageCancellation { get; } = new();
89+
90+
internal static CodeConversion CodeConversionInstance { get; private set; }
91+
internal static CodeConverterPackage Instance { get; private set; }
8992

9093
/// <summary>
9194
/// Initializes a new instance of package class.
@@ -110,6 +113,8 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
110113
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
111114
var visualStudioWorkspace = componentModel.GetService<VisualStudioWorkspace>();
112115
var codeConversion = await CodeConversion.CreateAsync(this, visualStudioWorkspace, this.GetDialogPageAsync<ConverterOptionsPage>);
116+
CodeConversionInstance = codeConversion;
117+
Instance = this;
113118
ConvertCSToVBCommand.Initialize(this, oleMenuCommandService, codeConversion);
114119
ConvertVBToCSCommand.Initialize(this, oleMenuCommandService, codeConversion);
115120
PasteAsVB.Initialize(this, oleMenuCommandService, codeConversion);

Vsix/ConvertCSToVBExtensibilityCommand.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,27 @@ public ConvertCSToVBExtensibilityCommand(CodeConverterPackage package)
1919

2020
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
2121
{
22-
// This will be wired up to the existing logic in Phase 2
23-
await Task.CompletedTask;
22+
var codeConversion = CodeConverterPackage.CodeConversionInstance;
23+
var serviceProvider = CodeConverterPackage.Instance;
24+
if (codeConversion == null || serviceProvider == null) return;
25+
26+
var itemsPath = await VisualStudioInteraction.GetSelectedItemsPathAsync(CodeConversion.IsCSFileName);
27+
if (itemsPath.Count > 0)
28+
{
29+
await codeConversion.ConvertDocumentsAsync<ICSharpCode.CodeConverter.VB.CSToVBConversion>(itemsPath, cancellationToken);
30+
return;
31+
}
32+
33+
var projects = await VisualStudioInteraction.GetSelectedProjectsAsync(".csproj");
34+
if (projects.Count > 0)
35+
{
36+
await codeConversion.ConvertProjectsAsync<ICSharpCode.CodeConverter.VB.CSToVBConversion>(projects, cancellationToken);
37+
return;
38+
}
39+
40+
(string filePath, var selection) = await VisualStudioInteraction.GetCurrentFilenameAndSelectionAsync(serviceProvider, CodeConversion.IsCSFileName, false);
41+
if (filePath != null && selection != null) {
42+
await codeConversion.ConvertDocumentAsync<ICSharpCode.CodeConverter.VB.CSToVBConversion>(filePath, selection.Value, cancellationToken);
43+
}
2444
}
2545
}

Vsix/ConvertVBToCSExtensibilityCommand.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,27 @@ public ConvertVBToCSExtensibilityCommand(CodeConverterPackage package)
1919

2020
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
2121
{
22-
// This will be wired up to the existing logic in Phase 2
23-
await Task.CompletedTask;
22+
var codeConversion = CodeConverterPackage.CodeConversionInstance;
23+
var serviceProvider = CodeConverterPackage.Instance;
24+
if (codeConversion == null || serviceProvider == null) return;
25+
26+
var itemsPath = await VisualStudioInteraction.GetSelectedItemsPathAsync(CodeConversion.IsVBFileName);
27+
if (itemsPath.Count > 0)
28+
{
29+
await codeConversion.ConvertDocumentsAsync<ICSharpCode.CodeConverter.CSharp.VBToCSConversion>(itemsPath, cancellationToken);
30+
return;
31+
}
32+
33+
var projects = await VisualStudioInteraction.GetSelectedProjectsAsync(".vbproj");
34+
if (projects.Count > 0)
35+
{
36+
await codeConversion.ConvertProjectsAsync<ICSharpCode.CodeConverter.CSharp.VBToCSConversion>(projects, cancellationToken);
37+
return;
38+
}
39+
40+
(string filePath, var selection) = await VisualStudioInteraction.GetCurrentFilenameAndSelectionAsync(serviceProvider, CodeConversion.IsVBFileName, false);
41+
if (filePath != null && selection != null) {
42+
await codeConversion.ConvertDocumentAsync<ICSharpCode.CodeConverter.CSharp.VBToCSConversion>(filePath, selection.Value, cancellationToken);
43+
}
2444
}
2545
}

0 commit comments

Comments
 (0)