| OLD | NEW |
| 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 |
| 17 /// Returns the offset for 0-based [line] and [column] numbers. |
| 18 int getOffset(int line, int column); |
| 16 } | 19 } |
| 17 | 20 |
| 18 /// [CodeOutputListener] that collects line information. | 21 /// [CodeOutputListener] that collects line information. |
| 19 class LineColumnCollector extends CodeOutputListener | 22 class LineColumnCollector extends CodeOutputListener |
| 20 implements LineColumnProvider { | 23 implements LineColumnProvider { |
| 21 int length = 0; | 24 int length = 0; |
| 22 List<int> lineStarts = <int>[0]; | 25 List<int> lineStarts = <int>[0]; |
| 23 | 26 |
| 24 void _collect(String text) { | 27 void _collect(String text) { |
| 25 int index = 0; | 28 int index = 0; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 } | 61 } |
| 59 } | 62 } |
| 60 return first; | 63 return first; |
| 61 } | 64 } |
| 62 | 65 |
| 63 @override | 66 @override |
| 64 int getColumn(int line, int offset) { | 67 int getColumn(int line, int offset) { |
| 65 return offset - lineStarts[line]; | 68 return offset - lineStarts[line]; |
| 66 } | 69 } |
| 67 | 70 |
| 71 int getOffset(int line, int column) => lineStarts[line] + column; |
| 72 |
| 68 @override | 73 @override |
| 69 void onDone(int length) { | 74 void onDone(int length) { |
| 70 lineStarts.add(length + 1); | 75 lineStarts.add(length + 1); |
| 71 this.length = length; | 76 this.length = length; |
| 72 } | 77 } |
| 73 | 78 |
| 74 String toString() { | 79 String toString() { |
| 75 return 'lineStarts=$lineStarts,length=$length'; | 80 return 'lineStarts=$lineStarts,length=$length'; |
| 76 } | 81 } |
| 77 } | 82 } |
| OLD | NEW |