@@ -9,12 +9,13 @@ import URI from 'vs/base/common/uri';
9
9
import Event , { Emitter } from 'vs/base/common/event' ;
10
10
import * as vscode from 'vscode' ;
11
11
import { ExtHostWorkspace } from 'vs/workbench/api/node/extHostWorkspace' ;
12
- import { ExtHostConfigurationShape , MainThreadConfigurationShape , IWorkspaceConfigurationChangeEventData } from './extHost.protocol' ;
12
+ import { ExtHostConfigurationShape , MainThreadConfigurationShape , IWorkspaceConfigurationChangeEventData , IConfigurationInitData } from './extHost.protocol' ;
13
13
import { ConfigurationTarget as ExtHostConfigurationTarget } from './extHostTypes' ;
14
14
import { IConfigurationData , ConfigurationTarget } from 'vs/platform/configuration/common/configuration' ;
15
15
import { Configuration , ConfigurationModel , ConfigurationChangeEvent } from 'vs/platform/configuration/common/configurationModels' ;
16
16
import { WorkspaceConfigurationChangeEvent } from 'vs/workbench/services/configuration/common/configurationModels' ;
17
17
import { StrictResourceMap } from 'vs/base/common/map' ;
18
+ import { ConfigurationScope } from 'vs/platform/configuration/common/configurationRegistry' ;
18
19
19
20
function lookUp ( tree : any , key : string ) {
20
21
if ( key ) {
@@ -40,12 +41,14 @@ export class ExtHostConfiguration implements ExtHostConfigurationShape {
40
41
private readonly _onDidChangeConfiguration = new Emitter < vscode . ConfigurationChangeEvent > ( ) ;
41
42
private readonly _proxy : MainThreadConfigurationShape ;
42
43
private readonly _extHostWorkspace : ExtHostWorkspace ;
44
+ private _configurationScopes : Map < string , ConfigurationScope > ;
43
45
private _configuration : Configuration ;
44
46
45
- constructor ( proxy : MainThreadConfigurationShape , extHostWorkspace : ExtHostWorkspace , data : IConfigurationData ) {
47
+ constructor ( proxy : MainThreadConfigurationShape , extHostWorkspace : ExtHostWorkspace , data : IConfigurationInitData ) {
46
48
this . _proxy = proxy ;
47
49
this . _extHostWorkspace = extHostWorkspace ;
48
50
this . _configuration = Configuration . parse ( data ) ;
51
+ this . _readConfigurationScopes ( data . configurationScopes ) ;
49
52
}
50
53
51
54
get onDidChangeConfiguration ( ) : Event < vscode . ConfigurationChangeEvent > {
@@ -54,14 +57,18 @@ export class ExtHostConfiguration implements ExtHostConfigurationShape {
54
57
55
58
$acceptConfigurationChanged ( data : IConfigurationData , eventData : IWorkspaceConfigurationChangeEventData ) {
56
59
this . _configuration = Configuration . parse ( data ) ;
57
- this . _onDidChangeConfiguration . fire ( this . toConfigurationChangeEvent ( eventData ) ) ;
60
+ this . _onDidChangeConfiguration . fire ( this . _toConfigurationChangeEvent ( eventData ) ) ;
58
61
}
59
62
60
- getConfiguration ( section ?: string , resource ?: URI ) : vscode . WorkspaceConfiguration {
63
+ getConfiguration ( section ?: string , resource ?: URI , extensionId ?: string ) : vscode . WorkspaceConfiguration {
61
64
const config = section
62
65
? lookUp ( this . _configuration . getSection ( null , { resource } , this . _extHostWorkspace . workspace ) , section )
63
66
: this . _configuration . getSection ( null , { resource } , this . _extHostWorkspace . workspace ) ;
64
67
68
+ if ( section ) {
69
+ this . _validateConfigurationAccess ( section , resource , extensionId ) ;
70
+ }
71
+
65
72
function parseConfigurationTarget ( arg : boolean | ExtHostConfigurationTarget ) : ConfigurationTarget {
66
73
if ( arg === void 0 || arg === null ) {
67
74
return null ;
@@ -81,7 +88,8 @@ export class ExtHostConfiguration implements ExtHostConfigurationShape {
81
88
has ( key : string ) : boolean {
82
89
return typeof lookUp ( config , key ) !== 'undefined' ;
83
90
} ,
84
- get < T > ( key : string , defaultValue ?: T ) : T {
91
+ get : < T > ( key : string , defaultValue ?: T ) => {
92
+ this . _validateConfigurationAccess ( section ? `${ section } .${ key } ` : key , resource , extensionId ) ;
85
93
let result = lookUp ( config , key ) ;
86
94
if ( typeof result === 'undefined' ) {
87
95
result = defaultValue ;
@@ -90,6 +98,7 @@ export class ExtHostConfiguration implements ExtHostConfigurationShape {
90
98
} ,
91
99
update : ( key : string , value : any , arg : ExtHostConfigurationTarget | boolean ) => {
92
100
key = section ? `${ section } .${ key } ` : key ;
101
+ this . _validateConfigurationAccess ( key , resource , extensionId ) ;
93
102
const target = parseConfigurationTarget ( arg ) ;
94
103
if ( value !== void 0 ) {
95
104
return this . _proxy . $updateConfigurationOption ( target , key , value , resource ) ;
@@ -120,7 +129,36 @@ export class ExtHostConfiguration implements ExtHostConfigurationShape {
120
129
return < vscode . WorkspaceConfiguration > Object . freeze ( result ) ;
121
130
}
122
131
123
- private toConfigurationChangeEvent ( data : IWorkspaceConfigurationChangeEventData ) : vscode . ConfigurationChangeEvent {
132
+ private _validateConfigurationAccess ( key : string , resource : URI , extensionId : string ) : void {
133
+ const scope = this . _configurationScopes . get ( key ) ;
134
+ const extensionIdText = extensionId ? `[${ extensionId } ] ` : '' ;
135
+ if ( ConfigurationScope . RESOURCE === scope ) {
136
+ if ( ! resource ) {
137
+ console . warn ( `${ extensionIdText } Accessing a resource scoped configuration without providing a resource is not expected. To get the effective value for '${ key } ', provide the resource for which the value is needed. If you would like to look up all values, use 'inspect' method instead.` ) ;
138
+ }
139
+ return ;
140
+ }
141
+ if ( ConfigurationScope . WINDOW === scope ) {
142
+ if ( resource ) {
143
+ console . warn ( `${ extensionIdText } Accessing a window scoped configuration for a resource is not expected. To associate '${ key } ' to a resource, define its scope to 'resource' in configuration contributions in 'package.json'.` ) ;
144
+ }
145
+ return ;
146
+ }
147
+ }
148
+
149
+ private _readConfigurationScopes ( scopes : ConfigurationScope [ ] ) : void {
150
+ this . _configurationScopes = new Map < string , ConfigurationScope > ( ) ;
151
+ if ( scopes . length ) {
152
+ const defaultKeys = this . _configuration . keys ( this . _extHostWorkspace . workspace ) . default ;
153
+ if ( defaultKeys . length === scopes . length ) {
154
+ for ( let i = 0 ; i < defaultKeys . length ; i ++ ) {
155
+ this . _configurationScopes . set ( defaultKeys [ i ] , scopes [ i ] ) ;
156
+ }
157
+ }
158
+ }
159
+ }
160
+
161
+ private _toConfigurationChangeEvent ( data : IWorkspaceConfigurationChangeEventData ) : vscode . ConfigurationChangeEvent {
124
162
const changedConfiguration = new ConfigurationModel ( data . changedConfiguration . contents , data . changedConfiguration . keys , data . changedConfiguration . overrides ) ;
125
163
const changedConfigurationByResource : StrictResourceMap < ConfigurationModel > = new StrictResourceMap < ConfigurationModel > ( ) ;
126
164
for ( const key of Object . keys ( data . changedConfigurationByResource ) ) {
0 commit comments