mirror of
https://github.com/DioxusLabs/dioxus
synced 2025-03-08 17:27:27 +00:00
feat: fix bug with applying multiple edits in sequence
This commit is contained in:
parent
84f0245f6c
commit
201dcc144d
4 changed files with 120 additions and 16 deletions
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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<String>,
|
||||
|
||||
/// Input file
|
||||
#[clap(short, long)]
|
||||
pub file: Option<String>,
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue