zed icon indicating copy to clipboard operation
zed copied to clipboard

Soft wrap and rewrap break at different places

Open scottyeager opened this issue 1 year ago • 3 comments

With the same text triggering a editor::rewrap and letting zed soft wrap will break the lines at different locations. E.g. rewrap will generate lines that are 80 chars long, while softwrap will wrap the same text following the 79 character.

Original Issue as reported

Check for existing issues

  • [X] Completed

Describe the bug / provide steps to reproduce it

When using soft wrapping in bounded mode or preferred_line_length mode, I noticed that rewrapping text sometimes results in lines that still get soft wrapped. This seems to be caused by the fact that soft wrapping happens one character sooner than rewrapping. I would expect these to be consistent, such that rewrapping a text means it doesn't get soft wrapped anymore.

Here's the relevant config options:

  "preferred_line_length": 80,
  "soft_wrap": "bounded",

And a sample text that demonstrates the issue for line length of 80:

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.

Here is is before rewrapping:

image

And after:

image

Notice how the cursor is at the end of the soft wrapped line and this is position 80. I guess the soft wrapping actually gives a max line length of 79 but rewrapping includes the 80th character when that's where a word ends.

Zed Version and System Specs

Zed: v0.166.1 (Zed Preview) OS: Arch Linux X11 Memory: 23.2 GiB Architecture: x86_64 GPU: Intel(R) UHD Graphics 620 (KBL GT2) || Intel open-source Mesa driver || Mesa 24.2.5-arch1.1

If applicable, add screenshots or screencasts of the incorrect state / behavior

No response

If applicable, attach your Zed.log file to this issue.

Zed.log

scottyeager avatar Dec 20 '24 03:12 scottyeager

Simple reproduce of the issue you are describing. With the following settings:

  "preferred_line_length": 80,
  "soft_wrap": "bounded",
  1. Paste this into a buffer.
Child: Mister Owl, how many licks does it take to get to the tootsie roll center of a tootsie pop?
  1. Trigger editor::Rewrap
  2. Paste again Results:
Image

Expected results: hard wrapped version of text and soft wrapped version of some text should be displayed identically.

notpeter avatar Jan 20 '25 17:01 notpeter

This issue is still valid, reproduced in 0.184.9 as described in the 2nd comment above.

elpres avatar May 21 '25 07:05 elpres

Removing the bite-size label after discussion with @maxbrunsfeld. We should change how soft wrap behaves here, but it's slightly tricky. Notes:

  • We can't make soft wrap and editor::Rewrap agree in all cases, because editor::Rewrap is allowed to remove space characters, while soft wrap must preserve the ability to put your cursor on every space character in the original text.
  • It would be good to allow soft wrap to put the space character after center on the first line in Peter's example, where it's technically past the wrap limit, instead of forcing it to backtrack to the beginning of center like it does now. (We definitely shouldn't put the space character at the start of the second line.)
  • But we shouldn't do that in SoftWrap::EditorWidth mode, because it would result in a horizontal scroll. So we need to extend this code to pass down two widths instead of one in the SoftWrap::Bounded case, and propagate that down to LineWrapper::wrap_line.

cole-miller avatar Jun 03 '25 17:06 cole-miller