Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Condense output in presence of multiple suggestions #43

Merged
merged 4 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 54 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,38 @@ enum AutofixMode {
Yolo,
}

fn prelude(suggestion: &Replacement) {
let snippet = &suggestion.snippet;
if snippet.text.1.is_empty() {
// Check whether this suggestion wants to be inserted before or after another line
let wants_to_be_on_own_line = suggestion.replacement.ends_with('\n')
|| suggestion.replacement.starts_with('\n');
if wants_to_be_on_own_line {
println!("{}", "Insert line:".yellow().bold());
} else {
println!("{}", "At:".yellow().bold());
println!(
"{lead}{text}{tail}",
lead = indent(4, &snippet.text.0),
Copy link
Member

Choose a reason for hiding this comment

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

Memo for later refactoring: Turn text from (String, String, String) into struct ReplacementText { leading: String, content: String, trailing: String, }

text = "⍉".red(),
Copy link
Member

Choose a reason for hiding this comment

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

What's up with the apl functional symbol circle backslash here? Do you mean to convey that the text is empty here? Then: Did you mean to use the empty set—or actually ε?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just wanted a pretty symbol. I would have preferred something like the google maps "you are here" marker. You know. with the pointy bottom and the round top.

Copy link
Member

Choose a reason for hiding this comment

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

like, ▲ or ▼?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

idc. If you have sth you like, I'll use it.

Copy link
Member

Choose a reason for hiding this comment

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

It's probably best to use something people actually have in their terminal font… so, v or ^? I don't mind either way

Copy link
Member

Choose a reason for hiding this comment

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

if you want to change it, go ahead, otherwise i'm fine with merging with backslashed circle.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll change it when I get back to a PC. Github web editing on mobile is a pain and would produce many commits ;)

tail = snippet.text.2,
);
println!("{}\n", indent(snippet.text.0.len() as u32, "^").red());
println!("{}\n", "insert:".yellow().bold());
}
} else {
println!("{}\n", "Replace:".yellow().bold());
println!(
"{lead}{text}{tail}\n\n\
{with}\n",
with = "with:".yellow().bold(),
lead = indent(4, &snippet.text.0),
text = snippet.text.1.red(),
tail = snippet.text.2,
);
}
}

fn handle_suggestions(
suggestions: Vec<Suggestion>,
mode: AutofixMode,
Expand All @@ -164,19 +196,28 @@ fn handle_suggestions(
let mut i = 0;
for solution in suggestion.solutions.iter() {
println!("\n{}", solution.message);
for suggestion in solution.replacements.iter() {
let snippet = &suggestion.snippet;
println!("[{id}]: {suggestion}\n\n\
{lead}{text}{tail}\n\n\
{with}\n\n\
{replacement}\n",
id = i,
suggestion = "Suggestion - Replace:".yellow().bold(),
lead = indent(4, &snippet.text.0),
text = snippet.text.1.red(),
tail = snippet.text.2,
with = "with:".yellow().bold(),
replacement = indent(4, &suggestion.replacement));

// check whether we can squash all suggestions into a list
if solution.replacements.len() > 1 {
let first = solution.replacements[0].clone();
let all_suggestions_replace_the_same_span = solution
.replacements
.iter()
.all(|s| first.snippet.file_name == s.snippet.file_name
&& first.snippet.line_range == s.snippet.line_range);
if all_suggestions_replace_the_same_span {
prelude(&first);
for suggestion in solution.replacements.iter() {
Copy link
Member

Choose a reason for hiding this comment

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

Didn't clippy suggest &solution.replacements here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think clippy throws up all kinds of things when used on rustfix. I never used it ;) I should. And I should feel bad for not having it done yet. ... Ah now I remember I wanted to fix rustfix with rustfix, but some clippy suggestions were breaking it. So I wanted to fix those first. I'm probably trapped in a loop

println!("[{}]: {}", i, suggestion.replacement.trim());
i += 1;
}
continue;
}
}
for suggestion in &solution.replacements {
print!("[{}]: ", i);
prelude(&suggestion);
println!("{}", indent(4, &suggestion.replacement));
i += 1;
}
}
Expand Down
15 changes: 6 additions & 9 deletions tests/tests/libui-rs/simple/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
 --> src/controls.rs:245:17-245:78

try
[0]: Suggestion - Replace:
[0]: Replace:

mem::transmute::<*mut c_void, &mut Box<FnMut(&Button)>>(data)(&button)

with:

&mut *(data as *mut Box<FnMut(&Button)>)


==> What do you want to do? [0-9] | [r]eplace | [s]kip | save and [q]uit | [a]bort (without saving)
 > Suggestion accepted. I'll remember that and apply it later.

Expand All @@ -21,15 +20,14 @@ try
 --> src/controls.rs:350:17-350:77

try
[0]: Suggestion - Replace:
[0]: Replace:

mem::transmute::<*mut c_void, &mut Box<FnMut(&Entry)>>(data)(&entry);

with:

&mut *(data as *mut Box<FnMut(&Entry)>)


==> What do you want to do? [0-9] | [r]eplace | [s]kip | save and [q]uit | [a]bort (without saving)
 > Suggestion accepted. I'll remember that and apply it later.

Expand All @@ -38,11 +36,11 @@ try
 --> src/controls.rs:373:5-378:6

try this
[0]: Suggestion - Replace:
[0]: At:
⍉pub fn new() -> Entry {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fun bug, but this is a clippy bug, so I won't fix it here. Clippy should be placing this suggestion properly.

^

pub fn new() -> Entry {

with:
insert:

impl Default for controls::Entry {
fn default() -> Self {
Expand All @@ -52,7 +50,6 @@ try this




==> What do you want to do? [0-9] | [r]eplace | [s]kip | save and [q]uit | [a]bort (without saving)
 > Thanks for playing!
Good work. Let me just apply these 2 changes!
Expand Down
15 changes: 6 additions & 9 deletions tests/tests/libui-rs/skipping/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
 --> src/controls.rs:245:17-245:78

try
[0]: Suggestion - Replace:
[0]: Replace:

mem::transmute::<*mut c_void, &mut Box<FnMut(&Button)>>(data)(&button)

with:

&mut *(data as *mut Box<FnMut(&Button)>)


==> What do you want to do? [0-9] | [r]eplace | [s]kip | save and [q]uit | [a]bort (without saving)
 > Suggestion accepted. I'll remember that and apply it later.

Expand All @@ -21,15 +20,14 @@ try
 --> src/controls.rs:350:17-350:77

try
[0]: Suggestion - Replace:
[0]: Replace:

mem::transmute::<*mut c_void, &mut Box<FnMut(&Entry)>>(data)(&entry);

with:

&mut *(data as *mut Box<FnMut(&Entry)>)


==> What do you want to do? [0-9] | [r]eplace | [s]kip | save and [q]uit | [a]bort (without saving)
 > Skipped.

Expand All @@ -38,11 +36,11 @@ try
 --> src/controls.rs:373:5-378:6

try this
[0]: Suggestion - Replace:
[0]: At:
⍉pub fn new() -> Entry {
^

pub fn new() -> Entry {

with:
insert:

impl Default for controls::Entry {
fn default() -> Self {
Expand All @@ -52,7 +50,6 @@ try this




==> What do you want to do? [0-9] | [r]eplace | [s]kip | save and [q]uit | [a]bort (without saving)
 > Thanks for playing!
Good work. Let me just apply these 1 changes!
Expand Down
Loading