Skip to content

Commit 3bf231e

Browse files
committed
Clean up ExtensionService.cs
1 parent 17b9c45 commit 3bf231e

File tree

1 file changed

+52
-62
lines changed

1 file changed

+52
-62
lines changed

src/PowerShellEditorServices/Services/Extension/ExtensionService.cs

Lines changed: 52 additions & 62 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,
@@ -73,16 +72,15 @@ internal ExtensionService(
7372
ExecutionService = executionService;
7473
_languageServer = languageServer;
7574

76-
EditorObject =
77-
new EditorObject(
78-
serviceProvider,
79-
this,
80-
editorOperations);
75+
EditorObject = new EditorObject(
76+
serviceProvider,
77+
this,
78+
editorOperations);
8179

8280
// Attach to ExtensionService events
83-
CommandAdded += ExtensionService_ExtensionAddedAsync;
84-
CommandUpdated += ExtensionService_ExtensionUpdatedAsync;
85-
CommandRemoved += ExtensionService_ExtensionRemovedAsync;
81+
CommandAdded += ExtensionService_ExtensionAdded;
82+
CommandUpdated += ExtensionService_ExtensionUpdated;
83+
CommandRemoved += ExtensionService_ExtensionRemoved;
8684
}
8785

8886
#endregion
@@ -93,7 +91,6 @@ internal ExtensionService(
9391
/// Initializes this ExtensionService using the provided IEditorOperations
9492
/// implementation for future interaction with the host editor.
9593
/// </summary>
96-
/// <param name="editorOperations">An IEditorOperations implementation.</param>
9794
/// <returns>A Task that can be awaited for completion.</returns>
9895
internal Task InitializeAsync()
9996
{
@@ -121,28 +118,29 @@ internal Task InitializeAsync()
121118
/// <param name="commandName">The unique name of the command to be invoked.</param>
122119
/// <param name="editorContext">The context in which the command is being invoked.</param>
123120
/// <returns>A Task that can be awaited for completion.</returns>
121+
/// <exception cref="KeyNotFoundException">The command being invoked was not registered.</exception>
124122
public async Task InvokeCommandAsync(string commandName, EditorContext editorContext)
125123
{
126-
127-
if (this.editorCommands.TryGetValue(commandName, out EditorCommand editorCommand))
124+
if (editorCommands.TryGetValue(commandName, out EditorCommand editorCommand))
128125
{
129-
PSCommand executeCommand = new PSCommand();
130-
executeCommand.AddCommand("Invoke-Command");
131-
executeCommand.AddParameter("ScriptBlock", editorCommand.ScriptBlock);
132-
executeCommand.AddParameter("ArgumentList", new object[] { editorContext });
126+
PSCommand executeCommand = new PSCommand()
127+
.AddCommand("Invoke-Command")
128+
.AddParameter("ScriptBlock", editorCommand.ScriptBlock)
129+
.AddParameter("ArgumentList", new object[] { editorContext });
133130

134131
await ExecutionService.ExecutePSCommandAsync(
135132
executeCommand,
136133
CancellationToken.None,
137-
new PowerShellExecutionOptions { WriteOutputToHost = !editorCommand.SuppressOutput, ThrowOnError = false, AddToHistory = !editorCommand.SuppressOutput })
138-
.ConfigureAwait(false);
134+
new PowerShellExecutionOptions
135+
{
136+
WriteOutputToHost = !editorCommand.SuppressOutput,
137+
ThrowOnError = false,
138+
AddToHistory = !editorCommand.SuppressOutput
139+
}).ConfigureAwait(false);
139140
}
140141
else
141142
{
142-
throw new KeyNotFoundException(
143-
string.Format(
144-
"Editor command not found: '{0}'",
145-
commandName));
143+
throw new KeyNotFoundException($"Editor command not found: '{commandName}'");
146144
}
147145
}
148146

@@ -156,20 +154,18 @@ public bool RegisterCommand(EditorCommand editorCommand)
156154
{
157155
Validate.IsNotNull(nameof(editorCommand), editorCommand);
158156

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

163159
// Add or replace the editor command
164-
this.editorCommands[editorCommand.Name] = editorCommand;
160+
editorCommands[editorCommand.Name] = editorCommand;
165161

166162
if (!commandExists)
167163
{
168-
this.OnCommandAdded(editorCommand);
164+
OnCommandAdded(editorCommand);
169165
}
170166
else
171167
{
172-
this.OnCommandUpdated(editorCommand);
168+
OnCommandUpdated(editorCommand);
173169
}
174170

175171
return !commandExists;
@@ -179,19 +175,17 @@ public bool RegisterCommand(EditorCommand editorCommand)
179175
/// Unregisters an existing EditorCommand based on its registered name.
180176
/// </summary>
181177
/// <param name="commandName">The name of the command to be unregistered.</param>
178+
/// <exception cref="KeyNotFoundException">The command being unregistered was not registered.</exception>
182179
public void UnregisterCommand(string commandName)
183180
{
184-
if (this.editorCommands.TryGetValue(commandName, out EditorCommand existingCommand))
181+
if (editorCommands.TryGetValue(commandName, out EditorCommand existingCommand))
185182
{
186-
this.editorCommands.Remove(commandName);
187-
this.OnCommandRemoved(existingCommand);
183+
editorCommands.Remove(commandName);
184+
OnCommandRemoved(existingCommand);
188185
}
189186
else
190187
{
191-
throw new KeyNotFoundException(
192-
string.Format(
193-
"Command '{0}' is not registered",
194-
commandName));
188+
throw new KeyNotFoundException($"Command '{commandName}' is not registered");
195189
}
196190
}
197191

@@ -201,8 +195,8 @@ public void UnregisterCommand(string commandName)
201195
/// <returns>An Array of all registered EditorCommands.</returns>
202196
public EditorCommand[] GetCommands()
203197
{
204-
EditorCommand[] commands = new EditorCommand[this.editorCommands.Count];
205-
this.editorCommands.Values.CopyTo(commands,0);
198+
EditorCommand[] commands = new EditorCommand[editorCommands.Count];
199+
editorCommands.Values.CopyTo(commands, 0);
206200
return commands;
207201
}
208202

@@ -217,7 +211,7 @@ public EditorCommand[] GetCommands()
217211

218212
private void OnCommandAdded(EditorCommand command)
219213
{
220-
this.CommandAdded?.Invoke(this, command);
214+
CommandAdded?.Invoke(this, command);
221215
}
222216

223217
/// <summary>
@@ -227,7 +221,7 @@ private void OnCommandAdded(EditorCommand command)
227221

228222
private void OnCommandUpdated(EditorCommand command)
229223
{
230-
this.CommandUpdated?.Invoke(this, command);
224+
CommandUpdated?.Invoke(this, command);
231225
}
232226

233227
/// <summary>
@@ -237,35 +231,31 @@ private void OnCommandUpdated(EditorCommand command)
237231

238232
private void OnCommandRemoved(EditorCommand command)
239233
{
240-
this.CommandRemoved?.Invoke(this, command);
234+
CommandRemoved?.Invoke(this, command);
241235
}
242236

243-
private void ExtensionService_ExtensionAddedAsync(object sender, EditorCommand e)
237+
private void ExtensionService_ExtensionAdded(object sender, EditorCommand e)
244238
{
245-
_languageServer?.SendNotification<ExtensionCommandAddedNotification>("powerShell/extensionCommandAdded",
239+
_languageServer?.SendNotification<ExtensionCommandAddedNotification>(
240+
"powerShell/extensionCommandAdded",
246241
new ExtensionCommandAddedNotification
247-
{
248-
Name = e.Name,
249-
DisplayName = e.DisplayName
250-
});
242+
{ Name = e.Name, DisplayName = e.DisplayName });
251243
}
252244

253-
private void ExtensionService_ExtensionUpdatedAsync(object sender, EditorCommand e)
245+
private void ExtensionService_ExtensionUpdated(object sender, EditorCommand e)
254246
{
255-
_languageServer?.SendNotification<ExtensionCommandUpdatedNotification>("powerShell/extensionCommandUpdated",
247+
_languageServer?.SendNotification<ExtensionCommandUpdatedNotification>(
248+
"powerShell/extensionCommandUpdated",
256249
new ExtensionCommandUpdatedNotification
257-
{
258-
Name = e.Name,
259-
});
250+
{ Name = e.Name, });
260251
}
261252

262-
private void ExtensionService_ExtensionRemovedAsync(object sender, EditorCommand e)
253+
private void ExtensionService_ExtensionRemoved(object sender, EditorCommand e)
263254
{
264-
_languageServer?.SendNotification<ExtensionCommandRemovedNotification>("powerShell/extensionCommandRemoved",
255+
_languageServer?.SendNotification<ExtensionCommandRemovedNotification>(
256+
"powerShell/extensionCommandRemoved",
265257
new ExtensionCommandRemovedNotification
266-
{
267-
Name = e.Name,
268-
});
258+
{ Name = e.Name, });
269259
}
270260

271261
#endregion

0 commit comments

Comments
 (0)