Skip to content

Commit

Permalink
fix: after vs load init (#338)
Browse files Browse the repository at this point in the history
* fix: delay force init until project exists

* fix: execute LanguageClientManagerOnLanguageClientNotInitializedAsync once at a time
  • Loading branch information
ShawkyZ authored Dec 17, 2024
1 parent 5cd34f8 commit 1d27dd7
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions Snyk.VisualStudio.Extension.2022/SnykVSPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using EnvDTE;
using Microsoft;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.PlatformUI;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Serilog;
Expand Down Expand Up @@ -64,6 +65,7 @@ public sealed class SnykVSPackage : AsyncPackage, ISnykOptionsProvider
new TaskCompletionSource<bool>();

public static SnykVSPackage Instance;
private readonly SemaphoreSlim languageClientInitSemaphore = new SemaphoreSlim(1, 1);

private ISnykServiceProvider serviceProvider;

Expand Down Expand Up @@ -229,7 +231,7 @@ private async Task InitializeLanguageClientAsync()
{
try
{
this.serviceProvider.LanguageClientManager.OnLanguageClientNotInitializedAsync += LanguageClientManagerOnOnLanguageClientNotInitializedAsync;
this.serviceProvider.LanguageClientManager.OnLanguageClientNotInitializedAsync += LanguageClientManagerOnLanguageClientNotInitializedAsync;
this.serviceProvider.LanguageClientManager.OnLanguageServerReadyAsync += LanguageClientManagerOnOnLanguageServerReadyAsync;
if (!LanguageClientHelper.IsLanguageServerReady())
{
Expand Down Expand Up @@ -257,21 +259,47 @@ private async Task LanguageClientManagerOnOnLanguageServerReadyAsync(object send
}

private Window tempOpenedFileWindow;
private async Task LanguageClientManagerOnOnLanguageClientNotInitializedAsync(object sender, SnykLanguageServerEventArgs args)
private async Task LanguageClientManagerOnLanguageClientNotInitializedAsync(object sender, SnykLanguageServerEventArgs args)
{
await JoinableTaskFactory.SwitchToMainThreadAsync();
await languageClientInitSemaphore.WaitAsync(DisposalToken);

ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
try
{
await JoinableTaskFactory.SwitchToMainThreadAsync();
while (!LanguageClientHelper.IsLanguageServerReady())
{
var isSolutionOrFolderOpen = SnykSolutionService.Instance.IsSolutionOpen();
if (isSolutionOrFolderOpen)
{
var dte = (DTE)await GetServiceAsync(typeof(DTE));
if (dte == null) return;

var dte = (DTE)await GetServiceAsync(typeof(DTE));
if (dte == null) return;
// Get the path to the file within the installed extension directory
var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (assemblyLocation == null) return;

// Get the path to the file within the installed extension directory
var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (assemblyLocation == null) return;
var filePath = Path.Combine(assemblyLocation, "Resources", "SnykLsInit.cs");

var filePath = Path.Combine(assemblyLocation, "Resources", "SnykLsInit.cs");
// Open the file
tempOpenedFileWindow =
dte.ItemOperations.OpenFile(filePath, EnvDTE.Constants.vsViewKindTextView);
return;
}

// Open the file
tempOpenedFileWindow = dte.ItemOperations.OpenFile(filePath, EnvDTE.Constants.vsViewKindTextView);
await Task.Delay(3000, DisposalToken);
}
}
catch (Exception ex)
{
Logger.Error("LanguageClientManagerOnLanguageClientNotInitializedAsync Failed with {Ex}", ex);
}
finally
{
languageClientInitSemaphore.Release();
}
}).FireAndForget();
}

private async Task InitializeGeneralOptionsAsync()
Expand Down

0 comments on commit 1d27dd7

Please sign in to comment.