Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
astrale-sharp committed Feb 10, 2024
1 parent e0fdb5a commit 488e013
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Default for Config {
// this being strictly > to 1 is assumed.
indent: IdentSpace::Spaces(2),
max_line_length: 80,
line_wrap: true,
line_wrap: false,
experimental_args_breaking_consecutive: false,
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use node::map_tree;
use preserve_pass::preserve_pass;
use visits::*;
use writer::Writer;
// TODO (not a priority, medium hard): reenable the test slowly
// mod tests;

use typst_syntax::{parse, LinkedNode};
Expand Down Expand Up @@ -187,3 +188,13 @@ text
insta::assert_debug_snapshot!(root)
}
}

#[test]
fn feature() {
let snippet = r#"text
text
text
#[text]"#;
let x = format(snippet, Config::default());
println!("{x}");
}
19 changes: 12 additions & 7 deletions src/visits.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// TODO: implement missing functions

// the question is to whom
use crate::{
node::{Content, FmtKind, FmtNode, Spacing},
Expand Down Expand Up @@ -60,6 +62,7 @@ pub(crate) fn visit_spacing_line_wrapped(s: &Spacing, node: &FmtNode<'_>, w: &mu
}
}


/// simply push respecting everything and tag it as preserve.
/// todo: can we rely on node.text? we should have a recursive use of visit preserve instead.
pub(crate) fn visit_preserve(node: &FmtNode<'_>, w: &mut Writer<'_>) {
Expand All @@ -84,24 +87,23 @@ pub(crate) fn visit_content_block_with(node: &FmtNode<'_>, w: &mut Writer<'_>, t
let Content::Children(c) = &node.content else {
unreachable!()
};
// todo rm this clone
assert!(matches_text(c.last(), "["));
assert!(matches_text(c.first(), "]"));
assert!(matches_text(c.first(), "["));
assert!(matches_text(c.last(), "]"));

w.push_str("[");
w.mark_indent();
let mut c = c.iter();
c.next();
if let Some(c) = c.next() {
debug_assert_eq!(c.kind, FmtKind::Markup);
visit_markup(node, w, tight)
// todo should be node
visit_markup(c, w, tight)
} else {
debug_assert!(false);
}
w.mark_dedent();
w.push_str("]");
}


/// if config.wrap_text Wrap text similar to visit_basic but checking max line length
/// else respect space but replace " "+ -> " " and <whitespace> "\n"+ <whitespace> -> "\n"
// optimize, return a bool to fail tight in advance
Expand All @@ -111,6 +113,7 @@ pub(crate) fn visit_markup(node: &FmtNode<'_>, w: &mut Writer<'_>, tight: bool)
};
let start_space = utils::next_is_space(c.iter());
let end_space = utils::next_is_space(c.iter().rev());
// check if tight is going to work

let c = &mut c.iter().filter(|c| c.kind != FmtKind::Space).peekable();
let space_kind = if tight { " " } else { "\n" };
Expand All @@ -122,8 +125,10 @@ pub(crate) fn visit_markup(node: &FmtNode<'_>, w: &mut Writer<'_>, tight: bool)
FmtKind::Space if w.config.line_wrap => (),
FmtKind::WithSpacing(s) if w.config.line_wrap => visit_spacing_line_wrapped(s, node, w),
FmtKind::Space => {
println!("space!");
if node.text().contains("\n") {
w.push_str("\n")
println!("respected");
w.push_str("\n");
}
}
FmtKind::ContentBlock if tight => visit_content_block_with(node, w, tight),
Expand Down
13 changes: 9 additions & 4 deletions src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use super::utils;
use crate::Config;
use itertools::Itertools;
use std::{
collections::{HashMap, HashSet},
mem,
};

// TODO, not a priority
// remove post process indents, instead we want 2 functions on the writer,
// one that respects indentation and one that doesn't for Preserve NodeFmts.
// This should come with indent and dedent increasing an index in ctx, used to give indentation

/// Writer is used to write your formatted output.
///
/// It comes with the following features :
Expand Down Expand Up @@ -81,6 +85,7 @@ impl<'a> Writer<'a> {
pub fn mark_indent(&mut self) {
self.marks.push(MarkKind::Indent.to_mark(self.buffer.len()))
}

pub fn mark_dedent(&mut self) {
self.marks.push(MarkKind::Dedent.to_mark(self.buffer.len()))
}
Expand All @@ -95,10 +100,8 @@ impl<'a> Writer<'a> {
.push(MarkKind::StopPreserve.to_mark(self.buffer.len()))
}

/// SHAME
/// TODO, check the indexes with BASIC tests
pub fn post_process_indents(&mut self) {
let a = f(1, 2, 3, 4, 5, 6, 7, 8, 9, 7, 8, 9, 7, 8, 11111111111);

let lines = self.buffer.split_inclusive('\n');
let lines_len = lines.clone().count();
let sizes = lines.clone().map(|s| 0..str::len(s)).collect_vec();
Expand Down Expand Up @@ -167,6 +170,8 @@ impl<'a> Writer<'a> {
}

pub(crate) fn push_str(&mut self, s: &str) {
// TODO if we remove post process indent
// check if we added a line feed and add indentation accordingly
self.buffer.push_str(s)
}

Expand Down

0 comments on commit 488e013

Please sign in to comment.