fix: autofmt duplicate whitespace on attributes, flickering of vscode autoformat (#5159)
* wip: failing test * edit harness a bit * wip, not fixed entirely * fix comment accumulation and whitespace * fix: autofmt syncrhonize edits with vscode waitUntil edit pipeline * fix optimization collapsing away whitespace * comment out test case for now
This commit is contained in:
1 parent
f537d72918
commit
1025bd69f0
8 files changed
+185
-55
No files matched your search
@@ -312,8 +312,10 @@ impl<'a> Writer<'a> {
|
||||
let mut opt_level = ShortOptimization::NoOpt;
|
||||
|
||||
// check if we have a lot of attributes
|
||||
let attr_len = self.is_short_attrs(attributes, spreads);
|
||||
let is_short_attr_list = (attr_len + self.out.indent_level * 4) < 80;
|
||||
let attr_len = self.is_short_attrs(brace, attributes, spreads);
|
||||
let has_postbrace_comments = self.brace_has_trailing_comments(brace);
|
||||
let is_short_attr_list =
|
||||
((attr_len + self.out.indent_level * 4) < 80) && !has_postbrace_comments;
|
||||
let children_len = self
|
||||
.is_short_children(children)
|
||||
.map_err(|_| std::fmt::Error)?;
|
||||
@@ -331,6 +333,7 @@ impl<'a> Writer<'a> {
|
||||
&& attributes.len() <= 1
|
||||
&& spreads.is_empty()
|
||||
&& !has_trailing_comments
|
||||
&& !has_postbrace_comments
|
||||
{
|
||||
if children.is_empty() {
|
||||
opt_level = ShortOptimization::Oneliner;
|
||||
@@ -595,6 +598,15 @@ impl<'a> Writer<'a> {
|
||||
let attr_line = attr_span.start().line;
|
||||
|
||||
if brace_line != attr_line {
|
||||
// Get the raw line of the attribute
|
||||
let line = self.src.get(attr_line - 1).unwrap_or(&"");
|
||||
|
||||
// Only write comments if the line is empty before the attribute start
|
||||
let row_start = line.get(..attr_span.start().column - 1).unwrap_or("");
|
||||
if !row_start.trim().is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
self.write_comments(attr_span.start())?;
|
||||
}
|
||||
|
||||
@@ -648,16 +660,35 @@ impl<'a> Writer<'a> {
|
||||
return comments;
|
||||
};
|
||||
|
||||
// We go backwards to collect comments and empty lines. We only want to keep one empty line,
|
||||
// the rest should be `//` comments
|
||||
let mut last_line_was_empty = false;
|
||||
for (id, line) in lines.iter().enumerate().rev() {
|
||||
if line.trim().starts_with("//") || line.is_empty() && id != 0 {
|
||||
if id != 0 {
|
||||
let trimmed = line.trim();
|
||||
if trimmed.starts_with("//") {
|
||||
comments.push_front(id);
|
||||
last_line_was_empty = false;
|
||||
} else if trimmed.is_empty() {
|
||||
if !last_line_was_empty {
|
||||
comments.push_front(id);
|
||||
last_line_was_empty = true;
|
||||
}
|
||||
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If there is more than 1 comment, make sure the first comment is not an empty line
|
||||
if comments.len() > 1 {
|
||||
if let Some(&first) = comments.back() {
|
||||
if self.src[first].trim().is_empty() {
|
||||
comments.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
comments
|
||||
}
|
||||
|
||||
@@ -725,7 +756,12 @@ impl<'a> Writer<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_short_attrs(&mut self, attributes: &[Attribute], spreads: &[Spread]) -> usize {
|
||||
fn is_short_attrs(
|
||||
&mut self,
|
||||
_brace: &Brace,
|
||||
attributes: &[Attribute],
|
||||
spreads: &[Spread],
|
||||
) -> usize {
|
||||
let mut total = 0;
|
||||
|
||||
// No more than 3 attributes before breaking the line
|
||||
@@ -746,7 +782,7 @@ impl<'a> Writer<'a> {
|
||||
};
|
||||
}
|
||||
|
||||
let name_len = match &attr.name {
|
||||
total += match &attr.name {
|
||||
AttributeName::BuiltIn(name) => {
|
||||
let name = name.to_string();
|
||||
name.len()
|
||||
@@ -754,7 +790,6 @@ impl<'a> Writer<'a> {
|
||||
AttributeName::Custom(name) => name.value().len() + 2,
|
||||
AttributeName::Spread(_) => unreachable!(),
|
||||
};
|
||||
total += name_len;
|
||||
|
||||
if attr.can_be_shorthand() {
|
||||
total += 2;
|
||||
@@ -1090,6 +1125,13 @@ impl<'a> Writer<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn brace_has_trailing_comments(&self, brace: &Brace) -> bool {
|
||||
let span = brace.span.span();
|
||||
let line = self.src.get(span.start().line - 1).unwrap_or(&"");
|
||||
let after_brace = line.get(span.start().column + 1..).unwrap_or("").trim();
|
||||
after_brace.starts_with("//")
|
||||
}
|
||||
|
||||
fn has_trailing_comments(&self, children: &[BodyNode], brace: &Brace) -> bool {
|
||||
let brace_span = brace.span.span();
|
||||
|
||||
|
||||
Reference in new issue
Block a user