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

Make indent preserve the of \n in the input string (stop adding a trailing \n) #209

Closed
wants to merge 1 commit into from

Conversation

jRimbault
Copy link
Contributor

@jRimbault jRimbault commented Sep 17, 2020

Wait for split_inclusive to be stable before merging.

closes #207

@mgeisler mgeisler changed the title Fix indent Let indent keep number of \n unchanged, in particular, avoid adding a trailing \n if the input string does not end in \n Sep 18, 2020
@mgeisler mgeisler changed the title Let indent keep number of \n unchanged, in particular, avoid adding a trailing \n if the input string does not end in \n Make indent preserve the of \n in the input string (stop adding a trailing \n) Sep 18, 2020
src/indentation.rs Outdated Show resolved Hide resolved
src/indentation.rs Outdated Show resolved Hide resolved
"//bar",
"// baz"];
assert_eq!(indent(&add_nl(&x), "//"), add_nl(&y));
let text = r##" foo
Copy link
Owner

Choose a reason for hiding this comment

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

I tried to use the add_nl helper function together with a vector to align the lines in the source code and to make it crystal clear if a string has trailing whitespace. I would prefer to keep this somehow, but I'm open for better ways of doing it :-)

Copy link
Contributor Author

@jRimbault jRimbault Sep 26, 2020

Choose a reason for hiding this comment

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

What do you think would be good way to signal trailing whitespaces are intentional ?

Copy link
Owner

@mgeisler mgeisler Sep 26, 2020

Choose a reason for hiding this comment

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

Hmm... yeah, good question. I guess the most direct way would be to include things like \n in the strings, and then join them without adding a separator. So perhaps we can throw out the add_nl function and let the tests be explicit:

        let lines = &[
            "foo\n", //
            "bar\n",
        ];
        let expected = &[
            "  foo\n", //
            "  bar\n",
        ];
        assert_eq!(&indent(&lines.join(""), "  "), &expected.join("")); 

the // makes rustfmt align things consistently so that it is easy to compare the two lists visually. I like that each line is shown in full, with escape characters and everything. Trailing whitespace would then be

        let lines = &[
            "foo  \n", //
            "bar    ",
        ];
        let expected = &[
            "  foo  \n", //
            "  bar    ",
        ];
        assert_eq!(&indent(&lines.join(""), "  "), &expected.join("")); 

If you want, you could try updating the existing tests to use this scheme. It would be a good preparation for this PR.

Copy link
Contributor Author

@jRimbault jRimbault Sep 26, 2020

Choose a reason for hiding this comment

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

I'll get on it Monday :)

And thanks for the review !

Edit: had time tonight, so...

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks, the explicit lists look good!

Can I get you to squash all your commits into a single commit with a commit message such as

Make indent preserve the lack of a trailing \n

Before, indent("foo", " ") would give " foo\n". It now preserves any trailing \n present in the input string. This makes indent behave consistently with dedent. New tests were added to ensure this on a number of corner cases.

You could also mention that we now require Rust version N.NN (when we know this at some point).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure :)

Before, indent("foo", "") would give "foo\n".
It now preserves any trailing newline character present in the
input string. This makes indent behave consistently with dedent.
New tests ere added to ensure this on a number of corner cases.

closes mgeisler#207
@mgeisler
Copy link
Owner

I just noticed that a stabilization PR has been put up for the split_inclusive feature: rust-lang/rust#77858 — I'm not sure how long it will take to be reviewed, though.

@mgeisler
Copy link
Owner

mgeisler commented Jan 21, 2021

This was solved in a simpler way in #279 so I'm closing this now. Thanks @jRimbault!

@mgeisler mgeisler closed this Jan 21, 2021
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.

The indent function always adds a final \n, even if the input string has no \n
2 participants