4
4
using System ;
5
5
using System . Collections . Generic ;
6
6
using System . Diagnostics ;
7
+ using System . IO ;
7
8
using System . Linq ;
9
+ using System . Linq . Expressions ;
8
10
using System . Management . Automation ;
9
11
using System . Management . Automation . Language ;
10
12
using System . Reflection ;
@@ -21,13 +23,26 @@ namespace Microsoft.PowerShell.EditorServices.Services.Symbols
21
23
/// </summary>
22
24
internal static class AstOperations
23
25
{
24
- // TODO: When netstandard is upgraded to 2.0, see if
25
- // Delegate.CreateDelegate can be used here instead
26
- private static readonly MethodInfo s_extentCloneWithNewOffset = typeof ( PSObject ) . Assembly
27
- . GetType ( "System.Management.Automation.Language.InternalScriptPosition" )
28
- . GetMethod ( "CloneWithNewOffset" , BindingFlags . Instance | BindingFlags . NonPublic ) ;
26
+ private static readonly Func < IScriptPosition , int , IScriptPosition > s_clonePositionWithNewOffset ;
27
+ static AstOperations ( )
28
+ {
29
+ Type internalScriptPositionType = typeof ( PSObject ) . GetTypeInfo ( ) . Assembly
30
+ . GetType ( "System.Management.Automation.Language.InternalScriptPosition" ) ;
31
+
32
+ MethodInfo cloneWithNewOffsetMethod = internalScriptPositionType . GetMethod ( "CloneWithNewOffset" , BindingFlags . Instance | BindingFlags . NonPublic ) ;
33
+
34
+ ParameterExpression originalPosition = Expression . Parameter ( typeof ( IScriptPosition ) ) ;
35
+ ParameterExpression newOffset = Expression . Parameter ( typeof ( int ) ) ;
36
+
37
+ var parameters = new ParameterExpression [ ] { originalPosition , newOffset } ;
38
+ s_clonePositionWithNewOffset = Expression . Lambda < Func < IScriptPosition , int , IScriptPosition > > (
39
+ Expression . Call (
40
+ Expression . Convert ( originalPosition , internalScriptPositionType ) ,
41
+ cloneWithNewOffsetMethod ,
42
+ newOffset ) ,
43
+ parameters ) . Compile ( ) ;
44
+ }
29
45
30
- // TODO: BRING THIS BACK
31
46
/// <summary>
32
47
/// Gets completions for the symbol found in the Ast at
33
48
/// the given file offset.
@@ -60,9 +75,7 @@ public static async Task<CommandCompletion> GetCompletionsAsync(
60
75
ILogger logger ,
61
76
CancellationToken cancellationToken )
62
77
{
63
- IScriptPosition cursorPosition = ( IScriptPosition ) s_extentCloneWithNewOffset . Invoke (
64
- scriptAst . Extent . StartScriptPosition ,
65
- new object [ ] { fileOffset } ) ;
78
+ IScriptPosition cursorPosition = s_clonePositionWithNewOffset ( scriptAst . Extent . StartScriptPosition , fileOffset ) ;
66
79
67
80
logger . LogTrace (
68
81
string . Format (
0 commit comments