diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/RecentCommandsManager.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/RecentCommandsManager.cs index c740341c7a..8551b5f964 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/RecentCommandsManager.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/RecentCommandsManager.cs @@ -12,63 +12,71 @@ public partial class RecentCommandsManager : ObservableObject [JsonInclude] internal List History { get; set; } = []; + private readonly Lock _lock = new(); + public RecentCommandsManager() { } public int GetCommandHistoryWeight(string commandId) { - var entry = History + lock (_lock) + { + var entry = History .Index() .Where(item => item.Item.CommandId == commandId) .FirstOrDefault(); - // These numbers are vaguely scaled so that "VS" will make "Visual Studio" the - // match after one use. - // Usually it has a weight of 84, compared to 109 for the VS cmd prompt - if (entry.Item != null) - { - var index = entry.Index; - - // First, add some weight based on how early in the list this appears - var bucket = index switch + // These numbers are vaguely scaled so that "VS" will make "Visual Studio" the + // match after one use. + // Usually it has a weight of 84, compared to 109 for the VS cmd prompt + if (entry.Item != null) { - var i when index <= 2 => 35, - var i when index <= 10 => 25, - var i when index <= 15 => 15, - var i when index <= 35 => 10, - _ => 5, - }; + var index = entry.Index; - // Then, add weight for how often this is used, but cap the weight from usage. - var uses = Math.Min(entry.Item.Uses * 5, 35); + // First, add some weight based on how early in the list this appears + var bucket = index switch + { + var i when index <= 2 => 35, + var i when index <= 10 => 25, + var i when index <= 15 => 15, + var i when index <= 35 => 10, + _ => 5, + }; - return bucket + uses; + // Then, add weight for how often this is used, but cap the weight from usage. + var uses = Math.Min(entry.Item.Uses * 5, 35); + + return bucket + uses; + } + + return 0; } - - return 0; } public void AddHistoryItem(string commandId) { - var entry = History + lock (_lock) + { + var entry = History .Where(item => item.CommandId == commandId) .FirstOrDefault(); - if (entry == null) - { - var newitem = new HistoryItem() { CommandId = commandId, Uses = 1 }; - History.Insert(0, newitem); - } - else - { - History.Remove(entry); - entry.Uses++; - History.Insert(0, entry); - } + if (entry == null) + { + var newitem = new HistoryItem() { CommandId = commandId, Uses = 1 }; + History.Insert(0, newitem); + } + else + { + History.Remove(entry); + entry.Uses++; + History.Insert(0, entry); + } - if (History.Count > 50) - { - History.RemoveRange(50, History.Count - 50); + if (History.Count > 50) + { + History.RemoveRange(50, History.Count - 50); + } } } }