Skip to content

Add a new "modified lines" write mode. #2086

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

Merged
merged 7 commits into from
Feb 15, 2018

Conversation

jugglerchris
Copy link
Contributor

This is work towards #1173. I've added a new Modified write mode (not convinced it's a good name; very happy to change to a better one, especially as it's user-exposed).

This builds on top of #2070 (make make_diff work with zero context).

Some concerns I have:

  • The name of the write mode (Modified) is not very clear; maybe UpdatedLines or something like that?
  • The formatting of the output, especially the header line.
    • Perhaps it should be a very simple "123,4,5" format so that it would be a reasonably machine readable format to feed to the RLS?
  • I think the RLS (where this feature is actually wanted AIUI) probably still wants a modified format_input function to get the result directly rather than as text.

@nrc
Copy link
Member

nrc commented Oct 26, 2017

Thanks for the PR! It's a bit tricky to tell exactly what is going on because this is based on #2070.

From a high level, while I think implementing this as a write mode internally is probably right, I don't think it should be user-facing and thus doesn't need a text version of the output.

I'd hope you could provide a function which would call format_input and then post-process the result to make the 'modified lines' data structure.

@jugglerchris
Copy link
Contributor Author

As far as being based on the other PR I was hoping Github would make those changes disappear from here if that was merged, but looks like it doesn't, sorry!

I'll rework based on your feedback (which all makes sense!), thanks!

@jugglerchris
Copy link
Contributor Author

I've had a bit more time to play with this; there's now a function imaginatively called get_modified_lines which returns a type giving the updated lines. It uses format_input internally with an internal write mode which does some simple serialisation (of line numbers etc.) into the out stream.

I'd appreciate some feedback (when convenient; I have kids too!) on whether this is the right direction. Also (either way) whether you'd prefer a new branch or for me to just fix this one. :-)

Copy link
Member

@nrc nrc left a comment

Choose a reason for hiding this comment

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

A couple of comments inline, otherwise looks great! Could you squash the commits please?

src/lib.rs Outdated
pub fn get_modified_lines(
input: Input,
config: &Config,
) -> Result<(Summary, FileMap, FormatReport, ModifiedLines), (io::Error, Summary)> {
Copy link
Member

Choose a reason for hiding this comment

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

Could you return a struct rather than a tuple please? It is more self-documenting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done (feel free to suggest a better name).

/// lineno num_removed num_added
/// followd by (num_added) lines of added text. The line numbers are
/// relative to the original file.
pub fn output_modified<W>(mut out: W, diff: Vec<Mismatch>)
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this function? I thought you removed the facility to write out the 'modified' output

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, it's used to serialise the result (really just line count header lines before the changed lines) into the out passed into write_file. The alternatives (either of which I'm happy to do) are adding another parameter to write_file or coming up with another similar function which had something else instead of the out: &mut T to return the modified lines more directly.

@nrc
Copy link
Member

nrc commented Nov 20, 2017

And it looks like the code is not compiling now.

changed lines from rustfmting.

Squashed commit of the following:

commit e90f9da
Author: Chris Emerson <[email protected]>
Date:   Sat Jan 20 20:10:16 2018 +0000

    Fix tests after merging with master.

commit c3af004
Merge: 0386858 e0e3e22
Author: Chris Emerson <[email protected]>
Date:   Sat Jan 20 17:45:05 2018 +0000

    Merge remote-tracking branch 'origin/master' into HEAD

commit 0386858
Author: Chris Emerson <[email protected]>
Date:   Mon Nov 20 01:57:56 2017 +0000

    Fix some warnings.

commit 162b134
Author: Chris Emerson <[email protected]>
Date:   Mon Nov 20 01:48:02 2017 +0000

    Remove unneeded import.

commit 20cce3c
Merge: 81e9814 fa794f5
Author: Chris Emerson <[email protected]>
Date:   Mon Nov 20 01:07:17 2017 +0000

    Merge branch 'master' into difflines_mode

commit 81e9814
Author: Chris Emerson <[email protected]>
Date:   Mon Nov 20 01:02:50 2017 +0000

    Add a simple "modified lines" test.

commit 018390c
Author: Chris Emerson <[email protected]>
Date:   Thu Nov 2 23:06:21 2017 +0000

    Update test output.

commit 7909f49
Author: Chris Emerson <[email protected]>
Date:   Thu Nov 2 23:03:22 2017 +0000

    Rerun rustfmt.

commit 6275f1a
Merge: 7a66d28 175c0c6
Author: Chris Emerson <[email protected]>
Date:   Thu Nov 2 21:40:29 2017 +0000

    Merge remote-tracking branch 'origin/master' into difflines_mode

commit 7a66d28
Author: Chris Emerson <[email protected]>
Date:   Thu Nov 2 21:36:40 2017 +0000

    WIP: Add a separate API to get changed lines.
    Currently calls format_input() and adjusts the output.

commit c8163a9
Author: Chris Emerson <[email protected]>
Date:   Fri Oct 27 22:53:33 2017 +0100

    Remove "modified" from the documentation again.

commit 94041fa
Merge: acaa3c7 2adf7ee
Author: Chris Emerson <[email protected]>
Date:   Fri Oct 27 22:47:05 2017 +0100

    Merge branch 'master' into difflines_mode

commit acaa3c7
Author: Chris Emerson <[email protected]>
Date:   Tue Oct 24 23:34:14 2017 +0100

    Update the Modified write mode to use `out` instead of just prinln!().

    This means we can test it more easily, so do so.

commit 9f1bbca
Author: Chris Emerson <[email protected]>
Date:   Tue Oct 24 23:11:55 2017 +0100

    Add "Modified" to the various lists of modes.

commit e12f023
Author: Chris Emerson <[email protected]>
Date:   Tue Oct 24 22:57:33 2017 +0100

    Rerun cargo fmt.

commit 0f8a436
Author: Chris Emerson <[email protected]>
Date:   Tue Oct 24 22:46:26 2017 +0100

    Add `line_number_orig` to instances of `Mismatch` in tests.

commit d432a70
Author: Chris Emerson <[email protected]>
Date:   Tue Oct 24 22:41:40 2017 +0100

    Add a `line_number_orig` field to `Mismatch` to track the pre-format line number.
    Use that for the write-mode=modified output.

commit bdb7d1d
Author: Chris Emerson <[email protected]>
Date:   Tue Oct 24 22:35:50 2017 +0100

    First basic --write-mode=modified implementation.

commit ea1433d
Author: Chris Emerson <[email protected]>
Date:   Fri Oct 20 00:04:16 2017 +0100

    WIP on new "modified" mode.

commit 27ee948
Merge: e48dd81 2a84352
Author: Chris Emerson <[email protected]>
Date:   Tue Oct 24 21:56:44 2017 +0100

    Merge remote-tracking branch 'jc/diff_zero_context' into difflines_mode
@jugglerchris
Copy link
Contributor Author

So, two months later (oops!) I've now made the suggested updates. Let me know if you want me to do anything else, and I'll try to be slightly quicker next time!

@@ -1766,7 +1766,7 @@ See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
What Write Mode to use when none is supplied: Replace, Overwrite, Display, Diff, Coverage

- **Default value**: `"Overwrite"`
- **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`
- **Possible values**: `"Checkstyle"`, `"Coverage"`, `"Diff"`, `"Display"`, `"Overwrite"`, `"Plain"`, `"Replace"`, `"Modified"`
Copy link
Member

Choose a reason for hiding this comment

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

Given this is not meant to be a user-facing option, it shouldn't be documented here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

src/config.rs Outdated
@@ -128,6 +128,8 @@ configuration_option_enum! { WriteMode:
Plain,
// Outputs a checkstyle XML file.
Checkstyle,
// Output the changed lines
Modified,
Copy link
Member

Choose a reason for hiding this comment

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

Could you note that this option is only meant to be used via an API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok.

src/config.rs Outdated
@@ -677,7 +679,7 @@ create_config! {
// Control options (changes the operation of rustfmt, rather than the formatting)
write_mode: WriteMode, WriteMode::Overwrite, false,
"What Write Mode to use when none is supplied: \
Replace, Overwrite, Display, Plain, Diff, Coverage";
Replace, Overwrite, Display, Plain, Diff, Coverage, Modified";
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't be here, iiuc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

src/lib.rs Outdated
#[derive(Debug, PartialEq, Eq)]
pub struct ModifiedChunk {
/// The first affected line before formatting.
pub line_number: u32,
Copy link
Member

Choose a reason for hiding this comment

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

To correspond with Mismatch, this should be line_number_orig, the comment might be better phrased as "The first line to be removed from the original text"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sounds like better wording, thanks!

src/lib.rs Outdated
summary: summary,
filemap: filemap,
report: formatreport,
modified_lines: ModifiedLines { chunks: chunks },
Copy link
Member

Choose a reason for hiding this comment

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

nit: s/chunks: chunks/chunks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

src/filemap.rs Outdated
@@ -167,6 +167,15 @@ where
return Ok(has_diff);
}
}
WriteMode::Modified => {
let filename = filename_to_path();
Copy link
Member

Choose a reason for hiding this comment

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

I think we can just do nothing here, or just do the has_diff part and then remove output_modified since I think the important bit of this work is returning the info from get_modified_lines rather than writing something out as in other write modes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Writing to out here is needed as that's how get_modified_lines gets its results (it's passed down via format_input), as there's no other way (as format_input exists at the moment) to pass the results back other than through this T: Write.

I did look initially at factoring the guts of format_input out in the hope of sharing that with get_modified_lines but it seemed hard to avoid ending up with two fairly long and very similar functions. I can look again if you think that's worth it.

@nrc nrc merged commit 091ed2f into rust-lang:master Feb 15, 2018
@nrc
Copy link
Member

nrc commented Feb 15, 2018

Thank you!

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.

2 participants