mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
Settings editor - autofix missing commas in settings file.
Fixes #42232
This commit is contained in:
parent
e1959f7284
commit
db70084dca
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue