Skip to content

Commit

Permalink
Initial prototype of inserting textwrap into comment filter.
Browse files Browse the repository at this point in the history
Does not text wrapping functionality yet, just proves non-breaking for existing usage.
  • Loading branch information
jsuereth committed Nov 8, 2024
1 parent e425341 commit a282a5c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 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
34 changes: 28 additions & 6 deletions crates/weaver_forge/src/extensions/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::formats::markdown::MarkdownRenderer;
use minijinja::value::{Kwargs, ValueKind};
use minijinja::{Environment, ErrorKind, Value};
use std::collections::HashMap;
use std::usize;

/// Add code-oriented filters to the environment.
///
Expand Down Expand Up @@ -176,26 +177,43 @@ 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(usize::MAX))
.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');
}
if header.is_empty() && new_comment.is_empty() {
// For the first line we don't add the indentation
new_comment.push_str(&format!("{}{}", prefix, line));
if line.is_empty() && !new_comment.is_empty() {
new_comment.push_str(&format!("{indent}{prefix}"));
} 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 @@ -285,6 +303,7 @@ mod tests {
trim: true,
remove_trailing_dots: true,
enforce_trailing_dots: false,
line_length: None,
},
)]
.into_iter()
Expand Down Expand Up @@ -462,6 +481,7 @@ it's RECOMMENDED to:
trim: true,
remove_trailing_dots: true,
enforce_trailing_dots: false,
line_length: None,
},
)]
.into_iter()
Expand Down Expand Up @@ -523,6 +543,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 @@ -589,6 +610,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 a282a5c

Please sign in to comment.