-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
IFeedbackProvider.cs
304 lines (261 loc) · 11.3 KB
/
IFeedbackProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using System.Collections.Generic;
using System.IO;
using System.Management.Automation.Internal;
using System.Management.Automation.Language;
using System.Management.Automation.Runspaces;
using System.Threading;
namespace System.Management.Automation.Subsystem.Feedback
{
/// <summary>
/// Types of trigger for the feedback provider.
/// </summary>
[Flags]
public enum FeedbackTrigger
{
/// <summary>
/// The last command line executed successfully.
/// </summary>
Success = 0x0001,
/// <summary>
/// The last command line failed due to a command-not-found error.
/// This is a special case of <see cref="Error"/>.
/// </summary>
CommandNotFound = 0x0002,
/// <summary>
/// The last command line failed with an error record.
/// This includes the case of command-not-found error.
/// </summary>
Error = CommandNotFound | 0x0004,
/// <summary>
/// All possible triggers.
/// </summary>
All = Success | Error
}
/// <summary>
/// Layout for displaying the recommended actions.
/// </summary>
public enum FeedbackDisplayLayout
{
/// <summary>
/// Display one recommended action per row.
/// </summary>
Portrait,
/// <summary>
/// Display all recommended actions in the same row.
/// </summary>
Landscape,
}
/// <summary>
/// Context information about the last command line.
/// </summary>
public sealed class FeedbackContext
{
/// <summary>
/// Gets the feedback trigger.
/// </summary>
public FeedbackTrigger Trigger { get; }
/// <summary>
/// Gets the last command line that was just executed.
/// </summary>
public string CommandLine { get; }
/// <summary>
/// Gets the abstract syntax tree (AST) generated from parsing the last command line.
/// </summary>
public Ast CommandLineAst { get; }
/// <summary>
/// Gets the tokens generated from parsing the last command line.
/// </summary>
public IReadOnlyList<Token> CommandLineTokens { get; }
/// <summary>
/// Gets the current location of the default session.
/// </summary>
public PathInfo CurrentLocation { get; }
/// <summary>
/// Gets the last error record generated from executing the last command line.
/// </summary>
public ErrorRecord? LastError { get; }
/// <summary>
/// Initializes a new instance of the <see cref="FeedbackContext"/> class.
/// </summary>
/// <param name="trigger">The trigger of this feedback call.</param>
/// <param name="commandLine">The command line that was just executed.</param>
/// <param name="cwd">The current location of the default session.</param>
/// <param name="lastError">The error that was triggerd by the last command line.</param>
public FeedbackContext(FeedbackTrigger trigger, string commandLine, PathInfo cwd, ErrorRecord? lastError)
{
ArgumentException.ThrowIfNullOrEmpty(commandLine);
ArgumentNullException.ThrowIfNull(cwd);
Trigger = trigger;
CommandLine = commandLine;
CommandLineAst = Parser.ParseInput(commandLine, out Token[] tokens, out _);
CommandLineTokens = tokens;
LastError = lastError;
CurrentLocation = cwd;
}
/// <summary>
/// Initializes a new instance of the <see cref="FeedbackContext"/> class.
/// </summary>
/// <param name="trigger">The trigger of this feedback call.</param>
/// <param name="commandLineAst">The abstract syntax tree (AST) from parsing the last command line.</param>
/// <param name="commandLineTokens">The tokens from parsing the last command line.</param>
/// <param name="cwd">The current location of the default session.</param>
/// <param name="lastError">The error that was triggerd by the last command line.</param>
public FeedbackContext(FeedbackTrigger trigger, Ast commandLineAst, Token[] commandLineTokens, PathInfo cwd, ErrorRecord? lastError)
{
ArgumentNullException.ThrowIfNull(commandLineAst);
ArgumentNullException.ThrowIfNull(commandLineTokens);
ArgumentNullException.ThrowIfNull(cwd);
Trigger = trigger;
CommandLine = commandLineAst.Extent.Text;
CommandLineAst = commandLineAst;
CommandLineTokens = commandLineTokens;
LastError = lastError;
CurrentLocation = cwd;
}
}
/// <summary>
/// The class represents a feedback item generated by the feedback provider.
/// </summary>
public sealed class FeedbackItem
{
/// <summary>
/// Gets the description message about this feedback.
/// </summary>
public string Header { get; }
/// <summary>
/// Gets the footer message about this feedback.
/// </summary>
public string? Footer { get; }
/// <summary>
/// Gets the recommended actions -- command lines or even code snippets to run.
/// </summary>
public List<string>? RecommendedActions { get; }
/// <summary>
/// Gets the layout to use for displaying the recommended actions.
/// </summary>
public FeedbackDisplayLayout Layout { get; }
/// <summary>
/// Gets or sets the next feedback item, if there is one.
/// </summary>
public FeedbackItem? Next { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="FeedbackItem"/> class.
/// </summary>
/// <param name="header">The description message (must be not null or empty).</param>
/// <param name="actions">The recommended actions to take (optional).</param>
public FeedbackItem(string header, List<string>? actions)
: this(header, actions, footer: null, FeedbackDisplayLayout.Portrait)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FeedbackItem"/> class.
/// </summary>
/// <param name="header">The description message (must be not null or empty).</param>
/// <param name="actions">The recommended actions to take (optional).</param>
/// <param name="layout">The layout for displaying the actions.</param>
public FeedbackItem(string header, List<string>? actions, FeedbackDisplayLayout layout)
: this(header, actions, footer: null, layout)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FeedbackItem"/> class.
/// </summary>
/// <param name="header">The description message (must be not null or empty).</param>
/// <param name="actions">The recommended actions to take (optional).</param>
/// <param name="footer">The footer message (optional).</param>
/// <param name="layout">The layout for displaying the actions.</param>
public FeedbackItem(string header, List<string>? actions, string? footer, FeedbackDisplayLayout layout)
{
ArgumentException.ThrowIfNullOrEmpty(header);
Header = header;
RecommendedActions = actions;
Footer = footer;
Layout = layout;
}
}
/// <summary>
/// Interface for implementing a feedback provider on command failures.
/// </summary>
public interface IFeedbackProvider : ISubsystem
{
/// <summary>
/// Default implementation. No function is required for a feedback provider.
/// </summary>
Dictionary<string, string>? ISubsystem.FunctionsToDefine => null;
/// <summary>
/// Gets the types of trigger for this feedback provider.
/// </summary>
/// <remarks>
/// The default implementation triggers a feedback provider by <see cref="FeedbackTrigger.CommandNotFound"/> only.
/// </remarks>
FeedbackTrigger Trigger => FeedbackTrigger.CommandNotFound;
/// <summary>
/// Gets feedback based on the given commandline and error record.
/// </summary>
/// <param name="context">The context for the feedback call.</param>
/// <param name="token">The cancellation token to cancel the operation.</param>
/// <returns>The feedback item.</returns>
FeedbackItem? GetFeedback(FeedbackContext context, CancellationToken token);
}
internal sealed class GeneralCommandErrorFeedback : IFeedbackProvider
{
private readonly Guid _guid;
internal GeneralCommandErrorFeedback()
{
_guid = new Guid("A3C6B07E-4A89-40C9-8BE6-2A9AAD2786A4");
}
public Guid Id => _guid;
public string Name => "general";
public string Description => "The built-in general feedback source for command errors.";
public FeedbackItem? GetFeedback(FeedbackContext context, CancellationToken token)
{
var rsToUse = Runspace.DefaultRunspace;
if (rsToUse is null)
{
return null;
}
// This feedback provider is only triggered by 'CommandNotFound' error, so the
// 'LastError' property is guaranteed to be not null.
ErrorRecord lastError = context.LastError!;
SessionState sessionState = rsToUse.ExecutionContext.SessionState;
var target = (string)lastError.TargetObject;
CommandInvocationIntrinsics invocation = sessionState.InvokeCommand;
// See if target is actually an executable file in current directory.
var localTarget = Path.Combine(".", target);
var command = invocation.GetCommand(
localTarget,
CommandTypes.Application | CommandTypes.ExternalScript);
if (command is not null)
{
return new FeedbackItem(
StringUtil.Format(SuggestionStrings.Suggestion_CommandExistsInCurrentDirectory, target),
new List<string> { localTarget });
}
// Check fuzzy matching command names.
if (ExperimentalFeature.IsEnabled("PSCommandNotFoundSuggestion"))
{
var pwsh = PowerShell.Create(RunspaceMode.CurrentRunspace);
var results = pwsh.AddCommand("Get-Command")
.AddParameter("UseFuzzyMatching")
.AddParameter("FuzzyMinimumDistance", 1)
.AddParameter("Name", target)
.AddCommand("Select-Object")
.AddParameter("First", 5)
.AddParameter("Unique")
.AddParameter("ExpandProperty", "Name")
.Invoke<string>();
if (results.Count > 0)
{
return new FeedbackItem(
SuggestionStrings.Suggestion_CommandNotFound,
new List<string>(results),
FeedbackDisplayLayout.Landscape);
}
}
return null;
}
}
}