From d247cc92cd526e10a69a3bd88b4f9689c6f2b03c Mon Sep 17 00:00:00 2001 From: GrahamTheCoder Date: Sun, 15 Sep 2024 18:51:57 +0100 Subject: [PATCH] Closer to compiling --- Directory.Build.props | 2 +- Vsix/CodeConversion.cs | 1 - Vsix/CodeConverterPackage.cs | 31 ++++----------- Vsix/ConverterOptionsPage.cs | 6 ++- Vsix/ConverterSettingsAccessor.cs | 28 +++++++++++++ Vsix/OleMenuCommandWithBlockingStatus.cs | 50 ------------------------ Vsix/ServiceProviderExtensions.cs | 9 ----- 7 files changed, 42 insertions(+), 85 deletions(-) create mode 100644 Vsix/ConverterSettingsAccessor.cs delete mode 100644 Vsix/OleMenuCommandWithBlockingStatus.cs diff --git a/Directory.Build.props b/Directory.Build.props index d85a44d5f..189c4145e 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,7 +2,7 @@ false true - $(NoWarn);1998;NU5100;NU1900;NU1901;NU1902;NU1903;NU1904 + $(NoWarn);1998;NU5100;NU1900;NU1901;NU1902;NU1903;NU1904;VSEXTPREVIEW_SETTINGS true 10.0 9.2.6.0 diff --git a/Vsix/CodeConversion.cs b/Vsix/CodeConversion.cs index c99b94caa..0111c4caf 100644 --- a/Vsix/CodeConversion.cs +++ b/Vsix/CodeConversion.cs @@ -7,7 +7,6 @@ using System.Threading; using System.Threading.Tasks; using System.Windows; -using CodeConv.Shared.Util; using EnvDTE; using ICSharpCode.CodeConverter.Common; using Microsoft.CodeAnalysis.Text; diff --git a/Vsix/CodeConverterPackage.cs b/Vsix/CodeConverterPackage.cs index 7094cfe95..43202b504 100644 --- a/Vsix/CodeConverterPackage.cs +++ b/Vsix/CodeConverterPackage.cs @@ -1,13 +1,7 @@ -using System; -using System.ComponentModel.Design; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.InteropServices; -using System.Threading; -using System.Threading.Tasks; +using System.Diagnostics.CodeAnalysis; +using ICSharpCode.CodeConverter.VsExtension.ICSharpCode.CodeConverter.VsExtension; using Microsoft.Extensions.DependencyInjection; using Microsoft.VisualStudio.Extensibility; -using Microsoft.VisualStudio.LanguageServices; -using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Threading; using Task = System.Threading.Tasks.Task; #pragma warning disable CEE0012 // TODO: I can't find any information on what this means @@ -15,16 +9,11 @@ namespace ICSharpCode.CodeConverter.VsExtension; /// -/// Implements the VS package exposed by this assembly. -/// -/// This package will load when: +/// This package should load when: /// * Visual Studio has been configured not to support UIContextRules and has a solution with a csproj or vbproj /// * Someone clicks one of the menu items /// * Someone opens the options page (it doesn't need to load in this case, but it seems to anyway) /// -/// -/// Until the package is loaded, converting a multiple selection of projects won't work because there's no way to set a ProvideUIContextRule that covers that case -/// [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "pkgdef, VS and vsixmanifest are valid VS terms")] [VisualStudioContribution] public sealed class CodeConverterPackage : Extension @@ -39,10 +28,6 @@ public sealed class CodeConverterPackage : Extension public CodeConverterPackage(VisualStudioExtensibility extensibility) { _extensibility = extensibility; - // Inside this method you can place any initialization code that does not require - // any Visual Studio service because at this point the package object is created but - // not sited yet inside Visual Studio environment. The place to do all the other - // initialization is the Initialize method. } protected override void Dispose(bool disposing) @@ -63,20 +48,20 @@ public override ExtensionConfiguration ExtensionConfiguration { } } - [Experimental("VSEXTPREVIEW_SETTINGS")] + protected override void InitializeServices(IServiceCollection serviceCollection) { base.InitializeServices(serviceCollection); this.InitializeSettingsAsync(_extensibility).Forget(); } - [Experimental("VSEXTPREVIEW_SETTINGS")] + private async Task InitializeSettingsAsync(VisualStudioExtensibility extensibility) { await extensibility.Settings().SubscribeAsync( [ConverterSettings.ConverterSettingsCategory], - CancellationToken.None, - values => { - // look up in dictionary the values + CancellationToken.None, values => { + var settingsAccessor = new ConverterSettingsAccessor(values); + //TODO: Bind/pass this somewhere }); } } \ No newline at end of file diff --git a/Vsix/ConverterOptionsPage.cs b/Vsix/ConverterOptionsPage.cs index 6a9411ebd..aa269ead0 100644 --- a/Vsix/ConverterOptionsPage.cs +++ b/Vsix/ConverterOptionsPage.cs @@ -10,7 +10,6 @@ namespace ICSharpCode.CodeConverter.VsExtension; /// /// Sample https://github.com/microsoft/VSExtensibility/blob/main/New_Extensibility_Model/Samples/SettingsSample/SettingDefinitions.cs /// -[Experimental("VSEXTPREVIEW_SETTINGS")] internal static class ConverterSettings { [VisualStudioContribution] @@ -39,3 +38,8 @@ internal static class ConverterSettings Minimum = 1, }; } + +namespace ICSharpCode.CodeConverter.VsExtension +{ +} + diff --git a/Vsix/ConverterSettingsAccessor.cs b/Vsix/ConverterSettingsAccessor.cs new file mode 100644 index 000000000..b0249fc25 --- /dev/null +++ b/Vsix/ConverterSettingsAccessor.cs @@ -0,0 +1,28 @@ +using Microsoft.VisualStudio.Extensibility.Settings; + +namespace ICSharpCode.CodeConverter.VsExtension.ICSharpCode.CodeConverter.VsExtension; + +public class ConverterSettingsAccessor +{ + private readonly SettingValues _settings; + + public ConverterSettingsAccessor(SettingValues settings) + { + _settings = settings ?? throw new ArgumentNullException(nameof(settings)); + } + + public bool CopyResultToClipboardForSingleDocument => GetBooleanSetting(ConverterSettings.CopyResultToClipboardForSingleDocument); + public bool AlwaysOverwriteFiles => GetBooleanSetting(ConverterSettings.AlwaysOverwriteFiles); + public bool CreateBackups => GetBooleanSetting(ConverterSettings.CreateBackups); + public int FormattingTimeout => GetIntegerSetting(ConverterSettings.FormattingTimeout); + + private bool GetBooleanSetting(Setting.Boolean setting) + { + return _settings.TryGetValue(setting.FullId, out var value) ? value.ValueOrDefault(setting.DefaultValue) : setting.DefaultValue; + } + + private int GetIntegerSetting(Setting.Integer setting) + { + return _settings.TryGetValue(setting.FullId, out var value) ? value.ValueOrDefault(setting.DefaultValue) : setting.DefaultValue; + } +} \ No newline at end of file diff --git a/Vsix/OleMenuCommandWithBlockingStatus.cs b/Vsix/OleMenuCommandWithBlockingStatus.cs deleted file mode 100644 index 262760faa..000000000 --- a/Vsix/OleMenuCommandWithBlockingStatus.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.ComponentModel.Design; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.VisualStudio.Shell; -using Microsoft.VisualStudio.Threading; -using Task = System.Threading.Tasks.Task; - -namespace ICSharpCode.CodeConverter.VsExtension; - -internal class OleMenuCommandWithBlockingStatus -{ - private readonly JoinableTaskFactory _joinableTaskFactory; - private readonly OleMenuCommand _command; - - public OleMenuCommandWithBlockingStatus(JoinableTaskFactory joinableTaskFactory, Cancellation _packageCancellation, Func callbackAsync, CommandID menuCommandId) - { - _joinableTaskFactory = joinableTaskFactory; - _command = new OleMenuCommand(Execute, menuCommandId); - - void Execute(object sender, EventArgs eventArgs) - { - var cancellationTokenSource = _packageCancellation.ResetCommandCancellation(); - - async Task ExecuteAsync() - { - await TaskScheduler.Default; - await callbackAsync(cancellationTokenSource.Token); - } - _joinableTaskFactory.RunAsync(ExecuteAsync).Task.Forget(); - } - } - - public event Func BeforeQueryStatus { - add => _command.BeforeQueryStatus += WaitFor(value); - remove => _command.BeforeQueryStatus -= WaitFor(value); - } - - public static implicit operator OleMenuCommand(OleMenuCommandWithBlockingStatus oleMenuCommandWithBlockingCommand) - { - return oleMenuCommandWithBlockingCommand._command; - } - - private EventHandler WaitFor(Func task) - { - return (o, a) => { - _joinableTaskFactory.Run(() => task(o, a)); - }; - } -} \ No newline at end of file diff --git a/Vsix/ServiceProviderExtensions.cs b/Vsix/ServiceProviderExtensions.cs index 47df19a2f..cefbd672f 100644 --- a/Vsix/ServiceProviderExtensions.cs +++ b/Vsix/ServiceProviderExtensions.cs @@ -10,15 +10,6 @@ public static async Task GetServiceAsync(this IAsyncServiceProvider provid { return (T)(await provider.GetServiceAsync(typeof(T))); } - - public static async Task GetDialogPageAsync(this Package provider) where T : DialogPage - { - await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); - var dialogPage = (T) provider.GetDialogPage(typeof(T)); - await TaskScheduler.Default; - return dialogPage; - } - public static async Task GetServiceAsync(this IAsyncServiceProvider provider) { return (TReturn)(await provider.GetServiceAsync(typeof(TService)));