@@ -122,19 +122,43 @@ internal virtual void OnConnected() { }
122
122
internal virtual void OnAttach ( ) { }
123
123
internal virtual void OnDetach ( ) { }
124
124
125
+ private PythonProjectNode GetAssociatedPythonProject ( InterpreterConfiguration interpreter = null ) {
126
+ var moniker = ProjectMoniker ;
127
+ if ( interpreter == null ) {
128
+ interpreter = Configuration ? . Interpreter ;
129
+ }
130
+
131
+ if ( string . IsNullOrEmpty ( moniker ) && interpreter != null ) {
132
+ var interpreterService = _serviceProvider . GetComponentModel ( ) . GetService < IInterpreterRegistryService > ( ) ;
133
+ moniker = interpreterService . GetProperty ( interpreter . Id , "ProjectMoniker" ) as string ;
134
+ }
135
+
136
+ if ( string . IsNullOrEmpty ( moniker ) ) {
137
+ return null ;
138
+ }
139
+
140
+ return _serviceProvider . GetProjectFromFile ( moniker ) ;
141
+ }
142
+
143
+
125
144
public VsProjectAnalyzer Analyzer {
126
145
get {
127
146
if ( _analyzer != null ) {
128
147
return _analyzer ;
129
148
}
130
149
131
- var interpreterService = _serviceProvider . GetComponentModel ( ) . GetService < IInterpreterRegistryService > ( ) ;
132
150
var config = Configuration ;
133
151
134
152
if ( config == null ) {
135
153
_analyzer = _serviceProvider . GetPythonToolsService ( ) . DefaultAnalyzer ;
136
154
} else {
137
- _analyzer = new VsProjectAnalyzer ( _serviceProvider , interpreterService . FindInterpreter ( config . Interpreter . Id ) ) ;
155
+ var projectFile = GetAssociatedPythonProject ( config . Interpreter ) ? . BuildProject ;
156
+ var interpreterService = _serviceProvider . GetComponentModel ( ) . GetService < IInterpreterRegistryService > ( ) ;
157
+ _analyzer = new VsProjectAnalyzer (
158
+ _serviceProvider ,
159
+ interpreterService . FindInterpreter ( config . Interpreter . Id ) ,
160
+ projectFile : projectFile
161
+ ) ;
138
162
}
139
163
return _analyzer ;
140
164
}
@@ -283,22 +307,20 @@ protected async Task<CommandProcessorThread> EnsureConnectedAsync() {
283
307
}
284
308
285
309
return await _serviceProvider . GetUIThread ( ) . InvokeTask ( async ( ) => {
286
- if ( ! string . IsNullOrEmpty ( ProjectMoniker ) ) {
287
- try {
288
- UpdatePropertiesFromProjectMoniker ( ) ;
289
- } catch ( NoInterpretersException ex ) {
290
- WriteError ( ex . ToString ( ) ) ;
291
- return null ;
292
- } catch ( MissingInterpreterException ex ) {
293
- WriteError ( ex . ToString ( ) ) ;
294
- return null ;
295
- } catch ( DirectoryNotFoundException ex ) {
296
- WriteError ( ex . ToString ( ) ) ;
297
- return null ;
298
- } catch ( Exception ex ) when ( ! ex . IsCriticalException ( ) ) {
299
- WriteError ( ex . ToUnhandledExceptionMessage ( GetType ( ) ) ) ;
300
- return null ;
301
- }
310
+ try {
311
+ UpdatePropertiesFromProjectMoniker ( ) ;
312
+ } catch ( NoInterpretersException ex ) {
313
+ WriteError ( ex . ToString ( ) ) ;
314
+ return null ;
315
+ } catch ( MissingInterpreterException ex ) {
316
+ WriteError ( ex . ToString ( ) ) ;
317
+ return null ;
318
+ } catch ( DirectoryNotFoundException ex ) {
319
+ WriteError ( ex . ToString ( ) ) ;
320
+ return null ;
321
+ } catch ( Exception ex ) when ( ! ex . IsCriticalException ( ) ) {
322
+ WriteError ( ex . ToUnhandledExceptionMessage ( GetType ( ) ) ) ;
323
+ return null ;
302
324
}
303
325
304
326
var scriptsPath = ScriptsPath ;
@@ -351,17 +373,7 @@ protected virtual async Task ExecuteStartupScripts(string scriptsPath) {
351
373
}
352
374
353
375
internal void UpdatePropertiesFromProjectMoniker ( ) {
354
- var solution = _serviceProvider . GetService ( typeof ( SVsSolution ) ) as IVsSolution ;
355
- if ( solution == null ) {
356
- return ;
357
- }
358
-
359
- IVsHierarchy hier ;
360
- if ( string . IsNullOrEmpty ( ProjectMoniker ) ||
361
- ErrorHandler . Failed ( solution . GetProjectOfUniqueName ( ProjectMoniker , out hier ) ) ) {
362
- return ;
363
- }
364
- var pyProj = hier ? . GetProject ( ) ? . GetPythonProject ( ) ;
376
+ var pyProj = GetAssociatedPythonProject ( ) ;
365
377
if ( pyProj == null ) {
366
378
return ;
367
379
}
0 commit comments