From df264ffbb4328b8916034c317a1a3c35b5eadcb6 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Tue, 25 Feb 2025 16:15:41 -0800 Subject: [PATCH] Only insert hard-wraps at whitespace. This is a better behavior for :reflow since it prevents breaking URLs across lines. --- helix-core/src/doc_formatter.rs | 8 +++----- helix-core/src/doc_formatter/test.rs | 1 + helix-term/src/commands/typed.rs | 1 + helix-term/tests/test/commands.rs | 4 ++-- helix-view/src/annotations/diagnostics.rs | 1 + helix-view/src/document.rs | 1 + 6 files changed, 9 insertions(+), 7 deletions(-) diff --git a/helix-core/src/doc_formatter.rs b/helix-core/src/doc_formatter.rs index e841475b4..8ab4cdfda 100644 --- a/helix-core/src/doc_formatter.rs +++ b/helix-core/src/doc_formatter.rs @@ -136,10 +136,6 @@ impl<'a> GraphemeWithSource<'a> { fn width(&self) -> usize { self.grapheme.width() } - - fn is_word_boundary(&self) -> bool { - self.grapheme.is_word_boundary() - } } #[derive(Debug, Clone)] @@ -153,6 +149,7 @@ pub struct TextFormat { pub viewport_width: u16, pub soft_wrap_at_text_width: bool, pub continue_comments: Vec, + pub is_word_boundary: fn(&Grapheme) -> bool, } // test implementation is basically only used for testing or when softwrap is always disabled @@ -168,6 +165,7 @@ impl Default for TextFormat { wrap_indicator_highlight: None, soft_wrap_at_text_width: false, continue_comments: Vec::new(), + is_word_boundary: |g| g.is_word_boundary(), } } } @@ -418,7 +416,7 @@ impl<'t> DocumentFormatter<'t> { self.indent_level = None; } - let is_word_boundary = grapheme.is_word_boundary(); + let is_word_boundary = (self.text_fmt.is_word_boundary)(&grapheme.grapheme); word_width += grapheme.width(); self.word_buf.push(grapheme); diff --git a/helix-core/src/doc_formatter/test.rs b/helix-core/src/doc_formatter/test.rs index e4b76bfb2..4e7351768 100644 --- a/helix-core/src/doc_formatter/test.rs +++ b/helix-core/src/doc_formatter/test.rs @@ -14,6 +14,7 @@ impl TextFormat { viewport_width: 17, soft_wrap_at_text_width: false, continue_comments: Vec::new(), + is_word_boundary: |g| g.is_word_boundary(), } } } diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 48f595a92..1c8ee546e 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -2175,6 +2175,7 @@ fn reflow(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyho .and_then(|config| config.comment_tokens.as_deref()) .unwrap_or(&[]), ), + is_word_boundary: |g| g.is_whitespace(), }; let annotations = TextAnnotations::default(); diff --git a/helix-term/tests/test/commands.rs b/helix-term/tests/test/commands.rs index b7eddaaf3..c59873ef5 100644 --- a/helix-term/tests/test/commands.rs +++ b/helix-term/tests/test/commands.rs @@ -892,9 +892,9 @@ bla]#", .await?; test(( - "#[|Very_long_words_should_not_be_broken_by_hard_wrap]#", + "#[|Very-long-words-should-not-be-broken-by-hard-wrap]#", ":reflow 2", - "#[|Very_long_words_should_not_be_broken_by_hard_wrap]#", + "#[|Very-long-words-should-not-be-broken-by-hard-wrap]#", )) .await?; diff --git a/helix-view/src/annotations/diagnostics.rs b/helix-view/src/annotations/diagnostics.rs index 17e7ae85a..e02ce34a3 100644 --- a/helix-view/src/annotations/diagnostics.rs +++ b/helix-view/src/annotations/diagnostics.rs @@ -103,6 +103,7 @@ impl InlineDiagnosticsConfig { viewport_width: width, soft_wrap_at_text_width: true, continue_comments: Vec::new(), + is_word_boundary: |g| g.is_word_boundary(), } } } diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index d90e0ff69..5a1d51ddb 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -2222,6 +2222,7 @@ impl Document { .map(Highlight), soft_wrap_at_text_width, continue_comments: Vec::new(), + is_word_boundary: |g| g.is_word_boundary(), } }