Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Unified Diff: pkg/compiler/lib/src/io/location_provider.dart

Issue 2788373002: Add Source.getTextLine and use it to display source snippets in error messages. (Closed)
Patch Set: dartfmt Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/compiler/lib/src/io/location_provider.dart
diff --git a/pkg/compiler/lib/src/io/location_provider.dart b/pkg/compiler/lib/src/io/location_provider.dart
new file mode 100644
index 0000000000000000000000000000000000000000..1ac9af76e7429e52f5d41ff655691939e2baeff4
--- /dev/null
+++ b/pkg/compiler/lib/src/io/location_provider.dart
@@ -0,0 +1,56 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library dart2js.io.location_provider;
+
+import 'code_output.dart' show CodeOutputListener;
+
+import 'package:kernel/ast.dart' show Location, Source;
+
+/// Interface for providing line/column information.
+abstract class LocationProvider {
+ /// Translates the zero-based character [offset] (from the beginning of a
+ /// file) to a [Location].
+ Location getLocation(int offset);
+}
+
+/// [CodeOutputListener] that collects line information.
+class LocationCollector extends CodeOutputListener implements LocationProvider {
+ int length = 0;
+ List<int> lineStarts = <int>[0];
+
+ void _collect(String text) {
+ int index = 0;
+ while (index < text.length) {
+ // Unix uses '\n' and Windows uses '\r\n', so this algorithm works for
+ // both platforms.
+ index = text.indexOf('\n', index) + 1;
+ if (index <= 0) break;
+ lineStarts.add(length + index);
+ }
+ length += text.length;
+ }
+
+ @override
+ void onText(String text) {
+ _collect(text);
+ }
+
+ @override
+ Location getLocation(int offset) {
+ RangeError.checkValueInInterval(offset, 0, length, "offset",
+ "The value of 'offset' ($offset) must be between 0 and $length.");
+ return new Source(lineStarts, null).getLocation(null, offset);
+ }
+
+ @override
+ void onDone(int length) {
+ lineStarts.add(length + 1);
+ this.length = length;
+ }
+
+ String toString() {
+ return 'lineStarts=$lineStarts,length=$length';
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698