Skip to content

Commit 06111c4

Browse files
committed
Finally get the REPL and completions back
1 parent e63e709 commit 06111c4

File tree

6 files changed

+64
-59
lines changed

6 files changed

+64
-59
lines changed

src/PowerShellEditorServices/Server/PsesLanguageServer.cs

+2-12
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public async Task StartAsync()
100100
.WithHandler<ExpandAliasHandler>()
101101
.WithHandler<PsesSemanticTokensHandler>()
102102
.OnInitialize(
103-
async (languageServer, request, cancellationToken) =>
103+
(languageServer, request, cancellationToken) =>
104104
{
105105
IServiceProvider serviceProvider = languageServer.Services;
106106

@@ -122,17 +122,7 @@ public async Task StartAsync()
122122
}
123123
}
124124

125-
// Set the working directory of the PowerShell session to the workspace path
126-
if (workspaceService.WorkspacePath != null
127-
&& Directory.Exists(workspaceService.WorkspacePath))
128-
{
129-
await serviceProvider.GetService<PowerShellExecutionService>()
130-
.ExecutePSCommandAsync(
131-
new PSCommand().AddCommand("Set-Location").AddParameter("-LiteralPath", workspaceService.WorkspacePath),
132-
new PowerShellExecutionOptions(),
133-
cancellationToken)
134-
.ConfigureAwait(false);
135-
}
125+
return Task.CompletedTask;
136126
});
137127
}).ConfigureAwait(false);
138128

src/PowerShellEditorServices/Server/PsesServiceCollectionExtensions.cs

+6-12
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,12 @@ public static IServiceCollection AddPsesLanguageServices(
4141
.AddSingleton<TemplateService>()
4242
.AddSingleton<EditorOperationsService>()
4343
.AddSingleton<RemoteFileManagerService>()
44-
.AddSingleton(async (provider) =>
45-
{
46-
var extensionService = new ExtensionService(
47-
provider.GetService<PowerShellExecutionService>(),
48-
provider.GetService<ILanguageServer>());
49-
50-
await extensionService.InitializeAsync(
51-
serviceProvider: provider,
52-
editorOperations: provider.GetService<EditorOperationsService>()).ConfigureAwait(false);
53-
54-
return extensionService;
55-
})
44+
.AddSingleton<ExtensionService>((provider) =>
45+
new ExtensionService(
46+
provider.GetService<ILanguageServer>(),
47+
provider,
48+
provider.GetService<EditorOperationsService>(),
49+
provider.GetService<PowerShellExecutionService>()))
5650
.AddSingleton<AnalysisService>();
5751
}
5852

src/PowerShellEditorServices/Services/Extension/ExtensionService.cs

+18-16
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,25 @@ internal sealed class ExtensionService
5959
/// PowerShellContext for loading and executing extension code.
6060
/// </summary>
6161
/// <param name="powerShellContext">A PowerShellContext used to execute extension code.</param>
62-
internal ExtensionService(PowerShellExecutionService executionService, ILanguageServer languageServer)
62+
internal ExtensionService(
63+
ILanguageServer languageServer,
64+
IServiceProvider serviceProvider,
65+
IEditorOperations editorOperations,
66+
PowerShellExecutionService executionService)
6367
{
6468
ExecutionService = executionService;
6569
_languageServer = languageServer;
70+
71+
EditorObject =
72+
new EditorObject(
73+
serviceProvider,
74+
this,
75+
editorOperations);
76+
77+
// Attach to ExtensionService events
78+
CommandAdded += ExtensionService_ExtensionAddedAsync;
79+
CommandUpdated += ExtensionService_ExtensionUpdatedAsync;
80+
CommandRemoved += ExtensionService_ExtensionRemovedAsync;
6681
}
6782

6883
#endregion
@@ -75,23 +90,10 @@ internal ExtensionService(PowerShellExecutionService executionService, ILanguage
7590
/// </summary>
7691
/// <param name="editorOperations">An IEditorOperations implementation.</param>
7792
/// <returns>A Task that can be awaited for completion.</returns>
78-
internal async Task InitializeAsync(
79-
IServiceProvider serviceProvider,
80-
IEditorOperations editorOperations)
93+
internal async Task InitializeAsync()
8194
{
82-
// Attach to ExtensionService events
83-
this.CommandAdded += ExtensionService_ExtensionAddedAsync;
84-
this.CommandUpdated += ExtensionService_ExtensionUpdatedAsync;
85-
this.CommandRemoved += ExtensionService_ExtensionRemovedAsync;
86-
87-
this.EditorObject =
88-
new EditorObject(
89-
serviceProvider,
90-
this,
91-
editorOperations);
92-
9395
// Assign the new EditorObject to be the static instance available to binary APIs
94-
this.EditorObject.SetAsStaticInstance();
96+
EditorObject.SetAsStaticInstance();
9597

9698
// Register the editor object in the runspace
9799
await ExecutionService.ExecuteDelegateAsync((pwsh, cancellationToken) =>

src/PowerShellEditorServices/Services/PowerShell/Execution/PipelineThreadExecutor.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public PipelineThreadExecutor(
6060
_psesHost = psesHost;
6161
_debugContext = psesHost.DebugContext;
6262
_readLineProvider = readLineProvider;
63+
_consumerThreadCancellationSource = new CancellationTokenSource();
64+
_executionQueue = new BlockingCollection<ISynchronousTask>();
65+
_loopCancellationContext = new CancellationContext();
66+
_commandCancellationContext = new CancellationContext();
67+
_taskProcessingLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
6368

6469
_pipelineThread = new Thread(Run)
6570
{
@@ -75,11 +80,9 @@ public Task<TResult> QueueTask<TResult>(SynchronousTask<TResult> synchronousTask
7580
_executionQueue.Add(synchronousTask);
7681
return synchronousTask.Task;
7782
}
83+
7884
public void Start()
7985
{
80-
// We need to override the idle handler here,
81-
// since readline will be overridden by this point
82-
_readLineProvider.ReadLine.TryOverrideIdleHandler(OnPowerShellIdle);
8386
_pipelineThread.Start();
8487
}
8588

@@ -107,6 +110,10 @@ public IDisposable TakeTaskWriterLock()
107110
private void Run()
108111
{
109112
_psesHost.PushInitialPowerShell();
113+
// We need to override the idle handler here,
114+
// since readline will be overridden when the initial Powershell runspace is instantiated above
115+
_readLineProvider.ReadLine.TryOverrideIdleHandler(OnPowerShellIdle);
116+
_psesHost.PushNewReplTask();
110117
RunTopLevelConsumerLoop();
111118
}
112119

src/PowerShellEditorServices/Services/PowerShell/Host/EditorServicesConsolePSHost.cs

+12-10
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public EditorServicesConsolePSHost(
100100

101101
public override void EnterNestedPrompt()
102102
{
103-
PushPowerShell(_psFactory.CreateNestedPowerShell(CurrentRunspace), PowerShellFrameType.Nested);
103+
PushPowerShellAndRunLoop(_psFactory.CreateNestedPowerShell(CurrentRunspace), PowerShellFrameType.Nested);
104104
}
105105

106106
public override void ExitNestedPrompt()
@@ -127,7 +127,7 @@ public void PopRunspace()
127127
public void PushRunspace(Runspace runspace)
128128
{
129129
IsRunspacePushed = true;
130-
PushPowerShell(_psFactory.CreatePowerShellForRunspace(runspace), PowerShellFrameType.Remote);
130+
PushPowerShellAndRunLoop(_psFactory.CreatePowerShellForRunspace(runspace), PowerShellFrameType.Remote);
131131
}
132132

133133
public override void SetShouldExit(int exitCode)
@@ -145,7 +145,7 @@ public void PushInitialPowerShell()
145145

146146
internal void PushNonInteractivePowerShell()
147147
{
148-
PushPowerShell(_psFactory.CreateNestedPowerShell(CurrentRunspace), PowerShellFrameType.Nested | PowerShellFrameType.NonInteractive);
148+
PushPowerShellAndRunLoop(_psFactory.CreateNestedPowerShell(CurrentRunspace), PowerShellFrameType.Nested | PowerShellFrameType.NonInteractive);
149149
}
150150

151151
internal void CancelCurrentPrompt()
@@ -193,8 +193,6 @@ await ExecutionService.ExecuteDelegateAsync((pwsh, delegateCancellation) =>
193193
{
194194
await SetInitialWorkingDirectoryAsync(hostStartOptions.InitialWorkingDirectory, CancellationToken.None).ConfigureAwait(false);
195195
}
196-
197-
_consoleReplRunner?.StartRepl();
198196
}
199197

200198
private void SetExit()
@@ -212,12 +210,18 @@ private void SetExit()
212210
}
213211
}
214212

215-
private void PushPowerShell(SMA.PowerShell pwsh, PowerShellFrameType frameType)
213+
private void PushPowerShellAndRunLoop(SMA.PowerShell pwsh, PowerShellFrameType frameType)
216214
{
217215
// TODO: Improve runspace origin detection here
218216
RunspaceOrigin runspaceOrigin = pwsh.Runspace.RunspaceIsRemote ? RunspaceOrigin.EnteredProcess : RunspaceOrigin.Local;
219217
var runspaceInfo = RunspaceInfo.CreateFromPowerShell(_logger, pwsh, runspaceOrigin, _localComputerName);
220-
PushPowerShell(new PowerShellContextFrame(pwsh, runspaceInfo, frameType));
218+
PushPowerShellAndRunLoop(new PowerShellContextFrame(pwsh, runspaceInfo, frameType));
219+
}
220+
221+
private void PushPowerShellAndRunLoop(PowerShellContextFrame frame)
222+
{
223+
PushPowerShell(frame);
224+
_pipelineExecutor.RunPowerShellLoop(frame.FrameType);
221225
}
222226

223227
private void PushPowerShell(PowerShellContextFrame frame)
@@ -229,8 +233,6 @@ private void PushPowerShell(PowerShellContextFrame frame)
229233
AddRunspaceEventHandlers(frame.PowerShell.Runspace);
230234

231235
_psFrameStack.Push(frame);
232-
233-
_pipelineExecutor.RunPowerShellLoop(frame.FrameType);
234236
}
235237

236238
internal void PopPowerShell()
@@ -271,7 +273,7 @@ private void OnDebuggerStopped(object sender, DebuggerStopEventArgs debuggerStop
271273
try
272274
{
273275
CurrentPowerShell.WaitForRemoteOutputIfNeeded();
274-
PushPowerShell(_psFactory.CreateNestedPowerShell(CurrentRunspace), PowerShellFrameType.Debug | PowerShellFrameType.Nested);
276+
PushPowerShellAndRunLoop(_psFactory.CreateNestedPowerShell(CurrentRunspace), PowerShellFrameType.Debug | PowerShellFrameType.Nested);
275277
CurrentPowerShell.ResumeRemoteOutputIfNeeded();
276278
}
277279
finally

src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs

+16-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.IO;
1818
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
1919
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
20+
using Microsoft.PowerShell.EditorServices.Services.Extension;
2021

2122
namespace Microsoft.PowerShell.EditorServices.Handlers
2223
{
@@ -25,22 +26,26 @@ internal class PsesConfigurationHandler : IDidChangeConfigurationHandler
2526
private readonly ILogger _logger;
2627
private readonly WorkspaceService _workspaceService;
2728
private readonly ConfigurationService _configurationService;
29+
private readonly ExtensionService _extensionService;
2830
private readonly EditorServicesConsolePSHost _psesHost;
2931
private DidChangeConfigurationCapability _capability;
3032
private bool _profilesLoaded;
3133
private bool _consoleReplStarted;
34+
private bool _extensionServiceInitialized;
3235
private bool _cwdSet;
3336

3437
public PsesConfigurationHandler(
3538
ILoggerFactory factory,
3639
WorkspaceService workspaceService,
3740
AnalysisService analysisService,
3841
ConfigurationService configurationService,
42+
ExtensionService extensionService,
3943
EditorServicesConsolePSHost psesHost)
4044
{
4145
_logger = factory.CreateLogger<PsesConfigurationHandler>();
4246
_workspaceService = workspaceService;
4347
_configurationService = configurationService;
48+
_extensionService = extensionService;
4449
_psesHost = psesHost;
4550

4651
ConfigurationUpdated += analysisService.OnConfigurationUpdated;
@@ -70,6 +75,14 @@ public async Task<Unit> Handle(DidChangeConfigurationParams request, Cancellatio
7075
_workspaceService.WorkspacePath,
7176
_logger);
7277

78+
if (!_psesHost.IsRunning)
79+
{
80+
await _psesHost.StartAsync(new HostStartOptions
81+
{
82+
LoadProfiles = _configurationService.CurrentSettings.EnableProfileLoading,
83+
}, CancellationToken.None).ConfigureAwait(false);
84+
}
85+
7386
if (!this._cwdSet)
7487
{
7588
if (!string.IsNullOrEmpty(_configurationService.CurrentSettings.Cwd)
@@ -90,16 +103,13 @@ await _psesHost.SetInitialWorkingDirectoryAsync(
90103
this._cwdSet = true;
91104
}
92105

93-
if (!_psesHost.IsRunning)
106+
if (!_extensionServiceInitialized)
94107
{
95-
await _psesHost.StartAsync(new HostStartOptions
96-
{
97-
LoadProfiles = _configurationService.CurrentSettings.EnableProfileLoading,
98-
}, CancellationToken.None).ConfigureAwait(false);
108+
await _extensionService.InitializeAsync();
99109
}
100110

101111
// Run any events subscribed to configuration updates
102-
ConfigurationUpdated(this, _configurationService.CurrentSettings);
112+
ConfigurationUpdated?.Invoke(this, _configurationService.CurrentSettings);
103113

104114
// Convert the editor file glob patterns into an array for the Workspace
105115
// Both the files.exclude and search.exclude hash tables look like (glob-text, is-enabled):

0 commit comments

Comments
 (0)