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 config for default line ending #5621

Merged
merged 1 commit into from
Jun 16, 2023

Conversation

CptPotato
Copy link
Contributor

@CptPotato CptPotato commented Jan 21, 2023

I've finally gotten around to giving this a shot.

This adds a key to the editor config that allows specifying the default line ending for new documents. I added variants for native, lf, crlf, ff, cr or nel. native uses the platform's native line ending (crlf on Windows, otherwise lf). The default is native, so the out-of-the-box behavior remains unchanged.

One thing I noticed is that the enum naming is a little odd - Crlf vs. LF. But I kept the existing variant names.

closes #4378

@CptPotato
Copy link
Contributor Author

I'm not quite sure what I should do with this test:

#[test]
fn test_line_ending() {
assert_eq!(
Document::default().text().to_string(),
DEFAULT_LINE_ENDING.as_str()
);
}

There's no way to create a document without specifying the desired line ending now. Adding one would make this test kinda useless.

@CptPotato CptPotato force-pushed the default-line-ending branch from e601faf to 26c49df Compare January 21, 2023 11:18
@the-mikedavis the-mikedavis added A-helix-term Area: Helix term improvements S-waiting-on-review Status: Awaiting review from a maintainer. labels Jan 21, 2023
@pascalkuthe
Copy link
Member

I'm not quite sure what I should do with this test:

#[test]
fn test_line_ending() {
assert_eq!(
Document::default().text().to_string(),
DEFAULT_LINE_ENDING.as_str()
);
}

There's no way to create a document without specifying the desired line ending now. Adding one would make this test kinda useless.

I think test mostly ensures that new documents just contain a newline ending and nothing else so I would just use LineEnding::LF and assert that the document text is equal to \n.

This test wasn't testing the correct detection if the default line ending anyway (as it was just comparing to the same constant used in the implememtation) so no need to start now

@CptPotato
Copy link
Contributor Author

CptPotato commented Jan 23, 2023

I think test mostly ensures that new documents just contain a newline ending and nothing else so I would just use LineEnding::LF and assert that the document text is equal to \n.

Alright, the test is fixed now. I added Crlf to the test as well, to have a test case with two characters.

Edit: actually, since tests are run on all platforms I'm now using LineEnding::native() in all tests. I also missed some tests at first since I wasn't running cargo with the --workspace flag, though it should be good to go now. I hope all the force pushes don't get confusing 👀

@CptPotato CptPotato force-pushed the default-line-ending branch 3 times, most recently from d264500 to 8dc07ca Compare January 23, 2023 20:59
return LineEnding::Crlf;
#[cfg(not(target_os = "windows"))]
return LineEnding::LF;
}
Copy link
Contributor

@g-re-g g-re-g Jan 27, 2023

Choose a reason for hiding this comment

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

I'm probably missing something here, but if the return of this function is going to be both constant and baked in at compile time, is there a reason it can't be a regular const like before? Perhaps with a different name:

#[cfg(target_os = "windows")]
pub const NATIVE_LINE_ENDING: LineEnding = LineEnding::Crlf;
#[cfg(not(target_os = "windows"))]
pub const NATIVE_LINE_ENDING: LineEnding = LineEnding::LF;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, you're right, it technically does the same thing. I initially removed DEFAULT_LINE_ENDING because because it made refactoring much easier - i.e. everything that depends on that constant needs to be changed, since it's not constant anymore.

Apart from that, having it tied to LineEnding made more sense to me so I added it there. Though, I don't have a strong preference for this, so if there's a consensus I can change it back to a constant.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh word I gotcha. If you'd like it to still be a constant and also tied to the LineEnding enum you can actually put constants in an enum's impl:

impl LineEnding {
    #[cfg(target_os = "windows")]
    pub const NATIVE_LINE_ENDING: LineEnding = LineEnding::Crlf;
    #[cfg(not(target_os = "windows"))]
    pub const NATIVE_LINE_ENDING: LineEnding = LineEnding::LF;


    ...

And then refer to it like a constant qualified by the enum:

let doc = Document::from(rope, None, LineEnding::NATIVE_LINE_ENDING);

Copy link
Member

Choose a reason for hiding this comment

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

If it's not absolutely necessary I would prefer if we could keep the constant same as it is currently in master. I think it's readable and used elsewhere and changes like these introduce loads of merge conflicts in other PRs. If there is no functional reason that this needs to be refactored (which I don't think there is) then I would prefer to just keep it as in master an only change the type of the constant.

Copy link
Contributor Author

@CptPotato CptPotato Jan 27, 2023

Choose a reason for hiding this comment

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

If it's not absolutely necessary I would prefer if we could keep the constant same as it is currently in master. I think it's readable and used elsewhere and changes like these introduce loads of merge conflicts in other PRs. If there is no functional reason that this needs to be refactored (which I don't think there is) then I would prefer to just keep it as in master an only change the type of the constant.

I'll do that then, thanks for the feedback. Though it's worth noting that this will still cause merge conflicts due to the rename. Keeping the same constant name DEFAULT_LINE_ENDING is out of question in my opinion because it is not the default line ending (anymore), but the platform's native one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just changed it back to a const.

@CptPotato CptPotato mentioned this pull request Jan 27, 2023
@CptPotato
Copy link
Contributor Author

Just merged current master due to conflicts.

Since there's a config parameter for document creation I'm now using that to get the default line ending config. Though I'm not sure if I'm accessing it correctly using config.load().default_line_ending.into()

@pascalkuthe
Copy link
Member

pascalkuthe commented Jan 31, 2023

Just merged current master due to conflicts.

Since there's a config parameter for document creation I'm now using that to get the default line ending config. Though I'm not sure if I'm accessing it correctly using config.load().default_line_ending.into()

That usage is correct 👍 The config field of Document acts always the same as the editor config field so use it the same as you would use that. I added that field in #5420 as otherwise we would need to add a config paramter to a ton of functions which would have been very nosy and accessing config trough the document is actually something that often comes in handy

@CptPotato CptPotato force-pushed the default-line-ending branch from 68e4738 to adbd70d Compare May 19, 2023 09:37
@CptPotato
Copy link
Contributor Author

Squashed and rebased onto latest master

@the-mikedavis the-mikedavis merged commit 3fb9faf into helix-editor:master Jun 16, 2023
Triton171 pushed a commit to Triton171/helix that referenced this pull request Jun 18, 2023
wes-adams pushed a commit to wes-adams/helix that referenced this pull request Jul 4, 2023
mtoohey31 pushed a commit to mtoohey31/helix that referenced this pull request Jun 2, 2024
smortime pushed a commit to smortime/helix that referenced this pull request Jul 10, 2024
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 S-waiting-on-review Status: Awaiting review from a maintainer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support config line ending in config.toml?
4 participants