|
| 1 | +--- |
| 2 | +external help file: EditorServicesCommandSuite-help.xml |
| 3 | +online version: https://github.com/SeeminglyScience/EditorServicesCommandSuite/blob/master/docs/en-US/ConvertTo-FunctionDefinition.md |
| 4 | +schema: 2.0.0 |
| 5 | +--- |
| 6 | + |
| 7 | +# ConvertTo-FunctionDefinition |
| 8 | + |
| 9 | +## SYNOPSIS |
| 10 | + |
| 11 | +Create a new function from a selection or specified script extent object. |
| 12 | + |
| 13 | +## SYNTAX |
| 14 | + |
| 15 | +### __AllParameterSets (Default) |
| 16 | + |
| 17 | +```powershell |
| 18 | +ConvertTo-FunctionDefinition [-Extent <IScriptExtent>] [-FunctionName <String>] |
| 19 | +``` |
| 20 | + |
| 21 | +### ExternalFile |
| 22 | + |
| 23 | +```powershell |
| 24 | +ConvertTo-FunctionDefinition [-Extent <IScriptExtent>] [-FunctionName <String>] [-DestinationPath <String>] |
| 25 | +``` |
| 26 | + |
| 27 | +### BeginBlock |
| 28 | + |
| 29 | +```powershell |
| 30 | +ConvertTo-FunctionDefinition [-Extent <IScriptExtent>] [-FunctionName <String>] [-BeginBlock] |
| 31 | +``` |
| 32 | + |
| 33 | +### Inline |
| 34 | + |
| 35 | +```powershell |
| 36 | +ConvertTo-FunctionDefinition [-Extent <IScriptExtent>] [-FunctionName <String>] [-Inline] |
| 37 | +``` |
| 38 | + |
| 39 | +## DESCRIPTION |
| 40 | + |
| 41 | +The ConvertTo-FunctionDefintion function takes a section of the current file and creates a function |
| 42 | +definition from it. The generated function includes a parameter block with parameters for variables |
| 43 | +that are not defined in the selection. In the place of the selected text will be the invocation of |
| 44 | +the generated command including parameters. |
| 45 | + |
| 46 | +## EXAMPLES |
| 47 | + |
| 48 | +### -------------------------- EXAMPLE 1 -------------------------- |
| 49 | + |
| 50 | +```powershell |
| 51 | +# Open a new untitled file |
| 52 | +$psEditor.Workspace.NewFile() |
| 53 | +
|
| 54 | +# Insert some text into the file |
| 55 | +$psEditor.GetEditorContext().CurrentFile.InsertText(' |
| 56 | +$myVar = "testing" |
| 57 | +Get-ChildItem $myVar |
| 58 | +') |
| 59 | +
|
| 60 | +# Select the Get-ChildItem line |
| 61 | +$psEditor.GetEditorContext().SetSelection(3, 1, 4, 1) |
| 62 | +
|
| 63 | +# Convert it to a function |
| 64 | +ConvertTo-FunctionDefinition -FunctionName GetMyDirectory -Inline |
| 65 | +
|
| 66 | +# Show the new contents of the file |
| 67 | +$psEditor.GetEditorContext.CurrentFile.GetText() |
| 68 | +
|
| 69 | +# $myVar = "testing" |
| 70 | +# function GetMyDirectory { |
| 71 | +# param([string] $MyVar) |
| 72 | +# end { |
| 73 | +# Get-ChildItem $MyVar |
| 74 | +# } |
| 75 | +# } |
| 76 | +# |
| 77 | +# GetMyDirectory -MyVar $myVar |
| 78 | +
|
| 79 | +``` |
| 80 | + |
| 81 | +Creates a new untitled file in the editor, inserts demo text, and then converts a line to a inline function. |
| 82 | + |
| 83 | +## PARAMETERS |
| 84 | + |
| 85 | +### -Extent |
| 86 | + |
| 87 | +The ScriptExtent to convert to a function. If not specified, the currently selected text in the editor will be used. |
| 88 | + |
| 89 | +```yaml |
| 90 | +Type: IScriptExtent |
| 91 | +Parameter Sets: (All) |
| 92 | +Aliases: |
| 93 | + |
| 94 | +Required: False |
| 95 | +Position: Named |
| 96 | +Default value: None |
| 97 | +Accept pipeline input: False |
| 98 | +Accept wildcard characters: False |
| 99 | +``` |
| 100 | +
|
| 101 | +### -FunctionName |
| 102 | +
|
| 103 | +Specifies the name to give the generated function. If not specified, a input prompt will be displayed |
| 104 | +in the editor. |
| 105 | +
|
| 106 | +```yaml |
| 107 | +Type: String |
| 108 | +Parameter Sets: (All) |
| 109 | +Aliases: |
| 110 | + |
| 111 | +Required: False |
| 112 | +Position: Named |
| 113 | +Default value: None |
| 114 | +Accept pipeline input: False |
| 115 | +Accept wildcard characters: False |
| 116 | +``` |
| 117 | +
|
| 118 | +### -DestinationPath |
| 119 | +
|
| 120 | +Specifies a path relative to the file open in the editor to save the function to. You can specify an |
| 121 | +existing or new file. If the file extension is omitted, the path is assumed to be a directory and a |
| 122 | +file name is assumed to be the function name. |
| 123 | +
|
| 124 | +```yaml |
| 125 | +Type: String |
| 126 | +Parameter Sets: ExternalFile |
| 127 | +Aliases: |
| 128 | + |
| 129 | +Required: False |
| 130 | +Position: Named |
| 131 | +Default value: None |
| 132 | +Accept pipeline input: False |
| 133 | +Accept wildcard characters: False |
| 134 | +``` |
| 135 | +
|
| 136 | +### -BeginBlock |
| 137 | +
|
| 138 | +If specified, the function will be saved to the Begin block of either the closest parent function |
| 139 | +definition, or of the root script block if no function definitions exist. |
| 140 | +
|
| 141 | +If there is no Begin block available, one will be created. If a begin block must be created and no |
| 142 | +named blocks exist yet, a separate End block will be created from the existing unnamed block. |
| 143 | +
|
| 144 | +```yaml |
| 145 | +Type: SwitchParameter |
| 146 | +Parameter Sets: BeginBlock |
| 147 | +Aliases: |
| 148 | + |
| 149 | +Required: False |
| 150 | +Position: Named |
| 151 | +Default value: False |
| 152 | +Accept pipeline input: False |
| 153 | +Accept wildcard characters: False |
| 154 | +``` |
| 155 | +
|
| 156 | +### -Inline |
| 157 | +
|
| 158 | +If specified, the function will be saved directly above the selection. |
| 159 | +
|
| 160 | +```yaml |
| 161 | +Type: SwitchParameter |
| 162 | +Parameter Sets: Inline |
| 163 | +Aliases: |
| 164 | + |
| 165 | +Required: False |
| 166 | +Position: Named |
| 167 | +Default value: False |
| 168 | +Accept pipeline input: False |
| 169 | +Accept wildcard characters: False |
| 170 | +``` |
| 171 | +
|
| 172 | +## INPUTS |
| 173 | +
|
| 174 | +### None |
| 175 | +
|
| 176 | +This function does not accept input from the pipeline. |
| 177 | +
|
| 178 | +## OUTPUTS |
| 179 | +
|
| 180 | +### None |
| 181 | +
|
| 182 | +This function does not return output to the pipeline. |
| 183 | +
|
| 184 | +## NOTES |
| 185 | +
|
| 186 | +## RELATED LINKS |
0 commit comments