Merge pull request #148 from DogeDark/fix-formatting-single-file

Fix formatting a single file with --file flag
This commit is contained in:
Jon Kelley authored and GitHub committed 2023-06-28 16:52:20 -07:00
commit 571ff5fedf
2 files changed
+29 -11

No files matched your search

+7 -7
View File
@@ -136,28 +136,26 @@ function fmtDocument(document: vscode.TextDocument) {
const [editor,] = vscode.window.visibleTextEditors.filter(editor => editor.document.fileName === document.fileName); const [editor,] = vscode.window.visibleTextEditors.filter(editor => editor.document.fileName === document.fileName);
if (!editor) return; // Need an editor to apply text edits. if (!editor) return; // Need an editor to apply text edits.
const text = document.getText();
const fileDir = document.fileName.slice(0, document.fileName.lastIndexOf('\\')); const fileDir = document.fileName.slice(0, document.fileName.lastIndexOf('\\'));
const child_proc = spawn(serverPath, ["fmt", "--file", document.fileName], {
const child_proc = spawn(serverPath, ["fmt", "--file", text], {
cwd: fileDir ? fileDir : undefined, cwd: fileDir ? fileDir : undefined,
}); });
let result = ''; let result = '';
child_proc.stdout?.on('data', data => result += data); child_proc.stdout?.on('data', data => result += data);
type RsxEdit = { /*type RsxEdit = {
formatted: string, formatted: string,
start: number, start: number,
end: number end: number
} }*/
child_proc.on('close', () => { child_proc.on('close', () => {
if (child_proc.exitCode !== 0) { if (child_proc.exitCode !== 0) {
vscode.window.showWarningMessage(`Errors occurred while formatting. Make sure you have the most recent Dioxus-CLI installed!\nDioxus-CLI exited with exit code ${child_proc.exitCode}\n\nData from Dioxus-CLI:\n${result}`); vscode.window.showWarningMessage(`Errors occurred while formatting. Make sure you have the most recent Dioxus-CLI installed!\nDioxus-CLI exited with exit code ${child_proc.exitCode}\n\nData from Dioxus-CLI:\n${result}`);
return; return;
} }
if (result.length === 0) return; /*if (result.length === 0) return;
// Used for error message: // Used for error message:
const originalResult = result; const originalResult = result;
@@ -245,9 +243,10 @@ function fmtDocument(document: vscode.TextDocument) {
undoStopAfter: false, undoStopAfter: false,
undoStopBefore: false undoStopBefore: false
}); });
} catch (err) { } catch (err) {
vscode.window.showWarningMessage(`Errors occurred while formatting. Make sure you have the most recent Dioxus-CLI installed!\n${err}\n\nData from Dioxus-CLI:\n${originalResult}`); vscode.window.showWarningMessage(`Errors occurred while formatting. Make sure you have the most recent Dioxus-CLI installed!\n${err}\n\nData from Dioxus-CLI:\n${originalResult}`);
} }*/
}); });
child_proc.on('error', (err) => { child_proc.on('error', (err) => {
@@ -259,6 +258,7 @@ function fmtDocument(document: vscode.TextDocument) {
} }
// I'm using the approach defined in rust-analyzer here // I'm using the approach defined in rust-analyzer here
// //
// We ship the server as part of the extension, but we need to handle external paths and such // We ship the server as part of the extension, but we need to handle external paths and such
+22 -4
View File
@@ -1,5 +1,5 @@
use futures::{stream::FuturesUnordered, StreamExt}; use futures::{stream::FuturesUnordered, StreamExt};
use std::process::exit; use std::{fs, process::exit};
use super::*; use super::*;
@@ -44,10 +44,28 @@ impl Autoformat {
} }
} }
// Format single file
if let Some(file) = self.file { if let Some(file) = self.file {
let edits = dioxus_autofmt::fmt_file(&file); let file_content = fs::read_to_string(&file);
let as_json = serde_json::to_string(&edits).unwrap();
println!("{}", as_json); match file_content {
Ok(s) => {
let edits = dioxus_autofmt::fmt_file(&s);
let out = dioxus_autofmt::apply_formats(&s, edits);
match fs::write(&file, out) {
Ok(_) => {
println!("formatted {}", file);
}
Err(e) => {
eprintln!("failed to write formatted content to file: {}", e);
}
}
}
Err(e) => {
eprintln!("failed to open file: {}", e);
exit(1);
}
}
} }
Ok(()) Ok(())