@@ -31,7 +31,10 @@ import {
31
31
WorkspaceEdit
32
32
} from 'vscode' ;
33
33
import {
34
+ ConfigurationParams ,
35
+ ConfigurationRequest ,
34
36
HandleDiagnosticsSignature ,
37
+ HandlerResult ,
35
38
Middleware ,
36
39
PrepareRenameSignature ,
37
40
ProvideCodeActionsSignature ,
@@ -51,13 +54,14 @@ import {
51
54
ProvideWorkspaceSymbolsSignature ,
52
55
ResolveCodeLensSignature ,
53
56
ResolveCompletionItemSignature ,
54
- ResolveDocumentLinkSignature
57
+ ResolveDocumentLinkSignature ,
58
+ ResponseError
55
59
} from 'vscode-languageclient' ;
56
60
57
61
import { ProvideDeclarationSignature } from 'vscode-languageclient/lib/declaration' ;
58
62
import { HiddenFilePrefix } from '../common/constants' ;
59
63
import { CollectLSRequestTiming , CollectNodeLSRequestTiming } from '../common/experimentGroups' ;
60
- import { IExperimentsManager , IPythonExtensionBanner } from '../common/types' ;
64
+ import { IConfigurationService , IExperimentsManager , IPythonExtensionBanner } from '../common/types' ;
61
65
import { StopWatch } from '../common/utils/stopWatch' ;
62
66
import { sendTelemetryEvent } from '../telemetry' ;
63
67
import { EventName } from '../telemetry/constants' ;
@@ -80,11 +84,48 @@ export class LanguageClientMiddleware implements Middleware {
80
84
public nextWindow : number = 0 ;
81
85
public eventCount : number = 0 ;
82
86
87
+ public workspace = {
88
+ // tslint:disable:no-any
89
+ configuration : (
90
+ params : ConfigurationParams ,
91
+ token : CancellationToken ,
92
+ next : ConfigurationRequest . HandlerSignature
93
+ ) : HandlerResult < any [ ] , void > => {
94
+ // Hand-collapse "Thenable<A> | Thenable<B> | Thenable<A|B>" into just "Thenable<A|B>" to make TS happy.
95
+ const result : any [ ] | ResponseError < void > | Thenable < any [ ] | ResponseError < void > > = next ( params , token ) ;
96
+
97
+ // For backwards compatibility, set python.pythonPath to the configured
98
+ // value as though it were in the user's settings.json file.
99
+ const addPythonPath = ( settings : any [ ] | ResponseError < void > ) => {
100
+ if ( settings instanceof ResponseError ) {
101
+ return settings ;
102
+ }
103
+
104
+ params . items . forEach ( ( item , i ) => {
105
+ if ( item . section === 'python' ) {
106
+ const uri = item . scopeUri ? Uri . parse ( item . scopeUri ) : undefined ;
107
+ settings [ i ] . pythonPath = this . configService . getSettings ( uri ) . pythonPath ;
108
+ }
109
+ } ) ;
110
+
111
+ return settings ;
112
+ } ;
113
+
114
+ if ( isThenable ( result ) ) {
115
+ return result . then ( addPythonPath ) ;
116
+ }
117
+
118
+ return addPythonPath ( result ) ;
119
+ }
120
+ // tslint:enable:no-any
121
+ } ;
122
+
83
123
private connected = false ; // Default to not forwarding to VS code.
84
124
85
125
public constructor (
86
126
private readonly surveyBanner : IPythonExtensionBanner ,
87
127
experimentsManager : IExperimentsManager ,
128
+ private readonly configService : IConfigurationService ,
88
129
serverType : LanguageServerType ,
89
130
public readonly serverVersion ?: string
90
131
) {
@@ -413,8 +454,8 @@ function captureTelemetryForLSPMethod(method: string, debounceMilliseconds: numb
413
454
const result = originalMethod . apply ( this , args ) ;
414
455
415
456
// tslint:disable-next-line:no-unsafe-any
416
- if ( result && typeof result . then === 'function' ) {
417
- ( result as Thenable < void > ) . then ( ( ) => {
457
+ if ( result && isThenable < void > ( result ) ) {
458
+ result . then ( ( ) => {
418
459
sendTelemetryEvent ( eventName , stopWatch . elapsedTime , properties ) ;
419
460
} ) ;
420
461
} else {
@@ -427,3 +468,8 @@ function captureTelemetryForLSPMethod(method: string, debounceMilliseconds: numb
427
468
return descriptor ;
428
469
} ;
429
470
}
471
+
472
+ // tslint:disable-next-line: no-any
473
+ function isThenable < T > ( v : any ) : v is Thenable < T > {
474
+ return typeof v ?. then === 'function' ;
475
+ }
0 commit comments