Skip to content

Commit

Permalink
WIP try using settings directly rather than through accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Sep 15, 2024
1 parent d247cc9 commit e733518
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
38 changes: 17 additions & 21 deletions Vsix/CodeConversion.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using EnvDTE;
using ICSharpCode.CodeConverter.Common;
using ICSharpCode.CodeConverter.VsExtension.ICSharpCode.CodeConverter.VsExtension;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Settings;
using Microsoft.VisualStudio.LanguageServices;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
Expand All @@ -20,8 +17,7 @@ namespace ICSharpCode.CodeConverter.VsExtension;

internal class CodeConversion
{
[Experimental("VSEXTPREVIEW_SETTINGS")] public Func<Task<ConverterSettings>> GetOptions { get; }
private readonly IAsyncServiceProvider _serviceProvider;
private readonly VisualStudioExtensibility _serviceProvider;
private readonly JoinableTaskFactory _joinableTaskFactory;
private readonly VisualStudioWorkspace _visualStudioWorkspace;
private static readonly string Intro = Environment.NewLine + Environment.NewLine + new string(Enumerable.Repeat('-', 80).ToArray()) + Environment.NewLine;
Expand All @@ -30,21 +26,17 @@ internal class CodeConversion

private string SolutionDir => Path.GetDirectoryName(_visualStudioWorkspace.CurrentSolution.FilePath);

public static async Task<CodeConversion> CreateAsync(CodeConverterPackage serviceProvider, VisualStudioWorkspace visualStudioWorkspace, Func<Task<ConverterOptionsPage>> getOptions)
public static async Task<CodeConversion> CreateAsync(VisualStudioExtensibility serviceProvider, VisualStudioWorkspace visualStudioWorkspace)
{
var options = await getOptions();
AppDomain.CurrentDomain.UseVersionAgnosticAssemblyResolution(options.BypassAssemblyLoadingErrors);

return new CodeConversion(serviceProvider, serviceProvider.JoinableTaskFactory, serviceProvider.PackageCancellation, visualStudioWorkspace,
getOptions, await OutputWindow.CreateAsync());
return new CodeConversion(serviceProvider, ThreadHelper.JoinableTaskFactory, serviceProvider, visualStudioWorkspace,
await OutputWindow.CreateAsync());
}

public CodeConversion(IAsyncServiceProvider serviceProvider,
public CodeConversion(VisualStudioExtensibility serviceProvider,
JoinableTaskFactory joinableTaskFactory, Cancellation packageCancellation, VisualStudioWorkspace visualStudioWorkspace,
Func<Task<ConverterOptionsPage>> getOptions, OutputWindow outputWindow)
Func<Task<ConverterSettingsAccessor>> getOptions, OutputWindow outputWindow)
{
JoinableTaskFactorySingleton.Instance = joinableTaskFactory;
GetOptions = getOptions;
_serviceProvider = serviceProvider;
_joinableTaskFactory = joinableTaskFactory;
_visualStudioWorkspace = visualStudioWorkspace;
Expand Down Expand Up @@ -94,7 +86,8 @@ await _joinableTaskFactory.RunAsync(async () => {
return result;
});

if ((await GetOptions()).CopyResultToClipboardForSingleDocument) {
var readEffectiveValueAsync = await ReadSettingValueAsync(ConverterSettings.CopyResultToClipboardForSingleDocument, cancellationToken);
if (readEffectiveValueAsync.Value) {
await SetClipboardTextOnUiThreadAsync(conversionResult.ConvertedCode ?? conversionResult.GetExceptionsAsString());
await _outputWindow.WriteToOutputWindowAsync(Environment.NewLine + "Conversion result copied to clipboard.");
await VisualStudioInteraction.ShowMessageBoxAsync("Conversion result copied to clipboard.", $"Conversion result copied to clipboard. {conversionResult.GetExceptionsAsString()}", false);
Expand All @@ -106,6 +99,11 @@ await _joinableTaskFactory.RunAsync(async () => {
}
}

private async Task<SettingValue<T>> ReadSettingValueAsync<T>(Setting<T> setting, CancellationToken cancellationToken) where T: Setting<T>
{
return await _serviceProvider.Settings().ReadEffectiveValueAsync(setting.FullId, cancellationToken);
}

/// <remarks>
/// https://github.com/icsharpcode/CodeConverter/issues/592
/// https://github.com/dotnet/roslyn/issues/6615
Expand Down Expand Up @@ -161,8 +159,6 @@ private async Task WriteConvertedFilesAndShowSummaryAsync(IAsyncEnumerable<Conve

private async Task FinalizeConversionAsync(List<string> files, List<string> errors, string longestFilePath, List<ConversionResult> filesToOverwrite)
{
var options = await GetOptions();

var pathsToOverwrite = filesToOverwrite.Select(f => PathRelativeToSolutionDir(f.SourcePathOrNull));
var shouldOverwriteSolutionAndProjectFiles =
filesToOverwrite.Any() &&
Expand Down
3 changes: 3 additions & 0 deletions Vsix/Vsix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<VssdkCompatibleExtension>true</VssdkCompatibleExtension>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\CodeConverter\Common\DefaultReferences.cs" Link="DefaultReferences.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Extensibility.Sdk" Version="17.11.40261" />
Expand Down

0 comments on commit e733518

Please sign in to comment.