Skip to content

Commit

Permalink
Improve dartfix output
Browse files Browse the repository at this point in the history
* include file path in suggestion output
* more verbose output for debugging in the field
* update pubspec.yaml
* display paths relative to the target directories

Change-Id: I64e519d72773c3e56e4502fab7591c43fda35bf3
Reviewed-on: https://dart-review.googlesource.com/c/81860
Reviewed-by: Devon Carew <[email protected]>
Commit-Queue: Dan Rubel <[email protected]>
  • Loading branch information
danrubel authored and [email protected] committed Oct 29, 2018
1 parent 388c41d commit e414b58
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
50 changes: 29 additions & 21 deletions pkg/dartfix/lib/src/driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io' show File, Directory;
import 'dart:io' show File, Platform;

import 'package:analysis_server_client/protocol.dart';
import 'package:cli_util/cli_logging.dart';
Expand Down Expand Up @@ -71,12 +71,16 @@ class Driver {
serverConnected = new Completer();
if (options.verbose) {
server.debugStdio();
logger.trace('Dart SDK version ${Platform.version}');
logger.trace(' ${Platform.resolvedExecutable}');
logger.trace('dartfix');
logger.trace(' ${Platform.script.toFilePath()}');
}
logger.trace('Starting...');
await server.start(
sdkPath: options.sdkPath, useSnapshot: !runAnalysisServerFromSource);
final runFromSource = runAnalysisServerFromSource;
logger.trace(runFromSource ? 'Starting from source...' : 'Starting...');
await server.start(sdkPath: options.sdkPath, useSnapshot: !runFromSource);
server.listenToOutput(dispatchNotification);
return serverConnected.future.timeout(connectTimeout, onTimeout: () {
await serverConnected.future.timeout(connectTimeout, onTimeout: () {
logger.stderr('Failed to connect to server');
context.exit(15);
});
Expand Down Expand Up @@ -135,7 +139,7 @@ class Driver {
logger.stdout('');
logger.stdout(ansi.emphasized('Files to be changed:'));
for (SourceFileEdit fileEdit in result.edits) {
logger.stdout(' ${_relativePath(fileEdit.file)}');
logger.stdout(' ${relativePath(fileEdit.file)}');
}
if (shouldApplyChanges(result)) {
for (SourceFileEdit fileEdit in result.edits) {
Expand All @@ -146,7 +150,7 @@ class Driver {
}
await file.writeAsString(code);
}
logger.stdout('Changes applied.');
logger.stdout(ansi.emphasized('Changes applied.'));
}
}

Expand All @@ -157,9 +161,14 @@ class Driver {
List<DartFixSuggestion> sorted = new List.from(suggestions)
..sort(compareSuggestions);
for (DartFixSuggestion suggestion in sorted) {
Location loc = suggestion.location;
logger.stdout(' ${_toSentenceFragment(suggestion.description)}'
'${loc == null ? "" : " • ${loc.startLine}:${loc.startColumn}"}');
final msg = new StringBuffer();
msg.write(' ${_toSentenceFragment(suggestion.description)}');
final loc = suggestion.location;
if (loc != null) {
msg.write(' • ${relativePath(loc.file)}');
msg.write(' • ${loc.startLine}:${loc.startColumn}');
}
logger.stdout(msg.toString());
}
}
}
Expand Down Expand Up @@ -291,7 +300,7 @@ class Driver {
if (!shouldFilterError(error)) {
if (!foundAtLeastOneError) {
foundAtLeastOneError = true;
logger.stdout('${_relativePath(params.file)}:');
logger.stdout('${relativePath(params.file)}:');
}
Location loc = error.location;
logger.stdout(' ${_toSentenceFragment(error.message)}'
Expand Down Expand Up @@ -336,6 +345,15 @@ class Driver {
return (s2.location?.offset ?? 0) - (s1.location?.offset ?? 0);
}

String relativePath(String filePath) {
for (String target in targets) {
if (filePath.startsWith(target)) {
return filePath.substring(target.length + 1);
}
}
return filePath;
}

bool shouldFilterError(AnalysisError error) {
// Do not show TODOs or errors that will be automatically fixed.

Expand All @@ -356,16 +374,6 @@ class Driver {
}
}

String _relativePath(String filePath) {
final String currentPath = Directory.current.absolute.path;

if (filePath.startsWith(currentPath)) {
return filePath.substring(currentPath.length + 1);
} else {
return filePath;
}
}

String _toSentenceFragment(String message) {
return message.endsWith('.')
? message.substring(0, message.length - 1)
Expand Down
2 changes: 1 addition & 1 deletion pkg/dartfix/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description:
A tool for migrating Dart source to newer versions of the Dart SDK,
and fixing common issues.
environment:
sdk: '>=2.0.0 <3.0.0'
sdk: '>=2.1.0 <3.0.0'
dependencies:
analysis_server_client: ^2.0.0
args: any
Expand Down

0 comments on commit e414b58

Please sign in to comment.