@@ -16,8 +16,12 @@ import 'package:dartfix/listener/bad_message_listener.dart';
1616import 'package:dartfix/src/context.dart' ;
1717import 'package:dartfix/src/options.dart' ;
1818import 'package:dartfix/src/util.dart' ;
19+ import 'package:path/path.dart' as path;
1920import 'package:pub_semver/pub_semver.dart' ;
2021
22+ import 'migrate/display.dart' ;
23+ import 'util.dart' ;
24+
2125class Driver {
2226 Context context;
2327 _Handler handler;
@@ -140,20 +144,7 @@ class Driver {
140144
141145 /// Return `true` if the changes should be applied.
142146 bool shouldApplyFixes (EditDartfixResult result) {
143- if (result.edits.isEmpty) {
144- logger.stdout ('' );
145- logger.stdout (result.otherSuggestions.isNotEmpty
146- ? 'None of the recommended changes can be automatically applied.'
147- : 'There are no recommended changes.' );
148- return false ;
149- }
150- if (overwrite || force) {
151- return true ;
152- }
153- logger.stdout ('' );
154- logger.stdout ('Would you like to apply these changes? (yes/no)' );
155- var response = stdin.readLineSync ();
156- return response.toLowerCase () == 'yes' ;
147+ return overwrite || force;
157148 }
158149
159150 void showDescriptions (String title, List <DartFixSuggestion > suggestions) {
@@ -241,8 +232,6 @@ These fixes can be enabled using --$includeFixOption:''');
241232 Progress progress;
242233 if (options.showHelp) {
243234 progress = logger.progress ('${ansi .emphasized ('Listing fixes' )}' );
244- } else if (options.isUpgrade) {
245- progress = logger.progress ('${ansi .emphasized ('Calculating changes' )}' );
246235 } else {
247236 progress = logger.progress ('${ansi .emphasized ('Calculating fixes' )}' );
248237 }
@@ -280,57 +269,28 @@ These fixes can be enabled using --$includeFixOption:''');
280269 editCount += fileEdit.edits.length;
281270 }
282271 logger.stdout ('Found $editCount changes in ${fileEdits .length } files.' );
272+
273+ previewFixes (logger, result);
274+
283275 //
284- // Print instructions for opening the preview tool .
276+ // Stop the server .
285277 //
286- var urls = result.urls;
287- if (urls != null ) {
288- // Server has already started the preview server.
289- if (result.hasErrors) {
290- // TODO(brianwilkerson) When we have previews for fixes, tailor the
291- // message to be appropriate for fixes.
292- logger.stdout ('' );
293- String warning = ansi.emphasized ('WARNING' );
294- logger.stdout ('$warning : The unmodified code contains errors that '
295- 'might affect the accuracy of the upgrade.' );
296- options.overwrite = false ;
297- }
298- if (options.isUpgrade && options.upgradeOptions.preview) {
299- logger.stdout ('' );
300- String open;
301- if (urls.length == 1 ) {
302- open = ansi.emphasized ('Please open this URL' );
303- } else {
304- open = ansi.emphasized ('Please open these URLs' );
305- }
306- logger.stdout ('$open in your browser to see the changes:' );
307- for (var url in urls) {
308- logger.stdout (' $url ' );
309- }
310- logger.stdout ('' );
311- String enter = ansi.emphasized ('ENTER' );
312- logger.stdout ('When done previewing, press $enter .' );
313- stdin.readLineSync ();
314- }
315- //
316- // Stop the server.
317- //
318- serverStopped = server.stop ();
319- if (shouldApplyFixes (result)) {
320- applyFixes ();
321- logger.stdout ('Changes have been applied.' );
322- } else {
323- logger.stdout ('No changes applied.' );
324- }
325- await serverStopped;
278+ serverStopped = server.stop ();
279+
280+ logger.stdout ('' );
281+
282+ // Check if we should apply fixes.
283+ if (result.edits.isEmpty) {
284+ logger.stdout (result.otherSuggestions.isNotEmpty
285+ ? 'None of the recommended changes can be automatically applied.'
286+ : 'There are no recommended changes.' );
287+ } else if (shouldApplyFixes (result)) {
288+ applyFixes ();
289+ logger.stdout ('Changes have been applied.' );
326290 } else {
327- //
328- // Stop the server.
329- //
330- serverStopped = server.stop ();
331- await printAndApplyFixes ();
332- await serverStopped;
291+ logger.stdout ('Re-run with --overwrite to apply the above changes.' );
333292 }
293+ await serverStopped;
334294 } finally {
335295 // If we didn't already try to stop the server, then stop it now.
336296 if (serverStopped == null ) {
@@ -398,6 +358,53 @@ analysis server
398358The --$option option is not supported by analysis server version $version .
399359Please upgrade to a newer version of the Dart SDK to use this option.''' );
400360 }
361+
362+ void previewFixes (
363+ Logger logger,
364+ EditDartfixResult results,
365+ ) {
366+ final Ansi ansi = logger.ansi;
367+
368+ Map <String , List <DartFixSuggestion >> fileSuggestions = {};
369+ for (DartFixSuggestion suggestion in results.suggestions) {
370+ String file = suggestion.location.file;
371+ fileSuggestions.putIfAbsent (file, () => < DartFixSuggestion > []);
372+ fileSuggestions[file].add (suggestion);
373+ }
374+
375+ // present a diff-like view
376+ for (SourceFileEdit sourceFileEdit in results.edits) {
377+ String file = sourceFileEdit.file;
378+ String relPath = path.relative (file);
379+ int count = sourceFileEdit.edits.length;
380+
381+ logger.stdout ('' );
382+ logger.stdout ('${ansi .emphasized (relPath )} '
383+ '($count ${pluralize ('change' , count )}):' );
384+
385+ String source;
386+ try {
387+ source = File (file).readAsStringSync ();
388+ } catch (_) {}
389+
390+ if (source == null ) {
391+ logger.stdout (' (unable to retrieve source for file)' );
392+ } else {
393+ SourcePrinter sourcePrinter = SourcePrinter (source);
394+
395+ List <SourceEdit > edits = sortEdits (sourceFileEdit);
396+
397+ // Apply edits.
398+ sourcePrinter.applyEdits (edits);
399+
400+ // Render the changed lines.
401+ sourcePrinter.processChangedLines ((lineNumber, lineText) {
402+ String prefix = ' line ${lineNumber .toString ().padRight (3 )} •' ;
403+ logger.stdout ('$prefix ${lineText .trim ()}' );
404+ });
405+ }
406+ }
407+ }
401408}
402409
403410class _Handler
@@ -454,15 +461,15 @@ class _Handler
454461 if (version > expectedVersion) {
455462 logger.stdout ('''
456463This version of dartfix is incompatible with the current Dart SDK.
457- Try installing a newer version of dartfix by running
464+ Try installing a newer version of dartfix by running:
458465
459466 pub global activate dartfix
460467''' );
461468 } else {
462469 logger.stdout ('''
463- This version of dartfix is too new to be used with the current Dart SDK.
464- Try upgrading the Dart SDK to a newer version
465- or installing an older version of dartfix using
470+ This version of dartfix is too new to be used with the current Dart SDK. Try
471+ upgrading the Dart SDK to a newer version or installing an older version of
472+ dartfix using:
466473
467474 pub global activate dartfix <version>
468475''' );
0 commit comments