@@ -84,7 +84,61 @@ static PowerShellContextService()
84
84
private readonly Stack < RunspaceDetails > runspaceStack = new Stack < RunspaceDetails > ( ) ;
85
85
86
86
private int isCommandLoopRestarterSet ;
87
+ /// <summary>
88
+ /// This is the default function to use for tab expansion.
89
+ /// </summary>
90
+ private static string tabExpansionFunctionText = @"
91
+ <# Options include:
92
+ RelativeFilePaths - [bool]
93
+ Always resolve file paths using Resolve-Path -Relative.
94
+ The default is to use some heuristics to guess if relative or absolute is better.
95
+
96
+ To customize your own custom options, pass a hashtable to CompleteInput, e.g.
97
+ return [System.Management.Automation.CommandCompletion]::CompleteInput($inputScript, $cursorColumn,
98
+ @{ RelativeFilePaths=$false }
99
+ #>
100
+
101
+ [CmdletBinding(DefaultParameterSetName = 'ScriptInputSet')]
102
+ Param(
103
+ [Parameter(ParameterSetName = 'ScriptInputSet', Mandatory = $true, Position = 0)]
104
+ [string] $inputScript,
105
+
106
+ [Parameter(ParameterSetName = 'ScriptInputSet', Position = 1)]
107
+ [int] $cursorColumn = $inputScript.Length,
87
108
109
+ [Parameter(ParameterSetName = 'AstInputSet', Mandatory = $true, Position = 0)]
110
+ [System.Management.Automation.Language.Ast] $ast,
111
+
112
+ [Parameter(ParameterSetName = 'AstInputSet', Mandatory = $true, Position = 1)]
113
+ [System.Management.Automation.Language.Token[]] $tokens,
114
+
115
+ [Parameter(ParameterSetName = 'AstInputSet', Mandatory = $true, Position = 2)]
116
+ [System.Management.Automation.Language.IScriptPosition] $positionOfCursor,
117
+
118
+ [Parameter(ParameterSetName = 'ScriptInputSet', Position = 2)]
119
+ [Parameter(ParameterSetName = 'AstInputSet', Position = 3)]
120
+ [Hashtable] $options = $null
121
+ )
122
+
123
+ End
124
+ {
125
+ if ($psCmdlet.ParameterSetName -eq 'ScriptInputSet')
126
+ {
127
+ return [System.Management.Automation.CommandCompletion]::CompleteInput(
128
+ <#inputScript#> $inputScript,
129
+ <#cursorColumn#> $cursorColumn,
130
+ <#options#> $options)
131
+ }
132
+ else
133
+ {
134
+ return [System.Management.Automation.CommandCompletion]::CompleteInput(
135
+ <#ast#> $ast,
136
+ <#tokens#> $tokens,
137
+ <#positionOfCursor#> $positionOfCursor,
138
+ <#options#> $options)
139
+ }
140
+ }
141
+ " ;
88
142
#endregion
89
143
90
144
#region Properties
@@ -216,8 +270,15 @@ public static PowerShellContextService Create(
216
270
if ( hostStartupInfo . InitialSessionState . LanguageMode != PSLanguageMode . FullLanguage )
217
271
{
218
272
if ( hostStartupInfo . AdditionalModules . Count > 0 )
273
+ {
219
274
hostStartupInfo . InitialSessionState . ImportPSModule ( hostStartupInfo . AdditionalModules as string [ ] ) ;
220
- hostStartupInfo . InitialSessionState . ImportPSModule ( new string [ ] { s_commandsModulePath } ) ;
275
+ }
276
+
277
+ hostStartupInfo . InitialSessionState . ImportPSModule ( new [ ] { s_commandsModulePath } ) ;
278
+ if ( ! hostStartupInfo . InitialSessionState . Commands . Any ( a=> a . Name . ToLower ( ) == "tabexpansion2" ) )
279
+ {
280
+ hostStartupInfo . InitialSessionState . Commands . Add ( new SessionStateFunctionEntry ( "TabExpansion2" , tabExpansionFunctionText ) ) ;
281
+ }
221
282
}
222
283
Runspace runspace = PowerShellContextService . CreateRunspace ( psHost , hostStartupInfo . InitialSessionState ) ;
223
284
powerShellContext . Initialize ( hostStartupInfo . ProfilePaths , runspace , true , hostUserInterface ) ;
0 commit comments