Skip to content

Commit 5005c0f

Browse files
committed
Replace superfluous Default member with default values
I think this was a case of premature optimization. We're using C# records, which should be as cheap as an object as they come, especially when we can rely on the compiler for default values. Now we're passing fewer objects around too.
1 parent 3a9defc commit 5005c0f

File tree

7 files changed

+18
-41
lines changed

7 files changed

+18
-41
lines changed

src/PowerShellEditorServices/Services/Extension/ExtensionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ internal Task InitializeAsync()
107107
// Register the editor object in the runspace
108108
return ExecutionService.ExecuteDelegateAsync(
109109
$"Create ${PSEditorVariableName} object",
110-
ExecutionOptions.Default,
110+
executionOptions: null,
111111
(pwsh, _) => pwsh.Runspace.SessionStateProxy.PSVariable.Set(psEditor),
112112
CancellationToken.None);
113113
}

src/PowerShellEditorServices/Services/PowerShell/Console/LegacyReadLine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ public override string ReadLine(CancellationToken cancellationToken)
8484
.AddParameter("CursorColumn", currentCursorIndex)
8585
.AddParameter("Options", null);
8686

87-
currentCompletion = _psesHost.InvokePSCommand<CommandCompletion>(command, PowerShellExecutionOptions.Default, cancellationToken).FirstOrDefault();
87+
currentCompletion = _psesHost.InvokePSCommand<CommandCompletion>(command, executionOptions: null, cancellationToken).FirstOrDefault();
8888
}
8989
else
9090
{
9191
currentCompletion = _psesHost.InvokePSDelegate(
9292
"Legacy readline inline command completion",
93-
ExecutionOptions.Default,
94-
(pwsh, cancellationToken) => CommandCompletion.CompleteInput(inputAfterCompletion, currentCursorIndex, options: null, pwsh),
93+
executionOptions: null,
94+
(pwsh, _) => CommandCompletion.CompleteInput(inputAfterCompletion, currentCursorIndex, options: null, pwsh),
9595
cancellationToken);
9696

9797
if (currentCompletion.CompletionMatches.Count > 0)
@@ -198,7 +198,7 @@ public override string ReadLine(CancellationToken cancellationToken)
198198
PSCommand command = new PSCommand()
199199
.AddCommand("Get-History");
200200

201-
currentHistory = _psesHost.InvokePSCommand<PSObject>(command, PowerShellExecutionOptions.Default, cancellationToken);
201+
currentHistory = _psesHost.InvokePSCommand<PSObject>(command, executionOptions: null, cancellationToken);
202202

203203
if (currentHistory != null)
204204
{

src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static Task<DscBreakpointCapability> GetDscCapabilityAsync(
164164

165165
return psesHost.ExecuteDelegateAsync<DscBreakpointCapability>(
166166
nameof(getDscBreakpointCapabilityFunc),
167-
ExecutionOptions.Default,
167+
executionOptions: null,
168168
getDscBreakpointCapabilityFunc,
169169
cancellationToken);
170170
}

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

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,16 @@ public enum ExecutionPriority
1515
// Generally the executor will do the right thing though; some options just priority over others.
1616
public record ExecutionOptions
1717
{
18-
public static ExecutionOptions Default = new()
19-
{
20-
Priority = ExecutionPriority.Normal,
21-
MustRunInForeground = false,
22-
InterruptCurrentForeground = false,
23-
};
24-
25-
public ExecutionPriority Priority { get; init; }
26-
18+
public ExecutionPriority Priority { get; init; } = ExecutionPriority.Normal;
2719
public bool MustRunInForeground { get; init; }
28-
2920
public bool InterruptCurrentForeground { get; init; }
3021
}
3122

3223
public record PowerShellExecutionOptions : ExecutionOptions
3324
{
34-
public static new PowerShellExecutionOptions Default = new()
35-
{
36-
Priority = ExecutionPriority.Normal,
37-
MustRunInForeground = false,
38-
InterruptCurrentForeground = false,
39-
WriteOutputToHost = false,
40-
WriteInputToHost = false,
41-
ThrowOnError = true,
42-
AddToHistory = false,
43-
};
44-
4525
public bool WriteOutputToHost { get; init; }
46-
4726
public bool WriteInputToHost { get; init; }
48-
49-
public bool ThrowOnError { get; init; }
50-
27+
public bool ThrowOnError { get; init; } = true;
5128
public bool AddToHistory { get; init; }
5229
}
5330
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public Task<TResult> ExecuteDelegateAsync<TResult>(
307307
Func<PowerShell, CancellationToken, TResult> func,
308308
CancellationToken cancellationToken)
309309
{
310-
return InvokeTaskOnPipelineThreadAsync(new SynchronousPSDelegateTask<TResult>(_logger, this, representation, executionOptions ?? ExecutionOptions.Default, func, cancellationToken));
310+
return InvokeTaskOnPipelineThreadAsync(new SynchronousPSDelegateTask<TResult>(_logger, this, representation, executionOptions ?? new ExecutionOptions(), func, cancellationToken));
311311
}
312312

313313
public Task ExecuteDelegateAsync(
@@ -316,7 +316,7 @@ public Task ExecuteDelegateAsync(
316316
Action<PowerShell, CancellationToken> action,
317317
CancellationToken cancellationToken)
318318
{
319-
return InvokeTaskOnPipelineThreadAsync(new SynchronousPSDelegateTask(_logger, this, representation, executionOptions ?? ExecutionOptions.Default, action, cancellationToken));
319+
return InvokeTaskOnPipelineThreadAsync(new SynchronousPSDelegateTask(_logger, this, representation, executionOptions ?? new ExecutionOptions(), action, cancellationToken));
320320
}
321321

322322
public Task<TResult> ExecuteDelegateAsync<TResult>(
@@ -325,7 +325,7 @@ public Task<TResult> ExecuteDelegateAsync<TResult>(
325325
Func<CancellationToken, TResult> func,
326326
CancellationToken cancellationToken)
327327
{
328-
return InvokeTaskOnPipelineThreadAsync(new SynchronousDelegateTask<TResult>(_logger, representation, executionOptions ?? ExecutionOptions.Default, func, cancellationToken));
328+
return InvokeTaskOnPipelineThreadAsync(new SynchronousDelegateTask<TResult>(_logger, representation, executionOptions ?? new ExecutionOptions(), func, cancellationToken));
329329
}
330330

331331
public Task ExecuteDelegateAsync(
@@ -334,7 +334,7 @@ public Task ExecuteDelegateAsync(
334334
Action<CancellationToken> action,
335335
CancellationToken cancellationToken)
336336
{
337-
return InvokeTaskOnPipelineThreadAsync(new SynchronousDelegateTask(_logger, representation, executionOptions ?? ExecutionOptions.Default, action, cancellationToken));
337+
return InvokeTaskOnPipelineThreadAsync(new SynchronousDelegateTask(_logger, representation, executionOptions ?? new ExecutionOptions(), action, cancellationToken));
338338
}
339339

340340
public Task<IReadOnlyList<TResult>> ExecutePSCommandAsync<TResult>(
@@ -346,7 +346,7 @@ public Task<IReadOnlyList<TResult>> ExecutePSCommandAsync<TResult>(
346346
_logger,
347347
this,
348348
psCommand,
349-
executionOptions ?? PowerShellExecutionOptions.Default,
349+
executionOptions ?? new PowerShellExecutionOptions(),
350350
cancellationToken));
351351
}
352352

@@ -660,7 +660,7 @@ private void DoOneRepl(CancellationToken cancellationToken)
660660
private string GetPrompt(CancellationToken cancellationToken)
661661
{
662662
var command = new PSCommand().AddCommand("prompt");
663-
IReadOnlyList<string> results = InvokePSCommand<string>(command, PowerShellExecutionOptions.Default, cancellationToken);
663+
IReadOnlyList<string> results = InvokePSCommand<string>(command, executionOptions: null, cancellationToken);
664664
string prompt = results.Count > 0 ? results[0] : DefaultPrompt;
665665

666666
if (CurrentRunspace.RunspaceOrigin != RunspaceOrigin.Local)
@@ -844,7 +844,7 @@ private void OnPowerShellIdle(CancellationToken idleCancellationToken)
844844
// to force event processing
845845
if (runPipelineForEventProcessing)
846846
{
847-
InvokePSCommand(new PSCommand().AddScript("0", useLocalScope: true), PowerShellExecutionOptions.Default, CancellationToken.None);
847+
InvokePSCommand(new PSCommand().AddScript("0", useLocalScope: true), executionOptions: null, CancellationToken.None);
848848
}
849849
}
850850

src/PowerShellEditorServices/Services/PowerShell/Utility/CommandHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public static async Task<string> GetCommandSynopsisAsync(
183183

184184
IEnumerable<CommandInfo> aliases = await executionService.ExecuteDelegateAsync<IEnumerable<CommandInfo>>(
185185
nameof(GetAliasesAsync),
186-
Execution.ExecutionOptions.Default,
186+
executionOptions: null,
187187
(pwsh, _) =>
188188
{
189189
CommandInvocationIntrinsics invokeCommand = pwsh.Runspace.SessionStateProxy.InvokeCommand;

src/PowerShellEditorServices/Services/Workspace/RemoteFileManagerService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,8 @@ private async void HandlePSEventReceivedAsync(object sender, PSEventArgs args)
657657
private Task RegisterPSEditFunctionAsync()
658658
=> _executionService.ExecuteDelegateAsync(
659659
"Register psedit function",
660-
ExecutionOptions.Default,
661-
(pwsh, cancellationToken) => RegisterPSEditFunction(pwsh.Runspace),
660+
executionOptions: null,
661+
(pwsh, _) => RegisterPSEditFunction(pwsh.Runspace),
662662
CancellationToken.None);
663663

664664
private void RegisterPSEditFunction(Runspace runspace)

0 commit comments

Comments
 (0)