nrwl / precise-commits

:sparkles: Painlessly apply Prettier by only formatting lines you have modified anyway!
MIT License
471 stars 20 forks source link

fix(windows): Windows compatibility #60

Closed djcsdy closed 2 years ago

djcsdy commented 4 years ago

This PR adds support to build, test, and run precise-commits on Windows. It also adds support for CRLF line separators, which are the preferred line separators on Windows. For completeness it also adds support for CR line separators although these are rarely used.

Unix-specific CLI tools used by the test scripts are replaced by pure-JavaScript alternatives:

I have rewritten calculateCharacterRangesFromLineChanges to properly support files with CRLF or CR separators. Any mix of line separators within a single file is also properly supported. Performance should also be improved since the routine now iterates through the text contents only once, whereas previously it iterated over portions of the file multiple times.

I also added additional tests that verify calculateCharacterRangesFromLineChanges works as expected with different line separators.

The behaviour of calculateCharacterRangesFromLineChanges is subtly different than before, even for LF files. Previously the function would return a character range that starts before the line separator preceding the changed lines, and ends before the line separator that follows the changed lines. With this change, the character range begins after the line separator that precedes the changed lines, and ends after the line separator that follows the changed lines. I think this makes more sense. I have updated the test snapshots for calculateCharacterRangesFromLineChanges to reflect this change. The formatting behaviour of precise-commits does not change either way. This can be verified since there was no need to update the test snapshots for index.spec.ts.

mannyhagman commented 4 years ago

Have you thought about forking this? I doubt this will ever be reviewed.

djcsdy commented 4 years ago

@mannyhagman I do intend to fork this project at some point if it continues to be unmaintained.

djcsdy commented 2 years ago

For anyone who is still interested (@mannyhagman?), I am maintaining a fork at @softwareventures/precise-commits.

Compared to this project, it fixes a bunch of bugs, and adds support for Windows and CRLF line breaks.

I've been using it for a couple of months on my projects and it works well. There are still some pathological cases where it does something weird, but I've never seen it break my code. If you use it on a project where the existing indentation is consistently different to the indentation you configure for prettier, then you are going to have a bad time, but otherwise it is suitable for general use.

You can install it from npm @softwareventures/precise-commits.