diff --git a/src/System.Management.Automation/engine/Subsystem/FeedbackSubsystem/IFeedbackProvider.cs b/src/System.Management.Automation/engine/Subsystem/FeedbackSubsystem/IFeedbackProvider.cs index 0e126f9f27..4a680f30db 100644 --- a/src/System.Management.Automation/engine/Subsystem/FeedbackSubsystem/IFeedbackProvider.cs +++ b/src/System.Management.Automation/engine/Subsystem/FeedbackSubsystem/IFeedbackProvider.cs @@ -188,168 +188,4 @@ namespace System.Management.Automation.Subsystem.Feedback return null; } } - - internal sealed class UnixCommandNotFound : IFeedbackProvider, ICommandPredictor - { - private readonly Guid _guid; - private List? _candidates; - - internal UnixCommandNotFound() - { - _guid = new Guid("47013747-CB9D-4EBC-9F02-F32B8AB19D48"); - } - - Dictionary? ISubsystem.FunctionsToDefine => null; - - public Guid Id => _guid; - - public string Name => "cmd-not-found"; - - public string Description => "The built-in feedback/prediction source for the Unix command utility."; - - #region IFeedbackProvider - - private static string? GetUtilityPath() - { - string cmd_not_found = "/usr/lib/command-not-found"; - bool exist = IsFileExecutable(cmd_not_found); - - if (!exist) - { - cmd_not_found = "/usr/share/command-not-found/command-not-found"; - exist = IsFileExecutable(cmd_not_found); - } - - return exist ? cmd_not_found : null; - - static bool IsFileExecutable(string path) - { - var file = new FileInfo(path); - return file.Exists && file.UnixFileMode.HasFlag(UnixFileMode.OtherExecute); - } - } - - public FeedbackItem? GetFeedback(string commandLine, ErrorRecord lastError, CancellationToken token) - { - if (Platform.IsWindows || lastError.FullyQualifiedErrorId != "CommandNotFoundException") - { - return null; - } - - var target = (string)lastError.TargetObject; - if (target is null) - { - return null; - } - - if (target.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase)) - { - return null; - } - - string? cmd_not_found = GetUtilityPath(); - if (cmd_not_found is not null) - { - var startInfo = new ProcessStartInfo(cmd_not_found); - startInfo.ArgumentList.Add(target); - startInfo.RedirectStandardError = true; - startInfo.RedirectStandardOutput = true; - - using var process = Process.Start(startInfo); - if (process is not null) - { - string? header = null; - List? actions = null; - - while (true) - { - string? line = process.StandardError.ReadLine(); - if (line is null) - { - break; - } - - if (line == string.Empty) - { - continue; - } - - if (line.StartsWith("sudo ", StringComparison.Ordinal)) - { - actions ??= new List(); - actions.Add(line.TrimEnd()); - } - else if (actions is null) - { - header = line; - } - } - - if (actions is not null && header is not null) - { - _candidates = actions; - - var footer = process.StandardOutput.ReadToEnd().Trim(); - return string.IsNullOrEmpty(footer) - ? new FeedbackItem(header, actions) - : new FeedbackItem(header, actions, footer, FeedbackDisplayLayout.Portrait); - } - } - } - - return null; - } - - #endregion - - #region ICommandPredictor - - public bool CanAcceptFeedback(PredictionClient client, PredictorFeedbackKind feedback) - { - return feedback switch - { - PredictorFeedbackKind.CommandLineAccepted => true, - _ => false, - }; - } - - public SuggestionPackage GetSuggestion(PredictionClient client, PredictionContext context, CancellationToken cancellationToken) - { - if (_candidates is not null) - { - string input = context.InputAst.Extent.Text; - List? result = null; - - foreach (string c in _candidates) - { - if (c.StartsWith(input, StringComparison.OrdinalIgnoreCase)) - { - result ??= new List(_candidates.Count); - result.Add(new PredictiveSuggestion(c)); - } - } - - if (result is not null) - { - return new SuggestionPackage(result); - } - } - - return default; - } - - public void OnCommandLineAccepted(PredictionClient client, IReadOnlyList history) - { - // Reset the candidate state. - _candidates = null; - } - - public void OnSuggestionDisplayed(PredictionClient client, uint session, int countOrIndex) { } - - public void OnSuggestionAccepted(PredictionClient client, uint session, string acceptedSuggestion) { } - - public void OnCommandLineExecuted(PredictionClient client, string commandLine, bool success) { } - - #endregion; - } } diff --git a/src/System.Management.Automation/engine/Subsystem/PredictionSubsystem/CommandPrediction.cs b/src/System.Management.Automation/engine/Subsystem/PredictionSubsystem/CommandPrediction.cs index 3e06d04648..edb4696061 100644 --- a/src/System.Management.Automation/engine/Subsystem/PredictionSubsystem/CommandPrediction.cs +++ b/src/System.Management.Automation/engine/Subsystem/PredictionSubsystem/CommandPrediction.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; -using System.Management.Automation.Internal; using System.Management.Automation.Language; using System.Threading; using System.Threading.Tasks; diff --git a/src/System.Management.Automation/engine/Subsystem/PredictionSubsystem/ICommandPredictor.cs b/src/System.Management.Automation/engine/Subsystem/PredictionSubsystem/ICommandPredictor.cs index 1c632c541c..275dc1733b 100644 --- a/src/System.Management.Automation/engine/Subsystem/PredictionSubsystem/ICommandPredictor.cs +++ b/src/System.Management.Automation/engine/Subsystem/PredictionSubsystem/ICommandPredictor.cs @@ -36,7 +36,7 @@ namespace System.Management.Automation.Subsystem.Prediction /// Represents the client that initiates the call. /// A specific type of feedback. /// True or false, to indicate whether the specific feedback is accepted. - bool CanAcceptFeedback(PredictionClient client, PredictorFeedbackKind feedback); + bool CanAcceptFeedback(PredictionClient client, PredictorFeedbackKind feedback) => false; /// /// One or more suggestions provided by the predictor were displayed to the user. @@ -47,7 +47,7 @@ namespace System.Management.Automation.Subsystem.Prediction /// When the value is greater than 0, it's the number of displayed suggestions from the list returned in , starting from the index 0. /// When the value is less than or equal to 0, it means a single suggestion from the list got displayed, and the index is the absolute value. /// - void OnSuggestionDisplayed(PredictionClient client, uint session, int countOrIndex); + void OnSuggestionDisplayed(PredictionClient client, uint session, int countOrIndex) { } /// /// The suggestion provided by the predictor was accepted. @@ -55,7 +55,7 @@ namespace System.Management.Automation.Subsystem.Prediction /// Represents the client that initiates the call. /// Represents the mini-session where the accepted suggestion came from. /// The accepted suggestion text. - void OnSuggestionAccepted(PredictionClient client, uint session, string acceptedSuggestion); + void OnSuggestionAccepted(PredictionClient client, uint session, string acceptedSuggestion) { } /// /// A command line was accepted to execute. @@ -63,7 +63,7 @@ namespace System.Management.Automation.Subsystem.Prediction /// /// Represents the client that initiates the call. /// History command lines provided as references for prediction. - void OnCommandLineAccepted(PredictionClient client, IReadOnlyList history); + void OnCommandLineAccepted(PredictionClient client, IReadOnlyList history) { } /// /// A command line was done execution. @@ -71,7 +71,7 @@ namespace System.Management.Automation.Subsystem.Prediction /// Represents the client that initiates the call. /// The last accepted command line. /// Shows whether the execution was successful. - void OnCommandLineExecuted(PredictionClient client, string commandLine, bool success); + void OnCommandLineExecuted(PredictionClient client, string commandLine, bool success) { } } /// @@ -131,6 +131,12 @@ namespace System.Management.Automation.Subsystem.Prediction /// public PredictionClientKind Kind { get; } + /// + /// Gets the current location of the default session. + /// It returns null if there is no default Runspace or if the default is a remote Runspace. + /// + public PathInfo? CurrentLocation { get; set; } + /// /// Initializes a new instance of the class. /// diff --git a/src/System.Management.Automation/engine/hostifaces/Connection.cs b/src/System.Management.Automation/engine/hostifaces/Connection.cs index a637655aee..50c7e49f92 100644 --- a/src/System.Management.Automation/engine/hostifaces/Connection.cs +++ b/src/System.Management.Automation/engine/hostifaces/Connection.cs @@ -703,7 +703,7 @@ namespace System.Management.Automation.Runspaces /// /// Runspace is not opened. /// - internal System.Management.Automation.ExecutionContext ExecutionContext + internal ExecutionContext ExecutionContext { get { @@ -1577,7 +1577,7 @@ namespace System.Management.Automation.Runspaces /// /// Gets the execution context. /// - internal abstract System.Management.Automation.ExecutionContext GetExecutionContext + internal abstract ExecutionContext GetExecutionContext { get; } diff --git a/src/System.Management.Automation/engine/hostifaces/LocalConnection.cs b/src/System.Management.Automation/engine/hostifaces/LocalConnection.cs index 64c9e19516..b88a466623 100644 --- a/src/System.Management.Automation/engine/hostifaces/LocalConnection.cs +++ b/src/System.Management.Automation/engine/hostifaces/LocalConnection.cs @@ -237,7 +237,7 @@ namespace System.Management.Automation.Runspaces /// /// Gets the execution context. /// - internal override System.Management.Automation.ExecutionContext GetExecutionContext + internal override ExecutionContext GetExecutionContext { get { diff --git a/test/xUnit/csharp/test_Subsystem.cs b/test/xUnit/csharp/test_Subsystem.cs index 1318f13139..2889bab378 100644 --- a/test/xUnit/csharp/test_Subsystem.cs +++ b/test/xUnit/csharp/test_Subsystem.cs @@ -71,18 +71,8 @@ namespace PSTests.Sequential #region ICommandPredictor - public bool CanAcceptFeedback(PredictionClient client, PredictorFeedbackKind feedback) => false; - public SuggestionPackage GetSuggestion(PredictionClient client, PredictionContext context, CancellationToken cancellationToken) => default; - public void OnCommandLineAccepted(PredictionClient client, IReadOnlyList history) { } - - public void OnSuggestionDisplayed(PredictionClient client, uint session, int countOrIndex) { } - - public void OnSuggestionAccepted(PredictionClient client, uint session, string acceptedSuggestion) { } - - public void OnCommandLineExecuted(PredictionClient client, string commandLine, bool success) { } - #endregion }