@@ -7,40 +7,49 @@ import { Registry } from 'vs/platform/registry/common/platform';
77import { IConfigurationRegistry , Extensions } from 'vs/platform/configuration/common/configurationRegistry' ;
88import { IDisposable , Disposable } from 'vs/base/common/lifecycle' ;
99import { IConfigurationService , IConfigurationChangeEvent , IConfigurationOverrides , ConfigurationTarget , compare , isConfigurationOverrides , IConfigurationData } from 'vs/platform/configuration/common/configuration' ;
10- import { DefaultConfigurationModel , Configuration , ConfigurationChangeEvent , ConfigurationModel } from 'vs/platform/configuration/common/configurationModels' ;
10+ import { DefaultConfigurationModel , Configuration , ConfigurationChangeEvent , ConfigurationModel , ConfigurationModelParser } from 'vs/platform/configuration/common/configurationModels' ;
1111import { Event , Emitter } from 'vs/base/common/event' ;
1212import { IWorkspaceFolder } from 'vs/platform/workspace/common/workspace' ;
13- import { NodeBasedUserConfiguration } from 'vs/platform/configuration/node/configuration' ;
13+ import { ConfigWatcher } from 'vs/base/node/config' ;
14+ import { onUnexpectedError } from 'vs/base/common/errors' ;
1415
1516export class ConfigurationService extends Disposable implements IConfigurationService , IDisposable {
1617
1718 _serviceBrand : any ;
1819
19- private _configuration : Configuration ;
20- private userConfiguration : NodeBasedUserConfiguration ;
20+ private configuration : Configuration ;
21+ private userConfigModelWatcher : ConfigWatcher < ConfigurationModelParser > | undefined ;
2122
2223 private readonly _onDidChangeConfiguration : Emitter < IConfigurationChangeEvent > = this . _register ( new Emitter < IConfigurationChangeEvent > ( ) ) ;
2324 readonly onDidChangeConfiguration : Event < IConfigurationChangeEvent > = this . _onDidChangeConfiguration . event ;
2425
2526 constructor (
26- configurationPath : string
27+ private readonly configurationPath : string
2728 ) {
2829 super ( ) ;
29-
30- this . userConfiguration = this . _register ( new NodeBasedUserConfiguration ( configurationPath ) ) ;
31-
32- // Initialize
33- const defaults = new DefaultConfigurationModel ( ) ;
34- const user = this . userConfiguration . initializeSync ( ) ;
35- this . _configuration = new Configuration ( defaults , user ) ;
36-
37- // Listeners
38- this . _register ( this . userConfiguration . onDidChangeConfiguration ( userConfigurationModel => this . onDidChangeUserConfiguration ( userConfigurationModel ) ) ) ;
30+ this . configuration = new Configuration ( new DefaultConfigurationModel ( ) , new ConfigurationModel ( ) ) ;
3931 this . _register ( Registry . as < IConfigurationRegistry > ( Extensions . Configuration ) . onDidUpdateConfiguration ( configurationProperties => this . onDidDefaultConfigurationChange ( configurationProperties ) ) ) ;
4032 }
4133
42- get configuration ( ) : Configuration {
43- return this . _configuration ;
34+ initialize ( ) : Promise < void > {
35+ if ( this . userConfigModelWatcher ) {
36+ this . userConfigModelWatcher . dispose ( ) ;
37+ }
38+
39+ return new Promise < void > ( ( c , e ) => {
40+ this . userConfigModelWatcher = this . _register ( new ConfigWatcher ( this . configurationPath , {
41+ changeBufferDelay : 300 , onError : error => onUnexpectedError ( error ) , defaultConfig : new ConfigurationModelParser ( this . configurationPath ) , parse : ( content : string , parseErrors : any [ ] ) => {
42+ const userConfigModelParser = new ConfigurationModelParser ( this . configurationPath ) ;
43+ userConfigModelParser . parseContent ( content ) ;
44+ parseErrors = [ ...userConfigModelParser . errors ] ;
45+ return userConfigModelParser ;
46+ } , initCallback : ( ) => {
47+ this . configuration = new Configuration ( new DefaultConfigurationModel ( ) , this . userConfigModelWatcher ! . getConfig ( ) . configurationModel ) ;
48+ this . _register ( this . userConfigModelWatcher ! . onDidUpdateConfiguration ( ( ) => this . onDidChangeUserConfiguration ( this . userConfigModelWatcher ! . getConfig ( ) . configurationModel ) ) ) ;
49+ c ( ) ;
50+ }
51+ } ) ) ;
52+ } ) ;
4453 }
4554
4655 getConfigurationData ( ) : IConfigurationData {
@@ -85,21 +94,26 @@ export class ConfigurationService extends Disposable implements IConfigurationSe
8594 }
8695
8796 reloadConfiguration ( folder ?: IWorkspaceFolder ) : Promise < void > {
88- return folder ? Promise . resolve ( undefined ) :
89- this . userConfiguration . reload ( ) . then ( userConfigurationModel => this . onDidChangeUserConfiguration ( userConfigurationModel ) ) ;
97+ if ( this . userConfigModelWatcher ) {
98+ return new Promise < void > ( c => this . userConfigModelWatcher ! . reload ( userConfigModelParser => {
99+ this . onDidChangeUserConfiguration ( userConfigModelParser . configurationModel ) ;
100+ c ( ) ;
101+ } ) ) ;
102+ }
103+ return this . initialize ( ) ;
90104 }
91105
92106 private onDidChangeUserConfiguration ( userConfigurationModel : ConfigurationModel ) : void {
93- const { added, updated, removed } = compare ( this . _configuration . localUserConfiguration , userConfigurationModel ) ;
107+ const { added, updated, removed } = compare ( this . configuration . localUserConfiguration , userConfigurationModel ) ;
94108 const changedKeys = [ ...added , ...updated , ...removed ] ;
95109 if ( changedKeys . length ) {
96- this . _configuration . updateLocalUserConfiguration ( userConfigurationModel ) ;
110+ this . configuration . updateLocalUserConfiguration ( userConfigurationModel ) ;
97111 this . trigger ( changedKeys , ConfigurationTarget . USER ) ;
98112 }
99113 }
100114
101115 private onDidDefaultConfigurationChange ( keys : string [ ] ) : void {
102- this . _configuration . updateDefaultConfiguration ( new DefaultConfigurationModel ( ) ) ;
116+ this . configuration . updateDefaultConfiguration ( new DefaultConfigurationModel ( ) ) ;
103117 this . trigger ( keys , ConfigurationTarget . DEFAULT ) ;
104118 }
105119
@@ -110,9 +124,9 @@ export class ConfigurationService extends Disposable implements IConfigurationSe
110124 private getTargetConfiguration ( target : ConfigurationTarget ) : any {
111125 switch ( target ) {
112126 case ConfigurationTarget . DEFAULT :
113- return this . _configuration . defaults . contents ;
127+ return this . configuration . defaults . contents ;
114128 case ConfigurationTarget . USER :
115- return this . _configuration . localUserConfiguration . contents ;
129+ return this . configuration . localUserConfiguration . contents ;
116130 }
117131 return { } ;
118132 }
0 commit comments