Sunday 20 January 2013

Open-FunctionInISE

  1. Function Open-FunctioninISE {
  2. <#
  3. .SYNOPSIS
  4. Opens a function in ISE
  5. .DESCRIPTION
  6.     This enables you to specify the function to open. The definition
  7.     comes from (Get-Command <command>).definition.
  8.     You specify the name of the function to open, or select it
  9.     in the ISE
  10. .NOTES
  11.     File Name : Open-FunctionInIse.ps1
  12.     Author : Thomas Lee - tfl@psp.co.uk
  13.     Requires : PowerShell Version 3.0
  14.     Based on a technet script published at http://gallery.technet.microsoft.com/scriptcenter/Open-defined-functions-in-22788d0f 
  15. .LINK
  16.     This script posted to:
  17.         http://www.pshscripts.blogspot.com
  18. .PARAMETER Function
  19.     The name of a defined function to open in new PowerShell ISE tab.
  20. .EXAMPLE
  21.     C:\Psh> Open-FunctionInIse Open-FunctioninISE
  22.     Opens Open-FunctionInIse in a new PowerShell ISE tab
  23. .EXAMPLE
  24.     Select a function name in an open edit window, then hit
  25.     Ctrl+Shift+E (or use the Add-ons menu)
  26. #>
  27. # Define parameters
  28. [Cmdletbinding()]
  29. Param(
  30. [Parameter(Position=0)]
  31. [ValidateScript({ Get-Command -commandtype function -name $_ })]
  32. [String] $function
  33. )
  34. # Start of the function.
  35. Process{
  36. # Get the function name (i.e. the selected text) and ensure it's not empty
  37. $fn = $psise.currentfile.Editor.selectedText
  38. # if nothing selected, see if we got called with it
  39. If (!$fn -or ($fn.length -LE 0)) {
  40. if ($function -and ($function.length -GT 0)){
  41. $fn = $function}
  42. Else {
  43. $fn = Read-Host -Prompt "Enter function name to view"
  44. }
  45. }
  46. If (!$Fn) {'No function to edit';return}
  47. # Get the definition, if there is one
  48. $definition = (Get-Command -commandtype function -name $fn).definition
  49. If (!$definition -or ($Definition.Length -le 0)) {return "Function [$fn] not found"}
  50. # Create What to see
  51. $FunctionHeader = "Function $fn {`n"
  52. $comments = "`# Description : $($definition.description)`n"
  53. $comments += "`# Module : $($definition.module)`n`n"
  54. $FunctionTrailer = "`n}"
  55. # Wrap it all up
  56. $definition = $functionHeader + $Comments + $Definition + $FunctionTrailer
  57. "function $fn {" + $definition + "}"
  58. # Add a tab, add text to the tab, set caret position to first character
  59. $tab = $Psise.CurrentPowerShellTab.Files.Add()
  60. $tab.Editor.text = $definition
  61. $tab.Editor.SetCaretPosition(1,1)
  62. # Sleep for a moment. Ran into issues without this.
  63. Start-Sleep -Milliseconds 200
  64. } # End process block
  65. } # End Function
  66. Set-ALias OFISE .\Open-FunctionInISE
  67. # Here we could test the function from the command line.
  68. # Function test123 {'testing 1-2-3'}
  69. # Open-FunctioninISE test123
  70. # I assume you'll just use this function so I've left the following
  71. # Code in place to add this as a menu item
  72. # Here add to the ISE as long as it's not there already!
  73. $x = ($psise.CurrentPowerShellTab.AddOnsMenu.Submenus).displayname
  74. if (! ($x -contains "_Edit Function in ISE")) {
  75. $Psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("_Edit Function in ISE", {Open-FunctioninISE},
  76. "Ctrl+Shift+E") | Out-Null
  77. }
  78. Else {
  79. 'Function already added in ISE'
  80. }

No comments: