|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. |
| 3 | + * See 'LICENSE' in the project root for license information. |
| 4 | + * ------------------------------------------------------------------------------------------ */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +import * as vscode from 'vscode'; |
| 8 | +import * as util from '../common'; |
| 9 | + |
| 10 | +/** |
| 11 | + * track settings changes for telemetry |
| 12 | + */ |
| 13 | +type FilterFunction = (key: string, val: string, settings: vscode.WorkspaceConfiguration) => boolean; |
| 14 | +type KeyValuePair = { key: string; value: string }; |
| 15 | + |
| 16 | +const maxSettingLengthForTelemetry: number = 50; |
| 17 | +let cache: SettingsTracker = undefined; |
| 18 | + |
| 19 | +export class SettingsTracker { |
| 20 | + private previousCppSettings: { [key: string]: any } = {}; |
| 21 | + private resource: vscode.Uri; |
| 22 | + |
| 23 | + constructor(resource: vscode.Uri) { |
| 24 | + this.resource = resource; |
| 25 | + this.collectSettings(() => true); |
| 26 | + } |
| 27 | + |
| 28 | + public getUserModifiedSettings(): { [key: string]: string } { |
| 29 | + let filter: FilterFunction = (key: string, val: string, settings: vscode.WorkspaceConfiguration) => { |
| 30 | + return !this.areEqual(val, settings.inspect(key).defaultValue); |
| 31 | + }; |
| 32 | + return this.collectSettings(filter); |
| 33 | + } |
| 34 | + |
| 35 | + public getChangedSettings(): { [key: string]: string } { |
| 36 | + let filter: FilterFunction = (key: string, val: string) => { |
| 37 | + return !(key in this.previousCppSettings) || !this.areEqual(val, this.previousCppSettings[key]); |
| 38 | + }; |
| 39 | + return this.collectSettings(filter); |
| 40 | + } |
| 41 | + |
| 42 | + private collectSettings(filter: FilterFunction): { [key: string]: string } { |
| 43 | + let settings: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("C_Cpp", this.resource); |
| 44 | + let result: { [key: string]: string } = {}; |
| 45 | + |
| 46 | + for (let key in settings) { |
| 47 | + let val: any = this.getSetting(settings, key); |
| 48 | + if (val === undefined) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + if (val instanceof Object && !(val instanceof Array)) { |
| 52 | + for (let subKey in val) { |
| 53 | + let newKey: string = key + "." + subKey; |
| 54 | + let subVal: any = this.getSetting(settings, newKey); |
| 55 | + if (subVal === undefined) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + let entry: KeyValuePair = this.filterAndSanitize(newKey, subVal, settings, filter); |
| 59 | + if (entry && entry.key && entry.value) { |
| 60 | + result[entry.key] = entry.value; |
| 61 | + } |
| 62 | + } |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + let entry: KeyValuePair = this.filterAndSanitize(key, val, settings, filter); |
| 67 | + if (entry && entry.key && entry.value) { |
| 68 | + result[entry.key] = entry.value; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | + private getSetting(settings: vscode.WorkspaceConfiguration, key: string): any { |
| 76 | + // Ignore methods and settings that don't exist |
| 77 | + if (settings.inspect(key).defaultValue !== undefined) { |
| 78 | + let val: any = settings.get(key); |
| 79 | + if (val instanceof Object) { |
| 80 | + return val; // It's a sub-section. |
| 81 | + } |
| 82 | + |
| 83 | + // Only return values that match the setting's type and enum (if applicable). |
| 84 | + let curSetting: any = util.packageJson.contributes.configuration.properties["C_Cpp." + key]; |
| 85 | + if (curSetting) { |
| 86 | + let type: string = this.typeMatch(val, curSetting["type"]); |
| 87 | + if (type) { |
| 88 | + if (type !== "string") { |
| 89 | + return val; |
| 90 | + } |
| 91 | + let curEnum: any[] = curSetting["enum"]; |
| 92 | + if (curEnum && curEnum.indexOf(val) === -1) { |
| 93 | + return "<invalid>"; |
| 94 | + } |
| 95 | + return val; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return undefined; |
| 100 | + } |
| 101 | + |
| 102 | + private typeMatch(value: any, type?: string | string[]): string { |
| 103 | + if (type) { |
| 104 | + if (type instanceof Array) { |
| 105 | + for (let i: number = 0; i < type.length; i++) { |
| 106 | + let t: string = type[i]; |
| 107 | + if (t) { |
| 108 | + if (typeof value === t) { |
| 109 | + return t; |
| 110 | + } |
| 111 | + if (t === "array" && value instanceof Array) { |
| 112 | + return t; |
| 113 | + } |
| 114 | + if (t === "null" && value === null) { |
| 115 | + return t; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } else if (typeof type === "string" && typeof value === type) { |
| 120 | + return type; |
| 121 | + } |
| 122 | + } |
| 123 | + return undefined; |
| 124 | + } |
| 125 | + |
| 126 | + private filterAndSanitize(key: string, val: any, settings: vscode.WorkspaceConfiguration, filter: FilterFunction): KeyValuePair { |
| 127 | + if (filter(key, val, settings)) { |
| 128 | + let value: string; |
| 129 | + this.previousCppSettings[key] = val; |
| 130 | + switch (key) { |
| 131 | + case "clang_format_style": |
| 132 | + case "clang_format_fallbackStyle": { |
| 133 | + let newKey: string = key + "2"; |
| 134 | + if (val) { |
| 135 | + switch (String(val).toLowerCase()) { |
| 136 | + case "visual studio": |
| 137 | + case "llvm": |
| 138 | + case "google": |
| 139 | + case "chromium": |
| 140 | + case "mozilla": |
| 141 | + case "webkit": |
| 142 | + case "file": |
| 143 | + case "none": { |
| 144 | + value = String(this.previousCppSettings[key]); |
| 145 | + break; |
| 146 | + } |
| 147 | + default: { |
| 148 | + value = "..."; |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | + } else { |
| 153 | + value = "null"; |
| 154 | + } |
| 155 | + key = newKey; |
| 156 | + break; |
| 157 | + } |
| 158 | + case "commentContinuationPatterns": { |
| 159 | + value = this.areEqual(val, settings.inspect(key).defaultValue) ? "<default>" : "..."; // Track whether it's being used, but nothing specific about it. |
| 160 | + break; |
| 161 | + } |
| 162 | + default: { |
| 163 | + if (key === "clang_format_path" || key.startsWith("default.")) { |
| 164 | + value = this.areEqual(val, settings.inspect(key).defaultValue) ? "<default>" : "..."; // Track whether it's being used, but nothing specific about it. |
| 165 | + } else { |
| 166 | + value = String(this.previousCppSettings[key]); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + if (value && value.length > maxSettingLengthForTelemetry) { |
| 171 | + value = value.substr(0, maxSettingLengthForTelemetry) + "..."; |
| 172 | + } |
| 173 | + return {key: key, value: value}; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private areEqual(value1: any, value2: any): boolean { |
| 178 | + if (value1 instanceof Object && value2 instanceof Object) { |
| 179 | + return JSON.stringify(value1) === JSON.stringify(value2); |
| 180 | + } |
| 181 | + return value1 === value2; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +export function getTracker(resource: vscode.Uri): SettingsTracker { |
| 186 | + if (!cache) { |
| 187 | + cache = new SettingsTracker(resource); |
| 188 | + } |
| 189 | + return cache; |
| 190 | +} |
0 commit comments