Files
dioxus/packages/extension/src/lib.rs
T
Alex Parrill 52fb080193 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
2023-11-01 17:24:18 -04:00

86 lines
2.0 KiB
Rust

//! This file exports functions into the vscode extension
use dioxus_autofmt::{FormattedBlock, IndentOptions, IndentType};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
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, 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 struct FormatBlockInstance {
new: String,
_edits: Vec<FormattedBlock>,
}
#[wasm_bindgen]
impl FormatBlockInstance {
#[wasm_bindgen]
pub fn formatted(&self) -> String {
self.new.clone()
}
#[wasm_bindgen]
pub fn length(&self) -> usize {
self._edits.len()
}
}
#[wasm_bindgen]
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 }
}
#[wasm_bindgen]
pub fn translate_rsx(contents: String, _component: bool) -> String {
// Ensure we're loading valid HTML
let dom = html_parser::Dom::parse(&contents).unwrap();
let callbody = rsx_rosetta::rsx_from_html(&dom);
// Convert the HTML to RSX
dioxus_autofmt::write_block_out(callbody).unwrap()
}