wip: fixing tabs

This commit is contained in:
Jonathan Kelley committed 2022-04-24 20:44:38 -04:00
1 parent 4fa909e864
commit 36ba49fe27
2 files changed
+65 -8

No files matched your search

+18 -6
View File
@@ -15,24 +15,24 @@ use triple_accel::{levenshtein_search, Match};
mod prettyplease;
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, PartialEq, Hash)]
pub struct ForamttedBlock {
pub struct FormattedBlock {
pub formatted: String,
pub start: usize,
pub end: usize,
}
/*
TODO: nested rsx! calls
*/
pub fn formmat_document(contents: &str) -> Vec<ForamttedBlock> {
pub fn get_format_blocks(contents: &str) -> Vec<FormattedBlock> {
let mut matches = levenshtein_search(b"rsx! {", contents.as_bytes()).peekable();
let mut cur_match: Option<Match> = None;
let mut formatted_blocks = Vec::new();
while let Some(item) = matches.next() {
for item in matches {
let Match { start, end, k } = item;
match cur_match {
@@ -54,10 +54,22 @@ pub fn formmat_document(contents: &str) -> Vec<ForamttedBlock> {
if let Some(bracket_end) = find_bracket_end(remaining) {
let sub_string = &contents[end..bracket_end + end - 1];
// with the edge brackets
// println!("{}", &contents[end - 1..bracket_end + end]);
if let Some(new) = fmt_block(sub_string) {
if !new.is_empty() {
formatted_blocks.push(ForamttedBlock {
formatted: new,
// if we have code to push, we want the code to end up on the right lines with the right indentation
let mut output = String::new();
writeln!(output).unwrap();
for line in new.lines() {
writeln!(output, "\t{}", line).ok();
}
formatted_blocks.push(FormattedBlock {
formatted: output,
start: end,
end: end + bracket_end - 1,
});
+47 -2
View File
@@ -109,7 +109,7 @@ rsx!{
"#;
let formatted = formmat_document(block);
let formatted = get_format_blocks(block);
print!("{formatted:?}");
}
@@ -127,7 +127,52 @@ rsx! {
}
"#;
let formatted = formmat_document(src);
let formatted = get_format_blocks(src);
println!("{formatted:?}");
}
#[test]
fn formats_valid_rust_src_with_indents() {
let mut src = r#"
#[inline_props]
fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
const ICON_SIZE: u32 = 36;
rsx! {
div {
h1 {"thing"}
}
}
}
"#
.to_string();
let formatted = get_format_blocks(&src);
let block = formatted.into_iter().next().unwrap();
// println!("{}", &src[block.start..block.end]);
// println!("{}", &src[block.start..block.end - 1]);
src.replace_range(
block.start - 1..block.end + 1,
&format!("{{ {} }}", &block.formatted),
// &format!("{{ {} }}", &block.formatted),
);
println!("{src}");
println!("\tthing");
// for block in formatted {
// println!("{}", block.formatted);
// }
}