@@ -24,12 +24,11 @@ internal sealed class ExtensionService
24
24
25
25
#region Fields
26
26
27
- private readonly Dictionary < string , EditorCommand > editorCommands =
28
- new Dictionary < string , EditorCommand > ( ) ;
27
+ private readonly Dictionary < string , EditorCommand > editorCommands = new ( ) ;
29
28
30
29
private readonly ILanguageServerFacade _languageServer ;
31
30
32
- private IdempotentLatch _initializedLatch = new ( ) ;
31
+ private readonly IdempotentLatch _initializedLatch = new ( ) ;
33
32
34
33
#endregion
35
34
@@ -39,18 +38,18 @@ internal sealed class ExtensionService
39
38
/// Gets the IEditorOperations implementation used to invoke operations
40
39
/// in the host editor.
41
40
/// </summary>
42
- public IEditorOperations EditorOperations { get ; private set ; }
41
+ public IEditorOperations EditorOperations { get ; }
43
42
44
43
/// <summary>
45
44
/// Gets the EditorObject which exists in the PowerShell session as the
46
45
/// '$psEditor' variable.
47
46
/// </summary>
48
- public EditorObject EditorObject { get ; private set ; }
47
+ public EditorObject EditorObject { get ; }
49
48
50
49
/// <summary>
51
50
/// Gets the PowerShellContext in which extension code will be executed.
52
51
/// </summary>
53
- internal IInternalPowerShellExecutionService ExecutionService { get ; private set ; }
52
+ internal IInternalPowerShellExecutionService ExecutionService { get ; }
54
53
55
54
#endregion
56
55
@@ -62,7 +61,7 @@ internal sealed class ExtensionService
62
61
/// </summary>
63
62
/// <param name="languageServer">The PSES language server instance.</param>
64
63
/// <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>
66
65
/// <param name="executionService">PowerShell execution service to run PowerShell execution requests.</param>
67
66
internal ExtensionService (
68
67
ILanguageServerFacade languageServer ,
@@ -80,9 +79,9 @@ internal ExtensionService(
80
79
editorOperations ) ;
81
80
82
81
// 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 ;
86
85
}
87
86
88
87
#endregion
@@ -93,7 +92,6 @@ internal ExtensionService(
93
92
/// Initializes this ExtensionService using the provided IEditorOperations
94
93
/// implementation for future interaction with the host editor.
95
94
/// </summary>
96
- /// <param name="editorOperations">An IEditorOperations implementation.</param>
97
95
/// <returns>A Task that can be awaited for completion.</returns>
98
96
internal Task InitializeAsync ( )
99
97
{
@@ -121,28 +119,29 @@ internal Task InitializeAsync()
121
119
/// <param name="commandName">The unique name of the command to be invoked.</param>
122
120
/// <param name="editorContext">The context in which the command is being invoked.</param>
123
121
/// <returns>A Task that can be awaited for completion.</returns>
122
+ /// <exception cref="KeyNotFoundException"></exception>
124
123
public async Task InvokeCommandAsync ( string commandName , EditorContext editorContext )
125
124
{
126
-
127
- if ( this . editorCommands . TryGetValue ( commandName , out EditorCommand editorCommand ) )
125
+ if ( editorCommands . TryGetValue ( commandName , out EditorCommand editorCommand ) )
128
126
{
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 } ) ;
133
131
134
132
await ExecutionService . ExecutePSCommandAsync (
135
133
executeCommand ,
136
134
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 ) ;
139
141
}
140
142
else
141
143
{
142
- throw new KeyNotFoundException (
143
- string . Format (
144
- "Editor command not found: '{0}'" ,
145
- commandName ) ) ;
144
+ throw new KeyNotFoundException ( $ "Editor command not found: '{ commandName } '") ;
146
145
}
147
146
}
148
147
@@ -156,20 +155,18 @@ public bool RegisterCommand(EditorCommand editorCommand)
156
155
{
157
156
Validate . IsNotNull ( nameof ( editorCommand ) , editorCommand ) ;
158
157
159
- bool commandExists =
160
- this . editorCommands . ContainsKey (
161
- editorCommand . Name ) ;
158
+ bool commandExists = editorCommands . ContainsKey ( editorCommand . Name ) ;
162
159
163
160
// Add or replace the editor command
164
- this . editorCommands [ editorCommand . Name ] = editorCommand ;
161
+ editorCommands [ editorCommand . Name ] = editorCommand ;
165
162
166
163
if ( ! commandExists )
167
164
{
168
- this . OnCommandAdded ( editorCommand ) ;
165
+ OnCommandAdded ( editorCommand ) ;
169
166
}
170
167
else
171
168
{
172
- this . OnCommandUpdated ( editorCommand ) ;
169
+ OnCommandUpdated ( editorCommand ) ;
173
170
}
174
171
175
172
return ! commandExists ;
@@ -179,19 +176,17 @@ public bool RegisterCommand(EditorCommand editorCommand)
179
176
/// Unregisters an existing EditorCommand based on its registered name.
180
177
/// </summary>
181
178
/// <param name="commandName">The name of the command to be unregistered.</param>
179
+ /// <exception cref="KeyNotFoundException"></exception>
182
180
public void UnregisterCommand ( string commandName )
183
181
{
184
- if ( this . editorCommands . TryGetValue ( commandName , out EditorCommand existingCommand ) )
182
+ if ( editorCommands . TryGetValue ( commandName , out EditorCommand existingCommand ) )
185
183
{
186
- this . editorCommands . Remove ( commandName ) ;
187
- this . OnCommandRemoved ( existingCommand ) ;
184
+ editorCommands . Remove ( commandName ) ;
185
+ OnCommandRemoved ( existingCommand ) ;
188
186
}
189
187
else
190
188
{
191
- throw new KeyNotFoundException (
192
- string . Format (
193
- "Command '{0}' is not registered" ,
194
- commandName ) ) ;
189
+ throw new KeyNotFoundException ( $ "Command '{ commandName } ' is not registered") ;
195
190
}
196
191
}
197
192
@@ -201,8 +196,8 @@ public void UnregisterCommand(string commandName)
201
196
/// <returns>An Array of all registered EditorCommands.</returns>
202
197
public EditorCommand [ ] GetCommands ( )
203
198
{
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 ) ;
206
201
return commands ;
207
202
}
208
203
@@ -217,7 +212,7 @@ public EditorCommand[] GetCommands()
217
212
218
213
private void OnCommandAdded ( EditorCommand command )
219
214
{
220
- this . CommandAdded ? . Invoke ( this , command ) ;
215
+ CommandAdded ? . Invoke ( this , command ) ;
221
216
}
222
217
223
218
/// <summary>
@@ -227,7 +222,7 @@ private void OnCommandAdded(EditorCommand command)
227
222
228
223
private void OnCommandUpdated ( EditorCommand command )
229
224
{
230
- this . CommandUpdated ? . Invoke ( this , command ) ;
225
+ CommandUpdated ? . Invoke ( this , command ) ;
231
226
}
232
227
233
228
/// <summary>
@@ -237,35 +232,31 @@ private void OnCommandUpdated(EditorCommand command)
237
232
238
233
private void OnCommandRemoved ( EditorCommand command )
239
234
{
240
- this . CommandRemoved ? . Invoke ( this , command ) ;
235
+ CommandRemoved ? . Invoke ( this , command ) ;
241
236
}
242
237
243
- private void ExtensionService_ExtensionAddedAsync ( object sender , EditorCommand e )
238
+ private void ExtensionService_ExtensionAdded ( object sender , EditorCommand e )
244
239
{
245
- _languageServer ? . SendNotification < ExtensionCommandAddedNotification > ( "powerShell/extensionCommandAdded" ,
240
+ _languageServer ? . SendNotification < ExtensionCommandAddedNotification > (
241
+ "powerShell/extensionCommandAdded" ,
246
242
new ExtensionCommandAddedNotification
247
- {
248
- Name = e . Name ,
249
- DisplayName = e . DisplayName
250
- } ) ;
243
+ { Name = e . Name , DisplayName = e . DisplayName } ) ;
251
244
}
252
245
253
- private void ExtensionService_ExtensionUpdatedAsync ( object sender , EditorCommand e )
246
+ private void ExtensionService_ExtensionUpdated ( object sender , EditorCommand e )
254
247
{
255
- _languageServer ? . SendNotification < ExtensionCommandUpdatedNotification > ( "powerShell/extensionCommandUpdated" ,
248
+ _languageServer ? . SendNotification < ExtensionCommandUpdatedNotification > (
249
+ "powerShell/extensionCommandUpdated" ,
256
250
new ExtensionCommandUpdatedNotification
257
- {
258
- Name = e . Name ,
259
- } ) ;
251
+ { Name = e . Name , } ) ;
260
252
}
261
253
262
- private void ExtensionService_ExtensionRemovedAsync ( object sender , EditorCommand e )
254
+ private void ExtensionService_ExtensionRemoved ( object sender , EditorCommand e )
263
255
{
264
- _languageServer ? . SendNotification < ExtensionCommandRemovedNotification > ( "powerShell/extensionCommandRemoved" ,
256
+ _languageServer ? . SendNotification < ExtensionCommandRemovedNotification > (
257
+ "powerShell/extensionCommandRemoved" ,
265
258
new ExtensionCommandRemovedNotification
266
- {
267
- Name = e . Name ,
268
- } ) ;
259
+ { Name = e . Name , } ) ;
269
260
}
270
261
271
262
#endregion
0 commit comments