diff --git a/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs b/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs index 4cd505fe4d..9fe90d336d 100644 --- a/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs +++ b/src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs @@ -111,6 +111,9 @@ namespace System.Management.Automation new ExperimentalFeature( name: "PSTempDrive", description: "Create TEMP: PS Drive mapped to user's temporary directory path"), + new ExperimentalFeature( + name: "PSCommandNotFoundSuggestion", + description: "Recommend potential commands based on fuzzy search on a CommandNotFoundException"), }; EngineExperimentalFeatures = new ReadOnlyCollection(engineFeatures); diff --git a/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs b/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs index cd660537b6..c4c00a368a 100644 --- a/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs +++ b/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs @@ -71,27 +71,52 @@ namespace System.Management.Automation $formatString -f [string]::Join(', ', (Get-Command $lastError.TargetObject -UseFuzzyMatch | Select-Object -First 10 -Unique -ExpandProperty Name)) "; - private static ArrayList s_suggestions = new ArrayList( - new Hashtable[] { - NewSuggestion(1, "Transactions", SuggestionMatchType.Command, "^Start-Transaction", - SuggestionStrings.Suggestion_StartTransaction, true), - NewSuggestion(2, "Transactions", SuggestionMatchType.Command, "^Use-Transaction", - SuggestionStrings.Suggestion_UseTransaction, true), - NewSuggestion(3, "General", SuggestionMatchType.Dynamic, - ScriptBlock.CreateDelayParsedScriptBlock(s_checkForCommandInCurrentDirectoryScript, isProductCode: true), - ScriptBlock.CreateDelayParsedScriptBlock(s_createCommandExistsInCurrentDirectoryScript, isProductCode: true), - new object[] { CodeGeneration.EscapeSingleQuotedStringContent(SuggestionStrings.Suggestion_CommandExistsInCurrentDirectory) }, - true), - NewSuggestion( - id: 4, - category: "General", - matchType: SuggestionMatchType.ErrorId, - rule: "CommandNotFoundException", - suggestion: ScriptBlock.CreateDelayParsedScriptBlock(s_getFuzzyMatchedCommands, isProductCode: true), - suggestionArgs: new object[] { CodeGeneration.EscapeSingleQuotedStringContent(SuggestionStrings.Suggestion_CommandNotFound) }, - enabled: true) + private static ArrayList s_suggestions = InitializeSuggestions(); + + private static ArrayList InitializeSuggestions() + { + ArrayList suggestions = new ArrayList( + new Hashtable[] + { + NewSuggestion( + id: 1, + category: "Transactions", + matchType: SuggestionMatchType.Command, + rule: "^Start-Transaction", + suggestion: SuggestionStrings.Suggestion_StartTransaction, + enabled: true), + NewSuggestion( + id: 2, + category: "Transactions", + matchType: SuggestionMatchType.Command, + rule: "^Use-Transaction", + suggestion: SuggestionStrings.Suggestion_UseTransaction, + enabled: true), + NewSuggestion( + id: 3, + category: "General", + matchType: SuggestionMatchType.Dynamic, + rule: ScriptBlock.CreateDelayParsedScriptBlock(s_checkForCommandInCurrentDirectoryScript, isProductCode: true), + suggestion: ScriptBlock.CreateDelayParsedScriptBlock(s_createCommandExistsInCurrentDirectoryScript, isProductCode: true), + suggestionArgs: new object[] { CodeGeneration.EscapeSingleQuotedStringContent(SuggestionStrings.Suggestion_CommandExistsInCurrentDirectory) }, + enabled: true) + }); + + if (ExperimentalFeature.IsEnabled("PSCommandNotFoundSuggestion")) + { + suggestions.Add( + NewSuggestion( + id: 4, + category: "General", + matchType: SuggestionMatchType.ErrorId, + rule: "CommandNotFoundException", + suggestion: ScriptBlock.CreateDelayParsedScriptBlock(s_getFuzzyMatchedCommands, isProductCode: true), + suggestionArgs: new object[] { CodeGeneration.EscapeSingleQuotedStringContent(SuggestionStrings.Suggestion_CommandNotFound) }, + enabled: true)); } - ); + + return suggestions; + } #region GetProfileCommands ///