File size: 22,943 Bytes
8c763fb | 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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Management.Automation.Language;
using System.Management.Automation.Runspaces;
#if LEGACYTELEMETRY
using System.Diagnostics;
using Microsoft.PowerShell.Telemetry.Internal;
#endif
namespace System.Management.Automation
{
/// <summary>
/// Provides a set of possible completions for given input.
/// </summary>
public class CommandCompletion
{
/// <summary>
/// Construct the result CompleteInput or TabExpansion2.
/// </summary>
public CommandCompletion(Collection<CompletionResult> matches, int currentMatchIndex, int replacementIndex, int replacementLength)
{
this.CompletionMatches = matches;
this.CurrentMatchIndex = currentMatchIndex;
this.ReplacementIndex = replacementIndex;
this.ReplacementLength = replacementLength;
}
#region Fields and Properties
/// <summary>
/// Current index in <see cref="CompletionMatches"/>.
/// </summary>
public int CurrentMatchIndex { get; set; }
/// <summary>
/// Returns the starting replacement index from the original input.
/// </summary>
public int ReplacementIndex { get; set; }
/// <summary>
/// Returns the length of the text to replace from the original input.
/// </summary>
public int ReplacementLength { get; set; }
/// <summary>
/// Gets all the completion results.
/// </summary>
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public Collection<CompletionResult> CompletionMatches { get; set; }
internal static readonly IList<CompletionResult> EmptyCompletionResult = Array.Empty<CompletionResult>();
private static readonly CommandCompletion s_emptyCommandCompletion = new CommandCompletion(
new Collection<CompletionResult>(EmptyCompletionResult), -1, -1, -1);
#endregion Fields and Properties
#region public methods
/// <summary>
/// </summary>
/// <param name="input"></param>
/// <param name="cursorIndex"></param>
/// <returns></returns>
public static Tuple<Ast, Token[], IScriptPosition> MapStringInputToParsedInput(string input, int cursorIndex)
{
if (cursorIndex > input.Length)
{
throw PSTraceSource.NewArgumentException(nameof(cursorIndex));
}
Token[] tokens;
ParseError[] errors;
var ast = Parser.ParseInput(input, out tokens, out errors);
IScriptPosition cursorPosition =
((InternalScriptPosition)ast.Extent.StartScriptPosition).CloneWithNewOffset(cursorIndex);
return Tuple.Create<Ast, Token[], IScriptPosition>(ast, tokens, cursorPosition);
}
/// <summary>
/// </summary>
/// <param name="input">The input to complete.</param>
/// <param name="cursorIndex">The index of the cursor in the input.</param>
/// <param name="options">Optional options to configure how completion is performed.</param>
/// <returns></returns>
public static CommandCompletion CompleteInput(string input, int cursorIndex, Hashtable options)
{
if (input == null || input.Length == 0)
{
return s_emptyCommandCompletion;
}
var parsedInput = MapStringInputToParsedInput(input, cursorIndex);
return CompleteInputImpl(parsedInput.Item1, parsedInput.Item2, parsedInput.Item3, options);
}
/// <summary>
/// </summary>
/// <param name="ast">Ast for pre-parsed input.</param>
/// <param name="tokens">Tokens for pre-parsed input.</param>
/// <param name="positionOfCursor"></param>
/// <param name="options">Optional options to configure how completion is performed.</param>
/// <returns></returns>
public static CommandCompletion CompleteInput(Ast ast, Token[] tokens, IScriptPosition positionOfCursor, Hashtable options)
{
if (ast == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(ast));
}
if (tokens == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(tokens));
}
if (positionOfCursor == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(positionOfCursor));
}
if (ast.Extent.Text.Length == 0)
{
return s_emptyCommandCompletion;
}
return CompleteInputImpl(ast, tokens, positionOfCursor, options);
}
/// <summary>
/// Invokes the script function TabExpansion2.
/// </summary>
/// <param name="input">The input script to complete.</param>
/// <param name="cursorIndex">The offset in <paramref name="input"/> where completion is requested.</param>
/// <param name="options">Optional parameter that specifies configurable options for completion.</param>
/// <param name="powershell">The powershell to use to invoke the script function TabExpansion2.</param>
/// <returns>A collection of completions with the replacement start and length.</returns>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "powershell")]
public static CommandCompletion CompleteInput(string input, int cursorIndex, Hashtable options, PowerShell powershell)
{
if (input == null || input.Length == 0)
{
return s_emptyCommandCompletion;
}
if (cursorIndex > input.Length)
{
throw PSTraceSource.NewArgumentException(nameof(cursorIndex));
}
if (powershell == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(powershell));
}
// If we are in a debugger stop, let the debugger do the command completion.
var debugger = powershell.Runspace?.Debugger;
if ((debugger != null) && debugger.InBreakpoint)
{
return CompleteInputInDebugger(input, cursorIndex, options, debugger);
}
var remoteRunspace = powershell.Runspace as RemoteRunspace;
if (remoteRunspace != null)
{
// If the runspace is not available to run commands then exit here because nested commands are not
// supported on remote runspaces.
if (powershell.IsNested || (remoteRunspace.RunspaceAvailability != RunspaceAvailability.Available))
{
return s_emptyCommandCompletion;
}
// If it's in the nested prompt, the powershell instance is created by "PowerShell.Create(RunspaceMode.CurrentRunspace);".
// In this case, the powershell._runspace is null but if we try to access the property "Runspace", it will create a new
// local runspace - the default local runspace will be abandoned. So we check the powershell.IsChild first to make sure
// not to access the property "Runspace" in this case - powershell.isChild will be set to true only in this case.
if (!powershell.IsChild)
{
CheckScriptCallOnRemoteRunspace(remoteRunspace);
// TabExpansion2 script is not available prior to PSv3.
if (remoteRunspace.GetCapabilities().Equals(Runspaces.RunspaceCapability.Default))
{
return s_emptyCommandCompletion;
}
}
}
return CallScriptWithStringParameterSet(input, cursorIndex, options, powershell);
}
/// <summary>
/// Invokes the script function TabExpansion2.
/// </summary>
/// <param name="ast">The ast for pre-parsed input.</param>
/// <param name="tokens"></param>
/// <param name="cursorPosition"></param>
/// <param name="options">Optional options to configure how completion is performed.</param>
/// <param name="powershell">The powershell to use to invoke the script function TabExpansion2.</param>
/// <returns></returns>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "powershell")]
public static CommandCompletion CompleteInput(Ast ast, Token[] tokens, IScriptPosition cursorPosition, Hashtable options, PowerShell powershell)
{
if (ast == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(ast));
}
if (tokens == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(tokens));
}
if (cursorPosition == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(cursorPosition));
}
if (powershell == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(powershell));
}
if (ast.Extent.Text.Length == 0)
{
return s_emptyCommandCompletion;
}
// If we are in a debugger stop, let the debugger do the command completion.
var debugger = powershell.Runspace?.Debugger;
if ((debugger != null) && debugger.InBreakpoint)
{
return CompleteInputInDebugger(ast, tokens, cursorPosition, options, debugger);
}
var remoteRunspace = powershell.Runspace as RemoteRunspace;
if (remoteRunspace != null)
{
// If the runspace is not available to run commands then exit here because nested commands are not
// supported on remote runspaces.
if (powershell.IsNested || (remoteRunspace.RunspaceAvailability != RunspaceAvailability.Available))
{
return s_emptyCommandCompletion;
}
if (!powershell.IsChild)
{
CheckScriptCallOnRemoteRunspace(remoteRunspace);
// TabExpansion2 script is not available prior to PSv3.
if (remoteRunspace.GetCapabilities().Equals(Runspaces.RunspaceCapability.Default))
{
return s_emptyCommandCompletion;
}
// When calling the TabExpansion2 script, the input should be the whole script text
string input = ast.Extent.Text;
int cursorIndex = ((InternalScriptPosition)cursorPosition).Offset;
return CallScriptWithStringParameterSet(input, cursorIndex, options, powershell);
}
}
return CallScriptWithAstParameterSet(ast, tokens, cursorPosition, options, powershell);
}
/// <summary>
/// Get the next result, moving forward or backward. Supports wraparound, so if there are any results at all,
/// this method will never fail and never return null.
/// </summary>
/// <param name="forward">True if we should move forward through the list, false if backwards.</param>
/// <returns>The next completion result, or null if no results.</returns>
public CompletionResult GetNextResult(bool forward)
{
CompletionResult result = null;
var count = CompletionMatches.Count;
if (count > 0)
{
CurrentMatchIndex += forward ? 1 : -1;
if (CurrentMatchIndex >= count)
{
CurrentMatchIndex = 0;
}
else if (CurrentMatchIndex < 0)
{
CurrentMatchIndex = count - 1;
}
result = CompletionMatches[CurrentMatchIndex];
}
return result;
}
#endregion public methods
#region Internal methods
/// <summary>
/// Command completion while in debug break mode.
/// </summary>
/// <param name="input">The input script to complete.</param>
/// <param name="cursorIndex">The offset in <paramref name="input"/> where completion is requested.</param>
/// <param name="options">Optional parameter that specifies configurable options for completion.</param>
/// <param name="debugger">Current debugger.</param>
/// <returns>A collection of completions with the replacement start and length.</returns>
internal static CommandCompletion CompleteInputInDebugger(string input, int cursorIndex, Hashtable options, Debugger debugger)
{
if (input == null)
{
return s_emptyCommandCompletion;
}
if (cursorIndex > input.Length)
{
throw PSTraceSource.NewArgumentException(nameof(cursorIndex));
}
if (debugger == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(debugger));
}
Command cmd = new Command("TabExpansion2");
cmd.Parameters.Add("InputScript", input);
cmd.Parameters.Add("CursorColumn", cursorIndex);
cmd.Parameters.Add("Options", options);
return ProcessCompleteInputCommand(cmd, debugger);
}
/// <summary>
/// Command completion while in debug break mode.
/// </summary>
/// <param name="ast">The ast for pre-parsed input.</param>
/// <param name="tokens"></param>
/// <param name="cursorPosition"></param>
/// <param name="options">Optional options to configure how completion is performed.</param>
/// <param name="debugger">Current debugger.</param>
/// <returns>Command completion.</returns>
internal static CommandCompletion CompleteInputInDebugger(Ast ast, Token[] tokens, IScriptPosition cursorPosition, Hashtable options, Debugger debugger)
{
if (ast == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(ast));
}
if (tokens == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(tokens));
}
if (cursorPosition == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(cursorPosition));
}
if (debugger == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(debugger));
}
// For remote debugging just pass string input.
if ((debugger is RemoteDebugger) || debugger.IsPushed)
{
string input = ast.Extent.Text;
int cursorIndex = ((InternalScriptPosition)cursorPosition).Offset;
return CompleteInputInDebugger(input, cursorIndex, options, debugger);
}
Command cmd = new Command("TabExpansion2");
cmd.Parameters.Add("Ast", ast);
cmd.Parameters.Add("Tokens", tokens);
cmd.Parameters.Add("PositionOfCursor", cursorPosition);
cmd.Parameters.Add("Options", options);
return ProcessCompleteInputCommand(cmd, debugger);
}
private static CommandCompletion ProcessCompleteInputCommand(
Command cmd,
Debugger debugger)
{
PSCommand command = new PSCommand(cmd);
PSDataCollection<PSObject> output = new PSDataCollection<PSObject>();
debugger.ProcessCommand(command, output);
if (output.Count == 1)
{
var commandCompletion = output[0].BaseObject as CommandCompletion;
if (commandCompletion != null)
{
return commandCompletion;
}
}
return s_emptyCommandCompletion;
}
#endregion
#region private methods
private static void CheckScriptCallOnRemoteRunspace(RemoteRunspace remoteRunspace)
{
var remoteRunspaceInternal = remoteRunspace.RunspacePool.RemoteRunspacePoolInternal;
if (remoteRunspaceInternal != null)
{
var transportManager = remoteRunspaceInternal.DataStructureHandler.TransportManager;
if (transportManager != null && transportManager.TypeTable == null)
{
// The remote runspace was created without a TypeTable instance.
// The tab completion results cannot be deserialized if the TypeTable is not available
throw PSTraceSource.NewInvalidOperationException(TabCompletionStrings.CannotDeserializeTabCompletionResult);
}
}
}
private static CommandCompletion CallScriptWithStringParameterSet(string input, int cursorIndex, Hashtable options, PowerShell powershell)
{
try
{
powershell.Commands.Clear();
powershell.AddCommand("TabExpansion2")
.AddArgument(input)
.AddArgument(cursorIndex)
.AddArgument(options);
var results = powershell.Invoke();
if (results == null)
{
return s_emptyCommandCompletion;
}
if (results.Count == 1)
{
var result = PSObject.Base(results[0]);
var commandCompletion = result as CommandCompletion;
if (commandCompletion != null)
{
return commandCompletion;
}
}
}
catch (Exception)
{
}
finally
{
powershell.Commands.Clear();
}
return s_emptyCommandCompletion;
}
private static CommandCompletion CallScriptWithAstParameterSet(Ast ast, Token[] tokens, IScriptPosition cursorPosition, Hashtable options, PowerShell powershell)
{
try
{
powershell.Commands.Clear();
powershell.AddCommand("TabExpansion2")
.AddArgument(ast)
.AddArgument(tokens)
.AddArgument(cursorPosition)
.AddArgument(options);
var results = powershell.Invoke();
if (results == null)
{
return s_emptyCommandCompletion;
}
if (results.Count == 1)
{
var result = PSObject.Base(results[0]);
var commandCompletion = result as CommandCompletion;
if (commandCompletion != null)
{
return commandCompletion;
}
}
}
catch (Exception)
{
}
finally
{
powershell.Commands.Clear();
}
return s_emptyCommandCompletion;
}
// This is the start of the real implementation of autocomplete/intellisense/tab completion
private static CommandCompletion CompleteInputImpl(Ast ast, Token[] tokens, IScriptPosition positionOfCursor, Hashtable options)
{
#if LEGACYTELEMETRY
// We could start collecting telemetry at a later date.
// We will leave the #if to remind us that we did this once.
var sw = new Stopwatch();
sw.Start();
#endif
using (var powershell = PowerShell.Create(RunspaceMode.CurrentRunspace))
{
var context = LocalPipeline.GetExecutionContextFromTLS();
int replacementIndex = -1;
int replacementLength = -1;
List<CompletionResult> results = null;
{
// If we were invoked from TabExpansion2, we want to "remove" TabExpansion2 and anything it calls
// from our results. We do this by faking out the session so that TabExpansion2 isn't anywhere to be found.
SessionStateScope scopeToRestore;
if (context.CurrentCommandProcessor is not null
&& context.CurrentCommandProcessor.Command.CommandInfo.Name.Equals("TabExpansion2", StringComparison.OrdinalIgnoreCase)
&& context.CurrentCommandProcessor.UseLocalScope
&& context.EngineSessionState.CurrentScope.Parent is not null)
{
scopeToRestore = context.EngineSessionState.CurrentScope;
context.EngineSessionState.CurrentScope = scopeToRestore.Parent;
}
else
{
scopeToRestore = null;
}
try
{
var completionAnalysis = new CompletionAnalysis(ast, tokens, positionOfCursor, options);
results = completionAnalysis.GetResults(powershell, out replacementIndex, out replacementLength);
}
finally
{
if (scopeToRestore != null)
{
context.EngineSessionState.CurrentScope = scopeToRestore;
}
}
}
var completionResults = results ?? EmptyCompletionResult;
#if LEGACYTELEMETRY
// no telemetry here. We don't capture tab completion performance.
sw.Stop();
TelemetryAPI.ReportTabCompletionTelemetry(sw.ElapsedMilliseconds, completionResults.Count,
completionResults.Count > 0 ? completionResults[0].ResultType : CompletionResultType.Text);
#endif
return new CommandCompletion(
new Collection<CompletionResult>(completionResults),
-1,
replacementIndex,
replacementLength);
}
}
#endregion private methods
}
}
|