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

Side by Side Diff: pkg/compiler/lib/src/io/line_column_provider.dart

Issue 877753008: Fix shift in source map positions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated cf. comments. Created 5 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/compiler/dart2js/line_column_provider_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library dart2js.io.line_column; 5 library dart2js.io.line_column;
6 6
7 import 'code_output.dart'; 7 import 'code_output.dart';
8 8
9 /// Interface for providing line/column information. 9 /// Interface for providing line/column information.
10 abstract class LineColumnProvider { 10 abstract class LineColumnProvider {
11 /// Returns the line number (0-based) for [offset]. 11 /// Returns the line number (0-based) for [offset].
12 int getLine(int offset); 12 int getLine(int offset);
13 13
14 /// Returns the column number (0-based) for [offset] at the given [line]. 14 /// Returns the column number (0-based) for [offset] at the given [line].
15 int getColumn(int line, int offset); 15 int getColumn(int line, int offset);
16 } 16 }
17 17
18 /// [CodeOutputListener] that collects line information. 18 /// [CodeOutputListener] that collects line information.
19 class LineColumnCollector extends CodeOutputListener 19 class LineColumnCollector extends CodeOutputListener
20 implements LineColumnProvider { 20 implements LineColumnProvider {
21 int lastLineStart = 0; 21 int length = 0;
22 List<int> lineStarts = <int>[0]; 22 List<int> lineStarts = <int>[0];
23 23
24 void _collect(String text) { 24 void _collect(String text) {
25 int offset = lastLineStart;
26 int index = 0; 25 int index = 0;
27 while (index < text.length) { 26 while (index < text.length) {
28 // Unix uses '\n' and Windows uses '\r\n', so this algorithm works for 27 // Unix uses '\n' and Windows uses '\r\n', so this algorithm works for
29 // both platforms. 28 // both platforms.
30 index = text.indexOf('\n', index) + 1; 29 index = text.indexOf('\n', index) + 1;
31 if (index <= 0) break; 30 if (index <= 0) break;
32 lastLineStart = offset + index; 31 lineStarts.add(length + index);
33 lineStarts.add(lastLineStart);
34 } 32 }
33 length += text.length;
35 } 34 }
36 35
37 @override 36 @override
38 void onText(String text) { 37 void onText(String text) {
39 _collect(text); 38 _collect(text);
40 } 39 }
41 40
42 @override 41 @override
43 int getLine(int offset) { 42 int getLine(int offset) {
44 List<int> starts = lineStarts; 43 List<int> starts = lineStarts;
45 if (offset < 0 || starts.last <= offset) { 44 if (offset < 0 || starts.last <= offset) {
46 throw 'bad position #$offset in buffer with length ${lineStarts.last}.'; 45 throw 'bad position #$offset in buffer with length ${length}.';
47 } 46 }
48 int first = 0; 47 int first = 0;
49 int count = starts.length; 48 int count = starts.length;
50 while (count > 1) { 49 while (count > 1) {
51 int step = count ~/ 2; 50 int step = count ~/ 2;
52 int middle = first + step; 51 int middle = first + step;
53 int lineStart = starts[middle]; 52 int lineStart = starts[middle];
54 if (offset < lineStart) { 53 if (offset < lineStart) {
55 count = step; 54 count = step;
56 } else { 55 } else {
57 first = middle; 56 first = middle;
58 count -= step; 57 count -= step;
59 } 58 }
60 } 59 }
61 return first; 60 return first;
62 } 61 }
63 62
64 @override 63 @override
65 int getColumn(int line, int offset) { 64 int getColumn(int line, int offset) {
66 return offset - lineStarts[line]; 65 return offset - lineStarts[line];
67 } 66 }
68 67
69 @override 68 @override
70 void onDone(int length) { 69 void onDone(int length) {
71 lineStarts.add(length); 70 lineStarts.add(length + 1);
71 this.length = length;
72 }
73
74 String toString() {
75 return 'lineStarts=$lineStarts,length=$length';
72 } 76 }
73 } 77 }
OLDNEW
« no previous file with comments | « no previous file | tests/compiler/dart2js/line_column_provider_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698