Skip to content

Commit 4b02b1c

Browse files
initial runspacesynchronizer
1 parent 84a3d3f commit 4b02b1c

File tree

2 files changed

+344
-40
lines changed

2 files changed

+344
-40
lines changed

src/PowerShellEditorServices/Language/AstOperations.cs

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ internal static class AstOperations
3232

3333
private static readonly SemaphoreSlim s_completionHandle = AsyncUtils.CreateSimpleLockingSemaphore();
3434

35+
private static PowerShell pwsh = PowerShell.Create();
36+
3537
/// <summary>
3638
/// Gets completions for the symbol found in the Ast at
3739
/// the given file offset.
@@ -69,6 +71,11 @@ static public async Task<CommandCompletion> GetCompletionsAsync(
6971
return null;
7072
}
7173

74+
if (!RunspaceSynchronizer.IsReadyForEvents)
75+
{
76+
RunspaceSynchronizer.InitializeRunspaces(powerShellContext.CurrentRunspace.Runspace, pwsh.Runspace);
77+
}
78+
7279
try
7380
{
7481
IScriptPosition cursorPosition = (IScriptPosition)s_extentCloneWithNewOffset.Invoke(
@@ -90,49 +97,65 @@ static public async Task<CommandCompletion> GetCompletionsAsync(
9097

9198
var stopwatch = new Stopwatch();
9299

100+
stopwatch.Start();
101+
102+
try
103+
{
104+
return CommandCompletion.CompleteInput(
105+
scriptAst,
106+
currentTokens,
107+
cursorPosition,
108+
options: null,
109+
powershell: pwsh);
110+
}
111+
finally
112+
{
113+
stopwatch.Stop();
114+
logger.Write(LogLevel.Verbose, $"IntelliSense completed in {stopwatch.ElapsedMilliseconds}ms.");
115+
}
93116
// If the current runspace is out of process we can use
94117
// CommandCompletion.CompleteInput because PSReadLine won't be taking up the
95118
// main runspace.
96-
if (powerShellContext.IsCurrentRunspaceOutOfProcess())
97-
{
98-
using (RunspaceHandle runspaceHandle = await powerShellContext.GetRunspaceHandleAsync(cancellationToken))
99-
using (PowerShell powerShell = PowerShell.Create())
100-
{
101-
powerShell.Runspace = runspaceHandle.Runspace;
102-
stopwatch.Start();
103-
try
104-
{
105-
return CommandCompletion.CompleteInput(
106-
scriptAst,
107-
currentTokens,
108-
cursorPosition,
109-
options: null,
110-
powershell: powerShell);
111-
}
112-
finally
113-
{
114-
stopwatch.Stop();
115-
logger.Write(LogLevel.Verbose, $"IntelliSense completed in {stopwatch.ElapsedMilliseconds}ms.");
116-
}
117-
}
118-
}
119-
120-
CommandCompletion commandCompletion = null;
121-
await powerShellContext.InvokeOnPipelineThreadAsync(
122-
pwsh =>
123-
{
124-
stopwatch.Start();
125-
commandCompletion = CommandCompletion.CompleteInput(
126-
scriptAst,
127-
currentTokens,
128-
cursorPosition,
129-
options: null,
130-
powershell: pwsh);
131-
});
132-
stopwatch.Stop();
133-
logger.Write(LogLevel.Verbose, $"IntelliSense completed in {stopwatch.ElapsedMilliseconds}ms.");
134-
135-
return commandCompletion;
119+
// if (powerShellContext.IsCurrentRunspaceOutOfProcess())
120+
// {
121+
// using (RunspaceHandle runspaceHandle = await powerShellContext.GetRunspaceHandleAsync(cancellationToken))
122+
// using (PowerShell powerShell = PowerShell.Create())
123+
// {
124+
// powerShell.Runspace = runspaceHandle.Runspace;
125+
// stopwatch.Start();
126+
// try
127+
// {
128+
// return CommandCompletion.CompleteInput(
129+
// scriptAst,
130+
// currentTokens,
131+
// cursorPosition,
132+
// options: null,
133+
// powershell: powerShell);
134+
// }
135+
// finally
136+
// {
137+
// stopwatch.Stop();
138+
// logger.Write(LogLevel.Verbose, $"IntelliSense completed in {stopwatch.ElapsedMilliseconds}ms.");
139+
// }
140+
// }
141+
// }
142+
143+
// CommandCompletion commandCompletion = null;
144+
// await powerShellContext.InvokeOnPipelineThreadAsync(
145+
// pwsh =>
146+
// {
147+
// stopwatch.Start();
148+
// commandCompletion = CommandCompletion.CompleteInput(
149+
// scriptAst,
150+
// currentTokens,
151+
// cursorPosition,
152+
// options: null,
153+
// powershell: pwsh);
154+
// });
155+
// stopwatch.Stop();
156+
// logger.Write(LogLevel.Verbose, $"IntelliSense completed in {stopwatch.ElapsedMilliseconds}ms.");
157+
158+
// return commandCompletion;
136159
}
137160
finally
138161
{

0 commit comments

Comments
 (0)