Skip to content

Commit

Permalink
format new modules correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jun 25, 2024
2 parents e842435 + ae7d15f commit 768bf01
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
51 changes: 36 additions & 15 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ type GlyphMap = Vec<(CodeSpan, (Loc, Loc))>;

impl<'a> Formatter<'a> {
fn format_top_items(mut self, items: &[Item]) -> (String, GlyphMap) {
self.format_items(items);
self.format_items(items, 0);
let mut output = self.output;
while output.ends_with('\n') {
output.pop();
Expand All @@ -497,10 +497,12 @@ impl<'a> Formatter<'a> {
}
(output, self.glyph_map)
}
fn format_items(&mut self, items: &[Item]) {
for item in items {
self.format_item(item);
self.output.push('\n');
fn format_items(&mut self, items: &[Item], depth: usize) {
for (i, item) in items.iter().enumerate() {
if i > 0 || depth > 0 {
self.newline(depth);
}
self.format_item(item, depth);
}
// Align end-of-line comments
if self.config.align_comments && !self.end_of_line_comments.is_empty() {
Expand Down Expand Up @@ -573,22 +575,34 @@ impl<'a> Formatter<'a> {
self.output = new_output;
}
}
fn format_item(&mut self, item: &Item) {
fn newline(&mut self, depth: usize) {
self.output.push('\n');
self.indent(depth);
}
fn indent(&mut self, depth: usize) {
for _ in 0..self.config.multiline_indent * depth {
self.output.push(' ');
}
}
fn format_item(&mut self, item: &Item, depth: usize) {
match item {
Item::Module(m) => {
self.prev_import_function = None;
self.output.push_str("---");
if let Some(name) = &m.value.name {
self.push(&name.span, &name.value);
}
self.output.push('\n');
self.format_items(&m.value.items);
self.format_items(&m.value.items, depth + 1);
if self.output.ends_with('\n') {
self.output.pop();
}
self.newline(depth);
self.output.push_str("---");
}
Item::Words(lines) => {
self.prev_import_function = None;
let lines = unsplit_words(lines.iter().cloned().flat_map(split_words).collect());
self.format_multiline_words(&lines, false, false, 0);
self.format_multiline_words(&lines, false, false, false, depth);
}
Item::Binding(binding) => {
match binding.words.first().map(|w| &w.value) {
Expand Down Expand Up @@ -627,7 +641,7 @@ impl<'a> Formatter<'a> {
.unwrap_or_else(|| binding.arrow_span.clone());
let mut lines = unsplit_words(split_words(binding.words.clone()));
if lines.len() == 1 {
self.format_words(&lines[0], true, 0);
self.format_words(&lines[0], true, depth);
} else {
lines.push(Vec::new());
self.format_words(
Expand All @@ -638,7 +652,7 @@ impl<'a> Formatter<'a> {
closed: true,
}))],
true,
0,
depth,
);
}
}
Expand Down Expand Up @@ -876,7 +890,13 @@ impl<'a> Formatter<'a> {
self.output.pop();
}
}
self.format_multiline_words(&arr.lines, arr.signature.is_none(), true, depth + 1);
self.format_multiline_words(
&arr.lines,
arr.signature.is_none(),
true,
true,
depth + 1,
);
if arr.boxes {
self.output.push('}');
} else {
Expand Down Expand Up @@ -918,7 +938,7 @@ impl<'a> Formatter<'a> {
self.output.pop();
}
}
self.format_multiline_words(&func.lines, false, true, depth + 1);
self.format_multiline_words(&func.lines, false, true, true, depth + 1);
self.output.push(')');
}
Word::Pack(pack) => {
Expand Down Expand Up @@ -957,7 +977,7 @@ impl<'a> Formatter<'a> {
self.output.pop();
}
}
self.format_multiline_words(&br.value.lines, false, false, depth + 1);
self.format_multiline_words(&br.value.lines, false, false, true, depth + 1);
if any_multiline
&& br.value.lines.last().is_some_and(|line| !line.is_empty())
&& !self.output.trim_end_matches(' ').ends_with('\n')
Expand Down Expand Up @@ -1118,6 +1138,7 @@ impl<'a> Formatter<'a> {
lines: &[Vec<Sp<Word>>],
allow_compact: bool,
allow_leading_space: bool,
allow_trailing_newline: bool,
depth: usize,
) {
if lines.is_empty() {
Expand Down Expand Up @@ -1177,7 +1198,7 @@ impl<'a> Formatter<'a> {
self.format_words(line, true, depth);
}

if !compact {
if allow_trailing_newline && !compact {
if depth > 0 && !lines.iter().last().is_some_and(|line| line.is_empty()) {
self.output.push('\n');
}
Expand Down
15 changes: 2 additions & 13 deletions src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,11 +775,6 @@ impl<'a> Lexer<'a> {
}
fn run(mut self) -> (Vec<Sp<Token>>, Vec<Sp<LexError>>) {
use {self::AsciiToken::*, Token::*};
// Initial scope delimiters
let start = self.loc;
if self.next_chars_exact(["-", "-", "-"]) {
self.end(TripleMinus, start);
}
// Main loop
'main: loop {
let start = self.loc;
Expand All @@ -801,6 +796,7 @@ impl<'a> Lexer<'a> {
"_" => self.end(Underscore, start),
"|" => self.end(Bar, start),
";" => self.end(Semicolon, start),
"-" if self.next_chars_exact(["-", "-"]) => self.end(TripleMinus, start),
"'" if self.next_char_exact("'") => {
if let Some(swiz) = self.array_swizzle() {
self.end(ArraySwizzle(swiz), start)
Expand Down Expand Up @@ -1070,14 +1066,7 @@ impl<'a> Lexer<'a> {
self.end(Number, start)
}
// Newlines
"\n" | "\r\n" => {
self.end(Newline, start);
// Scope delimiters
let start = self.loc;
if self.next_chars_exact(["-", "-", "-"]) {
self.end(TripleMinus, start);
}
}
"\n" | "\r\n" => self.end(Newline, start),
" " | "\t" => {
while self.next_char_exact(" ") || self.next_char_exact("\t") {}
self.end(Spaces, start)
Expand Down
19 changes: 11 additions & 8 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub fn parse(
next_output_comment: 0,
depth: 0,
};
let items = parser.items(true);
let items = parser.items(false);
if parser.errors.is_empty() && parser.index < parser.tokens.len() {
parser.errors.push(
parser
Expand Down Expand Up @@ -282,13 +282,13 @@ impl<'i> Parser<'i> {
));
self.errors.push(err);
}
fn items(&mut self, parse_scopes: bool) -> Vec<Item> {
fn items(&mut self, in_scope: bool) -> Vec<Item> {
let mut items = Vec::new();
while self.try_exact(Newline).is_some() {
self.try_spaces();
}
loop {
match self.try_item(parse_scopes) {
match self.try_item(in_scope) {
Some(item) => items.push(item),
None => {
if self.try_exact(Newline).is_none() {
Expand All @@ -308,7 +308,7 @@ impl<'i> Parser<'i> {
}
items
}
fn try_item(&mut self, parse_scopes: bool) -> Option<Item> {
fn try_item(&mut self, in_scope: bool) -> Option<Item> {
self.try_spaces();
Some(if let Some(binding) = self.try_binding() {
Item::Binding(binding)
Expand All @@ -319,11 +319,16 @@ impl<'i> Parser<'i> {
// Convert multiline words into multiple items
if !lines.is_empty() {
Item::Words(lines)
} else if parse_scopes {
} else {
let backup = self.index;
let start = self.try_exact(TripleMinus.into())?;
self.try_spaces();
let name = self.try_ident();
let items = self.items(false);
if in_scope && name.is_none() {
self.index = backup;
return None;
}
let items = self.items(true);
let span = if let Some(end) = self.try_exact(TripleMinus.into()) {
start.merge(end)
} else {
Expand All @@ -332,8 +337,6 @@ impl<'i> Parser<'i> {
};
let module = ScopedModule { name, items };
Item::Module(span.sp(module))
} else {
return None;
}
})
}
Expand Down

0 comments on commit 768bf01

Please sign in to comment.