From 96509981209ce6e264d5b5caeec817e0514b5e0e Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Mon, 17 Sep 2018 14:36:50 -0700 Subject: [PATCH] Cache the reflection call done for completions (#736) --- .../Language/AstOperations.cs | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/PowerShellEditorServices/Language/AstOperations.cs b/src/PowerShellEditorServices/Language/AstOperations.cs index c28416280..38a8956a3 100644 --- a/src/PowerShellEditorServices/Language/AstOperations.cs +++ b/src/PowerShellEditorServices/Language/AstOperations.cs @@ -5,24 +5,30 @@ using Microsoft.PowerShell.EditorServices.Utility; using System; +using System.Diagnostics; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading; using System.Threading.Tasks; +using System.Management.Automation.Language; +using System.Management.Automation.Runspaces; namespace Microsoft.PowerShell.EditorServices { - using System.Diagnostics; using System.Management.Automation; - using System.Management.Automation.Language; - using System.Management.Automation.Runspaces; /// /// Provides common operations for the syntax tree of a parsed script. /// internal static class AstOperations { + // TODO: When netstandard is upgraded to 2.0, see if + // Delegate.CreateDelegate can be used here instead + private static readonly MethodInfo s_extentCloneWithNewOffset = typeof(PSObject).GetTypeInfo().Assembly + .GetType("System.Management.Automation.Language.InternalScriptPosition") + .GetMethod("CloneWithNewOffset", BindingFlags.Instance | BindingFlags.NonPublic); + /// /// Gets completions for the symbol found in the Ast at /// the given file offset. @@ -55,24 +61,10 @@ static public async Task GetCompletions( ILogger logger, CancellationToken cancellationToken) { - var type = scriptAst.Extent.StartScriptPosition.GetType(); - var method = -#if CoreCLR - type.GetMethod( - "CloneWithNewOffset", - BindingFlags.Instance | BindingFlags.NonPublic); -#else - type.GetMethod( - "CloneWithNewOffset", - BindingFlags.Instance | BindingFlags.NonPublic, - null, - new[] { typeof(int) }, null); -#endif - - IScriptPosition cursorPosition = - (IScriptPosition)method.Invoke( - scriptAst.Extent.StartScriptPosition, - new object[] { fileOffset }); + + IScriptPosition cursorPosition = (IScriptPosition)s_extentCloneWithNewOffset.Invoke( + scriptAst.Extent.StartScriptPosition, + new object[] { fileOffset }); logger.Write( LogLevel.Verbose,