PoshCode Archive  Artifact [beb764dfee]

Artifact beb764dfee3a960a41975a938919a5e166123be5683be07a1f11dafe15575757:

  • File Del-TrailingBlank-ISE.ps1 — part of check-in [65e76dd968] at 2018-06-10 12:59:23 on branch trunk — This function is intended to be uses as ISE add on. (user: unknown size: 1219)

# encoding: ascii
# api: powershell
# title: Del. TrailingBlank (ISE)
# description: This function is intended to be uses as ISE add on.
# version: 0.1
# type: function
# license: CC0
# function: Delete-TrailingBlanks
# x-poshcode-id: 1626
# x-archived: 2010-02-26T12:40:11
#
# When working with ISE multiline texts take care of correct `r`n handling. 
# Finding the regular expression was a bit tricky.
# Compare the different versions for yourself
#
function Delete-TrailingBlanks
{
    $editor = $psISE.CurrentFile.Editor
    $caretLine = $editor.CaretLine

#     First trial. Works.  
#     $newText = @()
#     foreach ( $line in $editor.Text.Split("`n") )
#     {
#         $newText += $line -replace ("\s+$", "")
#     }
#     $editor.Text = [String]::Join("`n", $newText)
 
#    2nd trial, but deletes empty lines too  \s matches `r and `n    
#    $editor.Text = $editor.Text -replace '(?m)\s*$', ''

#    3rd working again, but doesn't it look like perl ? 
#    $editor.Text = $editor.Text -replace  '(?m)[ \t]+(?:\r?)$', ''

#  the solution is so simple  
    $editor.Text = $editor.Text -replace '(?m)\s*?$', ''

       
    $editor.SetCaretPosition($caretLine, 1)
}