Skip to content

Commit

Permalink
Merge with main.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuereth committed Nov 8, 2024
1 parent ddd56ea commit eabc00d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/weaver_forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jaq-syn = "1.1.0"
indexmap = "2.6.0"
regex = "1.11.1"
markdown = "=1.0.0-alpha.21"
textwrap = "0.16.1"

itertools.workspace = true
thiserror.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/weaver_forge/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ pub struct CommentFormat {
/// Flag to enforce trailing dots on the comment content.
#[serde(default = "default_bool::<false>")]
pub enforce_trailing_dots: bool,
/// The maximum number of characters in a line.
pub(crate) line_length: Option<usize>,
}

/// The type of indentation to use for the comment.
Expand Down
38 changes: 29 additions & 9 deletions crates/weaver_forge/src/extensions/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,33 +176,49 @@ pub(crate) fn comment(
.map(|v: String| v)
.unwrap_or_else(|_| comment_format.footer.clone().unwrap_or("".to_owned()));

// Configure text-wrap algorithm to appropriately segment lines.
let subsequent_indent = format!("{indent}{prefix}");
// If there is a header, then we need to indent the first line.
let initial_indent = if header.is_empty() {
&prefix
} else {
&subsequent_indent
};
let wrap_options =
textwrap::Options::new(comment_format.line_length.unwrap_or(std::usize::MAX))

Check failure

Code scanning / clippy

usage of a legacy numeric constant Error

usage of a legacy numeric constant

Check failure

Code scanning / clippy

usage of a legacy numeric constant Error

usage of a legacy numeric constant
.initial_indent(initial_indent)
.subsequent_indent(&subsequent_indent)
.wrap_algorithm(textwrap::WrapAlgorithm::FirstFit)
.break_words(false);
// Wrap the comment as configured.
comment = textwrap::fill(&comment, wrap_options);

// The textwrap will leave empty lines, which we want to fill with the prefix.
let mut new_comment = String::new();
for line in comment.lines() {
if !new_comment.is_empty() {
new_comment.push('\n');
}
// We apply "trim" to all split lines.
if header.is_empty() && new_comment.is_empty() {
// For the first line we don't add the indentation
if line.is_empty() && !new_comment.is_empty() {
if comment_format.trim {
new_comment.push_str(format!("{}{}", prefix, line).trim_end());
new_comment.push_str(format!("{indent}{prefix}").trim_end());
} else {
new_comment.push_str(&format!("{}{}", prefix, line));
new_comment.push_str(&format!("{indent}{prefix}"));
}
} else if comment_format.trim {
new_comment.push_str(format!("{}{}{}", indent, prefix, line).trim_end());
new_comment.push_str(line.trim_end());
} else {
new_comment.push_str(&format!("{}{}{}", indent, prefix, line));
new_comment.push_str(line);
}
}
comment = new_comment;

// Add header + footer to the comment.
if !header.is_empty() {
comment = format!("{}\n{}", header, comment);
}

if !footer.is_empty() {
comment = format!("{}\n{}{}", comment, indent, footer);
comment = format!("{}\n{}{}", comment.trim_end(), indent, footer);
}

// Remove all trailing spaces from the comment
Expand Down Expand Up @@ -292,6 +308,7 @@ mod tests {
trim: true,
remove_trailing_dots: true,
enforce_trailing_dots: false,
line_length: None,
},
)]
.into_iter()
Expand Down Expand Up @@ -469,6 +486,7 @@ it's RECOMMENDED to:
trim: true,
remove_trailing_dots: true,
enforce_trailing_dots: false,
line_length: None,
},
)]
.into_iter()
Expand Down Expand Up @@ -530,6 +548,7 @@ it's RECOMMENDED to:
remove_trailing_dots: true,
enforce_trailing_dots: false,
indent_type: Default::default(),
line_length: None,
},
)]
.into_iter()
Expand Down Expand Up @@ -596,6 +615,7 @@ it's RECOMMENDED to:
remove_trailing_dots: false,
enforce_trailing_dots: true,
indent_type: Default::default(),
line_length: None,
},
)]
.into_iter()
Expand Down
1 change: 1 addition & 0 deletions crates/weaver_forge/src/formats/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ mod tests {
trim: true,
remove_trailing_dots: true,
enforce_trailing_dots: false,
line_length: None,
},
)]
.into_iter()
Expand Down
3 changes: 3 additions & 0 deletions crates/weaver_forge/src/formats/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ mod tests {
trim: true,
remove_trailing_dots: true,
enforce_trailing_dots: false,
line_length: None,
},
)]
.into_iter()
Expand Down Expand Up @@ -517,6 +518,7 @@ it's RECOMMENDED to:
remove_trailing_dots: true,
indent_type: Default::default(),
enforce_trailing_dots: false,
line_length: None,
},
)]
.into_iter()
Expand Down Expand Up @@ -557,6 +559,7 @@ The file \[extension\] extracted \[from] the `url.full`, excluding the leading d
remove_trailing_dots: true,
indent_type: Default::default(),
enforce_trailing_dots: false,
line_length: None,
},
)]
.into_iter()
Expand Down

0 comments on commit eabc00d

Please sign in to comment.