Skip to content

Commit 7f23c07

Browse files
committed
Removed unnecessary abstraction for loading PSReadline and Commands module. Removed unnecessary whitepsace changes
2 parents b8d30fc + 2260c6f commit 7f23c07

File tree

5 files changed

+32
-35
lines changed

5 files changed

+32
-35
lines changed

src/PowerShellEditorServices.Hosting/Commands/InvokeReadLineForEditorServicesCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ private delegate string ReadLineInvoker(
2222

2323
private static Lazy<ReadLineInvoker> s_readLine = new Lazy<ReadLineInvoker>(() =>
2424
{
25-
var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
26-
var assemblies = allAssemblies.FirstOrDefault(a => a.FullName.Contains("Microsoft.PowerShell.PSReadLine2"));
27-
var type = assemblies?.ExportedTypes?.FirstOrDefault(a => a.FullName == "Microsoft.PowerShell.PSConsoleReadLine");
25+
Type type = Type.GetType("Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2");
2826
MethodInfo method = type?.GetMethod(
2927
"ReadLine",
3028
new[] { typeof(Runspace), typeof(EngineIntrinsics), typeof(CancellationToken) });
3129

30+
// TODO: Handle method being null here. This shouldn't ever happen.
31+
3232
return (ReadLineInvoker)method.CreateDelegate(typeof(ReadLineInvoker));
3333
});
3434

src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private async Task CreateEditorServicesAndRunUntilShutdown()
137137
HostStartupInfo hostStartupInfo = CreateHostStartupInfo();
138138

139139
// If we just want a temp debug session, run that and do nothing else
140-
if(isTempDebugSession)
140+
if (isTempDebugSession)
141141
{
142142
await RunTempDebugSessionAsync(hostStartupInfo).ConfigureAwait(false);
143143
return;
@@ -153,9 +153,9 @@ private async Task CreateEditorServicesAndRunUntilShutdown()
153153

154154
// Unsubscribe the host logger here so that the integrated console is not polluted with input after the first prompt
155155
_logger.Log(PsesLogLevel.Verbose, "Starting server, deregistering host logger and registering shutdown listener");
156-
if(_loggersToUnsubscribe != null)
156+
if (_loggersToUnsubscribe != null)
157157
{
158-
foreach(IDisposable loggerToUnsubscribe in _loggersToUnsubscribe)
158+
foreach (IDisposable loggerToUnsubscribe in _loggersToUnsubscribe)
159159
{
160160
loggerToUnsubscribe.Dispose();
161161
}
@@ -166,22 +166,22 @@ private async Task CreateEditorServicesAndRunUntilShutdown()
166166
PsesLanguageServer languageServer = await CreateLanguageServerAsync(hostStartupInfo).ConfigureAwait(false);
167167

168168
Task<PsesDebugServer> debugServerCreation = null;
169-
if(creatingDebugServer)
169+
if (creatingDebugServer)
170170
{
171171
debugServerCreation = CreateDebugServerWithLanguageServerAsync(languageServer, usePSReadLine: _config.ConsoleRepl == ConsoleReplKind.PSReadLine);
172172
}
173173

174174
Task languageServerStart = languageServer.StartAsync();
175175

176176
Task debugServerStart = null;
177-
if(creatingDebugServer)
177+
if (creatingDebugServer)
178178
{
179179
// We don't need to wait for this to start, since we instead wait for it to complete later
180180
debugServerStart = StartDebugServer(debugServerCreation);
181181
}
182182

183183
await languageServerStart.ConfigureAwait(false);
184-
if(debugServerStart != null)
184+
if (debugServerStart != null)
185185
{
186186
await debugServerStart.ConfigureAwait(false);
187187
}
@@ -210,7 +210,7 @@ private async Task StartDebugServer(Task<PsesDebugServer> debugServerCreation)
210210

211211
// When the debug server shuts down, we want it to automatically restart
212212
// To do this, we set an event to allow it to create a new debug server as its session ends
213-
if(!_alreadySubscribedDebug)
213+
if (!_alreadySubscribedDebug)
214214
{
215215
_logger.Log(PsesLogLevel.Diagnostic, "Subscribing debug server for session ended event");
216216
_alreadySubscribedDebug = true;
@@ -268,7 +268,7 @@ private HostStartupInfo CreateHostStartupInfo()
268268
_logger.Log(PsesLogLevel.Diagnostic, "Creating startup info object");
269269

270270
ProfilePathInfo profilePaths = null;
271-
if(_config.ProfilePaths.AllUsersAllHosts != null
271+
if (_config.ProfilePaths.AllUsersAllHosts != null
272272
|| _config.ProfilePaths.AllUsersCurrentHost != null
273273
|| _config.ProfilePaths.CurrentUserAllHosts != null
274274
|| _config.ProfilePaths.CurrentUserCurrentHost != null)
@@ -298,7 +298,7 @@ private HostStartupInfo CreateHostStartupInfo()
298298

299299
private void WriteStartupBanner()
300300
{
301-
if(_config.ConsoleRepl == ConsoleReplKind.None)
301+
if (_config.ConsoleRepl == ConsoleReplKind.None)
302302
{
303303
return;
304304
}

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ public PowerShellContextService(
196196
RunspaceChanged += PowerShellContext_RunspaceChangedAsync;
197197
ExecutionStatusChanged += PowerShellContext_ExecutionStatusChangedAsync;
198198
}
199+
[SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Checked by Validate call")]
199200
public static PowerShellContextService Create(
200201
ILoggerFactory factory,
201202
OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServerFacade languageServer,
202-
HostStartupInfo hostStartupInfo
203-
)
203+
HostStartupInfo hostStartupInfo)
204204
{
205205
var logger = factory.CreateLogger<PowerShellContextService>();
206206

@@ -213,9 +213,9 @@ HostStartupInfo hostStartupInfo
213213

214214
EditorServicesPSHostUserInterface hostUserInterface =
215215
hostStartupInfo.ConsoleReplEnabled
216-
? (EditorServicesPSHostUserInterface)new TerminalPSHostUserInterface(powerShellContext, hostStartupInfo.PSHost, logger)
216+
? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, hostStartupInfo.PSHost, logger)
217217
: new ProtocolPSHostUserInterface(languageServer, powerShellContext, logger);
218-
218+
219219
EditorServicesPSHost psHost =
220220
new EditorServicesPSHost(
221221
powerShellContext,
@@ -224,13 +224,12 @@ HostStartupInfo hostStartupInfo
224224
logger);
225225

226226
logger.LogTrace("Creating initial PowerShell runspace");
227-
Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost,hostStartupInfo.InitialSessionState);
227+
Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost, hostStartupInfo.InitialSessionState);
228228
powerShellContext.Initialize(hostStartupInfo, initialRunspace, true, hostUserInterface);
229229

230230
return powerShellContext;
231231
}
232-
[SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Checked by Validate call")]
233-
232+
234233
/// <summary>
235234
/// Only used in testing. Creates a Runspace given HostStartupInfo instead of a PSHost.
236235
/// </summary>
@@ -300,7 +299,7 @@ public void Initialize(
300299
this.SessionState = PowerShellContextState.NotStarted;
301300
this.ConsoleWriter = consoleHost;
302301
this.ConsoleReader = consoleHost as IHostInput;
303-
302+
304303
// Get the PowerShell runtime version
305304
this.LocalPowerShellVersion =
306305
PowerShellVersionDetails.GetVersionDetails(
@@ -320,7 +319,7 @@ public void Initialize(
320319
connectionString: null);
321320
this.CurrentRunspace = this.initialRunspace;
322321
// Respect a user provided bundled module path.
323-
if(Directory.Exists(hostStartupInfo.BundledModulePath))
322+
if (Directory.Exists(hostStartupInfo.BundledModulePath))
324323
{
325324
logger.LogTrace($"Using new bundled module path: {hostStartupInfo.BundledModulePath}");
326325
s_bundledModulePath = hostStartupInfo.BundledModulePath;
@@ -373,7 +372,7 @@ public void Initialize(
373372
.PSVariable
374373
.GetValue("Host")
375374
as PSHost;
376-
375+
377376
// Now that the runspace is ready, enqueue it for first use
378377
this.PromptNest = new PromptNest(
379378
this,
@@ -384,14 +383,14 @@ public void Initialize(
384383

385384
// Import the PowerShellEditorServices.Commands module into the runspace.
386385
this.ImportModuleAsync(s_commandsModulePath).GetAwaiter().GetResult();
387-
if(isPSReadLineEnabled)
386+
if (isPSReadLineEnabled)
388387
{
389388
// Imports the PSReadLine2 module into the runspace.
390389
ImportModuleAsync(s_psReadLineModulePath).GetAwaiter().GetResult();
391390
}
392391
// TODO: This can be moved to the point after the $psEditor object
393392
// gets initialized when that is done earlier than LanguageServer.Initialize
394-
foreach(string module in hostStartupInfo.AdditionalModules)
393+
foreach (string module in hostStartupInfo.AdditionalModules)
395394
{
396395
this.ImportModuleAsync(module).GetAwaiter().GetResult();
397396
}
@@ -1697,7 +1696,7 @@ internal static string QuoteEscapeString(string escapedPath)
16971696
internal static string WildcardEscapePath(string path, bool escapeSpaces = false)
16981697
{
16991698
var sb = new StringBuilder();
1700-
for (int i = 0; i < path.Length; i++)
1699+
for(int i = 0;i < path.Length;i++)
17011700
{
17021701
char curr = path[i];
17031702
switch (curr)
@@ -1750,7 +1749,7 @@ internal static string UnescapeWildcardEscapedPath(string wildcardEscapedPath)
17501749
}
17511750

17521751
var sb = new StringBuilder(wildcardEscapedPath.Length);
1753-
for (int i = 0; i < wildcardEscapedPath.Length; i++)
1752+
for(int i = 0;i < wildcardEscapedPath.Length;i++)
17541753
{
17551754
// If we see a backtick perform a lookahead
17561755
char curr = wildcardEscapedPath[i];
@@ -2350,7 +2349,7 @@ private static IEnumerable<string> GetLoadableProfilePaths(ProfilePathInfo profi
23502349
yield break;
23512350
}
23522351

2353-
foreach (string path in new [] { profilePaths.AllUsersAllHosts, profilePaths.AllUsersCurrentHost, profilePaths.CurrentUserAllHosts, profilePaths.CurrentUserCurrentHost })
2352+
foreach(string path in new[] { profilePaths.AllUsersAllHosts, profilePaths.AllUsersCurrentHost, profilePaths.CurrentUserAllHosts, profilePaths.CurrentUserCurrentHost })
23542353
{
23552354
if (path != null && File.Exists(path))
23562355
{

src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ internal class PSReadLinePromptContext : IPromptContext
1919
{
2020
private static readonly Lazy<CmdletInfo> s_lazyInvokeReadLineForEditorServicesCmdletInfo = new Lazy<CmdletInfo>(() =>
2121
{
22-
var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
23-
var assemblies = allAssemblies.FirstOrDefault(a => a.FullName.Contains("Microsoft.PowerShell.EditorServices"));
24-
var type = assemblies?.ExportedTypes?.FirstOrDefault(a => a.Name == "InvokeReadLineForEditorServicesCommand");
25-
return new CmdletInfo("__Invoke-ReadLineForEditorServices", type ?? typeof(PSCmdlet));
22+
var type = Type.GetType("Microsoft.PowerShell.EditorServices.Commands.InvokeReadLineForEditorServicesCommand, Microsoft.PowerShell.EditorServices.Hosting");
23+
return new CmdletInfo("__Invoke-ReadLineForEditorServices", type);
2624
});
2725

2826
private static ExecutionOptions s_psrlExecutionOptions = new ExecutionOptions

test/PowerShellEditorServices.Test/Session/PowerShellContextTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class PowerShellContextTests : IDisposable
2727

2828
private static readonly string s_debugTestFilePath =
2929
TestUtilities.NormalizePath("../../../../PowerShellEditorServices.Test.Shared/Debugging/DebugTest.ps1");
30-
30+
3131
public PowerShellContextTests()
3232
{
3333
this.powerShellContext = PowerShellContextFactory.Create(NullLogger.Instance);
@@ -111,8 +111,8 @@ public async Task CanAbortExecution()
111111
[Fact]
112112
public async Task CanResolveAndLoadProfilesForHostId()
113113
{
114-
string [] expectedProfilePaths =
115-
new string []
114+
string[] expectedProfilePaths =
115+
new string[]
116116
{
117117
PowerShellContextFactory.TestProfilePaths.AllUsersAllHosts,
118118
PowerShellContextFactory.TestProfilePaths.AllUsersCurrentHost,
@@ -132,7 +132,7 @@ public async Task CanResolveAndLoadProfilesForHostId()
132132
"$($profile.CurrentUserAllHosts) " +
133133
"$($profile.CurrentUserCurrentHost) " +
134134
"$(Assert-ProfileLoaded)\"");
135-
135+
136136
var result =
137137
await this.powerShellContext.ExecuteCommandAsync<string>(
138138
psCommand);

0 commit comments

Comments
 (0)