PoshCode Logo PowerShell Code Repository

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

This is a variant of ISE-Snippets which determines the snippet based on selectes text or if nothing is selected bases on the text left to the carret.
Another example of how to extend ISE
http://pauerschell.blogspot.com/2009/01/ise-snippets-variant-2.html

  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.         "dowhile" = @( "do" , "{", "}", "while ( CONDITION )" )
  32.         "dountil" = @( "do" , "{", "}", "until ( CONDITION )" )
  33.         "try" = @( "try", "{", "}" )
  34.         "catch" = @( "catch", "{", "}" )
  35.         "catch [<error type>] " = @( "catch [ERROR_TYPE]", "{", "}" )
  36.         "finaly" = @( "finaly", "{", "}" )
  37.     }
  38.    
  39.  
  40.     $editor = $psISE.CurrentOpenedFile.Editor
  41.     if ( $editor.SelectedText.length -eq 0) {
  42.         $caretLine = $editor.CaretLine
  43.         $caretColumn = $editor.CaretColumn
  44.         $text = $editor.Text.Split("`n")
  45.         $line = $text[$caretLine -1]
  46.         $LeftOfCarret = $line.substring(0, $caretColumn - 1)
  47.         $parts = $LeftOfCarret.split("")
  48.         $len = $parts[-1].length
  49.         $start = $caretColumn - $len
  50.         $editor.select($caretLine, $start, $caretLine, $caretColumn )
  51.     }    
  52.  
  53.       $snippetName = $editor.SelectedText
  54.       $editor.insertText('')
  55.       Add-SnippetToEditor $snippets[$snippetName]
  56. }
  57.  
  58. ## Add-SnippetToEditor
  59. ##############################################################################################################
  60. ## Adds a snippet at caret position
  61. ##############################################################################################################
  62. function Add-SnippetToEditor
  63. {
  64.     param `
  65.     (
  66.         [string[]] $snippet
  67.     )
  68.  
  69.     $editor = $psISE.CurrentOpenedFile.Editor
  70.     $caretLine = $editor.CaretLine
  71.     $caretColumn = $editor.CaretColumn
  72.     $text = $editor.Text.Split("`n")
  73.     $newText = @()
  74.     if ( $caretLine -gt 1 )
  75.     {
  76.         $newText += $text[0..($caretLine -2)]
  77.     }
  78.     $curLine = $text[$caretLine -1]
  79.     $indent = $curline -replace "[^\t ]", ""
  80.     foreach ( $snipLine in $snippet )
  81.     {
  82.         $newText += $indent + $snipLine
  83.     }
  84.     if ( $caretLine -ne $text.Count )
  85.     {
  86.         $newText += $text[$caretLine..($text.Count -1)]
  87.     }
  88.     $editor.Text = [String]::Join("`n", $newText)
  89.     $editor.SetCaretPosition($caretLine, $caretColumn)
  90. }
  91.  
  92. #Export-ModuleMember Add-Snippet
  93.  
  94.  
  95. ##############################################################################################################
  96. ## Inserts command Add Snippet to custom menu
  97. ##############################################################################################################
  98. if (-not( $psISE.CustomMenu.Submenus | where { $_.DisplayName -eq "_Snippet" } ) )
  99. {
  100.     $null = $psISE.CustomMenu.Submenus.Add("_Snippet", {Add-Snippet}, "Ctrl+Alt+S")
  101. }

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