From 2ce156d3864737bf469cfe1f26e662048167687c Mon Sep 17 00:00:00 2001 From: Anthony Allen Date: Thu, 12 Nov 2020 08:44:35 +0000 Subject: [PATCH 1/9] Add select argument handler --- PSReadLine/SamplePSReadLineProfile.ps1 | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/PSReadLine/SamplePSReadLineProfile.ps1 b/PSReadLine/SamplePSReadLineProfile.ps1 index 9183224f..ca6d486a 100644 --- a/PSReadLine/SamplePSReadLineProfile.ps1 +++ b/PSReadLine/SamplePSReadLineProfile.ps1 @@ -600,3 +600,54 @@ Set-PSReadLineKeyHandler -Key RightArrow ` [Microsoft.PowerShell.PSConsoleReadLine]::AcceptNextSuggestionWord($key, $arg) } } + +Set-PSReadLineKeyHandler -Key Alt+a ` + -BriefDescription SelectCommandArguments ` + -LongDescription "Set current selection to next command argument in the command line. Use of digit argument selects argument by position" + -ScriptBlock { + param($key, $arg) + + $line = $null + $cursor = $null + [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor) + + $tokens = $null + $ast = $null + $parseErrors = $null + [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$parseErrors, [ref]$null) + + $asts = $ast.FindAll( { + $args[0] -is [System.Management.Automation.Language.StringConstantExpressionAst] -and + $args[0].Parent -is [System.Management.Automation.Language.CommandAst] -and + $args[0].Extent.StartOffset -ne $args[0].Parent.Extent.StartOffset + }, $true) + + if ($null -ne $arg) { + $nextAst = $asts[$arg - 1] + } + else { + foreach ($ast in $asts) { + if ($ast.Extent.StartOffset -ge $cursor) { + $nextAst = $ast + break + } + } + + if ($null -eq $nextAst) { + $nextAst = $asts[0] + } + } + + if ($nextAst.StringConstantType -ne [System.Management.Automation.Language.StringConstantType]::BareWord) { + $startOffsetAdjustment = 1 + $endOffsetAdjustment = 2 + } + else { + $startOffsetAdjustment = 0 + $endOffsetAdjustment = 0 + } + + [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($nextAst.Extent.StartOffset + $startOffsetAdjustment) + [Microsoft.PowerShell.PSConsoleReadLine]::SetMark($null, $null) + [Microsoft.PowerShell.PSConsoleReadLine]::SelectForwardChar($null, ($nextAst.Extent.EndOffset - $nextAst.Extent.StartOffset) - $endOffsetAdjustment) + } \ No newline at end of file From 448b20ee63d8f737b585394a028b8487a33bde2e Mon Sep 17 00:00:00 2001 From: Anthony Allen Date: Thu, 12 Nov 2020 08:49:53 +0000 Subject: [PATCH 2/9] Add comment description --- PSReadLine/SamplePSReadLineProfile.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/PSReadLine/SamplePSReadLineProfile.ps1 b/PSReadLine/SamplePSReadLineProfile.ps1 index ca6d486a..8bfc4223 100644 --- a/PSReadLine/SamplePSReadLineProfile.ps1 +++ b/PSReadLine/SamplePSReadLineProfile.ps1 @@ -601,6 +601,9 @@ Set-PSReadLineKeyHandler -Key RightArrow ` } } +# Cycle through arguments on current line and select the text. This makes it easier to quickly change the argument if re-running a previously run command from the history +# or if using a psreadline predictor. You can also use a digit argument to specify which argument you want to select, i.e. Alt+1, Alt+a selects the first argument +# on the command line. Set-PSReadLineKeyHandler -Key Alt+a ` -BriefDescription SelectCommandArguments ` -LongDescription "Set current selection to next command argument in the command line. Use of digit argument selects argument by position" From 4ce6af2b7013b99bb42ae4d61f5bc01a0f939100 Mon Sep 17 00:00:00 2001 From: Anthony Allen Date: Thu, 12 Nov 2020 08:52:29 +0000 Subject: [PATCH 3/9] Add missing backtick --- PSReadLine/SamplePSReadLineProfile.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PSReadLine/SamplePSReadLineProfile.ps1 b/PSReadLine/SamplePSReadLineProfile.ps1 index 8bfc4223..b3e30531 100644 --- a/PSReadLine/SamplePSReadLineProfile.ps1 +++ b/PSReadLine/SamplePSReadLineProfile.ps1 @@ -606,7 +606,7 @@ Set-PSReadLineKeyHandler -Key RightArrow ` # on the command line. Set-PSReadLineKeyHandler -Key Alt+a ` -BriefDescription SelectCommandArguments ` - -LongDescription "Set current selection to next command argument in the command line. Use of digit argument selects argument by position" + -LongDescription "Set current selection to next command argument in the command line. Use of digit argument selects argument by position" ` -ScriptBlock { param($key, $arg) From fcc77cc1117fe61e161df22860974d8f74ed1ec6 Mon Sep 17 00:00:00 2001 From: Anthony Allen Date: Fri, 13 Nov 2020 07:57:37 +0000 Subject: [PATCH 4/9] Remove unneeded getbufferstate --- PSReadLine/SamplePSReadLineProfile.ps1 | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/PSReadLine/SamplePSReadLineProfile.ps1 b/PSReadLine/SamplePSReadLineProfile.ps1 index b3e30531..3c5e64d6 100644 --- a/PSReadLine/SamplePSReadLineProfile.ps1 +++ b/PSReadLine/SamplePSReadLineProfile.ps1 @@ -610,14 +610,11 @@ Set-PSReadLineKeyHandler -Key Alt+a ` -ScriptBlock { param($key, $arg) - $line = $null - $cursor = $null - [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor) - $tokens = $null $ast = $null $parseErrors = $null - [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$parseErrors, [ref]$null) + $cursor = $null + [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$parseErrors, [ref]$cursor) $asts = $ast.FindAll( { $args[0] -is [System.Management.Automation.Language.StringConstantExpressionAst] -and @@ -625,6 +622,8 @@ Set-PSReadLineKeyHandler -Key Alt+a ` $args[0].Extent.StartOffset -ne $args[0].Parent.Extent.StartOffset }, $true) + $nextAst = $null + if ($null -ne $arg) { $nextAst = $asts[$arg - 1] } From 99e9e7efe5d9cd7e827a6d25e69b4e0a8fa2f048 Mon Sep 17 00:00:00 2001 From: Anthony Allen Date: Fri, 13 Nov 2020 07:59:35 +0000 Subject: [PATCH 5/9] Fix indentation --- PSReadLine/SamplePSReadLineProfile.ps1 | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/PSReadLine/SamplePSReadLineProfile.ps1 b/PSReadLine/SamplePSReadLineProfile.ps1 index 3c5e64d6..26d165e2 100644 --- a/PSReadLine/SamplePSReadLineProfile.ps1 +++ b/PSReadLine/SamplePSReadLineProfile.ps1 @@ -625,28 +625,28 @@ Set-PSReadLineKeyHandler -Key Alt+a ` $nextAst = $null if ($null -ne $arg) { - $nextAst = $asts[$arg - 1] + $nextAst = $asts[$arg - 1] } else { - foreach ($ast in $asts) { - if ($ast.Extent.StartOffset -ge $cursor) { - $nextAst = $ast - break + foreach ($ast in $asts) { + if ($ast.Extent.StartOffset -ge $cursor) { + $nextAst = $ast + break + } + } + + if ($null -eq $nextAst) { + $nextAst = $asts[0] } - } - - if ($null -eq $nextAst) { - $nextAst = $asts[0] - } } if ($nextAst.StringConstantType -ne [System.Management.Automation.Language.StringConstantType]::BareWord) { - $startOffsetAdjustment = 1 - $endOffsetAdjustment = 2 + $startOffsetAdjustment = 1 + $endOffsetAdjustment = 2 } else { - $startOffsetAdjustment = 0 - $endOffsetAdjustment = 0 + $startOffsetAdjustment = 0 + $endOffsetAdjustment = 0 } [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($nextAst.Extent.StartOffset + $startOffsetAdjustment) From 0844b110cf56e7b4e8b3fc8a1d41d6237ea435c8 Mon Sep 17 00:00:00 2001 From: Anthony Allen Date: Fri, 13 Nov 2020 08:15:56 +0000 Subject: [PATCH 6/9] Refactor to use any expression --- PSReadLine/SamplePSReadLineProfile.ps1 | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/PSReadLine/SamplePSReadLineProfile.ps1 b/PSReadLine/SamplePSReadLineProfile.ps1 index 26d165e2..1c425a21 100644 --- a/PSReadLine/SamplePSReadLineProfile.ps1 +++ b/PSReadLine/SamplePSReadLineProfile.ps1 @@ -617,7 +617,7 @@ Set-PSReadLineKeyHandler -Key Alt+a ` [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$parseErrors, [ref]$cursor) $asts = $ast.FindAll( { - $args[0] -is [System.Management.Automation.Language.StringConstantExpressionAst] -and + $args[0] -is [System.Management.Automation.Language.ExpressionAst] -and $args[0].Parent -is [System.Management.Automation.Language.CommandAst] -and $args[0].Extent.StartOffset -ne $args[0].Parent.Extent.StartOffset }, $true) @@ -639,17 +639,17 @@ Set-PSReadLineKeyHandler -Key Alt+a ` $nextAst = $asts[0] } } - - if ($nextAst.StringConstantType -ne [System.Management.Automation.Language.StringConstantType]::BareWord) { - $startOffsetAdjustment = 1 - $endOffsetAdjustment = 2 - } - else { - $startOffsetAdjustment = 0 - $endOffsetAdjustment = 0 + + $startOffsetAdjustment = 0 + $endOffsetAdjustment = 0 + + if ($nextAst -is [System.Management.Automation.Language.StringConstantExpressionAst] -and + $nextAst.StringConstantType -ne [System.Management.Automation.Language.StringConstantType]::BareWord) { + $startOffsetAdjustment = 1 + $endOffsetAdjustment = 2 } [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($nextAst.Extent.StartOffset + $startOffsetAdjustment) [Microsoft.PowerShell.PSConsoleReadLine]::SetMark($null, $null) [Microsoft.PowerShell.PSConsoleReadLine]::SelectForwardChar($null, ($nextAst.Extent.EndOffset - $nextAst.Extent.StartOffset) - $endOffsetAdjustment) - } \ No newline at end of file + } From 46adc0e8c26f79bce2c79b9b53f79ef5244edb3f Mon Sep 17 00:00:00 2001 From: Anthony Allen Date: Sat, 14 Nov 2020 16:13:02 +0100 Subject: [PATCH 7/9] Update PSReadLine/SamplePSReadLineProfile.ps1 Co-authored-by: Dongbo Wang --- PSReadLine/SamplePSReadLineProfile.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PSReadLine/SamplePSReadLineProfile.ps1 b/PSReadLine/SamplePSReadLineProfile.ps1 index 1c425a21..dd60a4fe 100644 --- a/PSReadLine/SamplePSReadLineProfile.ps1 +++ b/PSReadLine/SamplePSReadLineProfile.ps1 @@ -652,4 +652,4 @@ Set-PSReadLineKeyHandler -Key Alt+a ` [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($nextAst.Extent.StartOffset + $startOffsetAdjustment) [Microsoft.PowerShell.PSConsoleReadLine]::SetMark($null, $null) [Microsoft.PowerShell.PSConsoleReadLine]::SelectForwardChar($null, ($nextAst.Extent.EndOffset - $nextAst.Extent.StartOffset) - $endOffsetAdjustment) - } +} From 482d5d5ffafa2137a108ab43d7daac60f5f80791 Mon Sep 17 00:00:00 2001 From: Anthony Allen Date: Sat, 14 Nov 2020 15:16:08 +0000 Subject: [PATCH 8/9] Remove unused variables --- PSReadLine/SamplePSReadLineProfile.ps1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/PSReadLine/SamplePSReadLineProfile.ps1 b/PSReadLine/SamplePSReadLineProfile.ps1 index dd60a4fe..7d1ee797 100644 --- a/PSReadLine/SamplePSReadLineProfile.ps1 +++ b/PSReadLine/SamplePSReadLineProfile.ps1 @@ -610,11 +610,9 @@ Set-PSReadLineKeyHandler -Key Alt+a ` -ScriptBlock { param($key, $arg) - $tokens = $null $ast = $null - $parseErrors = $null $cursor = $null - [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$parseErrors, [ref]$cursor) + [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$null, [ref]$null, [ref]$cursor) $asts = $ast.FindAll( { $args[0] -is [System.Management.Automation.Language.ExpressionAst] -and From b66f2fcef66e8510f39df5cd368fdadf1a664610 Mon Sep 17 00:00:00 2001 From: Anthony Allen Date: Wed, 18 Nov 2020 20:54:32 +0100 Subject: [PATCH 9/9] Return early when no asts found --- PSReadLine/SamplePSReadLineProfile.ps1 | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/PSReadLine/SamplePSReadLineProfile.ps1 b/PSReadLine/SamplePSReadLineProfile.ps1 index 7d1ee797..8ccb8c1e 100644 --- a/PSReadLine/SamplePSReadLineProfile.ps1 +++ b/PSReadLine/SamplePSReadLineProfile.ps1 @@ -620,6 +620,11 @@ Set-PSReadLineKeyHandler -Key Alt+a ` $args[0].Extent.StartOffset -ne $args[0].Parent.Extent.StartOffset }, $true) + if ($asts.Count -eq 0) { + [Microsoft.PowerShell.PSConsoleReadLine]::Ding() + return + } + $nextAst = $null if ($null -ne $arg) {