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

add option to enforce newline at the end of document #5435

Closed
wants to merge 13 commits into from
Closed

add option to enforce newline at the end of document #5435

wants to merge 13 commits into from

Conversation

Xalfer
Copy link
Contributor

@Xalfer Xalfer commented Jan 6, 2023

resolves #4274

@Xalfer Xalfer changed the title add option to enfore newline at the end of document add option to enforce newline at the end of document Jan 6, 2023
@kirawi kirawi added A-helix-term Area: Helix term improvements S-waiting-on-review Status: Awaiting review from a maintainer. labels Jan 9, 2023
Copy link
Contributor

@filipdutescu filipdutescu left a comment

Choose a reason for hiding this comment

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

I have also worked on this, but you opened before me the PR, so I wanted to drop some suggestions, hope you won't mind. Also, you should update the book doc as well (you can take a look at my change to see where).

helix-view/src/editor.rs Show resolved Hide resolved
@Xalfer Xalfer closed this Jan 19, 2023
@Xalfer Xalfer reopened this Jan 19, 2023
Copy link
Member

@pascalkuthe pascalkuthe left a comment

Choose a reason for hiding this comment

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

I think this is a nice option to have. I do find myself editing EOF chars often which show up as changes in git and mess with some tooling. This PR doesn't change the core editing model of helix (all newline remain selecatble and editable) and only acts on save so it's a small non-intrusive option that I think would be a nice addition. I added some comments about the implementation

helix-view/src/editor.rs Outdated Show resolved Hide resolved
helix-view/src/editor.rs Outdated Show resolved Hide resolved
@pascalkuthe pascalkuthe added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. C-enhancement Category: Improvements E-easy Call for participation: Experience needed to fix: Easy / not much and removed S-waiting-on-review Status: Awaiting review from a maintainer. labels Jan 30, 2023
Comment on lines 1437 to 1440
doc.set_selection(view.id, selection);
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view);
doc.set_selection(view.id, old_selection);
Copy link
Member

Choose a reason for hiding this comment

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

I guess you missed my comment https://github.com/helix-editor/helix/pull/5435/files#r1100873864 here since you resolved the conversation too early. You don't need to call set_selection here at all. Just remove all changes to the selection.

Comment on lines 1437 to 1440
doc.set_selection(view.id, selection);
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view);
doc.set_selection(view.id, old_selection);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
doc.set_selection(view.id, selection);
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view);
doc.set_selection(view.id, old_selection);
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view);

I have applied the same changes in ab0c012 if that's simpler to use.

Copy link
Member

@dead10ck dead10ck left a comment

Choose a reason for hiding this comment

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

I agree with @pascalkuthe here: we shouldn't mess with the selection at all. But otherwise this change seems fine.

I did not realize this before but it was
completely useless.
Copy link
Member

@dead10ck dead10ck left a comment

Choose a reason for hiding this comment

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

This looks good to me. Thanks!

@Xalfer
Copy link
Contributor Author

Xalfer commented Feb 26, 2023

Sorry for not doing it earlier I was lazy didn't find the time to do it. Anyway the whole thing with the selections shouldn't have happened in the first place, but I, for some reason, thought that the selection needed to be applied since Helix is so selection oriented.

let text = doc.text();
let doc_len = text.len_chars();
if config.newline_at_eof && get_line_ending(&text.slice(..)).is_none() {
let view = view_mut!(self);
Copy link
Member

Choose a reason for hiding this comment

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

This will fetch the currently focused view which may not be the view that's looking at the document. This can cause a panic with this feature enabled: open a split with two different files without newline endings and :wa.

I think it might make sense to solve this in helix-term::commands::typed in write_impl and write_all_impl rather than here in Editor. We probably also do not want to add a trailing newline when documents are being auto-formatted because we may end up fighting with the auto-formatter (language server or external command)

@Xalfer Xalfer closed this Apr 6, 2023
zqianem added a commit to zqianem/helix that referenced this pull request Sep 3, 2023
This resolves helix-editor#4274 with the implementation largely based off of helix-editor#5435
and also addresses the review from @the-mikedavis on that PR.

The option name is from EditorConfig's `insert_final_newline`, which is
also used by VS Code as `files.insertFinalNewline`.

We match Vim's behavior in that :w will add the newline to unmodified
files but :wa will not; see helix-editor#1760.

Co-authored by: Xalfer <64538944+Xalfer@users.noreply.github.com>
zqianem added a commit to zqianem/helix that referenced this pull request Sep 3, 2023
This resolves helix-editor#4274 with the implementation largely based off of helix-editor#5435
and also addresses the review from @the-mikedavis on that PR.

The option name is from EditorConfig's `insert_final_newline`, which is
also used by VS Code as `files.insertFinalNewline`.

We match Vim's behavior in that :w will add the newline to unmodified
files but :wa will not; see helix-editor#1760.

Co-authored-by: Xalfer <64538944+Xalfer@users.noreply.github.com>
zqianem added a commit to zqianem/helix that referenced this pull request Sep 3, 2023
This resolves helix-editor#4274 with the implementation largely based off of helix-editor#5435
and also addresses the review from @the-mikedavis on that PR.

The option name is from EditorConfig's `insert_final_newline`, which is
also used by VS Code as `files.insertFinalNewline`.

We match Vim's behavior in that :w will add the newline to unmodified
files but :wa will not; see helix-editor#1760. Tests are included for this.

Co-authored-by: Xalfer <64538944+Xalfer@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-helix-term Area: Helix term improvements C-enhancement Category: Improvements E-easy Call for participation: Experience needed to fix: Easy / not much S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Toggle to enforce a newline at the end of a document
7 participants