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

fix(commands): don't indent empty lines #1653

Merged
merged 4 commits into from
Feb 25, 2022
Merged

fix(commands): don't indent empty lines #1653

merged 4 commits into from
Feb 25, 2022

Conversation

matoous
Copy link
Contributor

@matoous matoous commented Feb 11, 2022

Fixes: #1642

@@ -5125,9 +5125,12 @@ fn indent(cx: &mut Context) {

let transaction = Transaction::change(
doc.text(),
lines.into_iter().map(|line| {
lines.into_iter().filter_map(|line| {
if doc.text().get_line(line).unwrap().as_str().unwrap().trim() == "" {
Copy link
Contributor Author

@matoous matoous Feb 11, 2022

Choose a reason for hiding this comment

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

Not sure what's the best approach here, initially I was checking that the length of the line is equal to 1 (new line) but that could break for "\r\n". On the other hand, the current approach will skip also lines that already have for example a few spaces (which might be desired).

Copy link
Member

Choose a reason for hiding this comment

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

Vim indents lines with only whitespace too, but I think it's okay (desirable even) to skip them.

Copy link
Contributor Author

@matoous matoous Feb 11, 2022

Choose a reason for hiding this comment

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

I have the same opinion but wanted to leave it open for discussion.

Copy link
Member

@kirawi kirawi Feb 11, 2022

Choose a reason for hiding this comment

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

You can use doc.text().line(line) to avoid an unwrap.

Also, https://docs.rs/ropey/1.3.2/ropey/struct.RopeSlice.html#method.as_str

For large slices this method will typically fail and return None because large slices usually cross chunk boundaries in the rope.

Overall, I prefer Cow::from(doc.text().line(line)).trim().is_empty()

Copy link
Member

Choose a reason for hiding this comment

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

What about in languages where whitespace indentation is meaningful?

If we only check for empty lines then this could be simplified .line(...).len_chars() == 0

Copy link
Member

Choose a reason for hiding this comment

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

Let's use Cow::from(doc.text().line(line)).trim().is_empty() to avoid the unwrap.

Copy link
Contributor Author

@matoous matoous Feb 21, 2022

Choose a reason for hiding this comment

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

Updated, thanks for the suggestion.

Copy link
Contributor

@pickfire pickfire Feb 23, 2022

Choose a reason for hiding this comment

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

I think this is better although it is longer, it does not need any allocations.

            if doc.text().line(line).chunks().all(|s| s.trim().is_empty()) {       

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, thanks for suggestion, updated.

Copy link
Member

Choose a reason for hiding this comment

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

Seems to still use the old suggestion though

@archseer archseer merged commit 951fd1c into helix-editor:master Feb 25, 2022
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.

Indent adds spaces on empty lines
6 participants