Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/browser/config/migrateOptions.ts
3294 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IEditorOptions } from '../../common/config/editorOptions.js';
7
8
export interface ISettingsReader {
9
(key: string): any;
10
}
11
12
export interface ISettingsWriter {
13
(key: string, value: any): void;
14
}
15
16
export class EditorSettingMigration {
17
18
public static items: EditorSettingMigration[] = [];
19
20
constructor(
21
public readonly key: string,
22
public readonly migrate: (value: any, read: ISettingsReader, write: ISettingsWriter) => void
23
) { }
24
25
apply(options: any): void {
26
const value = EditorSettingMigration._read(options, this.key);
27
const read = (key: string) => EditorSettingMigration._read(options, key);
28
const write = (key: string, value: any) => EditorSettingMigration._write(options, key, value);
29
this.migrate(value, read, write);
30
}
31
32
private static _read(source: any, key: string): any {
33
if (typeof source === 'undefined') {
34
return undefined;
35
}
36
37
const firstDotIndex = key.indexOf('.');
38
if (firstDotIndex >= 0) {
39
const firstSegment = key.substring(0, firstDotIndex);
40
return this._read(source[firstSegment], key.substring(firstDotIndex + 1));
41
}
42
return source[key];
43
}
44
45
private static _write(target: any, key: string, value: any): void {
46
const firstDotIndex = key.indexOf('.');
47
if (firstDotIndex >= 0) {
48
const firstSegment = key.substring(0, firstDotIndex);
49
target[firstSegment] = target[firstSegment] || {};
50
this._write(target[firstSegment], key.substring(firstDotIndex + 1), value);
51
return;
52
}
53
target[key] = value;
54
}
55
}
56
57
function registerEditorSettingMigration(key: string, migrate: (value: any, read: ISettingsReader, write: ISettingsWriter) => void): void {
58
EditorSettingMigration.items.push(new EditorSettingMigration(key, migrate));
59
}
60
61
function registerSimpleEditorSettingMigration(key: string, values: [any, any][]): void {
62
registerEditorSettingMigration(key, (value, read, write) => {
63
if (typeof value !== 'undefined') {
64
for (const [oldValue, newValue] of values) {
65
if (value === oldValue) {
66
write(key, newValue);
67
return;
68
}
69
}
70
}
71
});
72
}
73
74
/**
75
* Compatibility with old options
76
*/
77
export function migrateOptions(options: IEditorOptions): void {
78
EditorSettingMigration.items.forEach(migration => migration.apply(options));
79
}
80
81
registerSimpleEditorSettingMigration('wordWrap', [[true, 'on'], [false, 'off']]);
82
registerSimpleEditorSettingMigration('lineNumbers', [[true, 'on'], [false, 'off']]);
83
registerSimpleEditorSettingMigration('cursorBlinking', [['visible', 'solid']]);
84
registerSimpleEditorSettingMigration('renderWhitespace', [[true, 'boundary'], [false, 'none']]);
85
registerSimpleEditorSettingMigration('renderLineHighlight', [[true, 'line'], [false, 'none']]);
86
registerSimpleEditorSettingMigration('acceptSuggestionOnEnter', [[true, 'on'], [false, 'off']]);
87
registerSimpleEditorSettingMigration('tabCompletion', [[false, 'off'], [true, 'onlySnippets']]);
88
registerSimpleEditorSettingMigration('hover', [[true, { enabled: true }], [false, { enabled: false }]]);
89
registerSimpleEditorSettingMigration('parameterHints', [[true, { enabled: true }], [false, { enabled: false }]]);
90
registerSimpleEditorSettingMigration('autoIndent', [[false, 'advanced'], [true, 'full']]);
91
registerSimpleEditorSettingMigration('matchBrackets', [[true, 'always'], [false, 'never']]);
92
registerSimpleEditorSettingMigration('renderFinalNewline', [[true, 'on'], [false, 'off']]);
93
registerSimpleEditorSettingMigration('cursorSmoothCaretAnimation', [[true, 'on'], [false, 'off']]);
94
registerSimpleEditorSettingMigration('occurrencesHighlight', [[true, 'singleFile'], [false, 'off']]);
95
registerSimpleEditorSettingMigration('wordBasedSuggestions', [[true, 'matchingDocuments'], [false, 'off']]);
96
registerSimpleEditorSettingMigration('defaultColorDecorators', [[true, 'auto'], [false, 'never']]);
97
registerSimpleEditorSettingMigration('minimap.autohide', [[true, 'mouseover'], [false, 'none']]);
98
99
registerEditorSettingMigration('autoClosingBrackets', (value, read, write) => {
100
if (value === false) {
101
write('autoClosingBrackets', 'never');
102
if (typeof read('autoClosingQuotes') === 'undefined') {
103
write('autoClosingQuotes', 'never');
104
}
105
if (typeof read('autoSurround') === 'undefined') {
106
write('autoSurround', 'never');
107
}
108
}
109
});
110
111
registerEditorSettingMigration('renderIndentGuides', (value, read, write) => {
112
if (typeof value !== 'undefined') {
113
write('renderIndentGuides', undefined);
114
if (typeof read('guides.indentation') === 'undefined') {
115
write('guides.indentation', !!value);
116
}
117
}
118
});
119
120
registerEditorSettingMigration('highlightActiveIndentGuide', (value, read, write) => {
121
if (typeof value !== 'undefined') {
122
write('highlightActiveIndentGuide', undefined);
123
if (typeof read('guides.highlightActiveIndentation') === 'undefined') {
124
write('guides.highlightActiveIndentation', !!value);
125
}
126
}
127
});
128
129
const suggestFilteredTypesMapping: Record<string, string> = {
130
method: 'showMethods',
131
function: 'showFunctions',
132
constructor: 'showConstructors',
133
deprecated: 'showDeprecated',
134
field: 'showFields',
135
variable: 'showVariables',
136
class: 'showClasses',
137
struct: 'showStructs',
138
interface: 'showInterfaces',
139
module: 'showModules',
140
property: 'showProperties',
141
event: 'showEvents',
142
operator: 'showOperators',
143
unit: 'showUnits',
144
value: 'showValues',
145
constant: 'showConstants',
146
enum: 'showEnums',
147
enumMember: 'showEnumMembers',
148
keyword: 'showKeywords',
149
text: 'showWords',
150
color: 'showColors',
151
file: 'showFiles',
152
reference: 'showReferences',
153
folder: 'showFolders',
154
typeParameter: 'showTypeParameters',
155
snippet: 'showSnippets',
156
};
157
158
registerEditorSettingMigration('suggest.filteredTypes', (value, read, write) => {
159
if (value && typeof value === 'object') {
160
for (const entry of Object.entries(suggestFilteredTypesMapping)) {
161
const v = value[entry[0]];
162
if (v === false) {
163
if (typeof read(`suggest.${entry[1]}`) === 'undefined') {
164
write(`suggest.${entry[1]}`, false);
165
}
166
}
167
}
168
write('suggest.filteredTypes', undefined);
169
}
170
});
171
172
registerEditorSettingMigration('quickSuggestions', (input, read, write) => {
173
if (typeof input === 'boolean') {
174
const value = input ? 'on' : 'off';
175
const newValue = { comments: value, strings: value, other: value };
176
write('quickSuggestions', newValue);
177
}
178
});
179
180
// Sticky Scroll
181
182
registerEditorSettingMigration('experimental.stickyScroll.enabled', (value, read, write) => {
183
if (typeof value === 'boolean') {
184
write('experimental.stickyScroll.enabled', undefined);
185
if (typeof read('stickyScroll.enabled') === 'undefined') {
186
write('stickyScroll.enabled', value);
187
}
188
}
189
});
190
191
registerEditorSettingMigration('experimental.stickyScroll.maxLineCount', (value, read, write) => {
192
if (typeof value === 'number') {
193
write('experimental.stickyScroll.maxLineCount', undefined);
194
if (typeof read('stickyScroll.maxLineCount') === 'undefined') {
195
write('stickyScroll.maxLineCount', value);
196
}
197
}
198
});
199
200
// Edit Context
201
202
registerEditorSettingMigration('editor.experimentalEditContextEnabled', (value, read, write) => {
203
if (typeof value === 'boolean') {
204
write('editor.experimentalEditContextEnabled', undefined);
205
if (typeof read('editor.editContext') === 'undefined') {
206
write('editor.editContext', value);
207
}
208
}
209
});
210
211
// Code Actions on Save
212
registerEditorSettingMigration('codeActionsOnSave', (value, read, write) => {
213
if (value && typeof value === 'object') {
214
let toBeModified = false;
215
const newValue = {} as any;
216
for (const entry of Object.entries(value)) {
217
if (typeof entry[1] === 'boolean') {
218
toBeModified = true;
219
newValue[entry[0]] = entry[1] ? 'explicit' : 'never';
220
} else {
221
newValue[entry[0]] = entry[1];
222
}
223
}
224
if (toBeModified) {
225
write(`codeActionsOnSave`, newValue);
226
}
227
}
228
});
229
230
// Migrate Quick Fix Settings
231
registerEditorSettingMigration('codeActionWidget.includeNearbyQuickfixes', (value, read, write) => {
232
if (typeof value === 'boolean') {
233
write('codeActionWidget.includeNearbyQuickfixes', undefined);
234
if (typeof read('codeActionWidget.includeNearbyQuickFixes') === 'undefined') {
235
write('codeActionWidget.includeNearbyQuickFixes', value);
236
}
237
}
238
});
239
240
// Migrate the lightbulb settings
241
registerEditorSettingMigration('lightbulb.enabled', (value, read, write) => {
242
if (typeof value === 'boolean') {
243
write('lightbulb.enabled', value ? undefined : 'off');
244
}
245
});
246
247
// NES Code Shifting
248
registerEditorSettingMigration('inlineSuggest.edits.codeShifting', (value, read, write) => {
249
if (typeof value === 'boolean') {
250
write('inlineSuggest.edits.codeShifting', undefined);
251
write('inlineSuggest.edits.allowCodeShifting', value ? 'always' : 'never');
252
}
253
});
254
255