-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathconfiguration.ts
More file actions
164 lines (137 loc) · 5.71 KB
/
configuration.ts
File metadata and controls
164 lines (137 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/********************************************************************************
* Copyright (C) 2019 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { WorkspaceExtImpl } from '../workspace';
import { isObject } from '../../common/types';
import cloneDeep = require('lodash.clonedeep');
import { URI } from 'vscode-uri';
/* eslint-disable @typescript-eslint/no-explicit-any */
export class Configuration {
private combinedConfig: ConfigurationModel | undefined;
private folderCombinedConfigs: { [resource: string]: ConfigurationModel } = {};
constructor(
private defaultConfiguration: ConfigurationModel,
private userConfiguration: ConfigurationModel,
private workspaceConfiguration: ConfigurationModel = new ConfigurationModel(),
private folderConfigurations: { [resource: string]: ConfigurationModel } = {},
) { }
getValue(section: string | undefined, workspace: WorkspaceExtImpl, resource?: URI): any {
return this.getCombinedResourceConfig(workspace, resource).getValue(section);
}
inspect<C>(key: string, workspace: WorkspaceExtImpl, resource?: URI): {
default: C,
user: C,
workspace: C | undefined,
workspaceFolder: C | undefined,
value: C,
} {
const combinedConfiguration = this.getCombinedResourceConfig(workspace, resource);
const folderConfiguration = this.getFolderResourceConfig(workspace, resource);
return {
default: this.defaultConfiguration.getValue(key),
user: this.userConfiguration.getValue(key),
workspace: workspace ? this.workspaceConfiguration.getValue(key) : undefined,
workspaceFolder: folderConfiguration ? folderConfiguration.getValue(key) : undefined,
value: combinedConfiguration.getValue(key)
};
}
private getCombinedResourceConfig(workspace: WorkspaceExtImpl, resource?: URI): ConfigurationModel {
const combinedConfig = this.getCombinedConfig();
if (!workspace || !resource) {
return combinedConfig;
}
const workspaceFolder = workspace.getWorkspaceFolder(resource);
if (!workspaceFolder) {
return combinedConfig;
}
return this.getFolderCombinedConfig(workspaceFolder.uri.toString()) || combinedConfig;
}
private getCombinedConfig(): ConfigurationModel {
if (!this.combinedConfig) {
this.combinedConfig = this.defaultConfiguration.merge(this.userConfiguration, this.workspaceConfiguration);
}
return this.combinedConfig;
}
private getFolderCombinedConfig(folder: string): ConfigurationModel | undefined {
if (this.folderCombinedConfigs[folder]) {
return this.folderCombinedConfigs[folder];
}
const combinedConfig = this.getCombinedConfig();
const folderConfig = this.folderConfigurations[folder];
if (!folderConfig) {
return combinedConfig;
}
const folderCombinedConfig = combinedConfig.merge(folderConfig);
this.folderCombinedConfigs[folder] = folderCombinedConfig;
return folderCombinedConfig;
}
private getFolderResourceConfig(workspace: WorkspaceExtImpl, resource?: URI): ConfigurationModel | undefined {
if (!workspace || !resource) {
return;
}
const workspaceFolder = workspace.getWorkspaceFolder(resource);
if (!workspaceFolder) {
return;
}
return this.folderConfigurations[workspaceFolder.uri.toString()];
}
}
export class ConfigurationModel {
constructor(
private contents: any = {},
private keys: string[] = [],
) { }
getValue(section?: string): any {
if (!section) {
return this.contents;
}
const path = section.split('.');
let current = this.contents;
for (let i = 0; i < path.length; i++) {
if (typeof current !== 'object' || current === null) {
return undefined;
}
current = current[path[i]];
}
return current;
}
merge(...others: ConfigurationModel[]): ConfigurationModel {
const contents = cloneDeep(this.contents);
const allKeys = [...this.keys];
for (const other of others) {
this.mergeContents(contents, other.contents);
this.mergeKeys(allKeys, other.keys);
}
return new ConfigurationModel(contents, allKeys);
}
private mergeContents(source: any, target: any): void {
for (const key of Object.keys(target)) {
if (key in source) {
if (isObject(source[key]) && isObject(target[key])) {
this.mergeContents(source[key], target[key]);
continue;
}
}
source[key] = cloneDeep(target[key]);
}
}
private mergeKeys(source: string[], target: string[]): void {
for (const key of target) {
if (source.indexOf(key) === -1) {
source.push(key);
}
}
}
}