'use strict' ; export * from './config' ; import { ConfigurationChangeEvent , ConfigurationTarget , Event , EventEmitter , ExtensionContext , Uri , workspace } from 'vscode' ; import { Config } from './config' ; import { extensionId } from './constants' ; import { Functions } from './system' ; const emptyConfig = new Proxy < Config > ( { } as Config , { get : function ( ) { return emptyConfig ; } } ) ; export interface ConfigurationWillChangeEvent { change : ConfigurationChangeEvent ; transform ? ( e ) : ConfigurationChangeEvent ; } export class Configuration { static configure ( context ) { context . subscriptions . push ( workspace . onDidChangeConfiguration ( configuration . onConfigurationChanged , configuration ) ) ; } private _onDidChange = new EventEmitter < ConfigurationChangeEvent > ( ) ; get onDidChange ( ) : Event < ConfigurationChangeEvent > { return this . _onDidChange . event ; } private _onDidChangeAny = new EventEmitter < ConfigurationChangeEvent > ( ) ; get onDidChangeAny ( ) : Event < ConfigurationChangeEvent > { return this . _onDidChange . event ; } private _onWillChange = new EventEmitter < ConfigurationWillChangeEvent > ( ) ; get onWillChange ( ) : Event < ConfigurationWillChangeEvent > { return this . _onWillChange . event ; } private onConfigurationChanged ( e ) { if ( ! e . affectsConfiguration ( extensionId , null ! ) ) { this . _onDidChangeAny . fire ( e ) ; return ; } const evt = { change : e } ; this . _onWillChange . fire ( evt ) ; if ( evt . transform !== undefined ) { e = evt . transform ( e ) ; } this . _onDidChange . fire ( e ) ; } readonly initializingChangeEvent : ConfigurationChangeEvent = { affectsConfiguration : ( section , resource ? ) => true } ; get < T > ( section ? , resource ? , defaultValue ? ) { return defaultValue === undefined ? workspace . getConfiguration ( section === undefined ? undefined : extensionId , resource ! ) . get < T > ( section === undefined ? extensionId : section ) ! : workspace . getConfiguration ( section === undefined ? undefined : extensionId , resource ! ) . get < T > ( section === undefined ? extensionId : section , defaultValue ) ! ; } getAny < T > ( section , resource ? , defaultValue ? ) { return defaultValue === undefined ? workspace . getConfiguration ( undefined , resource ! ) . get < T > ( section ) ! : workspace . getConfiguration ( undefined , resource ! ) . get < T > ( section , defaultValue ) ! ; } changed ( e , section , resource ? ) { return e . affectsConfiguration ( `${ extensionId }.${ section }` , resource ! ) ; } initializing ( e ) { return e === this . initializingChangeEvent ; } inspect ( section ? , resource ? ) { return workspace . getConfiguration ( section === undefined ? undefined : extensionId , resource ! ) . inspect ( section === undefined ? extensionId : section ) ; } inspectAny ( section , resource ? ) { return workspace . getConfiguration ( undefined , resource ! ) . inspect ( section ) ; } async migrate < TFrom , TTo > ( from , to , options = { } ) { const inspection = configuration . inspect ( from ) ; if ( inspection === undefined ) return false ; let migrated = false ; if ( inspection . globalValue !== undefined ) { await this . update ( to , options . migrationFn ? options . migrationFn ( inspection . globalValue as TFrom ) : inspection . globalValue , ConfigurationTarget . Global ) ; migrated = true ; } if ( inspection . workspaceValue !== undefined ) { await this . update ( to , options . migrationFn ? options . migrationFn ( inspection . workspaceValue as TFrom ) : inspection . workspaceValue , ConfigurationTarget . Workspace ) ; migrated = true ; } if ( inspection . workspaceFolderValue !== undefined ) { await this . update ( to , options . migrationFn ? options . migrationFn ( inspection . workspaceFolderValue as TFrom ) : inspection . workspaceFolderValue , ConfigurationTarget . WorkspaceFolder ) ; migrated = true ; } if ( ! migrated && options . fallbackValue !== undefined ) { await this . update ( to , options . fallbackValue , ConfigurationTarget . Global ) ; migrated = true ; } return migrated ; } async migrateIfMissing < TFrom , TTo > ( from , to , options = { } ) { const fromInspection = configuration . inspect ( from ) ; if ( fromInspection === undefined ) return ; const toInspection = configuration . inspect ( to ) ; if ( fromInspection . globalValue !== undefined ) { if ( toInspection === undefined || toInspection . globalValue === undefined ) { await this . update ( to , options . migrationFn ? options . migrationFn ( fromInspection . globalValue as TFrom ) : fromInspection . globalValue , ConfigurationTarget . Global ) ; } } if ( fromInspection . workspaceValue !== undefined ) { if ( toInspection === undefined || toInspection . workspaceValue === undefined ) { await this . update ( to , options . migrationFn ? options . migrationFn ( fromInspection . workspaceValue as TFrom ) : fromInspection . workspaceValue , ConfigurationTarget . Workspace ) ; } } if ( fromInspection . workspaceFolderValue !== undefined ) { if ( toInspection === undefined || toInspection . workspaceFolderValue === undefined ) { await this . update ( to , options . migrationFn ? options . migrationFn ( fromInspection . workspaceFolderValue as TFrom ) : fromInspection . workspaceFolderValue , ConfigurationTarget . WorkspaceFolder ) ; } } } name < K extends keyof Config > ( name ) { return Functions . propOf ( emptyConfig as Config , name ) ; } update ( section , value , target , resource ? ) { return workspace . getConfiguration ( extensionId , target === ConfigurationTarget . Global ? undefined : resource ! ) . update ( section , value , target ) ; } updateAny ( section , value , target , resource ? ) { return workspace . getConfiguration ( undefined , target === ConfigurationTarget . Global ? undefined : resource ! ) . update ( section , value , target ) ; } async updateEffective ( section , value , resource = null ) { const inspect = await configuration . inspect ( section , resource ) ! ; if ( inspect . workspaceFolderValue !== undefined ) { if ( value === inspect . workspaceFolderValue ) return undefined ; return void configuration . update ( section , value , ConfigurationTarget . WorkspaceFolder , resource ) ; } if ( inspect . workspaceValue !== undefined ) { if ( value === inspect . workspaceValue ) return undefined ; return void configuration . update ( section , value , ConfigurationTarget . Workspace ) ; } if ( inspect . globalValue === value || ( inspect . globalValue === undefined && value === inspect . defaultValue ) ) { return undefined ; } return void configuration . update ( section , value === inspect . defaultValue ? undefined : value , ConfigurationTarget . Global ) ; } } export const configuration = new Configuration ( ) ;