Welcome to MSDN Blogs Sign in | Join | Help

PowerShell_ISE Scripting: ConvertTo-Comment

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

Published Wednesday, December 31, 2008 4:50 PM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: PowerShell_ISE Scripting: ConvertTo-Comment

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

Wednesday, December 31, 2008 2:01 PM by Bernd Kriszio

# re: PowerShell_ISE Scripting: ConvertTo-Comment

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 :)

Wednesday, December 31, 2008 3:55 PM by Andy Schneider

# re: PowerShell_ISE Scripting: ConvertTo-Comment

Wicked cool, I've been wanting this.

Wednesday, December 31, 2008 5:28 PM by BooRadley

# re: PowerShell_ISE Scripting: ConvertTo-Comment

> 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

Wednesday, December 31, 2008 7:02 PM by PowerShellTeam

# re: PowerShell_ISE Scripting: ConvertTo-Comment

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")

Thursday, January 01, 2009 12:20 PM by David Mohundro

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker