PowerShell_ISE Scripting: ConvertTo-Comment

PowerShell_ISE Scripting: ConvertTo-Comment

  • Comments 6

Let me combine a couple of things. 

1) When I posted my PowerShell_ISE profile, I included a function Goto-Line that is now builtin to the ISE so I reposted the blog with this function commented out. I commented it out instead of deleting it because I wanted to provide an example of how to do it.

2) Vivek Sharma left a comment asked about programmatically controlling indentation so I gave a couple of pointers to the objects required to program the editor.

So let’s take those 2 and put them together and write a function which will convert the selected text into a PowerShell comment (using another CTP3 feature – block comments”). 

Here goes:

function ConvertTo-Comment
{
    $editor = $psISE.CurrentOpenedFile.Editor
    $CommentedText = "<#`n" + $editor.SelectedText + "#>"
    # INSERTING overwrites the SELECTED text
    $editor.InsertText($CommentedText)
}
$null = $psISE.CustomMenu.Submenus.Add("Comment Selected", {ConvertTo-Comment}, $null)

 

Give that a try and then start experimenting on your own.  Think TECOTPU, Emacs etc.  Go crazy and share share share.

Enjoy!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 7 and 8 and type the answer here:
  • Post
  • Hello Jeffrey,

    attention, block comments do not nest. Included  block ends must be escaped or the first one would finish the comment prematurely.

    In http://pauerschell.blogspot.com/2008/12/ise-blockcomment-blockuncomment-custom.html I show a function that works this way.

    The ways for escape are not unique. Perhaps it would help, if you propose some convention. This moment I prefer `#> .

    By the way many thanks for the excellent syntax coloring in ISE.

    Happy new year

    Bernd

    bkriszio@googlemail.com

  • Hi Bernd, good to see you over here. I was just going to mention I had come up with roughly the same thing at http://get-powershell.com/2008/12/29/ise-comment-out-a-block-of-text/ and that you had mentioned you didn't like the multi-line quote. It's a small world :)

  • Wicked cool, I've been wanting this.

  • > Wicked cool

    What are you from Mass?

    Wait ..  no... that would have been "wicked pissa cool" - never mind.

    :-)

    Jeffrey Snover [MSFT]

    Windows Management Partner Architect

    Graduate of Chelmsford (Mass) High School

  • Building on Bernd Kriszio's comment here and at Andy's blog, here's a Toggle Commenting menu item (with Ctrl+/ being the hotkey):

    function ISE-CommentSelectedText {

       $text = $psise.CurrentOpenedFile.editor.SelectedText

       $lines = $text.Split("`n") | %{

           $_ -replace "^", "# "

       }

       if ($lines[$lines.count -1] -eq "# ") {

           $lines[$lines.count -1] = ""

       }

       $text = [string]::join("`n", $lines)

       $psise.CurrentOpenedFile.editor.InsertText($text)

    }

    function ISE-UncommentSelectedText {

       $text = $psise.CurrentOpenedFile.editor.SelectedText

       $lines = $text.Split("`n") | %{

           $_ -replace "^ *# *", ""

       }

       $text = [string]::join("`n", $lines)

       $psise.CurrentOpenedFile.editor.InsertText($text)

    }

    function ISE-ToggleCommenting {

       if ($psISE.CurrentOpenedFile.Editor.SelectedText.StartsWith('#')) {

           ISE-UncommentSelectedText      

       }

       else {

           ISE-CommentSelectedText

       }

    }

    $psISE.CustomMenu.Submenus.Add('Comment/Uncomment', {ISE-ToggleCommenting}, "Ctrl+Oem2")

  • Will this be helpful to all viewers?

Page 1 of 1 (6 items)