Path: blob/main/src/vs/editor/browser/config/migrateOptions.ts
3294 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { IEditorOptions } from '../../common/config/editorOptions.js';67export interface ISettingsReader {8(key: string): any;9}1011export interface ISettingsWriter {12(key: string, value: any): void;13}1415export class EditorSettingMigration {1617public static items: EditorSettingMigration[] = [];1819constructor(20public readonly key: string,21public readonly migrate: (value: any, read: ISettingsReader, write: ISettingsWriter) => void22) { }2324apply(options: any): void {25const value = EditorSettingMigration._read(options, this.key);26const read = (key: string) => EditorSettingMigration._read(options, key);27const write = (key: string, value: any) => EditorSettingMigration._write(options, key, value);28this.migrate(value, read, write);29}3031private static _read(source: any, key: string): any {32if (typeof source === 'undefined') {33return undefined;34}3536const firstDotIndex = key.indexOf('.');37if (firstDotIndex >= 0) {38const firstSegment = key.substring(0, firstDotIndex);39return this._read(source[firstSegment], key.substring(firstDotIndex + 1));40}41return source[key];42}4344private static _write(target: any, key: string, value: any): void {45const firstDotIndex = key.indexOf('.');46if (firstDotIndex >= 0) {47const firstSegment = key.substring(0, firstDotIndex);48target[firstSegment] = target[firstSegment] || {};49this._write(target[firstSegment], key.substring(firstDotIndex + 1), value);50return;51}52target[key] = value;53}54}5556function registerEditorSettingMigration(key: string, migrate: (value: any, read: ISettingsReader, write: ISettingsWriter) => void): void {57EditorSettingMigration.items.push(new EditorSettingMigration(key, migrate));58}5960function registerSimpleEditorSettingMigration(key: string, values: [any, any][]): void {61registerEditorSettingMigration(key, (value, read, write) => {62if (typeof value !== 'undefined') {63for (const [oldValue, newValue] of values) {64if (value === oldValue) {65write(key, newValue);66return;67}68}69}70});71}7273/**74* Compatibility with old options75*/76export function migrateOptions(options: IEditorOptions): void {77EditorSettingMigration.items.forEach(migration => migration.apply(options));78}7980registerSimpleEditorSettingMigration('wordWrap', [[true, 'on'], [false, 'off']]);81registerSimpleEditorSettingMigration('lineNumbers', [[true, 'on'], [false, 'off']]);82registerSimpleEditorSettingMigration('cursorBlinking', [['visible', 'solid']]);83registerSimpleEditorSettingMigration('renderWhitespace', [[true, 'boundary'], [false, 'none']]);84registerSimpleEditorSettingMigration('renderLineHighlight', [[true, 'line'], [false, 'none']]);85registerSimpleEditorSettingMigration('acceptSuggestionOnEnter', [[true, 'on'], [false, 'off']]);86registerSimpleEditorSettingMigration('tabCompletion', [[false, 'off'], [true, 'onlySnippets']]);87registerSimpleEditorSettingMigration('hover', [[true, { enabled: true }], [false, { enabled: false }]]);88registerSimpleEditorSettingMigration('parameterHints', [[true, { enabled: true }], [false, { enabled: false }]]);89registerSimpleEditorSettingMigration('autoIndent', [[false, 'advanced'], [true, 'full']]);90registerSimpleEditorSettingMigration('matchBrackets', [[true, 'always'], [false, 'never']]);91registerSimpleEditorSettingMigration('renderFinalNewline', [[true, 'on'], [false, 'off']]);92registerSimpleEditorSettingMigration('cursorSmoothCaretAnimation', [[true, 'on'], [false, 'off']]);93registerSimpleEditorSettingMigration('occurrencesHighlight', [[true, 'singleFile'], [false, 'off']]);94registerSimpleEditorSettingMigration('wordBasedSuggestions', [[true, 'matchingDocuments'], [false, 'off']]);95registerSimpleEditorSettingMigration('defaultColorDecorators', [[true, 'auto'], [false, 'never']]);96registerSimpleEditorSettingMigration('minimap.autohide', [[true, 'mouseover'], [false, 'none']]);9798registerEditorSettingMigration('autoClosingBrackets', (value, read, write) => {99if (value === false) {100write('autoClosingBrackets', 'never');101if (typeof read('autoClosingQuotes') === 'undefined') {102write('autoClosingQuotes', 'never');103}104if (typeof read('autoSurround') === 'undefined') {105write('autoSurround', 'never');106}107}108});109110registerEditorSettingMigration('renderIndentGuides', (value, read, write) => {111if (typeof value !== 'undefined') {112write('renderIndentGuides', undefined);113if (typeof read('guides.indentation') === 'undefined') {114write('guides.indentation', !!value);115}116}117});118119registerEditorSettingMigration('highlightActiveIndentGuide', (value, read, write) => {120if (typeof value !== 'undefined') {121write('highlightActiveIndentGuide', undefined);122if (typeof read('guides.highlightActiveIndentation') === 'undefined') {123write('guides.highlightActiveIndentation', !!value);124}125}126});127128const suggestFilteredTypesMapping: Record<string, string> = {129method: 'showMethods',130function: 'showFunctions',131constructor: 'showConstructors',132deprecated: 'showDeprecated',133field: 'showFields',134variable: 'showVariables',135class: 'showClasses',136struct: 'showStructs',137interface: 'showInterfaces',138module: 'showModules',139property: 'showProperties',140event: 'showEvents',141operator: 'showOperators',142unit: 'showUnits',143value: 'showValues',144constant: 'showConstants',145enum: 'showEnums',146enumMember: 'showEnumMembers',147keyword: 'showKeywords',148text: 'showWords',149color: 'showColors',150file: 'showFiles',151reference: 'showReferences',152folder: 'showFolders',153typeParameter: 'showTypeParameters',154snippet: 'showSnippets',155};156157registerEditorSettingMigration('suggest.filteredTypes', (value, read, write) => {158if (value && typeof value === 'object') {159for (const entry of Object.entries(suggestFilteredTypesMapping)) {160const v = value[entry[0]];161if (v === false) {162if (typeof read(`suggest.${entry[1]}`) === 'undefined') {163write(`suggest.${entry[1]}`, false);164}165}166}167write('suggest.filteredTypes', undefined);168}169});170171registerEditorSettingMigration('quickSuggestions', (input, read, write) => {172if (typeof input === 'boolean') {173const value = input ? 'on' : 'off';174const newValue = { comments: value, strings: value, other: value };175write('quickSuggestions', newValue);176}177});178179// Sticky Scroll180181registerEditorSettingMigration('experimental.stickyScroll.enabled', (value, read, write) => {182if (typeof value === 'boolean') {183write('experimental.stickyScroll.enabled', undefined);184if (typeof read('stickyScroll.enabled') === 'undefined') {185write('stickyScroll.enabled', value);186}187}188});189190registerEditorSettingMigration('experimental.stickyScroll.maxLineCount', (value, read, write) => {191if (typeof value === 'number') {192write('experimental.stickyScroll.maxLineCount', undefined);193if (typeof read('stickyScroll.maxLineCount') === 'undefined') {194write('stickyScroll.maxLineCount', value);195}196}197});198199// Edit Context200201registerEditorSettingMigration('editor.experimentalEditContextEnabled', (value, read, write) => {202if (typeof value === 'boolean') {203write('editor.experimentalEditContextEnabled', undefined);204if (typeof read('editor.editContext') === 'undefined') {205write('editor.editContext', value);206}207}208});209210// Code Actions on Save211registerEditorSettingMigration('codeActionsOnSave', (value, read, write) => {212if (value && typeof value === 'object') {213let toBeModified = false;214const newValue = {} as any;215for (const entry of Object.entries(value)) {216if (typeof entry[1] === 'boolean') {217toBeModified = true;218newValue[entry[0]] = entry[1] ? 'explicit' : 'never';219} else {220newValue[entry[0]] = entry[1];221}222}223if (toBeModified) {224write(`codeActionsOnSave`, newValue);225}226}227});228229// Migrate Quick Fix Settings230registerEditorSettingMigration('codeActionWidget.includeNearbyQuickfixes', (value, read, write) => {231if (typeof value === 'boolean') {232write('codeActionWidget.includeNearbyQuickfixes', undefined);233if (typeof read('codeActionWidget.includeNearbyQuickFixes') === 'undefined') {234write('codeActionWidget.includeNearbyQuickFixes', value);235}236}237});238239// Migrate the lightbulb settings240registerEditorSettingMigration('lightbulb.enabled', (value, read, write) => {241if (typeof value === 'boolean') {242write('lightbulb.enabled', value ? undefined : 'off');243}244});245246// NES Code Shifting247registerEditorSettingMigration('inlineSuggest.edits.codeShifting', (value, read, write) => {248if (typeof value === 'boolean') {249write('inlineSuggest.edits.codeShifting', undefined);250write('inlineSuggest.edits.allowCodeShifting', value ? 'always' : 'never');251}252});253254255