| 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.source_file; | 5 library dart2js.io.source_file; |
| 6 | 6 |
| 7 import 'dart:math'; | 7 import 'dart:math'; |
| 8 import 'dart:convert' show UTF8; | 8 import 'dart:convert' show UTF8; |
| 9 import 'dart:typed_data' show Uint8List; | 9 import 'dart:typed_data' show Uint8List; |
| 10 | 10 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 * | 130 * |
| 131 * Use [colorize] to wrap source code text and marker characters in color | 131 * Use [colorize] to wrap source code text and marker characters in color |
| 132 * escape codes. | 132 * escape codes. |
| 133 */ | 133 */ |
| 134 String getLocationMessage(String message, int start, int end, | 134 String getLocationMessage(String message, int start, int end, |
| 135 {bool includeSourceLine: true, | 135 {bool includeSourceLine: true, |
| 136 String colorize(String text)}) { | 136 String colorize(String text)}) { |
| 137 if (colorize == null) { | 137 if (colorize == null) { |
| 138 colorize = (text) => text; | 138 colorize = (text) => text; |
| 139 } | 139 } |
| 140 var line = getLine(start); | 140 int lineStart = getLine(start); |
| 141 var column = getColumn(line, start); | 141 int columnStart = getColumn(lineStart, start); |
| 142 int lineEnd = getLine(end); |
| 143 int columnEnd = getColumn(lineEnd, end); |
| 142 | 144 |
| 143 var buf = new StringBuffer('${filename}:'); | 145 StringBuffer buf = new StringBuffer('${filename}:'); |
| 144 if (start != end || start != 0) { | 146 if (start != end || start != 0) { |
| 145 // Line/column info is relevant. | 147 // Line/column info is relevant. |
| 146 buf.write('${line + 1}:${column + 1}:'); | 148 buf.write('${lineStart + 1}:${columnStart + 1}:'); |
| 147 } | 149 } |
| 148 buf.write('\n$message\n'); | 150 buf.write('\n$message\n'); |
| 149 | 151 |
| 150 if (start != end && includeSourceLine) { | 152 if (start != end && includeSourceLine) { |
| 151 String textLine = getLineText(line); | 153 if (lineStart == lineEnd) { |
| 154 String textLine = getLineText(lineStart); |
| 152 | 155 |
| 153 int toColumn = min(column + (end-start), textLine.length); | 156 int toColumn = min(columnStart + (end-start), textLine.length); |
| 154 buf.write(textLine.substring(0, column)); | 157 buf.write(textLine.substring(0, columnStart)); |
| 155 buf.write(colorize(textLine.substring(column, toColumn))); | 158 buf.write(colorize(textLine.substring(columnStart, toColumn))); |
| 156 buf.write(textLine.substring(toColumn)); | 159 buf.write(textLine.substring(toColumn)); |
| 157 | 160 |
| 158 int i = 0; | 161 int i = 0; |
| 159 for (; i < column; i++) { | 162 for (; i < columnStart; i++) { |
| 160 buf.write(' '); | 163 buf.write(' '); |
| 161 } | 164 } |
| 162 | 165 |
| 163 for (; i < toColumn; i++) { | 166 for (; i < toColumn; i++) { |
| 164 buf.write(colorize('^')); | 167 buf.write(colorize('^')); |
| 168 } |
| 169 } else { |
| 170 for (int line = lineStart; line <= lineEnd; line++) { |
| 171 String textLine = getLineText(line); |
| 172 if (line == lineStart) { |
| 173 buf.write(textLine.substring(0, columnStart)); |
| 174 buf.write(colorize(textLine.substring(columnStart))); |
| 175 } else if (line == lineEnd) { |
| 176 buf.write(colorize(textLine.substring(0, columnEnd))); |
| 177 buf.write(textLine.substring(columnEnd)); |
| 178 } else { |
| 179 buf.write(colorize(textLine)); |
| 180 } |
| 181 } |
| 165 } | 182 } |
| 166 } | 183 } |
| 167 | 184 |
| 168 return buf.toString(); | 185 return buf.toString(); |
| 169 } | 186 } |
| 170 | 187 |
| 171 int get lines => lineStarts.length - 1; | 188 int get lines => lineStarts.length - 1; |
| 172 | 189 |
| 173 /// Returns the text of line at the 0-based [index] within this source file. | 190 /// Returns the text of line at the 0-based [index] within this source file. |
| 174 String getLineText(int index) { | 191 String getLineText(int index) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 set length(int v) { } | 280 set length(int v) { } |
| 264 | 281 |
| 265 String slowText() => text; | 282 String slowText() => text; |
| 266 | 283 |
| 267 List<int> slowUtf8ZeroTerminatedBytes() { | 284 List<int> slowUtf8ZeroTerminatedBytes() { |
| 268 return _zeroTerminateIfNecessary(UTF8.encode(text)); | 285 return _zeroTerminateIfNecessary(UTF8.encode(text)); |
| 269 } | 286 } |
| 270 | 287 |
| 271 String slowSubstring(int start, int end) => text.substring(start, end); | 288 String slowSubstring(int start, int end) => text.substring(start, end); |
| 272 } | 289 } |
| OLD | NEW |