Skip to content

Commit

Permalink
Handle errors when initializing the extension
Browse files Browse the repository at this point in the history
  • Loading branch information
thedemons committed Dec 13, 2023
1 parent 980861a commit d556405
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 44 deletions.
44 changes: 35 additions & 9 deletions CodeiumVS/CodeiumVSPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,33 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
Instance = this;

await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
//await this.SatisfyImportsOnceAsync();

LanguageServer = new LanguageServer();
OutputWindow = new OutputWindow();
SettingsPage = (SettingsPage)GetDialogPage(typeof(SettingsPage));
LanguageServer = new LanguageServer();
OutputWindow = new OutputWindow();
NotificationAuth = new NotificationInfoBar();

await this.RegisterCommandsAsync();
try
{
SettingsPage = GetDialogPage(typeof(SettingsPage)) as SettingsPage;
}
catch (Exception) { }

if (SettingsPage == null)
{
await LogAsync($"CodeiumVSPackage.InitializeAsync: Failed to get settings page, using the default settings");
SettingsPage = new SettingsPage();
}

try
{
await this.RegisterCommandsAsync();
}
catch (Exception ex)
{
await LogAsync($"CodeiumVSPackage.InitializeAsync: Failed to register commands; Exception {ex}");
await VS.MessageBox.ShowErrorAsync("Codeium: Failed to register commands.", "Codeium might not work correctly. Please check the output window for more details.");
}

await LanguageServer.InitializeAsync();
await LogAsync("Codeium Extension for Visual Studio");
}
Expand Down Expand Up @@ -87,10 +106,17 @@ public async Task UpdateSignedInStateAsync()
{
await JoinableTaskFactory.SwitchToMainThreadAsync();

OleMenuCommandService obj = (await GetServiceAsync(typeof(IMenuCommandService))) as OleMenuCommandService;
obj.FindCommand(new CommandID(PackageGuids.CodeiumVS, PackageIds.SignIn)).Visible = !IsSignedIn();
obj.FindCommand(new CommandID(PackageGuids.CodeiumVS, PackageIds.SignOut)).Visible = IsSignedIn();
obj.FindCommand(new CommandID(PackageGuids.CodeiumVS, PackageIds.EnterAuthToken)).Visible = !IsSignedIn();

if ((await GetServiceAsync(typeof(IMenuCommandService))) is OleMenuCommandService cmdService)
{
MenuCommand? commandSignIn = cmdService.FindCommand(new CommandID(PackageGuids.CodeiumVS, PackageIds.SignIn));
MenuCommand? commandSignOut = cmdService.FindCommand(new CommandID(PackageGuids.CodeiumVS, PackageIds.SignOut));
MenuCommand? commandEnterToken = cmdService.FindCommand(new CommandID(PackageGuids.CodeiumVS, PackageIds.EnterAuthToken));

if (commandSignIn != null) commandSignIn.Visible = !IsSignedIn();
if (commandSignOut != null) commandSignOut.Visible = IsSignedIn();
if (commandEnterToken != null) commandEnterToken.Visible = !IsSignedIn();
}

// notify the user they need to sign in
if (!IsSignedIn())
Expand Down
4 changes: 2 additions & 2 deletions CodeiumVS/InlineDiff/InlineDiffAdornment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,8 @@ private void Adornment_OnAccepted()
// format it
try
{
DTE2 dte = (DTE2)ServiceProvider.GlobalProvider.GetService(typeof(DTE));
dte.ExecuteCommand("Edit.FormatSelection");
DTE2? dte = ServiceProvider.GlobalProvider.GetService(typeof(DTE)) as DTE2;
dte?.ExecuteCommand("Edit.FormatSelection");

// this doesn't work
//var guid = VSConstants.CMDSETID.StandardCommandSet2K_guid;
Expand Down
19 changes: 11 additions & 8 deletions CodeiumVS/InlineDiff/InlineDiffView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,18 +458,18 @@ private void DiffView_OnClosed(object sender, EventArgs e)

private void LeftView_OnClosed(object sender, EventArgs e)
{
LeftView.VisualElement.GotFocus -= LeftView_OnGotFocus;
LeftView.VisualElement.LostFocus -= LeftView_OnLostFocus;
LeftView.Caret.PositionChanged -= LeftView_OnCaretPositionChanged;
LeftView.Closed -= LeftView_OnClosed;
LeftView.VisualElement.GotFocus -= LeftView_OnGotFocus;
LeftView.VisualElement.LostFocus -= LeftView_OnLostFocus;
LeftView.Caret.PositionChanged -= LeftView_OnCaretPositionChanged;
LeftView.Closed -= LeftView_OnClosed;
}

private void RightView_OnClosed(object sender, EventArgs e)
{
RightView.VisualElement.GotFocus -= RightView_OnGotFocus;
RightView.VisualElement.LostFocus -= RightView_OnLostFocus;
RightView.Caret.PositionChanged -= RightView_OnCaretPositionChanged;
RightView.Closed -= RightView_OnClosed;
RightView.VisualElement.GotFocus -= RightView_OnGotFocus;
RightView.VisualElement.LostFocus -= RightView_OnLostFocus;
RightView.Caret.PositionChanged -= RightView_OnCaretPositionChanged;
RightView.Closed -= RightView_OnClosed;
}

/// <summary>
Expand All @@ -481,6 +481,9 @@ private void DifferenceViewer_OnClosed(object sender, EventArgs e)
{
_diffBuffer.SnapshotDifferenceChanged -= DiffBuffer_SnapshotDifferenceChanged;
_viewer.Closed -= DifferenceViewer_OnClosed;

// restore the selected line highlight for the host view
ShowSelectedLineForView(_hostView);
}

/// <summary>
Expand Down
60 changes: 47 additions & 13 deletions CodeiumVS/LanguageServer/LanguageServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CodeiumVS.Packets;
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Threading;
Expand Down Expand Up @@ -45,15 +46,24 @@ public LanguageServer()
public async Task InitializeAsync()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

string ideVersion = "17.0", locale = "en-US";

try
{
locale = CultureInfo.CurrentUICulture.Name;
Version? version = await VS.Shell.GetVsVersionAsync();
if (version != null) ideVersion = version.ToString();
}
catch (Exception) { }

EnvDTE.DTE VSDTE = (EnvDTE.DTE)Marshal.GetActiveObject("VisualStudio.DTE");
Metadata.request_id = 0;
Metadata.ide_name = "visual_studio";
Metadata.ide_version = VSDTE.Version;
Metadata.ide_version = ideVersion;
Metadata.extension_name = Vsix.Name;
Metadata.extension_version = Version;
Metadata.session_id = Guid.NewGuid().ToString();
Metadata.locale = new CultureInfo(VSDTE.LocaleID).Name;
Metadata.locale = locale;
Metadata.disable_telemetry = false;

await PrepareAsync();
Expand Down Expand Up @@ -167,9 +177,6 @@ public async Task SignOutAsync()
// Download the language server (if not already) and start it
public async Task PrepareAsync()
{
string langServerFolder = Package.GetLanguageServerFolder();
Directory.CreateDirectory(langServerFolder);

string binaryPath = Package.GetLanguageServerPath();

if (File.Exists(binaryPath))
Expand All @@ -195,9 +202,13 @@ public async Task PrepareAsync()
// until VS is closing, not sure how we can fix that without spawning a separate thread
void ThreadDownloadLanguageServer()
{
string langServerFolder = Package.GetLanguageServerFolder();
string downloadDest = Path.Combine(langServerFolder, "language-server.gz");

Directory.CreateDirectory(langServerFolder);
if (File.Exists(downloadDest)) File.Delete(downloadDest);

Uri url = new($"https://github.com/Exafunction/codeium/releases/download/language-server-v{Version}/language_server_windows_x64.exe.gz");
string downloadDest = Path.Combine(Package.GetLanguageServerFolder(), "language-server.gz");
File.Delete(downloadDest);

WebClient webClient = new();

Expand Down Expand Up @@ -280,8 +291,20 @@ private async Task StartAsync()
string managerDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
string databaseDir = Package.GetDatabaseDirectory();

Directory.CreateDirectory(managerDir);
Directory.CreateDirectory(databaseDir);
try
{
Directory.CreateDirectory(managerDir);
Directory.CreateDirectory(databaseDir);
}
catch (Exception ex)
{
await Package.LogAsync($"LanguageServer.StartAsync: Failed to create directories; Exception: {ex}");
await VS.MessageBox.ShowErrorAsync(
"Codeium: Failed to create language server directories.",
"Please check the output window for more details."
);
return;
}

process = new();
process.StartInfo.FileName = Package.GetLanguageServerPath();
Expand All @@ -301,10 +324,21 @@ private async Task StartAsync()
process.Exited += LSP_OnExited;

await Package.LogAsync("Starting language server");
process.Start();
process.BeginErrorReadLine();

Utilities.ProcessExtensions.MakeProcessExitOnParentExit(process);
try
{
process.Start();
process.BeginErrorReadLine();
Utilities.ProcessExtensions.MakeProcessExitOnParentExit(process);
}
catch (Exception ex)
{
await Package.LogAsync($"LanguageServer.StartAsync: Failed to start the language server; Exception: {ex}");
await VS.MessageBox.ShowErrorAsync(
"Codeium: Failed to start the language server.",
"Please check the output window for more details."
);
}

string apiKeyFilePath = Package.GetAPIKeyPath();
if (File.Exists(apiKeyFilePath))
Expand Down
32 changes: 20 additions & 12 deletions CodeiumVS/NotificationBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,30 @@ public void Show(string text, ImageMoniker? icon = null, bool canClose = true, A
ThreadHelper.ThrowIfNotOnUIThread("Show");
if (view != null) return;

IVsShell vsShell = ServiceProvider.GlobalProvider.GetService<SVsShell, IVsShell>();
IVsInfoBarUIFactory vsInfoBarFactory = ServiceProvider.GlobalProvider.GetService<SVsInfoBarUIFactory, IVsInfoBarUIFactory>();
if (vsShell == null || vsInfoBarFactory == null) return;

if (vsInfoBarFactory != null && ErrorHandler.Succeeded(vsShell.GetProperty((int)__VSSPROPID7.VSSPROPID_MainWindowInfoBarHost, out var pvar)) && pvar is IVsInfoBarHost vsInfoBarHost)
try
{
InfoBarModel infoBar = new(text, GetActionsItems(actions), icon ?? KnownMonikers.StatusInformation, canClose);
IVsShell vsShell = ServiceProvider.GlobalProvider.GetService<SVsShell, IVsShell>();
IVsInfoBarUIFactory vsInfoBarFactory = ServiceProvider.GlobalProvider.GetService<SVsInfoBarUIFactory, IVsInfoBarUIFactory>();
if (vsShell == null || vsInfoBarFactory == null) return;

if (vsInfoBarFactory != null && ErrorHandler.Succeeded(vsShell.GetProperty((int)__VSSPROPID7.VSSPROPID_MainWindowInfoBarHost, out var pvar)) && pvar is IVsInfoBarHost vsInfoBarHost)
{
InfoBarModel infoBar = new(text, GetActionsItems(actions), icon ?? KnownMonikers.StatusInformation, canClose);

view = vsInfoBarFactory.CreateInfoBar(infoBar);
view.Advise(this, out infoBarEventsCookie);
view = vsInfoBarFactory.CreateInfoBar(infoBar);
view.Advise(this, out infoBarEventsCookie);

this.vsInfoBarHost = vsInfoBarHost;
this.vsInfoBarHost.AddInfoBar(view);
this.vsInfoBarHost = vsInfoBarHost;
this.vsInfoBarHost.AddInfoBar(view);

IsShown = true;
OnCloseCallback = onCloseCallback;
IsShown = true;
OnCloseCallback = onCloseCallback;
}
}
catch (Exception ex)
{
CodeiumVSPackage.Instance?.Log($"NotificationInfoBar.Show: Failed to show notificaiton; Exception: {ex}");
return;
}
}

Expand Down

0 comments on commit d556405

Please sign in to comment.