PoshCode Logo PowerShell Code Repository

ISE-Snippets (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/786"></script>download | new post

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

  1. #requires -version 2.0
  2. ## ISE-Snippets module v 1.0
  3. ## DEVELOPED FOR CTP3
  4. ## See comments for each function for changes ...
  5. ##############################################################################################################
  6. ## As a shortcut for every snippet would be to much, I created Add-Snippet which presents a menu.
  7. ## Feel free to add your own snippets to function Add-Snippet but please contribute your changes here
  8. ##############################################################################################################
  9. ## Provides Code Snippets for working with ISE
  10. ## Add-Snippet - Presents menu for snippet selection
  11. ## Add-SnippetToEditor - Adds a snippet at caret position
  12. ##############################################################################################################
  13.  
  14.  
  15. ## Add-Snippet
  16. ##############################################################################################################
  17. ## Presents menu for snippet selection
  18. ##############################################################################################################
  19. function Add-Snippet
  20. {
  21.     $snippets = @{
  22.         "region" = @( "#region", "#endregion" )
  23.         "function" = @( "function FUNCTION_NAME", "{", "}" )
  24.         "param" = @( "param ``", "(", ")" )
  25.         "if" = @( "if ( CONDITION )", "{", "}" )
  26.         "else" = @( "else", "{", "}" )
  27.         "elseif" = @( "elseif ( CONDITION )", "{", "}" )
  28.         "foreach" = @( "foreach ( ITEM in COLLECTION )", "{", "}" )
  29.         "for" = @( "foreach ( INIT; CONDITION; REPEAT )", "{", "}" )
  30.         "while" = @( "while ( CONDITION )", "{", "}" )
  31.         "do .. while" = @( "do" , "{", "}", "while ( CONDITION )" )
  32.         "do .. until" = @( "do" , "{", "}", "until ( CONDITION )" )
  33.         "try" = @( "try", "{", "}" )
  34.         "catch" = @( "catch", "{", "}" )
  35.         "catch [<error type>] " = @( "catch [ERROR_TYPE]", "{", "}" )
  36.         "finaly" = @( "finaly", "{", "}" )
  37.     }
  38.    
  39.     Write-Host "Select snippet:"
  40.     Write-Host
  41.     $i = 1
  42.     $snippetIndex = @()
  43.     foreach ( $snippetName in $snippets.Keys | Sort )
  44.     {
  45.         Write-Host ( "{0} - {1}" -f $i++, $snippetName)
  46.         $snippetIndex += $snippetName
  47.     }
  48.     try
  49.     {
  50.         [int]$choice = Read-Host ("Select 1-{0}" -f $i)
  51.         if ( ( $choice -gt 0 ) -and ( $choice -lt $i ) )
  52.         {
  53.             $snippetName = $snippetIndex[$choice -1]
  54.            
  55.             Add-SnippetToEditor $snippets[$snippetName]
  56.         }
  57.         else
  58.         {
  59.             Throw "Choice not in range"
  60.         }
  61.     }
  62.     catch
  63.     {
  64.         Write-Error "Choice was not in range or not even and integer"
  65.     }
  66. }
  67.  
  68. ## Add-SnippetToEditor
  69. ##############################################################################################################
  70. ## Adds a snippet at caret position
  71. ##############################################################################################################
  72. function Add-SnippetToEditor
  73. {
  74.     param `
  75.     (
  76.         [string[]] $snippet
  77.     )
  78.  
  79.     $editor = $psISE.CurrentOpenedFile.Editor
  80.     $caretLine = $editor.CaretLine
  81.     $caretColumn = $editor.CaretColumn
  82.     $text = $editor.Text.Split("`n")
  83.     $newText = @()
  84.     if ( $caretLine -gt 1 )
  85.     {
  86.         $newText += $text[0..($caretLine -2)]
  87.     }
  88.     $curLine = $text[$caretLine -1]
  89.     $indent = $curline -replace "[^\t ]", ""
  90.     foreach ( $snipLine in $snippet )
  91.     {
  92.         $newText += $indent + $snipLine
  93.     }
  94.     if ( $caretLine -ne $text.Count )
  95.     {
  96.         $newText += $text[$caretLine..($text.Count -1)]
  97.     }
  98.     $editor.Text = [String]::Join("`n", $newText)
  99.     $editor.SetCaretPosition($caretLine, $caretColumn)
  100. }
  101.  
  102. Export-ModuleMember Add-Snippet
  103.  
  104.  
  105. ##############################################################################################################
  106. ## Inserts command Add Snippet to custom menu
  107. ##############################################################################################################
  108. if (-not( $psISE.CustomMenu.Submenus | where { $_.DisplayName -eq "Snippet" } ) )
  109. {
  110.     $null = $psISE.CustomMenu.Submenus.Add("_Snippet", {Add-Snippet}, "Ctrl+Alt+S")
  111. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me