4
4
5
5
using System ;
6
6
using System . Collections . Generic ;
7
+ using System . Reflection ;
7
8
using System . Threading . Tasks ;
8
9
using System . Management . Automation ;
10
+ using System . Management . Automation . Runspaces ;
9
11
using System . Management . Automation . Language ;
10
12
using System . Management . Automation . Subsystem . Prediction ;
11
13
using System . Diagnostics . CodeAnalysis ;
@@ -16,8 +18,42 @@ namespace Microsoft.PowerShell
16
18
{
17
19
public partial class PSConsoleReadLine
18
20
{
19
- private const string PSReadLine = "PSReadLine" ;
20
- private static PredictionClient s_predictionClient = new ( PSReadLine , PredictionClientKind . Terminal ) ;
21
+ private const string DefaultName = "PSReadLine" ;
22
+ private static readonly PredictionClient s_predictionClient = new ( DefaultName , PredictionClientKind . Terminal ) ;
23
+ private static PropertyInfo s_pCurrentLocation = null ;
24
+
25
+ /// <summary>
26
+ /// Initialize the <see cref="PropertyInfo"/> objects for those public settable properties newly added to
27
+ /// <see cref="PredictionClient"/>.
28
+ /// </summary>
29
+ private static void InitializePropertyInfo ( )
30
+ {
31
+ Version ver = typeof ( PSObject ) . Assembly . GetName ( ) . Version ;
32
+ if ( ver . Major < 7 || ver . Minor < 4 )
33
+ {
34
+ return ;
35
+ }
36
+
37
+ Type pcType = typeof ( PredictionClient ) ;
38
+ // Property added in 7.4
39
+ s_pCurrentLocation = pcType . GetProperty ( "CurrentLocation" ) ;
40
+ }
41
+
42
+ /// <summary>
43
+ /// New public settable properties may be added to the <see cref="PredictionClient"/> type as it evolves to
44
+ /// offer more helpful context information. We dynamically set those properties here to avoid any backward
45
+ /// compatibility issues.
46
+ /// </summary>
47
+ private static void UpdatePredictionClient ( Runspace runspace , EngineIntrinsics engineIntrinsics )
48
+ {
49
+ // Set the current location if the 'CurrentLocation' property exists.
50
+ if ( s_pCurrentLocation is not null )
51
+ {
52
+ // Set the current location if it's a local Runspace. Otherwise, set it to null.
53
+ object path = runspace . RunspaceIsRemote ? null : engineIntrinsics . SessionState . Path . CurrentLocation ;
54
+ s_pCurrentLocation . SetValue ( s_predictionClient , path ) ;
55
+ }
56
+ }
21
57
22
58
// Stub helper methods so prediction can be mocked
23
59
[ ExcludeFromCodeCoverage ]
0 commit comments