Skip to content

Commit 9650998

Browse files
authored
Cache the reflection call done for completions (#736)
1 parent 564df21 commit 9650998

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

src/PowerShellEditorServices/Language/AstOperations.cs

+13-21
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,30 @@
55

66
using Microsoft.PowerShell.EditorServices.Utility;
77
using System;
8+
using System.Diagnostics;
89
using System.Collections.Generic;
910
using System.Linq;
1011
using System.Reflection;
1112
using System.Threading;
1213
using System.Threading.Tasks;
14+
using System.Management.Automation.Language;
15+
using System.Management.Automation.Runspaces;
1316

1417
namespace Microsoft.PowerShell.EditorServices
1518
{
16-
using System.Diagnostics;
1719
using System.Management.Automation;
18-
using System.Management.Automation.Language;
19-
using System.Management.Automation.Runspaces;
2020

2121
/// <summary>
2222
/// Provides common operations for the syntax tree of a parsed script.
2323
/// </summary>
2424
internal static class AstOperations
2525
{
26+
// TODO: When netstandard is upgraded to 2.0, see if
27+
// Delegate.CreateDelegate can be used here instead
28+
private static readonly MethodInfo s_extentCloneWithNewOffset = typeof(PSObject).GetTypeInfo().Assembly
29+
.GetType("System.Management.Automation.Language.InternalScriptPosition")
30+
.GetMethod("CloneWithNewOffset", BindingFlags.Instance | BindingFlags.NonPublic);
31+
2632
/// <summary>
2733
/// Gets completions for the symbol found in the Ast at
2834
/// the given file offset.
@@ -55,24 +61,10 @@ static public async Task<CommandCompletion> GetCompletions(
5561
ILogger logger,
5662
CancellationToken cancellationToken)
5763
{
58-
var type = scriptAst.Extent.StartScriptPosition.GetType();
59-
var method =
60-
#if CoreCLR
61-
type.GetMethod(
62-
"CloneWithNewOffset",
63-
BindingFlags.Instance | BindingFlags.NonPublic);
64-
#else
65-
type.GetMethod(
66-
"CloneWithNewOffset",
67-
BindingFlags.Instance | BindingFlags.NonPublic,
68-
null,
69-
new[] { typeof(int) }, null);
70-
#endif
71-
72-
IScriptPosition cursorPosition =
73-
(IScriptPosition)method.Invoke(
74-
scriptAst.Extent.StartScriptPosition,
75-
new object[] { fileOffset });
64+
65+
IScriptPosition cursorPosition = (IScriptPosition)s_extentCloneWithNewOffset.Invoke(
66+
scriptAst.Extent.StartScriptPosition,
67+
new object[] { fileOffset });
7668

7769
logger.Write(
7870
LogLevel.Verbose,

0 commit comments

Comments
 (0)