forked from microsoft/vscode-cpptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettingsTracker.ts
More file actions
225 lines (208 loc) · 10.3 KB
/
settingsTracker.ts
File metadata and controls
225 lines (208 loc) · 10.3 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import * as vscode from 'vscode';
import * as util from '../common';
/**
* track settings changes for telemetry
*/
type FilterFunction = (key: string, val: string, settings: vscode.WorkspaceConfiguration) => boolean;
type KeyValuePair = { key: string; value: string };
const maxSettingLengthForTelemetry: number = 50;
export class SettingsTracker {
private previousCppSettings: { [key: string]: any } = {};
private resource: vscode.Uri | undefined;
constructor(resource: vscode.Uri | undefined) {
this.resource = resource;
this.collectSettings(() => true);
}
public getUserModifiedSettings(): { [key: string]: string } {
const filter: FilterFunction = (key: string, val: string, settings: vscode.WorkspaceConfiguration) => !this.areEqual(val, settings.inspect(key)?.defaultValue);
return this.collectSettings(filter);
}
public getChangedSettings(): { [key: string]: string } {
const filter: FilterFunction = (key: string, val: string) => !(key in this.previousCppSettings) || !this.areEqual(val, this.previousCppSettings[key]);
return this.collectSettings(filter);
}
private collectSettings(filter: FilterFunction): { [key: string]: string } {
const settingsResourceScope: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("C_Cpp", this.resource);
const settingsNonScoped: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("C_Cpp");
const selectCorrectlyScopedSettings = (rawSetting: any): vscode.WorkspaceConfiguration =>
(!rawSetting || rawSetting.scope === "resource" || rawSetting.scope === "machine-overridable") ? settingsResourceScope : settingsNonScoped;
const result: { [key: string]: string } = {};
for (const key in settingsResourceScope) {
const rawSetting: any = util.getRawSetting("C_Cpp." + key);
const correctlyScopedSettings: vscode.WorkspaceConfiguration = selectCorrectlyScopedSettings(rawSetting);
const val: any = this.getSetting(correctlyScopedSettings, key);
if (val === undefined) {
continue;
}
// Iterate through dotted "sub" settings.
const collectSettingsRecursive = (key: string, val: Record<string, any>, depth: number) => {
if (depth > 4) {
// Limit settings recursion to 4 dots (not counting the first one in: `C_Cpp.`)
return;
}
for (const subKey in val) {
const newKey: string = key + "." + subKey;
const newRawSetting: any = util.getRawSetting("C_Cpp." + newKey);
const correctlyScopedSubSettings: vscode.WorkspaceConfiguration = selectCorrectlyScopedSettings(newRawSetting);
const subVal: any = this.getSetting(correctlyScopedSubSettings, newKey);
if (subVal === undefined) {
continue;
}
if (newRawSetting === undefined && subVal instanceof Object) {
collectSettingsRecursive(newKey, subVal, depth + 1);
} else {
const entry: KeyValuePair | undefined = this.filterAndSanitize(newKey, subVal, correctlyScopedSubSettings, filter);
if (entry?.key && entry.value !== undefined) {
result[entry.key] = entry.value;
}
}
}
};
if (rawSetting === undefined && val instanceof Object) {
collectSettingsRecursive(key, val, 1);
continue;
}
const entry: KeyValuePair | undefined = this.filterAndSanitize(key, val, correctlyScopedSettings, filter);
if (entry && entry.key && entry.value) {
result[entry.key] = entry.value;
}
}
return result;
}
private getSetting(settings: vscode.WorkspaceConfiguration, key: string): any {
// Ignore methods and settings that don't exist
if (settings.inspect(key)?.defaultValue !== undefined) {
const val: any = settings.get(key);
if (val instanceof Object) {
return val; // It's a sub-section.
}
// Only return values that match the setting's type and enum (if applicable).
const curSetting: any = util.getRawSetting("C_Cpp." + key);
if (curSetting) {
const type: string | undefined = this.typeMatch(val, curSetting["type"]);
if (type) {
if (type !== "string") {
return val;
}
const curEnum: any[] = curSetting["enum"];
if (curEnum && curEnum.indexOf(val) === -1
&& (key !== "loggingLevel" || util.getNumericLoggingLevel(val) === -1)) {
return "<invalid>";
}
return val;
} else if (curSetting["oneOf"]) {
// Currently only C_Cpp.default.compileCommands uses this case.
if (curSetting["oneOf"].some((x: any) => this.typeMatch(val, x.type))) {
return val;
}
} else if (val === curSetting["default"]) {
// C_Cpp.default.browse.path is a special case where the default value is null and doesn't match the type definition.
return val;
}
}
}
return undefined;
}
private typeMatch(value: any, type?: string | string[]): string | undefined {
if (type) {
if (type instanceof Array) {
for (let i: number = 0; i < type.length; i++) {
const t: string = type[i];
if (t) {
if (typeof value === t) {
return t;
}
if (t === "integer" && typeof value === "number") {
return "number";
}
if (t === "array" && value instanceof Array) {
return t;
}
if (t === "null" && value === null) {
return t;
}
}
}
} else if (typeof type === "string") {
if (typeof value === type) {
return type;
}
if (type === "integer" && typeof value === "number") {
return "number";
}
}
}
return undefined;
}
private filterAndSanitize(key: string, val: any, settings: vscode.WorkspaceConfiguration, filter: FilterFunction): KeyValuePair | undefined {
if (filter(key, val, settings)) {
let value: string;
this.previousCppSettings[key] = val;
switch (key) {
case "clang_format_style":
case "clang_format_fallbackStyle": {
const newKey: string = key + "2";
if (val) {
switch (String(val).toLowerCase()) {
case "emulated visual studio":
case "visual studio":
case "llvm":
case "google":
case "chromium":
case "mozilla":
case "webkit":
case "file":
case "none": {
value = String(this.previousCppSettings[key]);
break;
}
default: {
value = "...";
break;
}
}
} else {
value = "null";
}
key = newKey;
break;
}
case "commentContinuationPatterns": {
key = "commentContinuationPatterns2";
value = this.areEqual(val, settings.inspect(key)?.defaultValue) ? "<default>" : "..."; // Track whether it's being used, but nothing specific about it.
break;
}
default: {
if (key === "clang_format_path" || key === "intelliSenseCachePath" || key.startsWith("default.")
|| key === "codeAnalysis.clangTidy.path"
|| key === "codeAnalysis.clangTidy.headerFilter" || key === "codeAnalysis.clangTidy.args"
|| key === "codeAnalysis.clangTidy.config" || key === "codeAnalysis.clangTidy.fallbackConfig"
// Note: An existing bug prevents these settings of type "object" from getting processed here,
// so these checks are here just in case that bug gets fixed later on.
|| key === "files.exclude" || key === "codeAnalysis.exclude"
) {
value = this.areEqual(val, settings.inspect(key)?.defaultValue) ? "<default>" : "..."; // Track whether it's being used, but nothing specific about it.
} else {
value = String(this.previousCppSettings[key]);
}
}
}
if (value && value.length > maxSettingLengthForTelemetry) {
value = value.substring(0, maxSettingLengthForTelemetry) + "...";
}
return { key: key, value: value };
}
return undefined;
}
private areEqual(value1: any, value2: any): boolean {
if (value1 instanceof Object && value2 instanceof Object) {
return JSON.stringify(value1) === JSON.stringify(value2);
}
return value1 === value2;
}
}