Skip to content

Commit 419d4d4

Browse files
committed
Clean up ExtensionService.cs
1 parent 17b9c45 commit 419d4d4

File tree

1 file changed

+48
-57
lines changed

1 file changed

+48
-57
lines changed

src/PowerShellEditorServices/Services/Extension/ExtensionService.cs

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ internal sealed class ExtensionService
2424

2525
#region Fields
2626

27-
private readonly Dictionary<string, EditorCommand> editorCommands =
28-
new Dictionary<string, EditorCommand>();
27+
private readonly Dictionary<string, EditorCommand> editorCommands = new();
2928

3029
private readonly ILanguageServerFacade _languageServer;
3130

32-
private IdempotentLatch _initializedLatch = new();
31+
private readonly IdempotentLatch _initializedLatch = new();
3332

3433
#endregion
3534

@@ -39,18 +38,18 @@ internal sealed class ExtensionService
3938
/// Gets the IEditorOperations implementation used to invoke operations
4039
/// in the host editor.
4140
/// </summary>
42-
public IEditorOperations EditorOperations { get; private set; }
41+
public IEditorOperations EditorOperations { get; }
4342

4443
/// <summary>
4544
/// Gets the EditorObject which exists in the PowerShell session as the
4645
/// '$psEditor' variable.
4746
/// </summary>
48-
public EditorObject EditorObject { get; private set; }
47+
public EditorObject EditorObject { get; }
4948

5049
/// <summary>
5150
/// Gets the PowerShellContext in which extension code will be executed.
5251
/// </summary>
53-
internal IInternalPowerShellExecutionService ExecutionService { get; private set; }
52+
internal IInternalPowerShellExecutionService ExecutionService { get; }
5453

5554
#endregion
5655

@@ -62,7 +61,7 @@ internal sealed class ExtensionService
6261
/// </summary>
6362
/// <param name="languageServer">The PSES language server instance.</param>
6463
/// <param name="serviceProvider">Services for dependency injection into the editor object.</param>
65-
/// <param name="editorOptions">Options object to configure the editor.</param>
64+
/// <param name="editorOperations">The interface for operating an editor.</param>
6665
/// <param name="executionService">PowerShell execution service to run PowerShell execution requests.</param>
6766
internal ExtensionService(
6867
ILanguageServerFacade languageServer,
@@ -80,9 +79,9 @@ internal ExtensionService(
8079
editorOperations);
8180

8281
// Attach to ExtensionService events
83-
CommandAdded += ExtensionService_ExtensionAddedAsync;
84-
CommandUpdated += ExtensionService_ExtensionUpdatedAsync;
85-
CommandRemoved += ExtensionService_ExtensionRemovedAsync;
82+
CommandAdded += ExtensionService_ExtensionAdded;
83+
CommandUpdated += ExtensionService_ExtensionUpdated;
84+
CommandRemoved += ExtensionService_ExtensionRemoved;
8685
}
8786

8887
#endregion
@@ -93,7 +92,6 @@ internal ExtensionService(
9392
/// Initializes this ExtensionService using the provided IEditorOperations
9493
/// implementation for future interaction with the host editor.
9594
/// </summary>
96-
/// <param name="editorOperations">An IEditorOperations implementation.</param>
9795
/// <returns>A Task that can be awaited for completion.</returns>
9896
internal Task InitializeAsync()
9997
{
@@ -121,28 +119,29 @@ internal Task InitializeAsync()
121119
/// <param name="commandName">The unique name of the command to be invoked.</param>
122120
/// <param name="editorContext">The context in which the command is being invoked.</param>
123121
/// <returns>A Task that can be awaited for completion.</returns>
122+
/// <exception cref="KeyNotFoundException"></exception>
124123
public async Task InvokeCommandAsync(string commandName, EditorContext editorContext)
125124
{
126-
127-
if (this.editorCommands.TryGetValue(commandName, out EditorCommand editorCommand))
125+
if (editorCommands.TryGetValue(commandName, out EditorCommand editorCommand))
128126
{
129-
PSCommand executeCommand = new PSCommand();
130-
executeCommand.AddCommand("Invoke-Command");
131-
executeCommand.AddParameter("ScriptBlock", editorCommand.ScriptBlock);
132-
executeCommand.AddParameter("ArgumentList", new object[] { editorContext });
127+
PSCommand executeCommand = new PSCommand()
128+
.AddCommand("Invoke-Command")
129+
.AddParameter("ScriptBlock", editorCommand.ScriptBlock)
130+
.AddParameter("ArgumentList", new object[] { editorContext });
133131

134132
await ExecutionService.ExecutePSCommandAsync(
135133
executeCommand,
136134
CancellationToken.None,
137-
new PowerShellExecutionOptions { WriteOutputToHost = !editorCommand.SuppressOutput, ThrowOnError = false, AddToHistory = !editorCommand.SuppressOutput })
138-
.ConfigureAwait(false);
135+
new PowerShellExecutionOptions
136+
{
137+
WriteOutputToHost = !editorCommand.SuppressOutput,
138+
ThrowOnError = false,
139+
AddToHistory = !editorCommand.SuppressOutput
140+
}).ConfigureAwait(false);
139141
}
140142
else
141143
{
142-
throw new KeyNotFoundException(
143-
string.Format(
144-
"Editor command not found: '{0}'",
145-
commandName));
144+
throw new KeyNotFoundException($"Editor command not found: '{commandName}'");
146145
}
147146
}
148147

@@ -156,20 +155,18 @@ public bool RegisterCommand(EditorCommand editorCommand)
156155
{
157156
Validate.IsNotNull(nameof(editorCommand), editorCommand);
158157

159-
bool commandExists =
160-
this.editorCommands.ContainsKey(
161-
editorCommand.Name);
158+
bool commandExists = editorCommands.ContainsKey(editorCommand.Name);
162159

163160
// Add or replace the editor command
164-
this.editorCommands[editorCommand.Name] = editorCommand;
161+
editorCommands[editorCommand.Name] = editorCommand;
165162

166163
if (!commandExists)
167164
{
168-
this.OnCommandAdded(editorCommand);
165+
OnCommandAdded(editorCommand);
169166
}
170167
else
171168
{
172-
this.OnCommandUpdated(editorCommand);
169+
OnCommandUpdated(editorCommand);
173170
}
174171

175172
return !commandExists;
@@ -179,19 +176,17 @@ public bool RegisterCommand(EditorCommand editorCommand)
179176
/// Unregisters an existing EditorCommand based on its registered name.
180177
/// </summary>
181178
/// <param name="commandName">The name of the command to be unregistered.</param>
179+
/// <exception cref="KeyNotFoundException"></exception>
182180
public void UnregisterCommand(string commandName)
183181
{
184-
if (this.editorCommands.TryGetValue(commandName, out EditorCommand existingCommand))
182+
if (editorCommands.TryGetValue(commandName, out EditorCommand existingCommand))
185183
{
186-
this.editorCommands.Remove(commandName);
187-
this.OnCommandRemoved(existingCommand);
184+
editorCommands.Remove(commandName);
185+
OnCommandRemoved(existingCommand);
188186
}
189187
else
190188
{
191-
throw new KeyNotFoundException(
192-
string.Format(
193-
"Command '{0}' is not registered",
194-
commandName));
189+
throw new KeyNotFoundException($"Command '{commandName}' is not registered");
195190
}
196191
}
197192

@@ -201,8 +196,8 @@ public void UnregisterCommand(string commandName)
201196
/// <returns>An Array of all registered EditorCommands.</returns>
202197
public EditorCommand[] GetCommands()
203198
{
204-
EditorCommand[] commands = new EditorCommand[this.editorCommands.Count];
205-
this.editorCommands.Values.CopyTo(commands,0);
199+
EditorCommand[] commands = new EditorCommand[editorCommands.Count];
200+
editorCommands.Values.CopyTo(commands, 0);
206201
return commands;
207202
}
208203

@@ -217,7 +212,7 @@ public EditorCommand[] GetCommands()
217212

218213
private void OnCommandAdded(EditorCommand command)
219214
{
220-
this.CommandAdded?.Invoke(this, command);
215+
CommandAdded?.Invoke(this, command);
221216
}
222217

223218
/// <summary>
@@ -227,7 +222,7 @@ private void OnCommandAdded(EditorCommand command)
227222

228223
private void OnCommandUpdated(EditorCommand command)
229224
{
230-
this.CommandUpdated?.Invoke(this, command);
225+
CommandUpdated?.Invoke(this, command);
231226
}
232227

233228
/// <summary>
@@ -237,35 +232,31 @@ private void OnCommandUpdated(EditorCommand command)
237232

238233
private void OnCommandRemoved(EditorCommand command)
239234
{
240-
this.CommandRemoved?.Invoke(this, command);
235+
CommandRemoved?.Invoke(this, command);
241236
}
242237

243-
private void ExtensionService_ExtensionAddedAsync(object sender, EditorCommand e)
238+
private void ExtensionService_ExtensionAdded(object sender, EditorCommand e)
244239
{
245-
_languageServer?.SendNotification<ExtensionCommandAddedNotification>("powerShell/extensionCommandAdded",
240+
_languageServer?.SendNotification<ExtensionCommandAddedNotification>(
241+
"powerShell/extensionCommandAdded",
246242
new ExtensionCommandAddedNotification
247-
{
248-
Name = e.Name,
249-
DisplayName = e.DisplayName
250-
});
243+
{ Name = e.Name, DisplayName = e.DisplayName });
251244
}
252245

253-
private void ExtensionService_ExtensionUpdatedAsync(object sender, EditorCommand e)
246+
private void ExtensionService_ExtensionUpdated(object sender, EditorCommand e)
254247
{
255-
_languageServer?.SendNotification<ExtensionCommandUpdatedNotification>("powerShell/extensionCommandUpdated",
248+
_languageServer?.SendNotification<ExtensionCommandUpdatedNotification>(
249+
"powerShell/extensionCommandUpdated",
256250
new ExtensionCommandUpdatedNotification
257-
{
258-
Name = e.Name,
259-
});
251+
{ Name = e.Name, });
260252
}
261253

262-
private void ExtensionService_ExtensionRemovedAsync(object sender, EditorCommand e)
254+
private void ExtensionService_ExtensionRemoved(object sender, EditorCommand e)
263255
{
264-
_languageServer?.SendNotification<ExtensionCommandRemovedNotification>("powerShell/extensionCommandRemoved",
256+
_languageServer?.SendNotification<ExtensionCommandRemovedNotification>(
257+
"powerShell/extensionCommandRemoved",
265258
new ExtensionCommandRemovedNotification
266-
{
267-
Name = e.Name,
268-
});
259+
{ Name = e.Name, });
269260
}
270261

271262
#endregion

0 commit comments

Comments
 (0)