Skip to content

Cache the reflection call done for completions #736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 17, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/PowerShellEditorServices/Language/AstOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace Microsoft.PowerShell.EditorServices
/// </summary>
internal static class AstOperations
{
private static MethodInfo s_extentCloneWithNewOffset;

/// <summary>
/// Gets completions for the symbol found in the Ast at
/// the given file offset.
Expand Down Expand Up @@ -55,22 +57,9 @@ static public async Task<CommandCompletion> 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(
(IScriptPosition)GetExtentCloneMethod(scriptAst).Invoke(
scriptAst.Extent.StartScriptPosition,
new object[] { fileOffset });

Expand Down Expand Up @@ -337,5 +326,28 @@ static public string[] FindDotSourcedIncludes(Ast scriptAst)

return dotSourcedVisitor.DotSourcedFiles.ToArray();
}

private static MethodInfo GetExtentCloneMethod(Ast scriptAst)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be better as just a static field. If your looking for how you could do that without an instance, you could do this:

private static MethodInfo s_extentCloneWithNewOffset =
    typeof(PSObject).Assembly.GetType("System.Management.Automation.InternalScriptExtent")
        .GetMethod(...

Maybe set in a static constructor because of the compiler directives.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was wondering what the best way to do this was. Figured we'd find a better way in the PR. Thanks!

{
if (s_extentCloneWithNewOffset == null)
{
Type type = scriptAst.Extent.StartScriptPosition.GetType();
s_extentCloneWithNewOffset =
#if CoreCLR
type.GetMethod(
"CloneWithNewOffset",
BindingFlags.Instance | BindingFlags.NonPublic);
#else
type.GetMethod(
"CloneWithNewOffset",
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new[] { typeof(int) }, null);
#endif
}

return s_extentCloneWithNewOffset;
}

}
}