diff --git a/extensions/configuration-editing/src/extension.ts b/extensions/configuration-editing/src/extension.ts index ab31432ee85cd8f54877e5791a1dd8abd50f6692..76d473c365e8308a579a967ca23fed91c13a44fc 100644 --- a/extensions/configuration-editing/src/extension.ts +++ b/extensions/configuration-editing/src/extension.ts @@ -7,7 +7,7 @@ import * as nls from 'vscode-nls'; const localize = nls.loadMessageBundle(); import * as vscode from 'vscode'; -import { getLocation, visit, parse } from 'jsonc-parser'; +import { getLocation, visit, parse, ParseError, ParseErrorCode } from 'jsonc-parser'; import * as path from 'path'; import { SettingsDocument } from './settingsDocumentHelper'; @@ -42,6 +42,47 @@ export function activate(context: vscode.ExtensionContext): void { } }, null, context.subscriptions)); updateLaunchJsonDecorations(vscode.window.activeTextEditor); + + context.subscriptions.push(vscode.workspace.onWillSaveTextDocument(e => { + if (!e.document.fileName.endsWith('/settings.json')) { + return; + } + + autoFixSettingsJSON(e); + })); +} + +function autoFixSettingsJSON(willSaveEvent: vscode.TextDocumentWillSaveEvent): void { + const document = willSaveEvent.document; + const text = document.getText(); + const edit = new vscode.WorkspaceEdit(); + + let lastEndOfSomething = -1; + visit(text, { + onArrayEnd(offset: number, length: number): void { + lastEndOfSomething = offset + length; + }, + + onLiteralValue(value: any, offset: number, length: number): void { + lastEndOfSomething = offset + length; + }, + + onObjectEnd(offset: number, length: number): void { + lastEndOfSomething = offset + length; + }, + + onError(error: ParseErrorCode, offset: number, length: number): void { + if (error === ParseErrorCode.CommaExpected && lastEndOfSomething > -1) { + const errorPosition = document.positionAt(offset); + + const fixPosition = document.positionAt(lastEndOfSomething); + edit.insert(document.uri, fixPosition, ','); + } + } + }); + + willSaveEvent.waitUntil( + vscode.workspace.applyEdit(edit)); } function registerKeybindingsCompletions(): vscode.Disposable {