1
1
// Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
3
4
- "use strict" ;
5
-
6
4
import vscode = require( "vscode" ) ;
7
5
import { NotificationType , RequestType } from "vscode-languageclient" ;
8
6
import { LanguageClient } from "vscode-languageclient/node" ;
@@ -50,19 +48,17 @@ interface IShowChoicePromptRequestArgs {
50
48
}
51
49
52
50
interface IShowChoicePromptResponseBody {
53
- responseText : string ;
51
+ responseText : string | undefined ;
54
52
promptCancelled : boolean ;
55
53
}
56
54
57
55
interface IShowInputPromptResponseBody {
58
- responseText : string ;
56
+ responseText : string | undefined ;
59
57
promptCancelled : boolean ;
60
58
}
61
59
62
60
63
- function showChoicePrompt (
64
- promptDetails : IShowChoicePromptRequestArgs ,
65
- client : LanguageClient ) : Thenable < IShowChoicePromptResponseBody > {
61
+ function showChoicePrompt ( promptDetails : IShowChoicePromptRequestArgs ) : Thenable < IShowChoicePromptResponseBody > {
66
62
67
63
let resultThenable : Thenable < IShowChoicePromptResponseBody > ;
68
64
@@ -121,11 +117,12 @@ function showChoicePrompt(
121
117
return resultThenable ;
122
118
}
123
119
124
- function showInputPrompt ( promptDetails : IShowInputPromptRequestArgs ) : Thenable < IShowInputPromptResponseBody > {
125
- return vscode . window . showInputBox ( { placeHolder : promptDetails . name + ": " } ) . then ( onInputEntered ) ;
120
+ async function showInputPrompt ( promptDetails : IShowInputPromptRequestArgs ) : Promise < IShowInputPromptResponseBody > {
121
+ const responseText = await vscode . window . showInputBox ( { placeHolder : promptDetails . name + ": " } ) ;
122
+ return onInputEntered ( responseText ) ;
126
123
}
127
124
128
- function onItemsSelected ( chosenItems : ICheckboxQuickPickItem [ ] ) : IShowChoicePromptResponseBody {
125
+ function onItemsSelected ( chosenItems : ICheckboxQuickPickItem [ ] | undefined ) : IShowChoicePromptResponseBody {
129
126
if ( chosenItems !== undefined ) {
130
127
return {
131
128
promptCancelled : false ,
@@ -140,7 +137,7 @@ function onItemsSelected(chosenItems: ICheckboxQuickPickItem[]): IShowChoiceProm
140
137
}
141
138
}
142
139
143
- function onItemSelected ( chosenItem : vscode . QuickPickItem ) : IShowChoicePromptResponseBody {
140
+ function onItemSelected ( chosenItem : vscode . QuickPickItem | undefined ) : IShowChoicePromptResponseBody {
144
141
if ( chosenItem !== undefined ) {
145
142
return {
146
143
promptCancelled : false ,
@@ -155,7 +152,7 @@ function onItemSelected(chosenItem: vscode.QuickPickItem): IShowChoicePromptResp
155
152
}
156
153
}
157
154
158
- function onInputEntered ( responseText : string ) : IShowInputPromptResponseBody {
155
+ function onInputEntered ( responseText : string | undefined ) : IShowInputPromptResponseBody {
159
156
if ( responseText !== undefined ) {
160
157
return {
161
158
promptCancelled : false ,
@@ -171,7 +168,7 @@ function onInputEntered(responseText: string): IShowInputPromptResponseBody {
171
168
172
169
export class ConsoleFeature extends LanguageClientConsumer {
173
170
private commands : vscode . Disposable [ ] ;
174
- private handlers : vscode . Disposable [ ] ;
171
+ private handlers : vscode . Disposable [ ] = [ ] ;
175
172
176
173
constructor ( private log : Logger ) {
177
174
super ( ) ;
@@ -192,6 +189,10 @@ export class ConsoleFeature extends LanguageClientConsumer {
192
189
}
193
190
194
191
const editor = vscode . window . activeTextEditor ;
192
+ if ( editor === undefined ) {
193
+ return ;
194
+ }
195
+
195
196
let selectionRange : vscode . Range ;
196
197
197
198
if ( ! editor . selection . isEmpty ) {
@@ -200,7 +201,7 @@ export class ConsoleFeature extends LanguageClientConsumer {
200
201
selectionRange = editor . document . lineAt ( editor . selection . start . line ) . range ;
201
202
}
202
203
203
- await this . languageClient . sendRequest ( EvaluateRequestType , {
204
+ await this . languageClient ? .sendRequest ( EvaluateRequestType , {
204
205
expression : editor . document . getText ( selectionRange ) ,
205
206
} ) ;
206
207
@@ -221,12 +222,12 @@ export class ConsoleFeature extends LanguageClientConsumer {
221
222
}
222
223
}
223
224
224
- public setLanguageClient ( languageClient : LanguageClient ) {
225
+ public override setLanguageClient ( languageClient : LanguageClient ) {
225
226
this . languageClient = languageClient ;
226
227
this . handlers = [
227
228
this . languageClient . onRequest (
228
229
ShowChoicePromptRequestType ,
229
- ( promptDetails ) => showChoicePrompt ( promptDetails , this . languageClient ) ) ,
230
+ ( promptDetails ) => showChoicePrompt ( promptDetails ) ) ,
230
231
231
232
this . languageClient . onRequest (
232
233
ShowInputPromptRequestType ,
0 commit comments