diff --git a/src/vs/workbench/electron-browser/integration.ts b/src/vs/workbench/electron-browser/integration.ts index a34af0d9a396dfabef66159cd68d50f8d3efc9b8..a2b34c156baf0a0d125f79febc6979ec0f23a7d8 100644 --- a/src/vs/workbench/electron-browser/integration.ts +++ b/src/vs/workbench/electron-browser/integration.ts @@ -130,13 +130,25 @@ export class ElectronIntegration { ipc.on('vscode:showAutoSaveInfo', () => { this.messageService.show( Severity.Info, { - message: nls.localize('autoSaveInfo', "The **File | Auto Save** option moved into settings. Please configure **files.autoSaveDelay: 1** to restore the old behavior."), + message: nls.localize('autoSaveInfo', "The **File | Auto Save** option moved into settings and **files.autoSaveDelay: 1** will be added to preserve it."), actions: [ CloseAction, this.instantiationService.createInstance(OpenGlobalSettingsAction, OpenGlobalSettingsAction.ID, OpenGlobalSettingsAction.LABEL) ] - }); + }); }); + + ipc.on('vscode:showAutoSaveError', () => { + this.messageService.show( + Severity.Warning, { + message: nls.localize('autoSaveError', "Unable to write to settings. Please add **files.autoSaveDelay: 1** to settings.json."), + actions: [ + CloseAction, + this.instantiationService.createInstance(OpenGlobalSettingsAction, OpenGlobalSettingsAction.ID, OpenGlobalSettingsAction.LABEL) + ] + }); + }); + } private resolveKeybindings(actionIds: string[]): TPromise<{ id: string; binding: number; }[]> { diff --git a/src/vs/workbench/electron-main/windows.ts b/src/vs/workbench/electron-main/windows.ts index b174d2c82f2ebad1cf5f61653b6896b199020d41..06effaaba9986df50e1e5b49c435a473cec6c62a 100644 --- a/src/vs/workbench/electron-main/windows.ts +++ b/src/vs/workbench/electron-main/windows.ts @@ -23,6 +23,7 @@ import window = require('vs/workbench/electron-main/window'); import lifecycle = require('vs/workbench/electron-main/lifecycle'); import nls = require('vs/nls'); import paths = require('vs/base/common/paths'); +import json = require('vs/base/common/json'); import arrays = require('vs/base/common/arrays'); import objects = require('vs/base/common/objects'); import storage = require('vs/workbench/electron-main/storage'); @@ -175,10 +176,7 @@ export class WindowsManager { eventEmitter.emit(EventTypes.READY, win); // TODO@Ben remove me in a couple of versions - if (storage.getItem('autoSaveDelay') === 1000) { - storage.removeItem('autoSaveDelay'); - win.send('vscode:showAutoSaveInfo'); - } + this.migrateAutoSave(win); } }); @@ -342,6 +340,46 @@ export class WindowsManager { }); } + private migrateAutoSave(win: window.VSCodeWindow): void { + if (storage.getItem('autoSaveDelay') === 1000) { + storage.removeItem('autoSaveDelay'); + win.send('vscode:showAutoSaveInfo'); + + try { + + // Initial settings file + if (!fs.existsSync(env.appSettingsPath)) { + fs.writeFileSync(env.appSettingsPath, JSON.stringify({ 'files.autoSaveDelay': 1 }, null, ' ')); + } + + // Update existing settings file + else { + const settingsRaw = fs.readFileSync(env.appSettingsPath).toString(); + const lastClosing = settingsRaw.lastIndexOf('}'); + const errors = []; + const res = json.parse(settingsRaw, errors); + + // We found a closing '}' and the JSON does not contain errors + if (lastClosing > 0 && !errors.length) { + const hasOtherKeys = Object.getOwnPropertyNames(res).length > 0; + + const migratedSettings = settingsRaw.substring(0, lastClosing) + '\n // Migrated from previous File | Auto Save setting:\n' + (hasOtherKeys ? ' , "files.autoSaveDelay": 1\n' : ' "files.autoSaveDelay": 1\n') + '}'; + + fs.writeFileSync(env.appSettingsPath, migratedSettings); + } + + // Otherwise inform user that we cannot migrate the settings + else { + win.send('vscode:showAutoSaveError'); + } + } + } catch (error) { + env.log(error); + win.send('vscode:showAutoSaveError'); + } + } + } + public reload(win: window.VSCodeWindow, cli?: env.ICommandLineArguments): void { // Only reload when the window has not vetoed this @@ -600,7 +638,7 @@ export class WindowsManager { recentPaths.unshift(workspacePath); } - // Clear those dupes + // Clear those dupes recentPaths = arrays.distinct(recentPaths); // Make sure it is bounded