Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse soft-wrap infrastructure in :reflow #11738

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

rhogenson
Copy link
Contributor

This uses the single-line comment prefixes from the language config, so it should be able to handle different languages in a robust way. The logic is fairly simple, and doesn't handle block comments, for example.

Fixes #3332, #3622

Thanks for all your work on this editor, it is a joy to use.

@rhogenson rhogenson force-pushed the reflow branch 2 times, most recently from cfb6730 to fd96796 Compare September 21, 2024 04:48
@pascalkuthe
Copy link
Member

We want to move away from textwrap to our own wrapping system. That will allow us to implement these kind of thjngs without resorting to hacks.

@the-mikedavis
Copy link
Member

In particular see #3332 (comment)

@rhogenson
Copy link
Contributor Author

rhogenson commented Sep 21, 2024

Makes sense. Let me see if I can reuse the existing softwrap implementation instead of textwrap.

@rhogenson rhogenson force-pushed the reflow branch 2 times, most recently from 900497c to e66dc93 Compare September 22, 2024 05:31
@rhogenson
Copy link
Contributor Author

I integrated the reflow logic into DocumentFormatter so that we can reuse the existing soft-wrap logic. This does mean that :reflow always preserves existing newlines, so it no longer allows reflowing out to a larger width, but it also makes it more consistent and probably accidentally addresses #2419

@rhogenson rhogenson force-pushed the reflow branch 2 times, most recently from 5038a9c to c7fc362 Compare September 22, 2024 06:32
@the-mikedavis the-mikedavis changed the title Handle single-line comment prefixes in :reflow. Reuse soft-wrap infrastructure in :reflow Dec 31, 2024
Comment on lines +2132 to +2149
continue_comments: Vec::from(
doc.language_config()
.and_then(|config| config.comment_tokens.as_deref())
.unwrap_or(&[]),
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... I'm wondering if it's worth it to just provide an Option<&'a [&str]> instead of a Vec<&str> to avoid the allocation.
I mean the clone isn't that expensive so it should be fine as well.
Just my two cents 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little inconvenient to have TextFormat borrow from doc. For example, having TextFormat borrow from doc breaks this code:

let text_fmt = doc.text_format(viewport.width, None);

Since it's also using doc mutably in other places, having text_format borrow doc immutably causes a compile error. It's not obvious to me how to rewrite that code to avoid the issue.

Languages should typically only have 1 or 2 comment prefixes, so I think a small clone is worth it here to avoid lifetime hell.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, thank you for the insight 👍

Rose Hogenson added 2 commits January 7, 2025 20:34
I changed :reflow to use the DocumentFormatter object instead of the
textwrap crate. This allows using the same logic for soft wrap as
for :reflow. Because the logic is the same as for soft wrap, we end up
preserving all existing newlines, so it's more like "wrap" than reflow,
but I think this behavior makes sense anyway to avoid extraneous diffs.
@rhogenson
Copy link
Contributor Author

I fixed some bugs in the implementation, but there's still one big issue remaining: it breaks URLs across lines since soft-wrap currently wraps at punctuation. I think that's being tracked as a separate issue though (#11428)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reflow doesn't work consistently with comments
4 participants