2023-07-08 21:24:01 +00:00
|
|
|
//! This file exports functions into the vscode extension
|
|
|
|
|
2023-07-09 02:27:25 +00:00
|
|
|
use dioxus_autofmt::FormattedBlock;
|
2023-07-08 21:24:01 +00:00
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2023-07-09 02:27:25 +00:00
|
|
|
pub fn format_rsx(raw: String) -> String {
|
|
|
|
let block = dioxus_autofmt::fmt_block(&raw, 0);
|
|
|
|
block.unwrap()
|
2023-07-08 21:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2023-07-09 02:27:25 +00:00
|
|
|
pub fn format_selection(raw: String) -> String {
|
2023-07-08 21:24:01 +00:00
|
|
|
let block = dioxus_autofmt::fmt_block(&raw, 0);
|
|
|
|
block.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
2023-07-09 02:27:25 +00:00
|
|
|
pub struct FormatBlockInstance {
|
|
|
|
new: String,
|
|
|
|
_edits: Vec<FormattedBlock>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
impl FormatBlockInstance {
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn formatted(&self) -> String {
|
|
|
|
self.new.clone()
|
|
|
|
}
|
2023-07-09 02:36:02 +00:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn length(&self) -> usize {
|
|
|
|
self._edits.len()
|
|
|
|
}
|
2023-07-09 02:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn format_file(contents: String) -> FormatBlockInstance {
|
|
|
|
let _edits = dioxus_autofmt::fmt_file(&contents);
|
|
|
|
let out = dioxus_autofmt::apply_formats(&contents, _edits.clone());
|
|
|
|
FormatBlockInstance { new: out, _edits }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn translate_rsx(contents: String, _component: bool) -> String {
|
2023-07-08 21:24:01 +00:00
|
|
|
// 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
|
|
|
|
let out = dioxus_autofmt::write_block_out(callbody).unwrap();
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|