11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4- import { commands , Disposable , window , workspace , WorkspaceConfiguration } from 'vscode' ;
4+ import { inject , injectable } from 'inversify' ;
5+ import { Disposable , WorkspaceConfiguration } from 'vscode' ;
6+ import {
7+ IApplicationShell , ICommandManager , IWorkspaceService
8+ } from './application/types' ;
59import { launch } from './net/browser' ;
6- import { IPersistentStateFactory } from './types' ;
10+ import {
11+ DeprecatedFeatureInfo , DeprecatedSettingAndValue ,
12+ IFeatureDeprecationManager , IPersistentStateFactory
13+ } from './types' ;
714
8- type deprecatedFeatureInfo = {
9- doNotDisplayPromptStateKey : string ;
10- message : string ;
11- moreInfoUrl : string ;
12- commands ?: string [ ] ;
13- setting ?: deprecatedSettingAndValue ;
14- } ;
15-
16- type deprecatedSettingAndValue = {
17- setting : string ;
18- values ?: { } [ ] ;
19- } ;
20-
21- const deprecatedFeatures : deprecatedFeatureInfo [ ] = [
15+ const deprecatedFeatures : DeprecatedFeatureInfo [ ] = [
2216 {
2317 doNotDisplayPromptStateKey : 'SHOW_DEPRECATED_FEATURE_PROMPT_FORMAT_ON_SAVE' ,
2418 message : 'The setting \'python.formatting.formatOnSave\' is deprecated, please use \'editor.formatOnSave\'.' ,
@@ -33,64 +27,43 @@ const deprecatedFeatures: deprecatedFeatureInfo[] = [
3327 }
3428] ;
3529
36- export interface IFeatureDeprecationManager extends Disposable {
37- initialize ( ) : void ;
38- }
39-
30+ @injectable ( )
4031export class FeatureDeprecationManager implements IFeatureDeprecationManager {
4132 private disposables : Disposable [ ] = [ ] ;
42- constructor ( private persistentStateFactory : IPersistentStateFactory , private jupyterExtensionInstalled : boolean ) { }
33+ constructor (
34+ @inject ( IPersistentStateFactory ) private persistentStateFactory : IPersistentStateFactory ,
35+ @inject ( ICommandManager ) private cmdMgr : ICommandManager ,
36+ @inject ( IWorkspaceService ) private workspace : IWorkspaceService ,
37+ @inject ( IApplicationShell ) private appShell : IApplicationShell
38+ ) { }
39+
4340 public dispose ( ) {
4441 this . disposables . forEach ( disposable => disposable . dispose ( ) ) ;
4542 }
43+
4644 public initialize ( ) {
4745 deprecatedFeatures . forEach ( this . registerDeprecation . bind ( this ) ) ;
4846 }
49- private registerDeprecation ( deprecatedInfo : deprecatedFeatureInfo ) {
47+
48+ public registerDeprecation ( deprecatedInfo : DeprecatedFeatureInfo ) : void {
5049 if ( Array . isArray ( deprecatedInfo . commands ) ) {
5150 deprecatedInfo . commands . forEach ( cmd => {
52- this . disposables . push ( commands . registerCommand ( cmd , ( ) => this . notifyDeprecation ( deprecatedInfo ) , this ) ) ;
51+ this . disposables . push ( this . cmdMgr . registerCommand ( cmd , ( ) => this . notifyDeprecation ( deprecatedInfo ) , this ) ) ;
5352 } ) ;
5453 }
5554 if ( deprecatedInfo . setting ) {
5655 this . checkAndNotifyDeprecatedSetting ( deprecatedInfo ) ;
5756 }
5857 }
59- private checkAndNotifyDeprecatedSetting ( deprecatedInfo : deprecatedFeatureInfo ) {
60- let notify = false ;
61- if ( Array . isArray ( workspace . workspaceFolders ) && workspace . workspaceFolders . length > 0 ) {
62- workspace . workspaceFolders . forEach ( workspaceFolder => {
63- if ( notify ) {
64- return ;
65- }
66- notify = this . isDeprecatedSettingAndValueUsed ( workspace . getConfiguration ( 'python' , workspaceFolder . uri ) , deprecatedInfo . setting ! ) ;
67- } ) ;
68- } else {
69- notify = this . isDeprecatedSettingAndValueUsed ( workspace . getConfiguration ( 'python' ) , deprecatedInfo . setting ! ) ;
70- }
7158
72- if ( notify ) {
73- this . notifyDeprecation ( deprecatedInfo )
74- . catch ( ex => console . error ( 'Python Extension: notifyDeprecation' , ex ) ) ;
75- }
76- }
77- private isDeprecatedSettingAndValueUsed ( pythonConfig : WorkspaceConfiguration , deprecatedSetting : deprecatedSettingAndValue ) {
78- if ( ! pythonConfig . has ( deprecatedSetting . setting ) ) {
79- return false ;
80- }
81- if ( ! Array . isArray ( deprecatedSetting . values ) || deprecatedSetting . values . length === 0 ) {
82- return true ;
83- }
84- return deprecatedSetting . values . indexOf ( pythonConfig . get ( deprecatedSetting . setting ) ! ) >= 0 ;
85- }
86- private async notifyDeprecation ( deprecatedInfo : deprecatedFeatureInfo ) {
59+ private async notifyDeprecation ( deprecatedInfo : DeprecatedFeatureInfo ) : Promise < void > {
8760 const notificationPromptEnabled = this . persistentStateFactory . createGlobalPersistentState ( deprecatedInfo . doNotDisplayPromptStateKey , true ) ;
8861 if ( ! notificationPromptEnabled . value ) {
8962 return ;
9063 }
9164 const moreInfo = 'Learn more' ;
9265 const doNotShowAgain = 'Never show again' ;
93- const option = await window . showInformationMessage ( deprecatedInfo . message , moreInfo , doNotShowAgain ) ;
66+ const option = await this . appShell . showInformationMessage ( deprecatedInfo . message , moreInfo , doNotShowAgain ) ;
9467 if ( ! option ) {
9568 return ;
9669 }
@@ -107,5 +80,35 @@ export class FeatureDeprecationManager implements IFeatureDeprecationManager {
10780 throw new Error ( 'Selected option not supported.' ) ;
10881 }
10982 }
83+ return ;
84+ }
85+
86+ private checkAndNotifyDeprecatedSetting ( deprecatedInfo : DeprecatedFeatureInfo ) {
87+ let notify = false ;
88+ if ( Array . isArray ( this . workspace . workspaceFolders ) && this . workspace . workspaceFolders . length > 0 ) {
89+ this . workspace . workspaceFolders . forEach ( workspaceFolder => {
90+ if ( notify ) {
91+ return ;
92+ }
93+ notify = this . isDeprecatedSettingAndValueUsed ( this . workspace . getConfiguration ( 'python' , workspaceFolder . uri ) , deprecatedInfo . setting ! ) ;
94+ } ) ;
95+ } else {
96+ notify = this . isDeprecatedSettingAndValueUsed ( this . workspace . getConfiguration ( 'python' ) , deprecatedInfo . setting ! ) ;
97+ }
98+
99+ if ( notify ) {
100+ this . notifyDeprecation ( deprecatedInfo )
101+ . catch ( ex => console . error ( 'Python Extension: notifyDeprecation' , ex ) ) ;
102+ }
103+ }
104+
105+ private isDeprecatedSettingAndValueUsed ( pythonConfig : WorkspaceConfiguration , deprecatedSetting : DeprecatedSettingAndValue ) {
106+ if ( ! pythonConfig . has ( deprecatedSetting . setting ) ) {
107+ return false ;
108+ }
109+ if ( ! Array . isArray ( deprecatedSetting . values ) || deprecatedSetting . values . length === 0 ) {
110+ return true ;
111+ }
112+ return deprecatedSetting . values . indexOf ( pythonConfig . get ( deprecatedSetting . setting ) ! ) >= 0 ;
110113 }
111114}
0 commit comments