Skip to content

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

Merged
archseer merged 4 commits into
helix-editor:masterfrom
matoous:md/dont-indent-empty-lines
Feb 25, 2022
Merged

fix(commands): don't indent empty lines#1653
archseer merged 4 commits into
helix-editor:masterfrom
matoous:md/dont-indent-empty-lines

Conversation

@matoous

@matoous matoous commented Feb 11, 2022

Copy link
Copy Markdown
Contributor

Fixes: #1642

Comment thread helix-term/src/commands.rs Outdated
doc.text(),
lines.into_iter().map(|line| {
lines.into_iter().filter_map(|line| {
if doc.text().get_line(line).unwrap().as_str().unwrap().trim() == "" {

@matoous matoous Feb 11, 2022

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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.

@matoous matoous Feb 11, 2022

Copy link
Copy Markdown
Contributor Author

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.

@kirawi kirawi Feb 11, 2022

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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.

@matoous matoous Feb 21, 2022

Copy link
Copy Markdown
Contributor Author

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.

@pickfire pickfire Feb 23, 2022

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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

Comment thread helix-term/src/commands.rs Outdated
Comment thread helix-term/src/commands.rs Outdated
@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