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

Side by Side Diff: pkg/compiler/lib/src/io/line_column_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 unified diff | Download patch
« no previous file with comments | « no previous file | pkg/compiler/lib/src/io/location_provider.dart » ('j') | pkg/kernel/lib/ast.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library dart2js.io.line_column;
6
7 import 'code_output.dart' show CodeOutputListener;
8
9 /// Interface for providing line/column information.
10 abstract class LineColumnProvider {
11 /// Returns the line number (0-based) for [offset].
12 int getLine(int offset);
13
14 /// Returns the column number (0-based) for [offset] at the given [line].
15 int getColumn(int line, int offset);
16
17 /// Returns the offset for 0-based [line] and [column] numbers.
18 int getOffset(int line, int column);
19 }
20
21 /// [CodeOutputListener] that collects line information.
22 class LineColumnCollector extends CodeOutputListener
23 implements LineColumnProvider {
24 int length = 0;
25 List<int> lineStarts = <int>[0];
26
27 void _collect(String text) {
28 int index = 0;
29 while (index < text.length) {
30 // Unix uses '\n' and Windows uses '\r\n', so this algorithm works for
31 // both platforms.
32 index = text.indexOf('\n', index) + 1;
33 if (index <= 0) break;
34 lineStarts.add(length + index);
35 }
36 length += text.length;
37 }
38
39 @override
40 void onText(String text) {
41 _collect(text);
42 }
43
44 @override
45 int getLine(int offset) {
46 List<int> starts = lineStarts;
47 if (offset < 0 || starts.last <= offset) {
48 throw 'bad position #$offset in buffer with length ${length}.';
49 }
50 int first = 0;
51 int count = starts.length;
52 while (count > 1) {
53 int step = count ~/ 2;
54 int middle = first + step;
55 int lineStart = starts[middle];
56 if (offset < lineStart) {
57 count = step;
58 } else {
59 first = middle;
60 count -= step;
61 }
62 }
63 return first;
64 }
65
66 @override
67 int getColumn(int line, int offset) {
68 return offset - lineStarts[line];
69 }
70
71 int getOffset(int line, int column) => lineStarts[line] + column;
72
73 @override
74 void onDone(int length) {
75 lineStarts.add(length + 1);
76 this.length = length;
77 }
78
79 String toString() {
80 return 'lineStarts=$lineStarts,length=$length';
81 }
82 }
OLDNEW
« no previous file with comments | « no previous file | pkg/compiler/lib/src/io/location_provider.dart » ('j') | pkg/kernel/lib/ast.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698