Improve wrapping logic for hyphenated words#58
Conversation
Reviewer's GuideWrap routines now use a Class diagram for updated wrapping logic with NoHyphenationclassDiagram
class Options {
+new(width: usize)
+word_splitter(splitter: WordSplitter) Options
}
class WordSplitter {
}
class NoHyphenation {
}
WordSplitter <|-- NoHyphenation
Options o-- WordSplitter
class fill {
+fill(text: &str, opts: &Options) -> String
}
class wrap_text {
+wrap_text(lines: &[String], width: usize) -> Vec<String>
}
wrap_text ..> Options : uses
wrap_text ..> fill : uses
fill ..> Options : uses
Options ..> WordSplitter : configures
WordSplitter <|-- NoHyphenation : new splitter
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Summary by CodeRabbit
Summary by CodeRabbit
WalkthroughUpdate the README to clarify that hyphenated words are not split during wrapping. Adjust the core text wrapping logic in the source to disable hyphenation, ensuring hyphenated words move as single units. Add new tests in both unit and integration suites to verify this behaviour. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant CLI
participant WrappingLogic
User->>CLI: Provide text with hyphenated words and --wrap option
CLI->>WrappingLogic: Pass text and wrap settings
WrappingLogic->>WrappingLogic: Process text, avoid splitting hyphenated words
WrappingLogic-->>CLI: Return wrapped text
CLI-->>User: Output wrapped text with hyphenated words intact
Possibly related PRs
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🧰 Additional context used📓 Path-based instructions (1)**/*.rsInstructions used from: Sources:
⚙️ CodeRabbit Configuration File 🧬 Code Graph Analysis (1)tests/integration.rs (1)
⏰ Context from checks skipped due to timeout of 240000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Hey @leynos - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `tests/integration.rs:755` </location>
<code_context>
}
+
+#[test]
+fn test_wrap_hyphenated_word() {
+ let line = format!("{} extremely-very-long-word end", "A".repeat(60));
+ let output = process_stream(&[line]);
+ assert_eq!(
+ output,
+ vec![
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(),
+ "extremely-very-long-word end".to_string(),
+ ]
+ );
+}
</code_context>
<issue_to_address>
Add integration tests for multiple hyphenated words and edge cases.
Please add tests for multiple hyphenated words in a line, hyphenated words at line boundaries, words longer than the wrap width, and lines without hyphenated words to ensure comprehensive coverage.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
#[test]
fn test_wrap_hyphenated_word() {
let line = format!("{} extremely-very-long-word end", "A".repeat(60));
let output = process_stream(&[line]);
assert_eq!(
output,
vec![
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(),
"extremely-very-long-word end".to_string(),
]
);
}
=======
#[test]
fn test_wrap_hyphenated_word() {
let line = format!("{} extremely-very-long-word end", "A".repeat(60));
let output = process_stream(&[line]);
assert_eq!(
output,
vec![
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(),
"extremely-very-long-word end".to_string(),
]
);
}
#[test]
fn test_wrap_multiple_hyphenated_words() {
let line = format!("{} foo-bar baz-qux end", "A".repeat(60));
let output = process_stream(&[line]);
assert_eq!(
output,
vec![
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(),
"foo-bar baz-qux end".to_string(),
]
);
}
#[test]
fn test_wrap_hyphenated_word_at_boundary() {
// The hyphenated word starts at the wrap boundary
let line = format!("{} extremely-very-long-word", "A".repeat(60));
let output = process_stream(&[line]);
assert_eq!(
output,
vec![
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(),
"extremely-very-long-word".to_string(),
]
);
}
#[test]
fn test_wrap_word_longer_than_width() {
// A single word longer than the wrap width (assuming 60)
let long_word = "a".repeat(70);
let output = process_stream(&[long_word.clone()]);
assert_eq!(
output,
vec![
long_word,
]
);
}
#[test]
fn test_wrap_line_without_hyphenated_words() {
let line = format!("{} end of line", "A".repeat(60));
let output = process_stream(&[line]);
assert_eq!(
output,
vec![
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(),
"end of line".to_string(),
]
);
}
>>>>>>> REPLACE
</suggested_fix>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| #[test] | ||
| fn test_wrap_hyphenated_word() { | ||
| let line = format!("{} extremely-very-long-word end", "A".repeat(60)); | ||
| let output = process_stream(&[line]); | ||
| assert_eq!( | ||
| output, | ||
| vec![ | ||
| "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(), | ||
| "extremely-very-long-word end".to_string(), | ||
| ] | ||
| ); | ||
| } |
There was a problem hiding this comment.
suggestion (testing): Add integration tests for multiple hyphenated words and edge cases.
Please add tests for multiple hyphenated words in a line, hyphenated words at line boundaries, words longer than the wrap width, and lines without hyphenated words to ensure comprehensive coverage.
| #[test] | |
| fn test_wrap_hyphenated_word() { | |
| let line = format!("{} extremely-very-long-word end", "A".repeat(60)); | |
| let output = process_stream(&[line]); | |
| assert_eq!( | |
| output, | |
| vec![ | |
| "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(), | |
| "extremely-very-long-word end".to_string(), | |
| ] | |
| ); | |
| } | |
| #[test] | |
| fn test_wrap_hyphenated_word() { | |
| let line = format!("{} extremely-very-long-word end", "A".repeat(60)); | |
| let output = process_stream(&[line]); | |
| assert_eq!( | |
| output, | |
| vec![ | |
| "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(), | |
| "extremely-very-long-word end".to_string(), | |
| ] | |
| ); | |
| } | |
| #[test] | |
| fn test_wrap_multiple_hyphenated_words() { | |
| let line = format!("{} foo-bar baz-qux end", "A".repeat(60)); | |
| let output = process_stream(&[line]); | |
| assert_eq!( | |
| output, | |
| vec![ | |
| "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(), | |
| "foo-bar baz-qux end".to_string(), | |
| ] | |
| ); | |
| } | |
| #[test] | |
| fn test_wrap_hyphenated_word_at_boundary() { | |
| // The hyphenated word starts at the wrap boundary | |
| let line = format!("{} extremely-very-long-word", "A".repeat(60)); | |
| let output = process_stream(&[line]); | |
| assert_eq!( | |
| output, | |
| vec![ | |
| "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(), | |
| "extremely-very-long-word".to_string(), | |
| ] | |
| ); | |
| } | |
| #[test] | |
| fn test_wrap_word_longer_than_width() { | |
| // A single word longer than the wrap width (assuming 60) | |
| let long_word = "a".repeat(70); | |
| let output = process_stream(&[long_word.clone()]); | |
| assert_eq!( | |
| output, | |
| vec![ | |
| long_word, | |
| ] | |
| ); | |
| } | |
| #[test] | |
| fn test_wrap_line_without_hyphenated_words() { | |
| let line = format!("{} end of line", "A".repeat(60)); | |
| let output = process_stream(&[line]); | |
| assert_eq!( | |
| output, | |
| vec![ | |
| "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".to_string(), | |
| "end of line".to_string(), | |
| ] | |
| ); | |
| } |
Summary
NoHyphenationwrap_textTesting
cargo clippy -- -D warningsRUSTFLAGS="-D warnings" cargo testmarkdownlint README.md docs/html-table-support.md docs/release-process.md docs/rust-testing-with-rstest-fixtures.mdhttps://chatgpt.com/codex/tasks/task_e_6873ae41e18c8322950cb5e37d57086b
Summary by Sourcery
Improve text wrapping logic to keep hyphenated words intact by introducing the NoHyphenation word splitter, update documentation, and add corresponding unit and integration tests.
New Features:
Enhancements:
Documentation:
Tests: