Use indentation settings in dx fmt and vscode extension

Adds the ability to specify an indent string to public autofmt methods - either
a sequence of spaces or a tab character.

Get the indentation style and size from rustfmt for dx fmt, or from the editor
settings for the vscode extension.

Closes #1595
This commit is contained in:
Alex Parrill committed 2023-11-01 17:24:18 -04:00
1 parent 51f342417a
commit 52fb080193
23 files changed
+283 -51

No files matched your search

+39 -7
View File
@@ -1,17 +1,39 @@
//! This file exports functions into the vscode extension
use dioxus_autofmt::FormattedBlock;
use dioxus_autofmt::{FormattedBlock, IndentOptions, IndentType};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn format_rsx(raw: String) -> String {
let block = dioxus_autofmt::fmt_block(&raw, 0);
pub fn format_rsx(raw: String, use_tabs: bool, indent_size: usize) -> String {
let block = dioxus_autofmt::fmt_block(
&raw,
0,
IndentOptions::new(
if use_tabs {
IndentType::Tabs
} else {
IndentType::Spaces
},
indent_size,
),
);
block.unwrap()
}
#[wasm_bindgen]
pub fn format_selection(raw: String) -> String {
let block = dioxus_autofmt::fmt_block(&raw, 0);
pub fn format_selection(raw: String, use_tabs: bool, indent_size: usize) -> String {
let block = dioxus_autofmt::fmt_block(
&raw,
0,
IndentOptions::new(
if use_tabs {
IndentType::Tabs
} else {
IndentType::Spaces
},
indent_size,
),
);
block.unwrap()
}
@@ -35,8 +57,18 @@ impl FormatBlockInstance {
}
#[wasm_bindgen]
pub fn format_file(contents: String) -> FormatBlockInstance {
let _edits = dioxus_autofmt::fmt_file(&contents);
pub fn format_file(contents: String, use_tabs: bool, indent_size: usize) -> FormatBlockInstance {
let _edits = dioxus_autofmt::fmt_file(
&contents,
IndentOptions::new(
if use_tabs {
IndentType::Tabs
} else {
IndentType::Spaces
},
indent_size,
),
);
let out = dioxus_autofmt::apply_formats(&contents, _edits.clone());
FormatBlockInstance { new: out, _edits }
}
+1 -1
View File
@@ -90,7 +90,7 @@ function fmtDocument(document: vscode.TextDocument) {
if (!editor) return; // Need an editor to apply text edits.
const contents = editor.document.getText();
const formatted = dioxus.format_file(contents);
const formatted = dioxus.format_file(contents, !editor.options.insertSpaces, editor.options.tabSize);
// Replace the entire text document
// Yes, this is a bit heavy handed, but the dioxus side doesn't know the line/col scheme that vscode is using