Skip to content
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

Cache the reflection call done for completions #736

Merged
merged 4 commits into from
Sep 17, 2018
Merged
Changes from 3 commits
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
49 changes: 29 additions & 20 deletions src/PowerShellEditorServices/Language/AstOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,47 @@

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;

/// <summary>
/// Provides common operations for the syntax tree of a parsed script.
/// </summary>
internal static class AstOperations
{
private static readonly MethodInfo s_extentCloneWithNewOffset;

static AstOperations()
{
// TODO: When netstandard is upgraded to 2.0, see if
// Delegate.CreateDelegate can be used here instead
s_extentCloneWithNewOffset =
#if CoreCLR
typeof(PSObject).GetTypeInfo().Assembly
.GetType("System.Management.Automation.Language.InternalScriptPosition")
.GetMethod("CloneWithNewOffset", BindingFlags.Instance | BindingFlags.NonPublic);
#else
typeof(PSObject).GetType().Assembly
.GetType("System.Management.Automation.Language.InternalScriptPosition")
.GetMethod(
"CloneWithNewOffset",
BindingFlags.Instance | BindingFlags.NonPublic,
binder: null,
types: new [] { typeof(int) },
modifiers: null);
#endif
}

/// <summary>
/// Gets completions for the symbol found in the Ast at
/// the given file offset.
Expand Down Expand Up @@ -55,24 +78,10 @@ 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(
scriptAst.Extent.StartScriptPosition,
new object[] { fileOffset });
IScriptPosition cursorPosition = (IScriptPosition)s_extentCloneWithNewOffset.Invoke(
scriptAst.Extent.StartScriptPosition,
new object[] { fileOffset });

logger.Write(
LogLevel.Verbose,
Expand Down