Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comment in SamplePSReadLineProfile.ps1 #1712

Merged
merged 1 commit into from Aug 2, 2020

Conversation

sethvs
Copy link
Contributor

@sethvs sethvs commented Jul 29, 2020

PR Summary

Add comment in SamplePSReadLineProfile.ps1 so that the purpose of the code block is more clear.

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Make sure you've added one or more new tests
  • User-facing changes
    • Not Applicable
    • OR
    • Documentation needed at PowerShell-Docs
      • Doc Issue filed:
Microsoft Reviewers: Open in CodeFlow

@lzybkr
Copy link
Member

lzybkr commented Jul 29, 2020

This smart quote example can be pretty annoying sometimes. I was considering replacing it with something simpler. This is what I'm currently using and am finding it less annoying even though it does less:

Set-PSReadLineKeyHandler -Key '"',"'" `
                         -BriefDescription SmartInsertQuote `
                         -LongDescription "Insert paired quotes if not already on a quote" `
                         -ScriptBlock {
    param($key, $arg)

    $quote = $key.KeyChar

    $selectionStart = $null
    $selectionLength = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$selectionStart, [ref]$selectionLength)

    $line = $null
    $cursor = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

    # If text is selected, just quote it without any smarts
    if ($selectionStart -ne -1)
    {
        [Microsoft.PowerShell.PSConsoleReadLine]::Replace($selectionStart, $selectionLength, $quote + $line.SubString($selectionStart, $selectionLength) + $quote)
        [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($selectionStart + $selectionLength + 2)
        return
    }

    if ($cursor -lt $line.Length -and $line[$cursor] -eq $quote) {
        # Just move the cursor, the current character is most likely a closing quote.
        [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor + 1)
        return
    }

    # Only insert matching quotes if doing so avoids adding additional errors.
    $parseErrors = $null
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$null, [ref]$null, [ref]$parseErrors, [ref]$null)

    $insertSingleQuoteLine = $line.Substring(0, $cursor) + $quote
    if ($cursor -le $line.Length) {
        $insertSingleQuoteLine += $line.Substring($cursor)
    }

    $newParseErrors = $null
    $null = [Parser]::ParseInput($insertSingleQuoteLine, [ref]$null, [ref]$newParseErrors)
    if ($parseErrors.Length -ge $newParseErrors.Length) {
        [Microsoft.PowerShell.PSConsoleReadLine]::Insert("$quote")
    } else {
        # Insert matching quotes, move cursor to be in between the quotes
        [Microsoft.PowerShell.PSConsoleReadLine]::Insert("$quote$quote")
        [Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor + 1)
    }
}

@daxian-dbw
Copy link
Member

@lzybkr Do you want to submit a PR to replace the existing one in SamplePSReadLineProfile.ps1?

@daxian-dbw daxian-dbw merged commit a873097 into PowerShell:master Aug 2, 2020
@sethvs sethvs deleted the profile branch August 3, 2020 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants