Mittwoch, 7. Januar 2009

ISE - Snippets Variant 2

Hello again

poetter postet some ISE Custom menue extensions at poshcode.

He displays a list and uses a prompt dialog to select the choice.

Well the list might get quite long and I tried a different approach: selecting the choice by either the selected text or by the text left to the cursor. Here the result, which is based on his code.


#requires -version 2.0
#
# ISE-Snippets module v 1.0
#
# DEVELOPED FOR CTP3
#
# See comments for each function for changes ...
#
#############################################################################################################
#
# As a shortcut for every snippet would be to much, I created Add-Snippet which presents a menu.
#
# Feel free to add your own snippets to function Add-Snippet but please contribute your changes here
#
#############################################################################################################
#
# Provides Code Snippets for working with ISE
#
# Add-Snippet - Presents menu for snippet selection
#
# Add-SnippetToEditor - Adds a snippet at caret position
#
#############################################################################################################


## Add-Snippet
#
#############################################################################################################
#
# Presents menu for snippet selection
#
#############################################################################################################
function Add-Snippet
{
$snippets = @{
"region" = @( "#region", "#endregion" )
"function" = @( "function FUNCTION_NAME", "{", "}" )
"param" = @( "param ``", "(", ")" )
"if" = @( "if ( CONDITION )", "{", "}" )
"else" = @( "else", "{", "}" )
"elseif" = @( "elseif ( CONDITION )", "{", "}" )
"foreach" = @( "foreach ( ITEM in COLLECTION )", "{", "}" )
"for" = @( "foreach ( INIT; CONDITION; REPEAT )", "{", "}" )
"while" = @( "while ( CONDITION )", "{", "}" )
"dowhile" = @( "do" , "{", "}", "while ( CONDITION )" )
"dountil" = @( "do" , "{", "}", "until ( CONDITION )" )
"try" = @( "try", "{", "}" )
"catch" = @( "catch", "{", "}" )
"catch [<error type>] " = @( "catch [ERROR_TYPE]", "{", "}" )
"finaly" = @( "finaly", "{", "}" )
}


$editor = $psISE.CurrentOpenedFile.Editor
if ( $editor.SelectedText.length -eq 0) {
$caretLine = $editor.CaretLine
$caretColumn = $editor.CaretColumn
$text = $editor.Text.Split(
"`n")
$line = $text[$caretLine -1]
$LeftOfCarret = $line.substring(0, $caretColumn - 1)
$parts = $LeftOfCarret.split(
"")
$len = $parts[-1].length
$start = $caretColumn - $len
$editor.select($caretLine, $start, $caretLine, $caretColumn )
}

$snippetName = $editor.SelectedText
$editor.insertText('')
Add-SnippetToEditor $snippets[$snippetName]
}

## Add-SnippetToEditor
##############################################################################################################
## Adds a snippet at caret position
##############################################################################################################
function Add-SnippetToEditor
{
param `
(
[string[]] $snippet
)

$editor = $psISE.CurrentOpenedFile.Editor
$caretLine = $editor.CaretLine
$caretColumn = $editor.CaretColumn
$text = $editor.Text.Split(
"`n")
$newText = @()
if ( $caretLine -gt 1 )
{
$newText += $text[0..($caretLine -2)]
}
$curLine = $text[$caretLine -1]
$indent = $curline -replace
"[^\t ]", ""
foreach ( $snipLine in $snippet )
{
$newText += $indent + $snipLine
}
if ( $caretLine -ne $text.Count )
{
$newText += $text[$caretLine..($text.Count -1)]
}
$editor.Text = [String]::Join(
"`n", $newText)
$editor.SetCaretPosition($caretLine, $caretColumn)
}

#Export-ModuleMember Add-Snippet


##############################################################################################################
## Inserts command Add Snippet to custom menu
##############################################################################################################
if (-not( $psISE.CustomMenu.Submenus | where { $_.DisplayName -eq
"_Snippet" } ) )
{
$null = $psISE.CustomMenu.Submenus.Add(
"_Snippet", {Add-Snippet}, "Ctrl+Alt+S")
}


Thanks and congratulation to Dmitry for delivering the new CTP 3 compatible Powergui version and telling in his blog about export html. I just tried this here for the code above.

The above code is ready for every days use. It is just a design study in programming the selection. Personally I don't use snippets in any editor while coding, just a question of personal taste.

In the long term I will use this to interactivly transform T-SQL into PL/SQL. I guess in the end, this will save a lot of my time.

Thanks to the PowerShell Team who did such a good and extendable job. A real christmas present.

Enjoy extending

Bernd

Kommentare:

  1. Is it possible to attach the code as a file?
    I'm having trouble copy-pasting the code.
    Thanks!
    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

    AntwortenLöschen
  2. I don't know yet. Just postet it on http://poshcode.org/789. There you can download it.

    Bernd

    AntwortenLöschen