feat: fix bug with applying multiple edits in sequence
This commit is contained in:
1 parent
84f0245f6c
commit
201dcc144d
4 files changed
+120
-16
No files matched your search
+4
-2
@@ -46,8 +46,10 @@ flate2 = "1.0.22"
|
|||||||
tar = "0.4.38"
|
tar = "0.4.38"
|
||||||
tower = "0.4.12"
|
tower = "0.4.12"
|
||||||
|
|
||||||
dioxus-rsx = { git = "https://github.com/dioxuslabs/dioxus.git", branch = "jk/rsx-refactor" }
|
dioxus-rsx = { path = "../../dioxus/packages/rsx" }
|
||||||
dioxus-autofmt = { git = "https://github.com/dioxuslabs/dioxus.git", branch = "jk/rsx-refactor" }
|
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]]
|
[[bin]]
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|||||||
@@ -20,7 +20,8 @@
|
|||||||
"activationEvents": [
|
"activationEvents": [
|
||||||
"onCommand:extension.htmlToDioxusRsx",
|
"onCommand:extension.htmlToDioxusRsx",
|
||||||
"onCommand:extension.htmlToDioxusComponent",
|
"onCommand:extension.htmlToDioxusComponent",
|
||||||
"onCommand:extension.formatRsx"
|
"onCommand:extension.formatRsx",
|
||||||
|
"onCommand:extension.formatRsxDocument"
|
||||||
],
|
],
|
||||||
"main": "./out/extension",
|
"main": "./out/extension",
|
||||||
"contributes": {
|
"contributes": {
|
||||||
@@ -36,6 +37,10 @@
|
|||||||
{
|
{
|
||||||
"command": "extension.formatRsx",
|
"command": "extension.formatRsx",
|
||||||
"title": "Dioxus: Format RSX"
|
"title": "Dioxus: Format RSX"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "extension.formatRsxDocument",
|
||||||
|
"title": "Dioxus: Format RSX Document"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
+73
-12
@@ -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
|
const editor = vscode.window.activeTextEditor;// Get the active text editor
|
||||||
if (!editor) return;
|
if (!editor) return;
|
||||||
|
|
||||||
@@ -43,11 +43,10 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const params = ["fmt", "--raw", unformatted];
|
let args = ["fmt", "--raw", unformatted.toString()];
|
||||||
|
const child_proc = spawn("dioxus", args);
|
||||||
const child_proc = spawn("dioxus", params);
|
|
||||||
|
|
||||||
let result = '';
|
let result = '';
|
||||||
|
|
||||||
child_proc.stdout?.on('data', data => result += data);
|
child_proc.stdout?.on('data', data => result += data);
|
||||||
|
|
||||||
child_proc.on('close', () => {
|
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 =
|
console.error(text);
|
||||||
[
|
|
||||||
vscode.commands.registerCommand('extension.htmlToDioxusRsx', () => translate(false)),
|
|
||||||
vscode.commands.registerCommand('extension.htmlToDioxusComponent', () => translate(true)),
|
|
||||||
vscode.commands.registerCommand('extension.formatRsx', () => autoformat())
|
|
||||||
];
|
|
||||||
|
|
||||||
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.
|
/// Build the Rust WASM app and all of its assets.
|
||||||
#[derive(Clone, Debug, Parser)]
|
#[derive(Clone, Debug, Parser)]
|
||||||
pub struct Autoformat {
|
pub struct Autoformat {
|
||||||
/// Input file
|
/// Input rsx (selection)
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
pub raw: Option<String>,
|
pub raw: Option<String>,
|
||||||
|
|
||||||
|
/// Input file
|
||||||
|
#[clap(short, long)]
|
||||||
|
pub file: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Autoformat {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn spawn_properly() {
|
||||||
|
let out = Command::new("dioxus")
|
||||||
|
.args([
|
||||||
|
"fmt",
|
||||||
|
"-f",
|
||||||
|
r#"
|
||||||
|
//
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
|
||||||
|
div {}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
"#,
|
||||||
|
])
|
||||||
|
.output()
|
||||||
|
.expect("failed to execute process");
|
||||||
|
|
||||||
|
dbg!(out);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user