| 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('source_file'); | 5 #library('source_file'); |
| 6 | 6 |
| 7 #import('colors.dart'); | 7 #import('colors.dart', prefix: 'colors'); |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Represents a file of source code. | 10 * Represents a file of source code. |
| 11 */ | 11 */ |
| 12 class SourceFile { | 12 class SourceFile { |
| 13 | 13 |
| 14 /** The name of the file. */ | 14 /** The name of the file. */ |
| 15 final String filename; | 15 final String filename; |
| 16 | 16 |
| 17 /** The text content of the file. */ | 17 /** The text content of the file. */ |
| (...skipping 29 matching lines...) Expand all Loading... |
| 47 | 47 |
| 48 int getColumn(int line, int position) { | 48 int getColumn(int line, int position) { |
| 49 return position - lineStarts[line]; | 49 return position - lineStarts[line]; |
| 50 } | 50 } |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * Create a pretty string representation from a character position | 53 * Create a pretty string representation from a character position |
| 54 * in the file. | 54 * in the file. |
| 55 */ | 55 */ |
| 56 String getLocationMessage(String message, int start, | 56 String getLocationMessage(String message, int start, |
| 57 [int end, bool includeText=false, bool useColors = true]) { | 57 [int end, bool includeText = false]) { |
| 58 var line = getLine(start); | 58 var line = getLine(start); |
| 59 var column = getColumn(line, start); | 59 var column = getColumn(line, start); |
| 60 | 60 |
| 61 var buf = new StringBuffer( | 61 var buf = new StringBuffer( |
| 62 '${filename}:${line + 1}:${column + 1}: $message'); | 62 '${filename}:${line + 1}:${column + 1}: $message'); |
| 63 if (includeText) { | 63 if (includeText) { |
| 64 buf.add('\n'); | 64 buf.add('\n'); |
| 65 var textLine; | 65 var textLine; |
| 66 // +1 for 0-indexing, +1 again to avoid the last line of the file | 66 // +1 for 0-indexing, +1 again to avoid the last line of the file |
| 67 if ((line + 2) < _lineStarts.length) { | 67 if ((line + 2) < _lineStarts.length) { |
| 68 textLine = text.substring(_lineStarts[line], _lineStarts[line+1]); | 68 textLine = text.substring(_lineStarts[line], _lineStarts[line+1]); |
| 69 } else { | 69 } else { |
| 70 textLine = text.substring(_lineStarts[line]) + '\n'; | 70 textLine = text.substring(_lineStarts[line]) + '\n'; |
| 71 } | 71 } |
| 72 | 72 |
| 73 int toColumn = Math.min(column + (end-start), textLine.length); | 73 int toColumn = Math.min(column + (end-start), textLine.length); |
| 74 if (useColors) { | 74 if (colors.enabled) { |
| 75 buf.add(textLine.substring(0, column)); | 75 buf.add(textLine.substring(0, column)); |
| 76 buf.add(RED_COLOR); | 76 buf.add(colors.RED_COLOR); |
| 77 buf.add(textLine.substring(column, toColumn)); | 77 buf.add(textLine.substring(column, toColumn)); |
| 78 buf.add(NO_COLOR); | 78 buf.add(colors.NO_COLOR); |
| 79 buf.add(textLine.substring(toColumn)); | 79 buf.add(textLine.substring(toColumn)); |
| 80 } else { | 80 } else { |
| 81 buf.add(textLine); | 81 buf.add(textLine); |
| 82 } | 82 } |
| 83 | 83 |
| 84 int i = 0; | 84 int i = 0; |
| 85 for (; i < column; i++) { | 85 for (; i < column; i++) { |
| 86 buf.add(' '); | 86 buf.add(' '); |
| 87 } | 87 } |
| 88 | 88 |
| 89 if (useColors) buf.add(RED_COLOR); | 89 if (colors.enabled) buf.add(colors.RED_COLOR); |
| 90 for (; i < toColumn; i++) { | 90 for (; i < toColumn; i++) { |
| 91 buf.add('^'); | 91 buf.add('^'); |
| 92 } | 92 } |
| 93 if (useColors) buf.add(NO_COLOR); | 93 if (colors.enabled) buf.add(colors.NO_COLOR); |
| 94 } | 94 } |
| 95 | 95 |
| 96 return buf.toString(); | 96 return buf.toString(); |
| 97 } | 97 } |
| 98 } | 98 } |
| OLD | NEW |