feat: fix bug with applying multiple edits in sequence

This commit is contained in:
Jonathan Kelley committed 2022-04-26 18:45:27 -04:00
1 parent 84f0245f6c
commit 201dcc144d
4 files changed
+120 -16

No files matched your search

+73 -12
View File
@@ -33,7 +33,7 @@ export function activate(context: vscode.ExtensionContext) {
});
}
function autoformat() {
function autoformat_selection() {
const editor = vscode.window.activeTextEditor;// Get the active text editor
if (!editor) return;
@@ -43,11 +43,10 @@ export function activate(context: vscode.ExtensionContext) {
return;
}
const params = ["fmt", "--raw", unformatted];
const child_proc = spawn("dioxus", params);
let args = ["fmt", "--raw", unformatted.toString()];
const child_proc = spawn("dioxus", args);
let result = '';
child_proc.stdout?.on('data', data => result += data);
child_proc.on('close', () => {
@@ -60,14 +59,76 @@ export function activate(context: vscode.ExtensionContext) {
}
function autoformat_document(document: vscode.TextDocument) {
// check the settings to make sure format on save is configured
// const formatOnSave: string | undefined = vscode.workspace.getConfiguration('rust').get('formatOnSave');
if (document.languageId === "rust" && document.uri.scheme === "file") {
const editor = vscode.window.activeTextEditor;// Get the active text editor
if (editor) {
const text = editor.document.getText();
const handles =
[
vscode.commands.registerCommand('extension.htmlToDioxusRsx', () => translate(false)),
vscode.commands.registerCommand('extension.htmlToDioxusComponent', () => translate(true)),
vscode.commands.registerCommand('extension.formatRsx', () => autoformat())
];
console.error(text);
context.subscriptions.push(...handles);
const child_proc = spawn("dioxus", ["fmt", "-f", text]);
let result = '';
child_proc.stdout?.on('data', data => result += data);
type RsxEdit = {
formatted: string,
start: number,
end: number
}
child_proc.on('close', () => {
// if (result.length > 0) {
// editor.edit(editBuilder => editBuilder.insert(new vscode.Position(0, 0), result));
// } else {
// console.error("No result");
// }
if (result.length > 0) {
let decoded: RsxEdit[] = JSON.parse(result);
console.log("Decoded edits: ", decoded);
editor.edit(editBuilder => {
for (let edit of decoded) {
console.log("Handling Edit: ", edit);
let start = document.positionAt(edit.start - 1);
let end = document.positionAt(edit.end + 1);
const range = new vscode.Range(start, end);
editBuilder.replace(range, `{ ${edit.formatted} }`);
// editor.edit(editBuilder => editBuilder.replace(range, `{ ${edit.formatted} }`)).then((could_be_applied) => {
// });
// editor.edit(editBuilder => editBuilder.replace(range, `{ ${edit.formatted} }`)).then((could_be_applied) => {
// console.log("Edit applied: ", could_be_applied);
// });
}
})
}
});
child_proc.on('error', (err) => {
vscode.window.showWarningMessage(`Errors occurred while translating. Make sure you have the most recent Dioxus-CLI installed! \n${err}`);
});
}
}
}
context.subscriptions.push(
vscode.commands.registerCommand('extension.htmlToDioxusRsx', () => translate(false)),
vscode.commands.registerCommand('extension.htmlToDioxusComponent', () => translate(true)),
vscode.commands.registerCommand('extension.formatRsx', () => autoformat_selection()),
vscode.commands.registerCommand('extension.formatRsxDocument', () => {
const editor = vscode.window.activeTextEditor;// Get the active text editor
if (!editor) return;
autoformat_document(editor.document);
}),
vscode.workspace.onDidSaveTextDocument(autoformat_document)
);
}