Skip to content

Commit

Permalink
Merge pull request #34 from Exafunction/exafunction/update
Browse files Browse the repository at this point in the history
Bring to head of internal repo
  • Loading branch information
fortenforge authored Dec 19, 2023
2 parents 1b846dc + a956931 commit c86c0c0
Show file tree
Hide file tree
Showing 60 changed files with 2,624 additions and 1,808 deletions.
7 changes: 7 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Language: CSharp
BreakBeforeBraces: Allman
IndentWidth: 4
ColumnLimit: 100
AllowShortBlocksOnASingleLine: true
AllowShortIfStatementsOnASingleLine: WithoutElse
BinPackArguments: false
6 changes: 3 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
Expand Down Expand Up @@ -46,9 +46,9 @@

###############################################################################
# diff behavior for common document formats
#
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/build-extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:

- name: Setup NuGet
uses: NuGet/[email protected]

- name: Restore Packages
run: nuget restore CodeiumVS.sln

- name: Build Solution
run: msbuild CodeiumVS.sln /p:Configuration=Release /p:Platform="Any CPU" /p:TargetFrameworkVersion="v4.8" /restore
run: msbuild CodeiumVS.sln /p:Configuration=Release /p:Platform="Any CPU" /p:TargetFrameworkVersion="v4.8" /restore
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,4 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd
4 changes: 2 additions & 2 deletions CodeiumVS/CodeiumVS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\VSSDK\Microsoft.VsSDK.targets" Condition="'$(VSToolsPath)' != ''" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
<!-- 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">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
138 changes: 80 additions & 58 deletions CodeiumVS/CodeiumVSPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@
namespace CodeiumVS;

//[ProvideAutoLoad(VSConstants.UICONTEXT.NoSolution_string, PackageAutoLoadFlags.BackgroundLoad)]
//[ProvideAutoLoad(UIContextGuids80.SolutionExists, PackageAutoLoadFlags.BackgroundLoad)] // VisibilityConstraints example
//[ProvideAutoLoad(UIContextGuids80.SolutionExists, PackageAutoLoadFlags.BackgroundLoad)] //
// VisibilityConstraints example

[Guid(PackageGuids.CodeiumVSString)]
[InstalledProductRegistration(Vsix.Name, Vsix.Description, Vsix.Version)]
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[ProvideMenuResource("Menus.ctmenu", 1)]
[ProvideOptionPage(typeof(SettingsPage), "Codeium", "Codeium", 0, 0, true)]
[ProvideToolWindow(typeof(ChatToolWindow),
MultiInstances = false,
Style = VsDockStyle.Tabbed,
[ProvideToolWindow(
typeof(ChatToolWindow), MultiInstances = false, Style = VsDockStyle.Tabbed,
Orientation = ToolWindowOrientation.Right,
Window = "{3AE79031-E1BC-11D0-8F78-00A0C9110057}")] // default docking window, magic string for the guid of VSConstants.StandardToolWindows.SolutionExplorer
Window =
"{3AE79031-E1BC-11D0-8F78-00A0C9110057}")] // default docking window, magic string for the
// guid of
// VSConstants.StandardToolWindows.SolutionExplorer
public sealed class CodeiumVSPackage : ToolkitPackage
{
internal static CodeiumVSPackage? Instance { get; private set; }
Expand All @@ -39,36 +42,43 @@ public sealed class CodeiumVSPackage : ToolkitPackage
public SettingsPage SettingsPage;
public LanguageServer LanguageServer;

protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
protected override async Task InitializeAsync(CancellationToken cancellationToken,
IProgress<ServiceProgressData> progress)
{
Instance = this;

await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

LanguageServer = new LanguageServer();
OutputWindow = new OutputWindow();
LanguageServer = new LanguageServer();
OutputWindow = new OutputWindow();
NotificationAuth = new NotificationInfoBar();

try
try
{
SettingsPage = GetDialogPage(typeof(SettingsPage)) as SettingsPage;
}
catch (Exception)
{
SettingsPage = GetDialogPage(typeof(SettingsPage)) as SettingsPage;
}
catch (Exception) { }

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

try
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 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();
Expand All @@ -93,7 +103,8 @@ public static async Task EnsurePackageLoadedAsync()
if (Instance != null) return;

await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
IVsShell vsShell = (IVsShell)ServiceProvider.GlobalProvider.GetService(typeof(IVsShell)) ?? throw new NullReferenceException();
IVsShell vsShell = (IVsShell)ServiceProvider.GlobalProvider.GetService(typeof(IVsShell)) ??
throw new NullReferenceException();

Guid guidPackage = new(PackageGuids.CodeiumVSString);
if (vsShell.IsPackageLoaded(ref guidPackage, out var _) == VSConstants.S_OK) return;
Expand All @@ -106,12 +117,14 @@ public async Task UpdateSignedInStateAsync()
{
await JoinableTaskFactory.SwitchToMainThreadAsync();


if ((await GetServiceAsync(typeof(IMenuCommandService))) is OleMenuCommandService cmdService)
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));
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();
Expand All @@ -122,19 +135,24 @@ public async Task UpdateSignedInStateAsync()
if (!IsSignedIn())
{
KeyValuePair<string, Action>[] actions = [
new KeyValuePair<string, Action>("Sign in", delegate
{
ThreadHelper.JoinableTaskFactory.RunAsync(LanguageServer.SignInAsync).FireAndForget(true);
}),
new KeyValuePair<string, Action>("Use authentication token", delegate { new EnterTokenDialogWindow().ShowDialog(); }),
new KeyValuePair<string, Action>("Sign in",
delegate {
ThreadHelper.JoinableTaskFactory
.RunAsync(LanguageServer.SignInAsync)
.FireAndForget(true);
}),
new KeyValuePair<string, Action>(
"Use authentication token",
delegate { new EnterTokenDialogWindow().ShowDialog(); }),
];

NotificationAuth.Show("[Codeium] To enable Codeium, please sign in to your account", KnownMonikers.AddUser, true, null, actions);
}
else
{
await NotificationAuth.CloseAsync();
NotificationAuth.Show("[Codeium] To enable Codeium, please sign in to your account",
KnownMonikers.AddUser,
true,
null,
actions);
}
else { await NotificationAuth.CloseAsync(); }

ChatToolWindow.Instance?.Reload();
}
Expand All @@ -150,23 +168,23 @@ static string CleanifyBrowserPath(string p)
return clean;
}

string urlAssociation = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http";
string urlAssociation =
@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http";
string browserPathKey = @"$BROWSER$\shell\open\command";
try
{
//Read default browser path from userChoiceLKey
RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey(urlAssociation + @"\UserChoice", false);
// Read default browser path from userChoiceLKey
RegistryKey userChoiceKey =
Registry.CurrentUser.OpenSubKey(urlAssociation + @"\UserChoice", false);

//If user choice was not found, try machine default
// If user choice was not found, try machine default
if (userChoiceKey == null)
{
//Read default browser path from Win XP registry key
// Read default browser path from Win XP registry key
var browserKey = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);

//If browser path wasn’t found, try Win Vista (and newer) registry key
browserKey ??=
Registry.CurrentUser.OpenSubKey(
urlAssociation, false);
// If browser path wasn’t found, try Win Vista (and newer) registry key
browserKey ??= Registry.CurrentUser.OpenSubKey(urlAssociation, false);
var path = CleanifyBrowserPath(browserKey.GetValue(null) as string);
browserKey.Close();
return path;
Expand Down Expand Up @@ -195,15 +213,14 @@ static string CleanifyBrowserPath(string p)
public void OpenInBrowser(string url)
{
Action<string>[] methods = [
(_url) => {
(_url) =>
{
Process.Start(new ProcessStartInfo { FileName = _url, UseShellExecute = true });
},
(_url) => {
Process.Start("explorer.exe", _url);
},
(_url) => {
Process.Start(GetDefaultBrowserPath(), _url);
}
(_url) =>
{ Process.Start("explorer.exe", _url); },
(_url) =>
{ Process.Start(GetDefaultBrowserPath(), _url); }
];

foreach (var method in methods)
Expand All @@ -220,7 +237,9 @@ public void OpenInBrowser(string url)
}

Log($"Codeium failed to open the browser, please use this URL instead: {url}");
VS.MessageBox.Show("Codeium: Failed to open browser", $"Please use this URL instead (you can copy from the output window):\n{url}");
VS.MessageBox.Show(
"Codeium: Failed to open browser",
$"Please use this URL instead (you can copy from the output window):\n{url}");
}

public string GetAppDataPath()
Expand Down Expand Up @@ -254,15 +273,19 @@ public string GetAPIKeyPath()
return Path.Combine(GetAppDataPath(), "codeium_api_key");
}

public bool IsSignedIn() { return LanguageServer.GetKey().Length > 0; }
public bool HasEnterprise() { return SettingsPage.EnterpriseMode; }
public bool IsSignedIn()
{
return LanguageServer.GetKey().Length > 0;
}
public bool HasEnterprise()
{
return SettingsPage.EnterpriseMode;
}

internal void Log(string v)
{
ThreadHelper.JoinableTaskFactory.RunAsync(async delegate
{
await LogAsync(v);
}).FireAndForget(true);
ThreadHelper.JoinableTaskFactory.RunAsync(async delegate { await LogAsync(v); })
.FireAndForget(true);
}

internal async Task LogAsync(string v)
Expand All @@ -272,17 +295,16 @@ internal async Task LogAsync(string v)
}
}


// https://gist.github.com/madskristensen/4d205244dd92c37c82e7
// this increase load time idk why, not needed now
//public static class MefExtensions
// public static class MefExtensions
//{
// private static IComponentModel _compositionService;

// public static async Task SatisfyImportsOnceAsync(this object o)
// {
// await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// _compositionService ??= ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel)) as IComponentModel;
// _compositionService?.DefaultCompositionService.SatisfyImportsOnce(o);
// _compositionService ??= ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel))
// as IComponentModel; _compositionService?.DefaultCompositionService.SatisfyImportsOnce(o);
// }
//}
Loading

0 comments on commit c86c0c0

Please sign in to comment.