提交 db70084d 编写于 作者: R Rob Lourens

Settings editor - autofix missing commas in settings file.

Fixes #42232
上级 e1959f72
......@@ -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 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册