Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Keybinding.Test/DefaultProfileReactivationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,57 @@ public async Task CreateDefaultProfile_AfterActiveProfileWasDeleted_ActivatesExi
}
}

[TestMethod]
public async Task CreateDefaultProfile_DefaultAndActiveProfileDeleted_ActivatesRemainingProfileOnNextLaunch()
{
{
using KeybindingManager manager = new(_testDataDirectory);
await manager.InitializeAsync().ConfigureAwait(false);
manager.CreateDefaultProfile();
manager.Profiles.CreateProfile("vim", "Vim");
manager.Profiles.CreateProfile("emacs", "Emacs");
Assert.IsTrue(manager.Profiles.SetActiveProfile("vim"));
Assert.IsTrue(manager.Profiles.DeleteProfile("default"));
Assert.IsTrue(manager.Profiles.DeleteProfile("vim"));
await manager.SaveAsync().ConfigureAwait(false);
}

{
using KeybindingManager manager = new(_testDataDirectory);
await manager.InitializeAsync().ConfigureAwait(false);
Assert.IsNull(manager.CreateDefaultProfile());
manager.RegisterCommands([new Command("file.save", "Save")]);

Assert.AreEqual("emacs", manager.Profiles.GetActiveProfile()?.Id);
Assert.IsFalse(manager.Profiles.ProfileExists("default"));
Assert.IsTrue(manager.Keybindings.BindChord("file.save", Chord.Parse("Ctrl+S")));
Assert.AreEqual("file.save", manager.Keybindings.ExecuteChord(Chord.Parse("Ctrl+S")));
}
}

[TestMethod]
public void CreateDefaultProfile_NoDefaultAndNoneActive_ActivatesFirstProfileByOrdinalId()
{
using KeybindingManager manager = new(_testDataDirectory);
manager.Profiles.CreateProfile("vim", "Vim");
manager.Profiles.CreateProfile("Emacs", "Emacs");
manager.Profiles.CreateProfile("helix", "Helix");

Assert.IsNull(manager.CreateDefaultProfile());
Assert.AreEqual("Emacs", manager.Profiles.GetActiveProfile()?.Id);
Assert.IsFalse(manager.Profiles.ProfileExists("default"));
}

[TestMethod]
public void CreateDefaultProfile_NoDefaultAndNoneActiveWithDoNotActivate_LeavesNoActiveProfile()
{
using KeybindingManager manager = new(_testDataDirectory);
manager.Profiles.CreateProfile("vim", "Vim");

Assert.IsNull(manager.CreateDefaultProfile(activation: ProfileActivation.DoNotActivate));
Assert.IsNull(manager.Profiles.GetActiveProfile());
}

[TestMethod]
public void CreateDefaultProfile_ExistingDefaultWhileAnotherIsActive_LeavesActiveProfileAlone()
{
Expand Down
16 changes: 12 additions & 4 deletions Keybinding/KeybindingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,10 @@ .. storedCommands.Where(c => !commandIds.Contains(c.Id.ToString()) && !_persiste
/// <param name="profileId">The ID for the default profile</param>
/// <param name="profileName">The name for the default profile</param>
/// <param name="activation">
/// Whether to set as the active profile. The profile is activated only if no profile is active, whether it
/// was just created or already existed, so a startup call recovers from a deleted or unset active profile
/// without overriding the one the user chose.
/// Whether to set as the active profile. A profile is activated only if no profile is active, so a startup
/// call recovers from a deleted or unset active profile without overriding the one the user chose. The
/// profile with the given ID is activated if it exists or was just created; otherwise, when other profiles
/// exist, the first of them by ordinal ID is activated.
/// </param>
/// <returns>
/// The created profile, or null if it was not created: because a profile with the given ID already exists,
Expand All @@ -225,8 +226,15 @@ .. storedCommands.Where(c => !commandIds.Contains(c.Id.ToString()) && !_persiste
return null;
}

if (Profiles.GetAllProfiles().Count > 0)
IReadOnlyCollection<Profile> existingProfiles = Profiles.GetAllProfiles();
if (existingProfiles.Count > 0)
{
if (activation == ProfileActivation.Activate && Profiles.GetActiveProfile() is null)
{
string firstProfileId = existingProfiles.Select(p => p.Id).OrderBy(id => id, StringComparer.Ordinal).First();
Profiles.SetActiveProfile(firstProfileId);
}

return null;
}

Expand Down
Loading