From 201dcc144d363571865ace40d85336b3c792d474 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Tue, 26 Apr 2022 18:45:27 -0400 Subject: [PATCH] feat: fix bug with applying multiple edits in sequence --- Cargo.toml | 6 ++- extension/package.json | 7 +++- extension/src/extension.ts | 85 ++++++++++++++++++++++++++++++++------ src/cli/autoformat/mod.rs | 38 ++++++++++++++++- 4 files changed, 120 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b8f0e271b..9f85a89c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,8 +46,10 @@ flate2 = "1.0.22" tar = "0.4.38" tower = "0.4.12" -dioxus-rsx = { git = "https://github.com/dioxuslabs/dioxus.git", branch = "jk/rsx-refactor" } -dioxus-autofmt = { git = "https://github.com/dioxuslabs/dioxus.git", branch = "jk/rsx-refactor" } +dioxus-rsx = { path = "../../dioxus/packages/rsx" } +dioxus-autofmt = { path = "../../dioxus/packages/autofmt" } +# dioxus-rsx = { git = "https://github.com/dioxuslabs/dioxus.git", branch = "jk/rsx-refactor" } +# dioxus-autofmt = { git = "https://github.com/dioxuslabs/dioxus.git", branch = "jk/rsx-refactor" } [[bin]] path = "src/main.rs" diff --git a/extension/package.json b/extension/package.json index ca73c0ea8..f884637c8 100644 --- a/extension/package.json +++ b/extension/package.json @@ -20,7 +20,8 @@ "activationEvents": [ "onCommand:extension.htmlToDioxusRsx", "onCommand:extension.htmlToDioxusComponent", - "onCommand:extension.formatRsx" + "onCommand:extension.formatRsx", + "onCommand:extension.formatRsxDocument" ], "main": "./out/extension", "contributes": { @@ -36,6 +37,10 @@ { "command": "extension.formatRsx", "title": "Dioxus: Format RSX" + }, + { + "command": "extension.formatRsxDocument", + "title": "Dioxus: Format RSX Document" } ] }, diff --git a/extension/src/extension.ts b/extension/src/extension.ts index fd8e2906b..46ce2aa89 100644 --- a/extension/src/extension.ts +++ b/extension/src/extension.ts @@ -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) + ); } diff --git a/src/cli/autoformat/mod.rs b/src/cli/autoformat/mod.rs index 26ab86345..3008411db 100644 --- a/src/cli/autoformat/mod.rs +++ b/src/cli/autoformat/mod.rs @@ -3,9 +3,13 @@ use super::*; /// Build the Rust WASM app and all of its assets. #[derive(Clone, Debug, Parser)] pub struct Autoformat { - /// Input file + /// Input rsx (selection) #[clap(short, long)] pub raw: Option, + + /// Input file + #[clap(short, long)] + pub file: Option, } impl Autoformat { @@ -20,6 +24,38 @@ impl Autoformat { } } + if let Some(file) = self.file { + let edits = dioxus_autofmt::get_format_blocks(&file); + let as_json = serde_json::to_string(&edits).unwrap(); + println!("{}", as_json); + } + Ok(()) } } + +#[test] +fn spawn_properly() { + let out = Command::new("dioxus") + .args([ + "fmt", + "-f", + r#" +// + +rsx! { + + div {} +} + +// +// +// + + "#, + ]) + .output() + .expect("failed to execute process"); + + dbg!(out); +}