Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data/annotations/array-annotation.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
public class Xyz {
@Annotation(names = {"", ""}) Object o;
@Annotation(names = { "", "" }) Object o;
}
5 changes: 1 addition & 4 deletions data/control-flow/try-with-resources-multi.java
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
try (
var x = expr;
var y = expr
) { }
try (var x = expr; var y = expr) { }
26 changes: 9 additions & 17 deletions data/wrapping/comments/uncomfortable-block-comment-multiline.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
public
/*
*
*/
void
/*
*
*/
method
/*
*
*/
()
/*
*
*/
;
public /*
*
*/ void /*
*
*/ method /*
*
*/() /*
*
*/;
2 changes: 1 addition & 1 deletion data/wrapping/comments/uncomfortable-block-comment.java
Original file line number Diff line number Diff line change
@@ -1 +1 @@
public /* */ void /* */ method /* */ () /* */;
public /* */ void /* */ method /* */() /* */;
7 changes: 2 additions & 5 deletions data/wrapping/comments/uncomfortable-line-comment.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
public //
void //
method //
() //
;
public
// void // method //() //;
6 changes: 2 additions & 4 deletions data/wrapping/control-flow/for-each.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
for (
var x : myReallyReallyReallyReallyStupidlyLongMemberName
.myReallyReallyReallyReallyStupidlyLongFunctionName()
) { }
for (var x : myReallyReallyReallyReallyStupidlyLongMemberName
.myReallyReallyReallyReallyStupidlyLongFunctionName()) { }
8 changes: 2 additions & 6 deletions data/wrapping/control-flow/for-loop.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
for (
int i = 0;
myReallyReallyReallyReallyStupidlyLongMemberName
.myReallyReallyReallyReallyStupidlyLongFunctionName();
i++
) { }
for (int i = 0; myReallyReallyReallyReallyStupidlyLongMemberName
.myReallyReallyReallyReallyStupidlyLongFunctionName(); i++) { }
4 changes: 1 addition & 3 deletions data/wrapping/control-flow/try-with-resources.java
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
try (
var x = expr
) { }
try (var x = expr) { }
1 change: 1 addition & 0 deletions libs/format/src/format_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ pub struct WrapArguments {
pub child_wrap_prevents_wrap: bool,
pub wrap_with_indent: bool,
pub or_space: bool,
pub has_newline_in_source: bool,
}
52 changes: 7 additions & 45 deletions libs/format/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@
pub(crate) max_line_length: usize,
}

// TODO we need to integrate indentations with this

Check warning on line 33 in libs/format/src/render.rs

View workflow job for this annotation

GitHub Actions / Format

Diff in /home/runner/work/lint/lint/libs/format/src/render.rs
const MAX_LINE_LENGTH: usize = 100;

pub fn prettyprint(formatted: &FormatNode, arguments: &PrettyPrintParameters, parent_wrap: WrapParameters) -> PrettyPrintResult {
// println!("{:?}", formatted);

// Auto-wrapping based on line length is turned off. Instead, newlines are preserved from
// the source code at wrap positions. This means we simply print the tree once without
// checking line lengths and re-rendering with wrapping enabled.
let transformed = print(
formatted,
arguments,
Expand All @@ -46,50 +49,7 @@
parent_wrap,
);

let can_wrap = match formatted {
FormatNode::Group(elements) => elements
.iter()
.any(|element| matches!(element, FormatNode::Wrap { .. })),
_ => false,
};

if can_wrap
&& (transformed.is_wrapped
|| transformed
.result
.lines()
.any(|line| line.len() > arguments.max_line_length))
{
let wrap_because_child = transformed.is_wrapped;
let wrap_because_length = transformed
.result
.lines()
.any(|line| line.len() > arguments.max_line_length);

// TODO sometimes we fail to properly wrap a child, causing our parent to wrap when it shouldn't see unwrappable-child test example
// This check against all children can potentially cause slowdowns as well

let params = WrapParameters {
wrap_because_length,
wrap_because_child,
};

let new = print(formatted, arguments, params, parent_wrap);

if transformed.result.lines().any(|line| line.len() > arguments.max_line_length) {
// panic!("Unexpectedly long line! {:?} {:?}", formatted, transformed);
}

PrettyPrintResult {
result: new.result,
is_wrapped: true,
}
} else {
PrettyPrintResult {
result: transformed.result,
is_wrapped: transformed.is_wrapped,
}
}
transformed
}

// TODO should we build the tree different to avoid needing the parent_wrap struct? I.e. two separate types?
Expand Down Expand Up @@ -121,9 +81,11 @@
wrap_with_indent,
or_space,
child_wrap_prevents_wrap,
has_newline_in_source,
},
) => {
let should_wrap = parent_wrap.wrap_because_length || parent_wrap.wrap_because_child;
// Instead of auto-wrapping based on line length, honor existing newlines in source
let should_wrap = *has_newline_in_source;
let content = prettyprint(&element, arguments, wrap);
let transformed_content = if should_wrap {
"\n".to_owned()
Expand Down Expand Up @@ -151,7 +113,7 @@
// } else {
// "".to_owned()
// }
// }

Check warning on line 116 in libs/format/src/render.rs

View workflow job for this annotation

GitHub Actions / Format

Diff in /home/runner/work/lint/lint/libs/format/src/render.rs
// .into()
}
FormatNode::Indent(element) => indent(prettyprint(element, arguments, wrap).result, arguments.indent_size).into(),
Expand Down
11 changes: 11 additions & 0 deletions libs/format/src/transform/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ pub fn transform<'source>(node: &Tree<'source>) -> FormatNode {
}

if between.wrap {
// Check if there's a newline at this wrap position in the source
let has_newline_in_source = if let Some(previous) = index
.checked_sub(1)
.and_then(|prev_index| node.children().get(prev_index))
{
child.range().start_point.row > previous.range().end_point.row
} else {
false
};

let previous = stack.pop().unwrap();
if let Some(wrapping) = previous.wrapping {
stack.last_mut().unwrap().children.push(FormatNode::Wrap(
Expand All @@ -75,6 +85,7 @@ pub fn transform<'source>(node: &Tree<'source>) -> FormatNode {
child_wrap_prevents_wrap: between.child_wrap_prevents_wrap,
wrap_with_indent: between.indent,
or_space: between.space,
has_newline_in_source,
}),
});

Expand Down
Loading