ISE – Comment out a block of text

December 29, 2008 at 10:12 AMAndy Schneider

In a comment in an earlier post, reader Bernd asks the following question:

Now I'm guessing if there is a way to use $psise.CustomMenu to add CommentBlock and UnCommentBlock commands.

One way to tackle this would be the following.

$text = $psise.CurrentOpenedFile.editor.SelectedText                                                                                                        
$psise.CurrentOpenedFile.editor.InsertText("<# $text #>")      

Once you have this you can wrap it in a function and use Custom Menus to create a “Comment Block” Menu item.

Hope this helps.

Andy

Posted in:

Tags:

Comments (2) -

Bernd Kriszio
Bernd Kriszio says:

Thanks Andy,
that helps a lot and showes the posibilities to extend ISE.

Next we need a way to add submenues, or menu.height becomes greater screen.heigt very soon.

And if we could get at tab_switch event, we could display the full path of the loaded files.

Of course practically I would not use block comments, because their nesting capabilities are poor.  

Bernd
b.kriszio@googlemail.com

Reply

Bernd Kriszio
Bernd Kriszio says:

And here the final result:

function ISE-CommentOut () {
    $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-CommentIn () {
    $text = $psise.CurrentOpenedFile.editor.SelectedText
    $lines = $text.Split("`n") |% {
        $_ -replace "^ *# *", ""
    }
    $text = [string]::join("`n", $lines)
    $psise.CurrentOpenedFile.editor.InsertText($text)  
}

$psIse.CustomMenu.Submenus.Add("Comment Out", {ISE-CommentOut}, "Ctrl+K")  
$psIse.CustomMenu.Submenus.Add("Comment In", {ISE-CommentIn}, "Ctrl+SHift+K")

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading