Skip to content

Commit

Permalink
Merge pull request #10 from justcla/Async-Fix
Browse files Browse the repository at this point in the history
Switch to Asynch package loading.
  • Loading branch information
justcla authored May 6, 2020
2 parents 38141fe + 6651b17 commit 56cdfad
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 52 deletions.
1 change: 1 addition & 0 deletions SublimeVS/ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
SublimeVS
06-May-2020 v1.2.1 Switched package loading to be Async (VS2019 requirement)
05-May-2020 v1.2.0 Upgraded to VS2019
21-May-2018 v1.1.0 Replaced MixEdit with SelectNextOccurrence
08-Aug-2017 v1.0.4 Added WhackWhackTerminal v0.2.4
Expand Down
38 changes: 23 additions & 15 deletions SublimeVS/SublimeSettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class SublimeSettingsManager

private const string SublimeSettingsFileName = @"Shortcuts\SublimeShortcuts.vssettings";

private readonly Package package;
private readonly AsyncPackage package;

private IServiceProvider ServiceProvider
private Microsoft.VisualStudio.Shell.IAsyncServiceProvider AsyncServiceProvider
{
get
{
Expand All @@ -39,18 +39,25 @@ public static SublimeSettingsManager Instance
private set;
}

public static void Initialize(Package package)
public static async System.Threading.Tasks.Task InitializeAsync(AsyncPackage package)
{
Instance = new SublimeSettingsManager(package);
await Instance.InitializeAsync();
}

private SublimeSettingsManager(Package package)
private SublimeSettingsManager(AsyncPackage package)
{
// Register this command with the Global Command Service
this.package = package ?? throw new ArgumentNullException("package");
}

public async System.Threading.Tasks.Task InitializeAsync()
{

if (ServiceProvider.GetService(typeof(IMenuCommandService)) is OleMenuCommandService commandService)
if (await AsyncServiceProvider.GetServiceAsync(typeof(IMenuCommandService)) is OleMenuCommandService commandService)
{
// Switch to main thread before calling AddCommand because it calls GetService
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
commandService.AddCommand(CreateMenuItem(ApplySettingsCmdId, this.ApplySublimeVSSettings));
}
}
Expand All @@ -62,16 +69,16 @@ private MenuCommand CreateMenuItem(int cmdId, EventHandler menuItemCallback)

public void ApplySublimeVSSettings(object sender, EventArgs e)
{
ApplySublimeVSSettings();
ApplySublimeVSSettingsAsync();
}

public void ApplySublimeVSSettings()
public async System.Threading.Tasks.Task ApplySublimeVSSettingsAsync()
{
// Offer to apply Sublime shortcut scheme
ApplyShortcuts("Sublime Text Shortcuts", SublimeSettingsFileName);

// Offer to apply MiniMap (Map Mode Scrollbar)
ApplyMiniMap();

// Offer to apply Sublime shortcut scheme
await ApplyShortcutsAsync("Sublime Text Shortcuts", SublimeSettingsFileName);
}

//-------- MiniMap Settings --------
Expand Down Expand Up @@ -101,7 +108,7 @@ private void ApplyMapModeScrollbar()
{
try
{
var dte2 = (DTE2)(ServiceProvider.GetService(typeof(DTE)));
var dte2 = (DTE2)AsyncServiceProvider.GetServiceAsync(typeof(DTE));
UpdateSetting(dte2, "TextEditor", "AllLanguages", "UseMapMode", true);
UpdateSetting(dte2, "TextEditor", "AllLanguages", "OverviewWidth", (short)83);
}
Expand All @@ -119,13 +126,13 @@ private static void UpdateSetting(DTE2 dte2, string category, string page, strin

//------------ Shortcut Settings --------------

public void ApplyShortcuts(string shortcutSchemeName, string vssettingsFilename)
public async System.Threading.Tasks.Task ApplyShortcutsAsync(string shortcutSchemeName, string vssettingsFilename)
{

//Ask user if they want to apply shortcuts
if (ConfirmApplyShortcuts(shortcutSchemeName))
{
ImportUserSettings(vssettingsFilename);
await ImportUserSettingsAsync(vssettingsFilename);
}
}

Expand All @@ -140,16 +147,17 @@ private bool ConfirmApplyShortcuts(string shortcutSchemeName)
return MessageBox.Show(message, title, MessageBoxButtons.OKCancel) == DialogResult.OK;
}

private void ImportUserSettings(string settingsFileName)
private async System.Threading.Tasks.Task ImportUserSettingsAsync(string settingsFileName)
{
if (ServiceProvider.GetService(typeof(SVsUIShell)) is IVsUIShell shell)
if (await AsyncServiceProvider.GetServiceAsync(typeof(SVsUIShell)) is IVsUIShell shell)
{
// import the settings file into Visual Studio
var asmDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var settingsFilePath = Path.Combine(asmDirectory, settingsFileName);
var group = VSConstants.CMDSETID.StandardCommandSet2K_guid;

object arguments = string.Format(CultureInfo.InvariantCulture, "-import:\"{0}\"", settingsFilePath);
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
shell.PostExecCommand(ref group, (uint)VSConstants.VSStd2KCmdID.ManageUserSettings, 0, ref arguments);
}
}
Expand Down
34 changes: 18 additions & 16 deletions SublimeVS/SublimeVS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,12 @@
<HintPath>..\packages\Microsoft.VisualStudio.Imaging.14.3.25407\lib\net45\Microsoft.VisualStudio.Imaging.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.Imaging.Interop.14.0.DesignTime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Imaging.Interop.14.0.DesignTime.14.3.26930\lib\net20\Microsoft.VisualStudio.Imaging.Interop.14.0.DesignTime.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Microsoft.VisualStudio.OLE.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\Microsoft.VisualStudio.OLE.Interop.7.10.6070\lib\Microsoft.VisualStudio.OLE.Interop.dll</HintPath>
<Private>True</Private>
<HintPath>..\packages\Microsoft.VisualStudio.OLE.Interop.7.10.6071\lib\Microsoft.VisualStudio.OLE.Interop.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.Shell.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Shell.14.0.14.3.25407\lib\Microsoft.VisualStudio.Shell.14.0.dll</HintPath>
Expand All @@ -181,27 +184,26 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\Microsoft.VisualStudio.Shell.Interop.7.10.6071\lib\Microsoft.VisualStudio.Shell.Interop.dll</HintPath>
<Private>True</Private>
<HintPath>..\packages\Microsoft.VisualStudio.Shell.Interop.7.10.6072\lib\net11\Microsoft.VisualStudio.Shell.Interop.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.Shell.Interop.10.0, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Shell.Interop.10.0.10.0.30320\lib\net20\Microsoft.VisualStudio.Shell.Interop.10.0.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
<HintPath>..\packages\Microsoft.VisualStudio.Shell.Interop.10.0.10.0.30319\lib\Microsoft.VisualStudio.Shell.Interop.10.0.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.Shell.Interop.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Shell.Interop.11.0.11.0.61031\lib\net20\Microsoft.VisualStudio.Shell.Interop.11.0.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
<HintPath>..\packages\Microsoft.VisualStudio.Shell.Interop.11.0.11.0.61030\lib\Microsoft.VisualStudio.Shell.Interop.11.0.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.Shell.Interop.12.0, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Shell.Interop.12.0.12.0.30111\lib\net20\Microsoft.VisualStudio.Shell.Interop.12.0.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Microsoft.VisualStudio.Shell.Interop.14.0.DesignTime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Shell.Interop.14.0.DesignTime.14.3.26929\lib\net20\Microsoft.VisualStudio.Shell.Interop.14.0.DesignTime.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
<HintPath>..\packages\Microsoft.VisualStudio.Shell.Interop.12.0.12.0.30110\lib\Microsoft.VisualStudio.Shell.Interop.12.0.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.Shell.Interop.8.0, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\Microsoft.VisualStudio.Shell.Interop.8.0.8.0.50727\lib\Microsoft.VisualStudio.Shell.Interop.8.0.dll</HintPath>
<Private>True</Private>
<HintPath>..\packages\Microsoft.VisualStudio.Shell.Interop.8.0.8.0.50728\lib\net11\Microsoft.VisualStudio.Shell.Interop.8.0.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.Shell.Interop.9.0, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\Microsoft.VisualStudio.Shell.Interop.9.0.9.0.30729\lib\Microsoft.VisualStudio.Shell.Interop.9.0.dll</HintPath>
Expand All @@ -224,12 +226,10 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TextManager.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\Microsoft.VisualStudio.TextManager.Interop.7.10.6070\lib\Microsoft.VisualStudio.TextManager.Interop.dll</HintPath>
<Private>True</Private>
<HintPath>..\packages\Microsoft.VisualStudio.TextManager.Interop.7.10.6071\lib\net11\Microsoft.VisualStudio.TextManager.Interop.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TextManager.Interop.8.0, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\Microsoft.VisualStudio.TextManager.Interop.8.0.8.0.50727\lib\Microsoft.VisualStudio.TextManager.Interop.8.0.dll</HintPath>
<Private>True</Private>
<HintPath>..\packages\Microsoft.VisualStudio.TextManager.Interop.8.0.8.0.50728\lib\net11\Microsoft.VisualStudio.TextManager.Interop.8.0.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.Threading, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Threading.14.1.111\lib\net45\Microsoft.VisualStudio.Threading.dll</HintPath>
Expand Down Expand Up @@ -279,9 +279,11 @@
<Error Condition="!Exists('..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.VisualStudio.Threading.Analyzers.15.7.18\build\Microsoft.VisualStudio.Threading.Analyzers.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VisualStudio.Threading.Analyzers.15.7.18\build\Microsoft.VisualStudio.Threading.Analyzers.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.16\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.16\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets'))" />
</Target>
<Import Project="..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.targets" Condition="Exists('..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.targets')" />
<Import Project="..\packages\Microsoft.VisualStudio.Threading.Analyzers.15.7.18\build\Microsoft.VisualStudio.Threading.Analyzers.targets" Condition="Exists('..\packages\Microsoft.VisualStudio.Threading.Analyzers.15.7.18\build\Microsoft.VisualStudio.Threading.Analyzers.targets')" />
<Import Project="..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.16\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets" Condition="Exists('..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.16\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
26 changes: 14 additions & 12 deletions SublimeVS/SublimeVSPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace SublimeVS
{
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.2.0", IconResourceID = 400)] // Info on this package for Help/About
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[InstalledProductRegistration("#110", "#112", "1.2.1", IconResourceID = 400)] // Info on this package for Help/About
[ProvideMenuResource("Menus.ctmenu", 1)]
[Guid(SublimeVSPackage.PackageGuidString)]
[ProvideAutoLoad(VSConstants.UICONTEXT.ShellInitialized_string)]
[ProvideAutoLoad(VSConstants.UICONTEXT.ShellInitialized_string, PackageAutoLoadFlags.BackgroundLoad)]
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "pkgdef, VS and vsixmanifest are valid VS terms")]
public sealed class SublimeVSPackage : Package
public sealed class SublimeVSPackage : AsyncPackage
{
public const string PackageGuidString = "10faf7a3-f1bb-4836-9e6b-b5f52bd88031";
private const string SID_SVsSettingsPersistenceManager = "9B164E40-C3A2-4363-9BC5-EB4039DEF653";

private const string SublimeSettingsFileName = @"Shortcuts\SublimeShortcuts.vssettings";
//private const string SublimeSettingsFileName = @"Shortcuts\SublimeShortcuts.vssettings";

public static ISettingsManager SettingsManager { get; private set; }

Expand All @@ -31,17 +32,18 @@ public SublimeVSPackage()
/// Initialization of the package; this method is called right after the package is sited, so this is the place
/// where you can put all the initialization code that rely on services provided by VisualStudio.
/// </summary>
protected override void Initialize()
protected override async System.Threading.Tasks.Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await base.InitializeAsync(cancellationToken, progress);

// Initialize settings manager (TODO: could be done lazily on get)
SettingsManager = (ISettingsManager)GetGlobalService(typeof(SVsSettingsPersistenceManager));

base.Initialize();
SublimeSettingsManager.Initialize(this);
CheckFirstTimeSetup();
await SublimeSettingsManager.InitializeAsync(this);
await CheckFirstTimeSetupAsync();
}

private void CheckFirstTimeSetup()
private async System.Threading.Tasks.Task CheckFirstTimeSetupAsync()
{
// Check if we need to do first-time setup
const string firstTimeRunSettingName = "SublimeVSSetupAck02";
Expand All @@ -62,9 +64,9 @@ private void CheckFirstTimeSetup()
if (MessageBox.Show(message, title, MessageBoxButtons.OKCancel) == DialogResult.OK)
{
// Apply First Time Settings();
SettingsManager.SetValueAsync(firstTimeRunSettingName, true, isMachineLocal: true);
await SettingsManager.SetValueAsync(firstTimeRunSettingName, true, isMachineLocal: true);

SublimeSettingsManager.Instance.ApplySublimeVSSettings();
await SublimeSettingsManager.Instance.ApplySublimeVSSettingsAsync();
}
}
}
Expand Down
Loading

0 comments on commit 56cdfad

Please sign in to comment.