From c398372785f36cc0f9409820733f541f749b6e9d Mon Sep 17 00:00:00 2001 From: Miles Murgaw Date: Sun, 4 Jun 2023 12:44:43 -0400 Subject: [PATCH 1/4] fix: formatting a single file with --file --- src/cli/autoformat/mod.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/cli/autoformat/mod.rs b/src/cli/autoformat/mod.rs index b4e8b6f21..61d9a66ab 100644 --- a/src/cli/autoformat/mod.rs +++ b/src/cli/autoformat/mod.rs @@ -1,5 +1,5 @@ use futures::{stream::FuturesUnordered, StreamExt}; -use std::process::exit; +use std::{process::exit, fs}; use super::*; @@ -44,10 +44,28 @@ impl Autoformat { } } + // Format single file if let Some(file) = self.file { - let edits = dioxus_autofmt::fmt_file(&file); - let as_json = serde_json::to_string(&edits).unwrap(); - println!("{}", as_json); + let file_content = fs::read_to_string(&file); + + 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) => { + println!("failed to write formatted content to file: {}", e); + } + } + }, + Err(e) => { + eprintln!("failed to open file: {}", e); + exit(1); + } + } } Ok(()) From e8d011b476085635cd482dba294eb6e5dac01de6 Mon Sep 17 00:00:00 2001 From: Miles Murgaw Date: Sun, 4 Jun 2023 12:48:28 -0400 Subject: [PATCH 2/4] revision: fmt --- src/cli/autoformat/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli/autoformat/mod.rs b/src/cli/autoformat/mod.rs index 61d9a66ab..9a93942f8 100644 --- a/src/cli/autoformat/mod.rs +++ b/src/cli/autoformat/mod.rs @@ -1,5 +1,5 @@ use futures::{stream::FuturesUnordered, StreamExt}; -use std::{process::exit, fs}; +use std::{fs, process::exit}; use super::*; @@ -60,7 +60,7 @@ impl Autoformat { println!("failed to write formatted content to file: {}", e); } } - }, + } Err(e) => { eprintln!("failed to open file: {}", e); exit(1); From 8cf6f60e9390d4e4d0b88528fbf0c055d481a5a0 Mon Sep 17 00:00:00 2001 From: Miles Murgaw Date: Sun, 4 Jun 2023 12:49:22 -0400 Subject: [PATCH 3/4] fix: change error println to eprintln --- src/cli/autoformat/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/autoformat/mod.rs b/src/cli/autoformat/mod.rs index 9a93942f8..2bed439cd 100644 --- a/src/cli/autoformat/mod.rs +++ b/src/cli/autoformat/mod.rs @@ -57,7 +57,7 @@ impl Autoformat { println!("formatted {}", file); } Err(e) => { - println!("failed to write formatted content to file: {}", e); + eprintln!("failed to write formatted content to file: {}", e); } } } From 9d7aeb398b395beb521a679947e1a2457d80eb5a Mon Sep 17 00:00:00 2001 From: Miles Murgaw Date: Sun, 4 Jun 2023 21:40:52 -0400 Subject: [PATCH 4/4] fix: vscode extension autofmt --- extension/src/main.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/extension/src/main.ts b/extension/src/main.ts index e9580d3bb..adaed0a7d 100644 --- a/extension/src/main.ts +++ b/extension/src/main.ts @@ -136,28 +136,26 @@ function fmtDocument(document: vscode.TextDocument) { const [editor,] = vscode.window.visibleTextEditors.filter(editor => editor.document.fileName === document.fileName); if (!editor) return; // Need an editor to apply text edits. - const text = document.getText(); const fileDir = document.fileName.slice(0, document.fileName.lastIndexOf('\\')); - - const child_proc = spawn(serverPath, ["fmt", "--file", text], { + const child_proc = spawn(serverPath, ["fmt", "--file", document.fileName], { cwd: fileDir ? fileDir : undefined, }); let result = ''; child_proc.stdout?.on('data', data => result += data); - type RsxEdit = { + /*type RsxEdit = { formatted: string, start: number, end: number - } + }*/ child_proc.on('close', () => { 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}`); return; } - if (result.length === 0) return; + /*if (result.length === 0) return; // Used for error message: const originalResult = result; @@ -245,9 +243,10 @@ function fmtDocument(document: vscode.TextDocument) { undoStopAfter: false, undoStopBefore: false }); + } 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}`); - } + }*/ }); child_proc.on('error', (err) => { @@ -259,6 +258,7 @@ function fmtDocument(document: vscode.TextDocument) { } + // 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