|
4 | 4 |
|
5 | 5 | using System;
|
6 | 6 | using System.Collections.Generic;
|
| 7 | +using System.Linq; |
7 | 8 | using System.Management.Automation.Language;
|
8 | 9 | using Microsoft.PowerShell.Internal;
|
9 | 10 |
|
@@ -347,9 +348,9 @@ public static void YankLastArg(ConsoleKeyInfo? key = null, object arg = null)
|
347 | 348 | }
|
348 | 349 | }
|
349 | 350 |
|
350 |
| - private void VisualSelectionCommon(Action action) |
| 351 | + private void VisualSelectionCommon(Action action, bool forceSetMark = false) |
351 | 352 | {
|
352 |
| - if (_singleton._visualSelectionCommandCount == 0) |
| 353 | + if (_singleton._visualSelectionCommandCount == 0 || forceSetMark) |
353 | 354 | {
|
354 | 355 | SetMark();
|
355 | 356 | }
|
@@ -449,6 +450,197 @@ public static void SelectBackwardsLine(ConsoleKeyInfo? key = null, object arg =
|
449 | 450 | _singleton.VisualSelectionCommon(() => BeginningOfLine(key, arg));
|
450 | 451 | }
|
451 | 452 |
|
| 453 | + /// <summary> |
| 454 | + /// Select the command argument that the cursor is at, or the previous/next Nth command arguments from the current cursor position. |
| 455 | + /// </summary> |
| 456 | + public static void SelectCommandArgument(ConsoleKeyInfo? key = null, object arg = null) |
| 457 | + { |
| 458 | + if (!TryGetArgAsInt(arg, out var numericArg, 0)) |
| 459 | + { |
| 460 | + return; |
| 461 | + } |
| 462 | + |
| 463 | + _singleton.MaybeParseInput(); |
| 464 | + |
| 465 | + int cursor = _singleton._current; |
| 466 | + int prev = -1, curr = -1, next = -1; |
| 467 | + var sbAsts = _singleton._ast.FindAll(GetScriptBlockAst, searchNestedScriptBlocks: true).ToList(); |
| 468 | + var arguments = new List<ExpressionAst>(); |
| 469 | + |
| 470 | + // We start searching for command arguments from the most nested script block. |
| 471 | + for (int i = sbAsts.Count - 1; i >= 0; i --) |
| 472 | + { |
| 473 | + var sbAst = sbAsts[i]; |
| 474 | + var cmdAsts = sbAst.FindAll(ast => ast is CommandAst, searchNestedScriptBlocks: false); |
| 475 | + |
| 476 | + foreach (CommandAst cmdAst in cmdAsts) |
| 477 | + { |
| 478 | + for (int j = 1; j < cmdAst.CommandElements.Count; j++) |
| 479 | + { |
| 480 | + var argument = cmdAst.CommandElements[j] switch |
| 481 | + { |
| 482 | + CommandParameterAst paramAst => paramAst.Argument, |
| 483 | + ExpressionAst expAst => expAst, |
| 484 | + _ => null, |
| 485 | + }; |
| 486 | + |
| 487 | + if (argument is not null) |
| 488 | + { |
| 489 | + arguments.Add(argument); |
| 490 | + |
| 491 | + int start = argument.Extent.StartOffset; |
| 492 | + int end = argument.Extent.EndOffset; |
| 493 | + |
| 494 | + if (end <= cursor) |
| 495 | + { |
| 496 | + prev = arguments.Count - 1; |
| 497 | + } |
| 498 | + if (curr == -1 && start <= cursor && end > cursor) |
| 499 | + { |
| 500 | + curr = arguments.Count - 1; |
| 501 | + } |
| 502 | + else if (next == -1 && start > cursor) |
| 503 | + { |
| 504 | + next = arguments.Count - 1; |
| 505 | + } |
| 506 | + } |
| 507 | + } |
| 508 | + } |
| 509 | + |
| 510 | + // Stop searching the outer script blocks if we find any command arguments within the current script block. |
| 511 | + if (arguments.Count > 0) |
| 512 | + { |
| 513 | + break; |
| 514 | + } |
| 515 | + } |
| 516 | + |
| 517 | + // Simply return if we didn't find any command arguments. |
| 518 | + int count = arguments.Count; |
| 519 | + if (count == 0) |
| 520 | + { |
| 521 | + return; |
| 522 | + } |
| 523 | + |
| 524 | + if (prev == -1) { prev = count - 1; } |
| 525 | + if (next == -1) { next = 0; } |
| 526 | + if (curr == -1) { curr = numericArg > 0 ? prev : next; } |
| 527 | + |
| 528 | + int newStartCursor, newEndCursor; |
| 529 | + int selectCount = _singleton._visualSelectionCommandCount; |
| 530 | + |
| 531 | + // When an argument is already visually selected by the previous run of this function, the cursor would have past the selected argument. |
| 532 | + // In this case, if a user wants to move backward to an argument that is before the currently selected argument by having numericArg < 0, |
| 533 | + // we will need to adjust 'numericArg' to move to the expected argument. |
| 534 | + // Scenario: |
| 535 | + // 1) 'Alt+a' to select an argument; |
| 536 | + // 2) 'Alt+-' to make 'numericArg = -1'; |
| 537 | + // 3) 'Alt+a' to select the argument that is right before the currently selected argument. |
| 538 | + if (count > 1 && numericArg < 0 && curr == next && selectCount > 0) |
| 539 | + { |
| 540 | + var prevArg = arguments[prev]; |
| 541 | + if (_singleton._mark == prevArg.Extent.StartOffset && cursor == prevArg.Extent.EndOffset) |
| 542 | + { |
| 543 | + numericArg--; |
| 544 | + } |
| 545 | + } |
| 546 | + |
| 547 | + while (true) |
| 548 | + { |
| 549 | + ExpressionAst targetAst = null; |
| 550 | + if (numericArg == 0) |
| 551 | + { |
| 552 | + targetAst = arguments[curr]; |
| 553 | + } |
| 554 | + else |
| 555 | + { |
| 556 | + int index = curr + numericArg; |
| 557 | + index = index >= 0 ? index % count : (count + index % count) % count; |
| 558 | + targetAst = arguments[index]; |
| 559 | + } |
| 560 | + |
| 561 | + // Handle quoted-string arguments specially, by leaving the quotes out of the visual selection. |
| 562 | + StringConstantType? constantType = null; |
| 563 | + if (targetAst is StringConstantExpressionAst conString) |
| 564 | + { |
| 565 | + constantType = conString.StringConstantType; |
| 566 | + } |
| 567 | + else if (targetAst is ExpandableStringExpressionAst expString) |
| 568 | + { |
| 569 | + constantType = expString.StringConstantType; |
| 570 | + } |
| 571 | + |
| 572 | + int startOffsetAdjustment = 0, endOffsetAdjustment = 0; |
| 573 | + switch (constantType) |
| 574 | + { |
| 575 | + case StringConstantType.DoubleQuoted: |
| 576 | + case StringConstantType.SingleQuoted: |
| 577 | + startOffsetAdjustment = endOffsetAdjustment = 1; |
| 578 | + break; |
| 579 | + case StringConstantType.DoubleQuotedHereString: |
| 580 | + case StringConstantType.SingleQuotedHereString: |
| 581 | + startOffsetAdjustment = 2; |
| 582 | + endOffsetAdjustment = 3; |
| 583 | + break; |
| 584 | + default: break; |
| 585 | + } |
| 586 | + |
| 587 | + newStartCursor = targetAst.Extent.StartOffset + startOffsetAdjustment; |
| 588 | + newEndCursor = targetAst.Extent.EndOffset - endOffsetAdjustment; |
| 589 | + |
| 590 | + // For quoted-string arguments, due to the special handling above, the cursor would always be |
| 591 | + // within the selected argument (cursor is placed at the ending quote), and thus when running |
| 592 | + // the 'SelectCommandArgument' action again, the same argument would be chosen. |
| 593 | + // |
| 594 | + // Below is how we detect this and move to the next argument when there is one: |
| 595 | + // * the previous action was a visual selection command and the visual range was exactly |
| 596 | + // what we are going to make. AND |
| 597 | + // * count > 1, meaning that there are other arguments. AND |
| 598 | + // * numericArg == 0. When 'numericArg' is not 0, the user is leaping among the available |
| 599 | + // arguments, so it's possible that the same argument gets chosen. |
| 600 | + // In this case, we should select the next argument. |
| 601 | + if (numericArg == 0 && count > 1 && selectCount > 0 && |
| 602 | + _singleton._mark == newStartCursor && cursor == newEndCursor) |
| 603 | + { |
| 604 | + curr = next; |
| 605 | + continue; |
| 606 | + } |
| 607 | + |
| 608 | + break; |
| 609 | + } |
| 610 | + |
| 611 | + // Move cursor to the start of the argument. |
| 612 | + SetCursorPosition(newStartCursor); |
| 613 | + // Make the intended range visually selected. |
| 614 | + _singleton.VisualSelectionCommon(() => SetCursorPosition(newEndCursor), forceSetMark: true); |
| 615 | + |
| 616 | + |
| 617 | + // Get the script block AST's whose extent contains the cursor. |
| 618 | + bool GetScriptBlockAst(Ast ast) |
| 619 | + { |
| 620 | + if (ast is not ScriptBlockAst) |
| 621 | + { |
| 622 | + return false; |
| 623 | + } |
| 624 | + |
| 625 | + if (ast.Parent is null) |
| 626 | + { |
| 627 | + return true; |
| 628 | + } |
| 629 | + |
| 630 | + if (ast.Extent.StartOffset >= cursor) |
| 631 | + { |
| 632 | + return false; |
| 633 | + } |
| 634 | + |
| 635 | + // If the script block is closed, then we want the script block only if the cursor is within the script block. |
| 636 | + // Otherwise, if the script block is not completed, then we want the script block even if the cursor is at the end. |
| 637 | + int textLength = ast.Extent.Text.Length; |
| 638 | + return ast.Extent.Text[textLength - 1] == '}' |
| 639 | + ? ast.Extent.EndOffset - 1 > cursor |
| 640 | + : ast.Extent.EndOffset >= cursor; |
| 641 | + } |
| 642 | + } |
| 643 | + |
452 | 644 | /// <summary>
|
453 | 645 | /// Paste text from the system clipboard.
|
454 | 646 | /// </summary>
|
|
0 commit comments