@@ -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 ,
@@ -73,16 +72,15 @@ internal ExtensionService(
73
72
ExecutionService = executionService ;
74
73
_languageServer = languageServer ;
75
74
76
- EditorObject =
77
- new EditorObject (
78
- serviceProvider ,
79
- this ,
80
- editorOperations ) ;
75
+ EditorObject = new EditorObject (
76
+ serviceProvider ,
77
+ this ,
78
+ editorOperations ) ;
81
79
82
80
// 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 ;
86
84
}
87
85
88
86
#endregion
@@ -93,7 +91,6 @@ internal ExtensionService(
93
91
/// Initializes this ExtensionService using the provided IEditorOperations
94
92
/// implementation for future interaction with the host editor.
95
93
/// </summary>
96
- /// <param name="editorOperations">An IEditorOperations implementation.</param>
97
94
/// <returns>A Task that can be awaited for completion.</returns>
98
95
internal Task InitializeAsync ( )
99
96
{
@@ -121,28 +118,29 @@ internal Task InitializeAsync()
121
118
/// <param name="commandName">The unique name of the command to be invoked.</param>
122
119
/// <param name="editorContext">The context in which the command is being invoked.</param>
123
120
/// <returns>A Task that can be awaited for completion.</returns>
121
+ /// <exception cref="KeyNotFoundException">The command being invoked was not registered.</exception>
124
122
public async Task InvokeCommandAsync ( string commandName , EditorContext editorContext )
125
123
{
126
-
127
- if ( this . editorCommands . TryGetValue ( commandName , out EditorCommand editorCommand ) )
124
+ if ( editorCommands . TryGetValue ( commandName , out EditorCommand editorCommand ) )
128
125
{
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 } ) ;
133
130
134
131
await ExecutionService . ExecutePSCommandAsync (
135
132
executeCommand ,
136
133
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 ) ;
139
140
}
140
141
else
141
142
{
142
- throw new KeyNotFoundException (
143
- string . Format (
144
- "Editor command not found: '{0}'" ,
145
- commandName ) ) ;
143
+ throw new KeyNotFoundException ( $ "Editor command not found: '{ commandName } '") ;
146
144
}
147
145
}
148
146
@@ -156,20 +154,18 @@ public bool RegisterCommand(EditorCommand editorCommand)
156
154
{
157
155
Validate . IsNotNull ( nameof ( editorCommand ) , editorCommand ) ;
158
156
159
- bool commandExists =
160
- this . editorCommands . ContainsKey (
161
- editorCommand . Name ) ;
157
+ bool commandExists = editorCommands . ContainsKey ( editorCommand . Name ) ;
162
158
163
159
// Add or replace the editor command
164
- this . editorCommands [ editorCommand . Name ] = editorCommand ;
160
+ editorCommands [ editorCommand . Name ] = editorCommand ;
165
161
166
162
if ( ! commandExists )
167
163
{
168
- this . OnCommandAdded ( editorCommand ) ;
164
+ OnCommandAdded ( editorCommand ) ;
169
165
}
170
166
else
171
167
{
172
- this . OnCommandUpdated ( editorCommand ) ;
168
+ OnCommandUpdated ( editorCommand ) ;
173
169
}
174
170
175
171
return ! commandExists ;
@@ -179,19 +175,17 @@ public bool RegisterCommand(EditorCommand editorCommand)
179
175
/// Unregisters an existing EditorCommand based on its registered name.
180
176
/// </summary>
181
177
/// <param name="commandName">The name of the command to be unregistered.</param>
178
+ /// <exception cref="KeyNotFoundException">The command being unregistered was not registered.</exception>
182
179
public void UnregisterCommand ( string commandName )
183
180
{
184
- if ( this . editorCommands . TryGetValue ( commandName , out EditorCommand existingCommand ) )
181
+ if ( editorCommands . TryGetValue ( commandName , out EditorCommand existingCommand ) )
185
182
{
186
- this . editorCommands . Remove ( commandName ) ;
187
- this . OnCommandRemoved ( existingCommand ) ;
183
+ editorCommands . Remove ( commandName ) ;
184
+ OnCommandRemoved ( existingCommand ) ;
188
185
}
189
186
else
190
187
{
191
- throw new KeyNotFoundException (
192
- string . Format (
193
- "Command '{0}' is not registered" ,
194
- commandName ) ) ;
188
+ throw new KeyNotFoundException ( $ "Command '{ commandName } ' is not registered") ;
195
189
}
196
190
}
197
191
@@ -201,8 +195,8 @@ public void UnregisterCommand(string commandName)
201
195
/// <returns>An Array of all registered EditorCommands.</returns>
202
196
public EditorCommand [ ] GetCommands ( )
203
197
{
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 ) ;
206
200
return commands ;
207
201
}
208
202
@@ -217,7 +211,7 @@ public EditorCommand[] GetCommands()
217
211
218
212
private void OnCommandAdded ( EditorCommand command )
219
213
{
220
- this . CommandAdded ? . Invoke ( this , command ) ;
214
+ CommandAdded ? . Invoke ( this , command ) ;
221
215
}
222
216
223
217
/// <summary>
@@ -227,7 +221,7 @@ private void OnCommandAdded(EditorCommand command)
227
221
228
222
private void OnCommandUpdated ( EditorCommand command )
229
223
{
230
- this . CommandUpdated ? . Invoke ( this , command ) ;
224
+ CommandUpdated ? . Invoke ( this , command ) ;
231
225
}
232
226
233
227
/// <summary>
@@ -237,35 +231,31 @@ private void OnCommandUpdated(EditorCommand command)
237
231
238
232
private void OnCommandRemoved ( EditorCommand command )
239
233
{
240
- this . CommandRemoved ? . Invoke ( this , command ) ;
234
+ CommandRemoved ? . Invoke ( this , command ) ;
241
235
}
242
236
243
- private void ExtensionService_ExtensionAddedAsync ( object sender , EditorCommand e )
237
+ private void ExtensionService_ExtensionAdded ( object sender , EditorCommand e )
244
238
{
245
- _languageServer ? . SendNotification < ExtensionCommandAddedNotification > ( "powerShell/extensionCommandAdded" ,
239
+ _languageServer ? . SendNotification < ExtensionCommandAddedNotification > (
240
+ "powerShell/extensionCommandAdded" ,
246
241
new ExtensionCommandAddedNotification
247
- {
248
- Name = e . Name ,
249
- DisplayName = e . DisplayName
250
- } ) ;
242
+ { Name = e . Name , DisplayName = e . DisplayName } ) ;
251
243
}
252
244
253
- private void ExtensionService_ExtensionUpdatedAsync ( object sender , EditorCommand e )
245
+ private void ExtensionService_ExtensionUpdated ( object sender , EditorCommand e )
254
246
{
255
- _languageServer ? . SendNotification < ExtensionCommandUpdatedNotification > ( "powerShell/extensionCommandUpdated" ,
247
+ _languageServer ? . SendNotification < ExtensionCommandUpdatedNotification > (
248
+ "powerShell/extensionCommandUpdated" ,
256
249
new ExtensionCommandUpdatedNotification
257
- {
258
- Name = e . Name ,
259
- } ) ;
250
+ { Name = e . Name , } ) ;
260
251
}
261
252
262
- private void ExtensionService_ExtensionRemovedAsync ( object sender , EditorCommand e )
253
+ private void ExtensionService_ExtensionRemoved ( object sender , EditorCommand e )
263
254
{
264
- _languageServer ? . SendNotification < ExtensionCommandRemovedNotification > ( "powerShell/extensionCommandRemoved" ,
255
+ _languageServer ? . SendNotification < ExtensionCommandRemovedNotification > (
256
+ "powerShell/extensionCommandRemoved" ,
265
257
new ExtensionCommandRemovedNotification
266
- {
267
- Name = e . Name ,
268
- } ) ;
258
+ { Name = e . Name , } ) ;
269
259
}
270
260
271
261
#endregion
0 commit comments