Decouple colors.dart from CompilerContext
We're going to move this file into _fe_analyzer_shared, so we can't
have it importing CompilerContext. It turns out that we were only
using it to cache the decision of whether to display colors, and to
decide whether to verbosely explain how we made that decision. Those
can be accomplished in other ways.
Change-Id: If1e3a9fee7c0ca919444d8429936045b8408c246
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/123840
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
diff --git a/pkg/front_end/lib/src/fasta/colors.dart b/pkg/front_end/lib/src/fasta/colors.dart
index c8b4812..9924af0 100644
--- a/pkg/front_end/lib/src/fasta/colors.dart
+++ b/pkg/front_end/lib/src/fasta/colors.dart
@@ -74,10 +74,27 @@
op
""";
+/// Boolean value caching whether or not we should display ANSI colors.
+///
+/// If `null`, we haven't decided whether we should display ANSI colors or not.
+bool _enableColors;
+
+/// Finds out whether we are displaying ANSI colors.
+///
+/// The first time this getter is invoked (either by a client or by an attempt
+/// to use a color), it decides whether colors should be used based on the
+/// logic in [_computeEnableColors] (unless a value has previously been set).
+bool get enableColors => _enableColors ??= _computeEnableColors();
+
+/// Allows the client to override the decision of whether to disable ANSI
+/// colors.
+void set enableColors(bool value) {
+ assert(value != null);
+ _enableColors = value;
+}
+
String wrap(String string, String color) {
- return CompilerContext.enableColors
- ? "${color}$string${DEFAULT_COLOR}"
- : string;
+ return enableColors ? "${color}$string${DEFAULT_COLOR}" : string;
}
String black(String string) => wrap(string, BLACK_COLOR);
@@ -102,6 +119,12 @@
}
}
+typedef void _StringToNullFunction(String s);
+
+/// Callback used by [_computeEnableColors] to report why it has or hasn't
+/// chosen to use ANSI colors.
+_StringToNullFunction printEnableColorsReason = (_) {};
+
/// True if we should enable colors in output.
///
/// We enable colors when both `stdout` and `stderr` support ANSI escapes.
@@ -112,22 +135,18 @@
///
/// Note: do not call this method directly, as it is expensive to
/// compute. Instead, use [CompilerContext.enableColors].
-bool computeEnableColors(CompilerContext context) {
- // TODO(ahe): Remove this method.
-
+bool _computeEnableColors() {
bool stderrSupportsColors = _supportsAnsiEscapes(stdout);
bool stdoutSupportsColors = _supportsAnsiEscapes(stderr);
if (stdoutSupportsColors == false) {
- if (context.options.verbose) {
- print("Not enabling colors, stdout does not support ANSI colors.");
- }
+ printEnableColorsReason(
+ "Not enabling colors, stdout does not support ANSI colors.");
return false;
}
if (stderrSupportsColors == false) {
- if (context.options.verbose) {
- print("Not enabling colors, stderr does not support ANSI colors.");
- }
+ printEnableColorsReason(
+ "Not enabling colors, stderr does not support ANSI colors.");
return false;
}
@@ -136,14 +155,10 @@
// In this case, either [stdout] or [stderr] did not support the
// property `supportsAnsiEscapes`. Since we do not have another way
// to determine support for colors, we disable them.
- if (context.options.verbose) {
- print("Not enabling colors as ANSI is not supported.");
- }
+ printEnableColorsReason("Not enabling colors as ANSI is not supported.");
return false;
}
- if (context.options.verbose) {
- print("Enabling colors as OS is Windows.");
- }
+ printEnableColorsReason("Enabling colors as OS is Windows.");
return true;
}
@@ -157,42 +172,33 @@
"/bin/sh", ["-c", "printf '%s' '$TERMINAL_CAPABILITIES' | tput -S"]);
if (result.exitCode != 0) {
- if (context.options.verbose) {
- print("Not enabling colors, running tput failed.");
- }
+ printEnableColorsReason("Not enabling colors, running tput failed.");
return false;
}
List<String> lines = result.stdout.split("\n");
if (lines.length != 2) {
- if (context.options.verbose) {
- print("Not enabling colors, unexpected output from tput: "
- "${jsonEncode(result.stdout)}.");
- }
+ printEnableColorsReason("Not enabling colors, unexpected output from tput: "
+ "${jsonEncode(result.stdout)}.");
return false;
}
String numberOfColors = lines[0];
if ((int.tryParse(numberOfColors) ?? -1) < 8) {
- if (context.options.verbose) {
- print("Not enabling colors, less than 8 colors supported: "
- "${jsonEncode(numberOfColors)}.");
- }
+ printEnableColorsReason(
+ "Not enabling colors, less than 8 colors supported: "
+ "${jsonEncode(numberOfColors)}.");
return false;
}
String allCodes = lines[1].trim();
if (ALL_CODES != allCodes) {
- if (context.options.verbose) {
- print("Not enabling colors, color codes don't match: "
- "${jsonEncode(ALL_CODES)} != ${jsonEncode(allCodes)}.");
- }
+ printEnableColorsReason("Not enabling colors, color codes don't match: "
+ "${jsonEncode(ALL_CODES)} != ${jsonEncode(allCodes)}.");
return false;
}
- if (context.options.verbose) {
- print("Enabling colors.");
- }
+ printEnableColorsReason("Enabling colors.");
return true;
}
diff --git a/pkg/front_end/lib/src/fasta/command_line_reporting.dart b/pkg/front_end/lib/src/fasta/command_line_reporting.dart
index 0a3d7bf..e8d5bc27 100644
--- a/pkg/front_end/lib/src/fasta/command_line_reporting.dart
+++ b/pkg/front_end/lib/src/fasta/command_line_reporting.dart
@@ -25,7 +25,7 @@
import '../compute_platform_binaries_location.dart' show translateSdk;
-import 'colors.dart' show green, magenta, red;
+import 'colors.dart' show enableColors, green, magenta, red;
import 'compiler_context.dart' show CompilerContext;
@@ -56,7 +56,7 @@
if (message.tip != null) {
messageText += "\n${message.tip}";
}
- if (CompilerContext.enableColors) {
+ if (enableColors) {
switch (severity) {
case Severity.error:
case Severity.internalProblem:
diff --git a/pkg/front_end/lib/src/fasta/compiler_context.dart b/pkg/front_end/lib/src/fasta/compiler_context.dart
index 62439c4..801635d 100644
--- a/pkg/front_end/lib/src/fasta/compiler_context.dart
+++ b/pkg/front_end/lib/src/fasta/compiler_context.dart
@@ -17,9 +17,9 @@
import '../base/processed_options.dart' show ProcessedOptions;
-import 'command_line_reporting.dart' as command_line_reporting;
+import 'colors.dart' as colors;
-import 'colors.dart' show computeEnableColors;
+import 'command_line_reporting.dart' as command_line_reporting;
import 'fasta_codes.dart'
show LocatedMessage, Message, messageInternalProblemMissingContext;
@@ -53,16 +53,14 @@
FileSystem get fileSystem => options.fileSystem;
- bool enableColorsCached = null;
-
Uri cachedSdkRoot = null;
bool compilingPlatform = false;
- CompilerContext(this.options);
-
- void disableColors() {
- enableColorsCached = false;
+ CompilerContext(this.options) {
+ if (options.verbose) {
+ colors.printEnableColorsReason = print;
+ }
}
/// Report [message], for example, by printing it.
@@ -143,10 +141,6 @@
return new CompilerContext(new ProcessedOptions()).runInContext<T>(action);
}
- static bool get enableColors {
- return current.enableColorsCached ??= computeEnableColors(current);
- }
-
void clear() {
StringToken.canonicalizer.clear();
errors.clear();
diff --git a/pkg/front_end/test/fasta/expression_suite.dart b/pkg/front_end/test/fasta/expression_suite.dart
index 7d519bf..b28f428 100644
--- a/pkg/front_end/test/fasta/expression_suite.dart
+++ b/pkg/front_end/test/fasta/expression_suite.dart
@@ -28,6 +28,8 @@
import 'package:front_end/src/external_state_snapshot.dart'
show ExternalStateSnapshot;
+import 'package:front_end/src/fasta/colors.dart' as colors;
+
import 'package:front_end/src/fasta/compiler_context.dart' show CompilerContext;
import 'package:front_end/src/fasta/incremental_compiler.dart'
@@ -427,7 +429,7 @@
// Disable colors to ensure that expectation files are the same across
// platforms and independent of stdin/stderr.
- compilerContext.disableColors();
+ colors.enableColors = false;
return new Context(compilerContext, snapshot, errors, updateExpectations);
}
diff --git a/pkg/front_end/test/fasta/testing/suite.dart b/pkg/front_end/test/fasta/testing/suite.dart
index 43cb004..bf5ad0e 100644
--- a/pkg/front_end/test/fasta/testing/suite.dart
+++ b/pkg/front_end/test/fasta/testing/suite.dart
@@ -35,6 +35,8 @@
import 'package:front_end/src/compute_platform_binaries_location.dart'
show computePlatformBinariesLocation;
+import 'package:front_end/src/fasta/colors.dart' as colors;
+
import 'package:front_end/src/fasta/compiler_context.dart' show CompilerContext;
import 'package:front_end/src/fasta/dill/dill_target.dart' show DillTarget;
@@ -408,7 +410,7 @@
return await CompilerContext.runWithOptions(options, (_) async {
// Disable colors to ensure that expectation files are the same across
// platforms and independent of stdin/stderr.
- CompilerContext.current.disableColors();
+ colors.enableColors = false;
Component platform = await context.loadPlatform();
Ticker ticker = new Ticker();
DillTarget dillTarget = new DillTarget(
diff --git a/pkg/front_end/test/fasta/type_promotion_look_ahead_suite.dart b/pkg/front_end/test/fasta/type_promotion_look_ahead_suite.dart
index fbb4b2b..e1a5c98 100644
--- a/pkg/front_end/test/fasta/type_promotion_look_ahead_suite.dart
+++ b/pkg/front_end/test/fasta/type_promotion_look_ahead_suite.dart
@@ -15,6 +15,8 @@
import 'package:front_end/src/fasta/builder/builder.dart';
+import 'package:front_end/src/fasta/colors.dart' as colors;
+
import 'package:front_end/src/fasta/compiler_context.dart' show CompilerContext;
import 'package:front_end/src/fasta/messages.dart'
@@ -56,7 +58,7 @@
(CompilerContext context) =>
new Future<CompilerContext>.value(context),
errorOnMissingInput: false);
- context.disableColors();
+ colors.enableColors = false;
return new TypePromotionLookAheadContext(
context, environment["updateExpectations"] == "true");
}
diff --git a/pkg/front_end/test/lint_suite.dart b/pkg/front_end/test/lint_suite.dart
index 048916f..f825ddd 100644
--- a/pkg/front_end/test/lint_suite.dart
+++ b/pkg/front_end/test/lint_suite.dart
@@ -222,8 +222,7 @@
Token beginToken,
Token endToken) {
if (!_latestType.type) {
- onProblem(
- varFinalOrConst.offset, varFinalOrConst.length, "No explicit type.");
+ onProblem(beginToken.offset, beginToken.length, "No explicit type.");
}
}
diff --git a/pkg/front_end/test/lint_test.status b/pkg/front_end/test/lint_test.status
index f4d0a40..a95296b 100644
--- a/pkg/front_end/test/lint_test.status
+++ b/pkg/front_end/test/lint_test.status
@@ -21,6 +21,7 @@
src/api_prototype/constant_evaluator/Exports: Fail
src/api_prototype/front_end/Exports: Fail
src/api_prototype/incremental_kernel_generator/Exports: Fail
+src/fasta/colors/ExplicitType: Fail
src/fasta/fasta_codes/Exports: Fail
src/fasta/kernel/constant_evaluator/ExplicitType: Pass
src/fasta/kernel/kernel_api/Exports: Fail
diff --git a/pkg/front_end/test/spell_checking_list_common.txt b/pkg/front_end/test/spell_checking_list_common.txt
index e0dcb8c..aaef843 100644
--- a/pkg/front_end/test/spell_checking_list_common.txt
+++ b/pkg/front_end/test/spell_checking_list_common.txt
@@ -674,8 +674,10 @@
debugging
decide
decided
+decides
deciding
decimal
+decision
declarable
declarated
declaration