OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // TODO(jimhug): This should be an interface to work better with tools. | 5 // TODO(jimhug): This should be an interface to work better with tools. |
6 /** | 6 /** |
7 * Represents a file of source code. | 7 * Represents a file of source code. |
8 */ | 8 */ |
9 class SourceFile implements Comparable { | 9 class SourceFile implements Comparable { |
10 // TODO(terry): This filename for in memory buffer. May need to rework if | 10 // TODO(terry): This filename for in memory buffer. May need to rework if |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 } | 65 } |
66 | 66 |
67 int getColumn(int line, int position) { | 67 int getColumn(int line, int position) { |
68 return position - lineStarts[line]; | 68 return position - lineStarts[line]; |
69 } | 69 } |
70 | 70 |
71 /** | 71 /** |
72 * Create a pretty string representation from a character position | 72 * Create a pretty string representation from a character position |
73 * in the file. | 73 * in the file. |
74 */ | 74 */ |
75 String getLocationMessage(String message, int start, [int end, bool includeTex
t=false]) { | 75 String getLocationMessage(String message, int start, |
| 76 [int end, bool includeText=false]) { |
76 var line = getLine(start); | 77 var line = getLine(start); |
77 var column = getColumn(line, start); | 78 var column = getColumn(line, start); |
78 | 79 |
79 var buf = new StringBuffer( | 80 var buf = new StringBuffer( |
80 '${filename}:${line + 1}:${column + 1}: $message'); | 81 '${filename}:${line + 1}:${column + 1}: $message'); |
81 if (includeText) { | 82 if (includeText) { |
82 buf.add('\n'); | 83 buf.add('\n'); |
83 var textLine; | 84 var textLine; |
84 // +1 for 0-indexing, +1 again to avoid the last line of the file | 85 // +1 for 0-indexing, +1 again to avoid the last line of the file |
85 if ((line + 2) < _lineStarts.length) { | 86 if ((line + 2) < _lineStarts.length) { |
86 textLine = text.substring(_lineStarts[line], _lineStarts[line+1]); | 87 textLine = text.substring(_lineStarts[line], _lineStarts[line+1]); |
87 } else { | 88 } else { |
88 textLine = text.substring(_lineStarts[line]) + '\n'; | 89 textLine = text.substring(_lineStarts[line]) + '\n'; |
89 } | 90 } |
90 buf.add(textLine); | 91 |
| 92 int toColumn = Math.min(column + (end-start), textLine.length); |
| 93 if (options.useColors) { |
| 94 buf.add(textLine.substring(0, column)); |
| 95 buf.add(_RED_COLOR); |
| 96 buf.add(textLine.substring(column, toColumn)); |
| 97 buf.add(_NO_COLOR); |
| 98 buf.add(textLine.substring(toColumn)); |
| 99 } else { |
| 100 buf.add(textLine); |
| 101 } |
| 102 |
91 int i = 0; | 103 int i = 0; |
92 for (; i < column; i++) { | 104 for (; i < column; i++) { |
93 buf.add(' '); | 105 buf.add(' '); |
94 } | 106 } |
95 | 107 |
96 int toColumn = Math.min(column + (end-start), textLine.length); | 108 if (options.useColors) buf.add(_RED_COLOR); |
97 | |
98 for (; i < toColumn; i++) { | 109 for (; i < toColumn; i++) { |
99 buf.add('^'); | 110 buf.add('^'); |
100 } | 111 } |
| 112 if (options.useColors) buf.add(_NO_COLOR); |
101 } | 113 } |
102 | 114 |
103 return buf.toString(); | 115 return buf.toString(); |
104 } | 116 } |
105 | 117 |
106 /** Compares two source files. */ | 118 /** Compares two source files. */ |
107 int compareTo(SourceFile other) { | 119 int compareTo(SourceFile other) { |
108 if (orderInLibrary != null && other.orderInLibrary != null) { | 120 if (orderInLibrary != null && other.orderInLibrary != null) { |
109 return orderInLibrary - other.orderInLibrary; | 121 return orderInLibrary - other.orderInLibrary; |
110 } else { | 122 } else { |
(...skipping 20 matching lines...) Expand all Loading... |
131 final int end; | 143 final int end; |
132 | 144 |
133 SourceSpan(this.file, this.start, this.end); | 145 SourceSpan(this.file, this.start, this.end); |
134 | 146 |
135 /** Returns the source text corresponding to this [Span]. */ | 147 /** Returns the source text corresponding to this [Span]. */ |
136 String get text() { | 148 String get text() { |
137 return file.text.substring(start, end); | 149 return file.text.substring(start, end); |
138 } | 150 } |
139 | 151 |
140 toMessageString(String message) { | 152 toMessageString(String message) { |
141 return file.getLocationMessage(message, start, end, true); | 153 return file.getLocationMessage(message, start, end: end, includeText: true); |
142 } | 154 } |
143 | 155 |
144 String get locationText() { | 156 String get locationText() { |
145 var line = file.getLine(start); | 157 var line = file.getLine(start); |
146 var column = file.getColumn(line, start); | 158 var column = file.getColumn(line, start); |
147 return '${file.filename}:${line + 1}:${column + 1}'; | 159 return '${file.filename}:${line + 1}:${column + 1}'; |
148 } | 160 } |
149 | 161 |
150 /** Compares two source spans by file and position. Handles nulls. */ | 162 /** Compares two source spans by file and position. Handles nulls. */ |
151 int compareTo(SourceSpan other) { | 163 int compareTo(SourceSpan other) { |
152 if (file == other.file) { | 164 if (file == other.file) { |
153 int d = start - other.start; | 165 int d = start - other.start; |
154 return d == 0 ? (end - other.end) : d; | 166 return d == 0 ? (end - other.end) : d; |
155 } | 167 } |
156 return file.compareTo(other.file); | 168 return file.compareTo(other.file); |
157 } | 169 } |
158 } | 170 } |
OLD | NEW |