wip: refactor out translation into its own crate

This commit is contained in:
Jonathan Kelley committed 2022-12-28 11:51:41 -05:00
1 parent 4aac2cdae1
commit 14bc007c15
9 files changed
+153 -2

No files matched your search

+26 -2
View File
@@ -1,3 +1,5 @@
use dioxus_rsx::CallBody;
use crate::buffer::*;
use crate::util::*;
@@ -31,6 +33,11 @@ pub struct FormattedBlock {
/// Format a file into a list of `FormattedBlock`s to be applied by an IDE for autoformatting.
///
/// This function expects a complete file, not just a block of code. To format individual rsx! blocks, use fmt_block instead.
///
/// The point here is to provide precise modifications of a source file so an accompanying IDE tool can map these changes
/// back to the file precisely.
///
/// Nested blocks of RSX will be handled automatically
pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
let mut formatted_blocks = Vec::new();
let mut last_bracket_end = 0;
@@ -93,15 +100,32 @@ pub fn fmt_file(contents: &str) -> Vec<FormattedBlock> {
formatted_blocks
}
pub fn write_block_out(body: CallBody) -> Option<String> {
let mut buf = Buffer {
src: vec![],
indent: 0,
..Buffer::default()
};
// Oneliner optimization
if buf.is_short_children(&body.roots).is_some() {
buf.write_ident(&body.roots[0]).unwrap();
} else {
buf.write_body_indented(&body.roots).unwrap();
}
buf.consume()
}
pub fn fmt_block(block: &str, indent_level: usize) -> Option<String> {
let body = syn::parse_str::<dioxus_rsx::CallBody>(block).unwrap();
let mut buf = Buffer {
src: block.lines().map(|f| f.to_string()).collect(),
indent: indent_level,
..Buffer::default()
};
let body = syn::parse_str::<dioxus_rsx::CallBody>(block).unwrap();
// Oneliner optimization
if buf.is_short_children(&body.roots).is_some() {
buf.write_ident(&body.roots[0]).unwrap();