3
3
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4
4
//
5
5
6
+ using Microsoft . PowerShell . EditorServices . Extensions ;
6
7
using Microsoft . PowerShell . EditorServices . Protocol . LanguageServer ;
7
8
using Microsoft . PowerShell . EditorServices . Protocol . MessageProtocol ;
8
9
using Microsoft . PowerShell . EditorServices . Protocol . MessageProtocol . Channel ;
@@ -27,6 +28,7 @@ public class LanguageServer : LanguageServerBase
27
28
private bool profilesLoaded ;
28
29
private EditorSession editorSession ;
29
30
private OutputDebouncer outputDebouncer ;
31
+ private LanguageServerEditorOperations editorOperations ;
30
32
private LanguageServerSettings currentSettings = new LanguageServerSettings ( ) ;
31
33
32
34
/// <param name="hostDetails">
@@ -47,6 +49,17 @@ public LanguageServer(HostDetails hostDetails, ChannelBase serverChannel)
47
49
this . editorSession . StartSession ( hostDetails ) ;
48
50
this . editorSession . ConsoleService . OutputWritten += this . powerShellContext_OutputWritten ;
49
51
52
+ // Attach to ExtensionService events
53
+ this . editorSession . ExtensionService . CommandAdded += ExtensionService_ExtensionAdded ;
54
+ this . editorSession . ExtensionService . CommandUpdated += ExtensionService_ExtensionUpdated ;
55
+ this . editorSession . ExtensionService . CommandRemoved += ExtensionService_ExtensionRemoved ;
56
+
57
+ // Create the IEditorOperations implementation
58
+ this . editorOperations =
59
+ new LanguageServerEditorOperations (
60
+ this . editorSession ,
61
+ this ) ;
62
+
50
63
// Always send console prompts through the UI in the language service
51
64
// TODO: This will change later once we have a general REPL available
52
65
// in VS Code.
@@ -61,6 +74,11 @@ public LanguageServer(HostDetails hostDetails, ChannelBase serverChannel)
61
74
62
75
protected override void Initialize ( )
63
76
{
77
+ // Initialize the extension service
78
+ // TODO: This should be made awaited once Initialize is async!
79
+ this . editorSession . ExtensionService . Initialize (
80
+ this . editorOperations ) . Wait ( ) ;
81
+
64
82
// Register all supported message types
65
83
66
84
this . SetRequestHandler ( InitializeRequest . Type , this . HandleInitializeRequest ) ;
@@ -86,6 +104,8 @@ protected override void Initialize()
86
104
this . SetRequestHandler ( FindModuleRequest . Type , this . HandleFindModuleRequest ) ;
87
105
this . SetRequestHandler ( InstallModuleRequest . Type , this . HandleInstallModuleRequest ) ;
88
106
107
+ this . SetRequestHandler ( InvokeExtensionCommandRequest . Type , this . HandleInvokeExtensionCommandRequest ) ;
108
+
89
109
this . SetRequestHandler ( DebugAdapterMessages . EvaluateRequest . Type , this . HandleEvaluateRequest ) ;
90
110
}
91
111
@@ -169,6 +189,26 @@ RequestContext<object> requestContext
169
189
await requestContext . SendResult ( null ) ;
170
190
}
171
191
192
+ private Task HandleInvokeExtensionCommandRequest (
193
+ InvokeExtensionCommandRequest commandDetails ,
194
+ RequestContext < string > requestContext )
195
+ {
196
+ EditorContext editorContext =
197
+ this . editorOperations . ConvertClientEditorContext (
198
+ commandDetails . Context ) ;
199
+
200
+ Task commandTask =
201
+ this . editorSession . ExtensionService . InvokeCommand (
202
+ commandDetails . Name ,
203
+ editorContext ) ;
204
+
205
+ commandTask . ContinueWith ( t =>
206
+ {
207
+ return requestContext . SendResult ( null ) ;
208
+ } ) ;
209
+
210
+ return commandTask ;
211
+ }
172
212
173
213
private async Task HandleExpandAliasRequest (
174
214
string content ,
@@ -320,7 +360,7 @@ protected async Task HandleDidChangeConfigurationNotification(
320
360
// If there is a new settings file path, restart the analyzer with the new settigs.
321
361
bool settingsPathChanged = false ;
322
362
string newSettingsPath = this . currentSettings . ScriptAnalysis . SettingsPath ;
323
- if ( ! ( oldScriptAnalysisSettingsPath ? . Equals ( newSettingsPath , StringComparison . OrdinalIgnoreCase ) ?? false ) )
363
+ if ( ! string . Equals ( oldScriptAnalysisSettingsPath , newSettingsPath , StringComparison . OrdinalIgnoreCase ) )
324
364
{
325
365
this . editorSession . RestartAnalysisService ( newSettingsPath ) ;
326
366
settingsPathChanged = true ;
@@ -802,12 +842,44 @@ protected Task HandleEvaluateRequest(
802
842
803
843
#region Event Handlers
804
844
805
- async void powerShellContext_OutputWritten ( object sender , OutputWrittenEventArgs e )
845
+ private async void powerShellContext_OutputWritten ( object sender , OutputWrittenEventArgs e )
806
846
{
807
847
// Queue the output for writing
808
848
await this . outputDebouncer . Invoke ( e ) ;
809
849
}
810
850
851
+ private async void ExtensionService_ExtensionAdded ( object sender , EditorCommand e )
852
+ {
853
+ await this . SendEvent (
854
+ ExtensionCommandAddedNotification . Type ,
855
+ new ExtensionCommandAddedNotification
856
+ {
857
+ Name = e . Name ,
858
+ DisplayName = e . DisplayName
859
+ } ) ;
860
+ }
861
+
862
+ private async void ExtensionService_ExtensionUpdated ( object sender , EditorCommand e )
863
+ {
864
+ await this . SendEvent (
865
+ ExtensionCommandUpdatedNotification . Type ,
866
+ new ExtensionCommandUpdatedNotification
867
+ {
868
+ Name = e . Name ,
869
+ } ) ;
870
+ }
871
+
872
+ private async void ExtensionService_ExtensionRemoved ( object sender , EditorCommand e )
873
+ {
874
+ await this . SendEvent (
875
+ ExtensionCommandRemovedNotification . Type ,
876
+ new ExtensionCommandRemovedNotification
877
+ {
878
+ Name = e . Name ,
879
+ } ) ;
880
+ }
881
+
882
+
811
883
#endregion
812
884
813
885
#region Helper Methods
0 commit comments