@@ -40,40 +40,8 @@ let projectsFiles: Map<
40
40
> = new Map ( ) ;
41
41
// ^ caching AND states AND distributed system. Why does LSP has to be stupid like this
42
42
43
- type messageHandler = ( send : ( msg : m . Message ) => void , message : m . Message ) => void ;
44
-
45
- let makeStdioChannel = ( ) => {
46
- let writer = new rpc . StreamMessageWriter ( process . stdout ) ;
47
- let reader = new rpc . StreamMessageReader ( process . stdin ) ;
48
- let send = ( msg : m . Message ) => writer . write ( msg ) ;
49
- return {
50
- onMessage : ( ( func : messageHandler ) => {
51
- let callback = ( message : m . Message ) => {
52
- func ( send , message ) ;
53
- }
54
- reader . listen ( callback ) ;
55
- } ) ,
56
- send,
57
- } ;
58
- }
59
-
60
- let makeNodeIpcChannel = ( ) => {
61
- let send = ( msg : m . Message ) => process . send ! ( msg ) ;
62
- return {
63
- onMessage : ( func : messageHandler ) => {
64
- process . on ( "message" , ( msg : m . Message ) => {
65
- func ( send , msg ) ;
66
- } )
67
- } ,
68
- send,
69
- } ;
70
- }
71
-
72
- let channel = (
73
- process . argv . includes ( "--stdio" )
74
- ? makeStdioChannel ( )
75
- : makeNodeIpcChannel ( )
76
- ) ;
43
+ // will be properly defined below
44
+ let send : ( msg : m . Message ) => void = ( _ ) => { } ;
77
45
78
46
let sendUpdatedDiagnostics = ( ) => {
79
47
projectsFiles . forEach ( ( { filesWithDiagnostics } , projectRootPath ) => {
@@ -96,7 +64,7 @@ let sendUpdatedDiagnostics = () => {
96
64
method : "textDocument/publishDiagnostics" ,
97
65
params : params ,
98
66
} ;
99
- channel . send ( notification ) ;
67
+ send ( notification ) ;
100
68
101
69
filesWithDiagnostics . add ( file ) ;
102
70
} ) ;
@@ -114,7 +82,7 @@ let sendUpdatedDiagnostics = () => {
114
82
method : "textDocument/publishDiagnostics" ,
115
83
params : params ,
116
84
} ;
117
- channel . send ( notification ) ;
85
+ send ( notification ) ;
118
86
filesWithDiagnostics . delete ( file ) ;
119
87
}
120
88
} ) ;
@@ -134,7 +102,7 @@ let deleteProjectDiagnostics = (projectRootPath: string) => {
134
102
method : "textDocument/publishDiagnostics" ,
135
103
params : params ,
136
104
} ;
137
- channel . send ( notification ) ;
105
+ send ( notification ) ;
138
106
} ) ;
139
107
140
108
projectsFiles . delete ( projectRootPath ) ;
@@ -203,7 +171,7 @@ let openedFile = (fileUri: string, fileContent: string) => {
203
171
method : "window/showMessageRequest" ,
204
172
params : params ,
205
173
} ;
206
- channel . send ( request ) ;
174
+ send ( request ) ;
207
175
// the client might send us back the "start build" action, which we'll
208
176
// handle in the isResponseMessage check in the message handling way
209
177
// below
@@ -252,7 +220,22 @@ let getOpenedFileContent = (fileUri: string) => {
252
220
return content ;
253
221
} ;
254
222
255
- channel . onMessage ( ( send , msg : m . Message ) => {
223
+ // Start listening now!
224
+ // We support two modes: the regular node RPC mode for VSCode, and the --stdio
225
+ // mode for other editors The latter is _technically unsupported_. It's an
226
+ // implementation detail that might change at any time
227
+ if ( process . argv . includes ( "--stdio" ) ) {
228
+ let writer = new rpc . StreamMessageWriter ( process . stdout ) ;
229
+ let reader = new rpc . StreamMessageReader ( process . stdin ) ;
230
+ // proper `this` scope for writer
231
+ send = ( msg : m . Message ) => writer . write ( msg ) ;
232
+ reader . listen ( onMessage ) ;
233
+ } else {
234
+ // proper `this` scope for process
235
+ send = ( msg : m . Message ) => process . send ! ( msg ) ;
236
+ process . on ( "message" , onMessage ) ;
237
+ }
238
+ function onMessage ( msg : m . Message ) {
256
239
if ( m . isNotificationMessage ( msg ) ) {
257
240
// notification message, aka the client ends it and doesn't want a reply
258
241
if ( ! initialized && msg . method !== "exit" ) {
@@ -541,4 +524,4 @@ channel.onMessage((send, msg: m.Message) => {
541
524
}
542
525
}
543
526
}
544
- } ) ;
527
+ }
0 commit comments