import * as _ from 'lodash' ; import { Connection } from 'vscode-languageserver' ; import { DidChangeConfigurationParams } from 'vscode-languageserver-protocol' ; import { ExtensionSettings } from '../interfaces/extensionSettings' ; export class SettingsManager { private connection : Connection ; private clientSupportsConfigRequests ; private configurationChangeHandlers : Map < string , { ( ) : void ; } > = new Map ( ) ; private documentSettings : Map < string , Thenable < ExtensionSettings > > = new Map ( ) ; private defaultSettings : ExtensionSettings = { ansible : { path : 'ansible' , useFullyQualifiedCollectionNames : true } , ansibleLint : { enabled : true , path : 'ansible-lint' , arguments : '' } , python : { interpreterPath : '' , activationScript : '' } , executionEnvironment : { containerEngine : 'auto' , enabled : false , image : 'quay.io/ansible/creator-ee:latest' , pullPolicy : 'missing' } } ; private globalSettings : ExtensionSettings = this . defaultSettings ; constructor ( connection , clientSupportsConfigRequests ) { this . connection = connection ; this . clientSupportsConfigRequests = clientSupportsConfigRequests ; } /**
     * Register a handler for configuration change on particular URI.
     *
     * Change detection is cache-based. If the client does not support the
     * configuration requests, all handlers will be fired.
     */ public onConfigurationChanged ( uri , handler ) { this . configurationChangeHandlers . set ( uri , handler ) ; } public get ( uri ) { if ( ! this . clientSupportsConfigRequests ) { return Promise . resolve ( this . globalSettings ) ; } let result = this . documentSettings . get ( uri ) ; if ( ! result ) { result = this . connection . workspace . getConfiguration ( { scopeUri : uri , section : 'ansible' , } ) ; this . documentSettings . set ( uri , result ) ; } return result ; } public handleDocumentClosed ( uri ) { this . documentSettings . delete ( uri ) ; } public async handleConfigurationChanged ( params ) { if ( this . clientSupportsConfigRequests ) { const newDocumentSettings = new Map ( ) ; const handlersToFire = [ ] ; for ( const [ uri , handler ] of this . configurationChangeHandlers ) { const config = await this . documentSettings . get ( uri ) ; if ( config ) { const newConfigPromise = this . connection . workspace . getConfiguration ( { scopeUri : uri , section : 'ansible' , } ) ; newDocumentSettings . set ( uri , newConfigPromise ) ; if ( ! _ . isEqual ( config , await newConfigPromise ) ) { handlersToFire . push ( handler ) ; } } } this . documentSettings = newDocumentSettings ; handlersToFire . forEach ( ( h ) => h ( ) ) ; } else { if ( params . settings . ansible ) { this . configurationChangeHandlers . forEach ( ( h ) => h ( ) ) ; } this . globalSettings = params . settings . ansible || this . defaultSettings ; } } }