Skip to content

Commit

Permalink
Dedupe code
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Sep 7, 2020
1 parent 1a8b7c9 commit 9854857
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
11 changes: 11 additions & 0 deletions Vsix/CodeConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Windows;
using ICSharpCode.CodeConverter;
using ICSharpCode.CodeConverter.Shared;
using ICSharpCode.CodeConverter.VB;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServices;
using Microsoft.VisualStudio.Shell;
Expand Down Expand Up @@ -307,5 +308,15 @@ public static bool IsVBFileName(string fileName)
}
return false;
}

public async Task ConvertTextBestEffortAsync<TLanguageConversion>(CancellationToken cancellationToken) where TLanguageConversion : ILanguageConversion, new()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
string text = Clipboard.GetText();
var convertTextOnly = await ProjectConversion.ConvertTextAsync<TLanguageConversion>(text,
new TextConversionOptions(DefaultReferences.NetStandard2),
cancellationToken: cancellationToken);
await VisualStudioInteraction.WriteToCurrentWindowAsync(_serviceProvider, convertTextOnly.ConvertedCode);
}
}
}
11 changes: 5 additions & 6 deletions Vsix/PasteAsCS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@ await VisualStudioInteraction.GetFirstSelectedSpanInCurrentViewAsync(ServiceProv

private async Task CodeEditorMenuItemCallbackAsync(CancellationToken cancellationToken)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
string text = Clipboard.GetText();
var convertTextOnly = await ProjectConversion.ConvertTextAsync<VBToCSConversion>(text,
new TextConversionOptions(DefaultReferences.NetStandard2),
cancellationToken: cancellationToken);
await VisualStudioInteraction.WriteToCurrentWindowAsync(ServiceProvider, convertTextOnly.ConvertedCode);
try {
await _codeConversion.ConvertTextBestEffortAsync<VBToCSConversion>(cancellationToken);
} catch (Exception ex) {
await VisualStudioInteraction.ShowExceptionAsync(ex);
}
}
}
}
11 changes: 5 additions & 6 deletions Vsix/PasteAsVB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,11 @@ await VisualStudioInteraction.GetFirstSelectedSpanInCurrentViewAsync(ServiceProv

private async Task CodeEditorMenuItemCallbackAsync(CancellationToken cancellationToken)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
string text = Clipboard.GetText();
var convertTextOnly = await ProjectConversion.ConvertTextAsync<CSToVBConversion>(text,
new TextConversionOptions(DefaultReferences.NetStandard2),
cancellationToken: cancellationToken);
await VisualStudioInteraction.WriteToCurrentWindowAsync(ServiceProvider, convertTextOnly.ConvertedCode);
try {
await _codeConversion.ConvertTextBestEffortAsync<CSToVBConversion>(cancellationToken);
} catch (Exception ex) {
await VisualStudioInteraction.ShowExceptionAsync(ex);
}
}
}
}

0 comments on commit 9854857

Please sign in to comment.