mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-19 03:40:35 -05:00
Settings editor - autofix missing commas in settings file.
Fixes #42232
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user