| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 analyzer.src.generated.source; | 5 library analyzer.src.generated.source; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import "dart:math" as math; | 8 import "dart:math" as math; |
| 9 | 9 |
| 10 import 'package:analyzer/file_system/file_system.dart'; | 10 import 'package:analyzer/file_system/file_system.dart'; |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 } | 177 } |
| 178 | 178 |
| 179 /** | 179 /** |
| 180 * Information about line and column information within a source file. | 180 * Information about line and column information within a source file. |
| 181 */ | 181 */ |
| 182 class LineInfo { | 182 class LineInfo { |
| 183 /** | 183 /** |
| 184 * A list containing the offsets of the first character of each line in the | 184 * A list containing the offsets of the first character of each line in the |
| 185 * source code. | 185 * source code. |
| 186 */ | 186 */ |
| 187 final List<int> _lineStarts; | 187 final List<int> lineStarts; |
| 188 | 188 |
| 189 /** | 189 /** |
| 190 * The zero-based [_lineStarts] index resulting from the last call to | 190 * The zero-based [lineStarts] index resulting from the last call to |
| 191 * [getLocation]. | 191 * [getLocation]. |
| 192 */ | 192 */ |
| 193 int _previousLine = 0; | 193 int _previousLine = 0; |
| 194 | 194 |
| 195 /** | 195 /** |
| 196 * Initialize a newly created set of line information to represent the data | 196 * Initialize a newly created set of line information to represent the data |
| 197 * encoded in the given list of [_lineStarts]. | 197 * encoded in the given list of [lineStarts]. |
| 198 */ | 198 */ |
| 199 factory LineInfo(List<int> _lineStarts) => new LineInfoWithCount(_lineStarts); | 199 factory LineInfo(List<int> lineStarts) => new LineInfoWithCount(lineStarts); |
| 200 | 200 |
| 201 /** | 201 /** |
| 202 * Initialize a newly created set of line information corresponding to the | 202 * Initialize a newly created set of line information corresponding to the |
| 203 * given file [content]. | 203 * given file [content]. |
| 204 */ | 204 */ |
| 205 factory LineInfo.fromContent(String content) => | 205 factory LineInfo.fromContent(String content) => |
| 206 new LineInfoWithCount(StringUtilities.computeLineStarts(content)); | 206 new LineInfoWithCount(StringUtilities.computeLineStarts(content)); |
| 207 | 207 |
| 208 /** | 208 /** |
| 209 * Initialize a newly created set of line information to represent the data | 209 * Initialize a newly created set of line information to represent the data |
| 210 * encoded in the given list of [_lineStarts]. | 210 * encoded in the given list of [lineStarts]. |
| 211 */ | 211 */ |
| 212 LineInfo._(this._lineStarts) { | 212 LineInfo._(this.lineStarts) { |
| 213 if (_lineStarts == null) { | 213 if (lineStarts == null) { |
| 214 throw new IllegalArgumentException("lineStarts must be non-null"); | 214 throw new IllegalArgumentException("lineStarts must be non-null"); |
| 215 } else if (_lineStarts.length < 1) { | 215 } else if (lineStarts.length < 1) { |
| 216 throw new IllegalArgumentException("lineStarts must be non-empty"); | 216 throw new IllegalArgumentException("lineStarts must be non-empty"); |
| 217 } | 217 } |
| 218 } | 218 } |
| 219 | 219 |
| 220 /** | 220 /** |
| 221 * The number of lines. | 221 * The number of lines. |
| 222 */ | 222 */ |
| 223 int get lineCount => _lineStarts.length; | 223 int get lineCount => lineStarts.length; |
| 224 | 224 |
| 225 /** | 225 /** |
| 226 * Return the location information for the character at the given [offset]. | 226 * Return the location information for the character at the given [offset]. |
| 227 */ | 227 */ |
| 228 LineInfo_Location getLocation(int offset) { | 228 LineInfo_Location getLocation(int offset) { |
| 229 var min = 0; | 229 var min = 0; |
| 230 var max = _lineStarts.length - 1; | 230 var max = lineStarts.length - 1; |
| 231 | 231 |
| 232 // Subsequent calls to [getLocation] are often for offsets near each other. | 232 // Subsequent calls to [getLocation] are often for offsets near each other. |
| 233 // To take advantage of that, we cache the index of the line start we found | 233 // To take advantage of that, we cache the index of the line start we found |
| 234 // when this was last called. If the current offset is on that line or | 234 // when this was last called. If the current offset is on that line or |
| 235 // later, we'll skip those early indices completely when searching. | 235 // later, we'll skip those early indices completely when searching. |
| 236 if (offset >= _lineStarts[_previousLine]) { | 236 if (offset >= lineStarts[_previousLine]) { |
| 237 min = _previousLine; | 237 min = _previousLine; |
| 238 | 238 |
| 239 // Before kicking off a full binary search, do a quick check here to see | 239 // Before kicking off a full binary search, do a quick check here to see |
| 240 // if the new offset is on that exact line. | 240 // if the new offset is on that exact line. |
| 241 if (min == _lineStarts.length - 1 || offset < _lineStarts[min + 1]) { | 241 if (min == lineStarts.length - 1 || offset < lineStarts[min + 1]) { |
| 242 return new LineInfo_Location(min + 1, offset - _lineStarts[min] + 1); | 242 return new LineInfo_Location(min + 1, offset - lineStarts[min] + 1); |
| 243 } | 243 } |
| 244 } | 244 } |
| 245 | 245 |
| 246 // Binary search to fine the line containing this offset. | 246 // Binary search to fine the line containing this offset. |
| 247 while (min < max) { | 247 while (min < max) { |
| 248 var midpoint = (max - min + 1) ~/ 2 + min; | 248 var midpoint = (max - min + 1) ~/ 2 + min; |
| 249 | 249 |
| 250 if (_lineStarts[midpoint] > offset) { | 250 if (lineStarts[midpoint] > offset) { |
| 251 max = midpoint - 1; | 251 max = midpoint - 1; |
| 252 } else { | 252 } else { |
| 253 min = midpoint; | 253 min = midpoint; |
| 254 } | 254 } |
| 255 } | 255 } |
| 256 | 256 |
| 257 _previousLine = min; | 257 _previousLine = min; |
| 258 | 258 |
| 259 return new LineInfo_Location(min + 1, offset - _lineStarts[min] + 1); | 259 return new LineInfo_Location(min + 1, offset - lineStarts[min] + 1); |
| 260 } | 260 } |
| 261 | 261 |
| 262 /** | 262 /** |
| 263 * Return the offset of the first character on the line with the given | 263 * Return the offset of the first character on the line with the given |
| 264 * [lineNumber]. | 264 * [lineNumber]. |
| 265 */ | 265 */ |
| 266 int getOffsetOfLine(int lineNumber) { | 266 int getOffsetOfLine(int lineNumber) { |
| 267 if (lineNumber < 0 || lineNumber >= lineCount) { | 267 if (lineNumber < 0 || lineNumber >= lineCount) { |
| 268 throw new ArgumentError( | 268 throw new ArgumentError( |
| 269 'Invalid line number: $lineNumber; must be between 0 and ${lineCount -
1}'); | 269 'Invalid line number: $lineNumber; must be between 0 and ${lineCount -
1}'); |
| 270 } | 270 } |
| 271 return _lineStarts[lineNumber]; | 271 return lineStarts[lineNumber]; |
| 272 } | 272 } |
| 273 } | 273 } |
| 274 | 274 |
| 275 /** | 275 /** |
| 276 * Instances of the class `Location` represent the location of a character as a
line and | 276 * Instances of the class `Location` represent the location of a character as a
line and |
| 277 * column pair. | 277 * column pair. |
| 278 */ | 278 */ |
| 279 class LineInfo_Location { | 279 class LineInfo_Location { |
| 280 /** | 280 /** |
| 281 * The one-based index of the line containing the character. | 281 * The one-based index of the line containing the character. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 303 /** | 303 /** |
| 304 * Information about line and column information within a source file, | 304 * Information about line and column information within a source file, |
| 305 * including a count of the total number of lines. | 305 * including a count of the total number of lines. |
| 306 * | 306 * |
| 307 * TODO(paulberry): in the next major version roll of analyzer, merge this | 307 * TODO(paulberry): in the next major version roll of analyzer, merge this |
| 308 * class into [LineInfo]. | 308 * class into [LineInfo]. |
| 309 */ | 309 */ |
| 310 class LineInfoWithCount extends LineInfo { | 310 class LineInfoWithCount extends LineInfo { |
| 311 /** | 311 /** |
| 312 * Initialize a newly created set of line information to represent the data | 312 * Initialize a newly created set of line information to represent the data |
| 313 * encoded in the given list of [_lineStarts]. | 313 * encoded in the given list of [lineStarts]. |
| 314 */ | 314 */ |
| 315 LineInfoWithCount(List<int> _lineStarts) : super._(_lineStarts); | 315 LineInfoWithCount(List<int> lineStarts) : super._(lineStarts); |
| 316 | |
| 317 /** | |
| 318 * Return the number of lines in the file. | |
| 319 */ | |
| 320 int get lineCount => _lineStarts.length; | |
| 321 } | 316 } |
| 322 | 317 |
| 323 /** | 318 /** |
| 324 * Instances of interface `LocalSourcePredicate` are used to determine if the gi
ven | 319 * Instances of interface `LocalSourcePredicate` are used to determine if the gi
ven |
| 325 * [Source] is "local" in some sense, so can be updated. | 320 * [Source] is "local" in some sense, so can be updated. |
| 326 */ | 321 */ |
| 327 abstract class LocalSourcePredicate { | 322 abstract class LocalSourcePredicate { |
| 328 /** | 323 /** |
| 329 * Instance of [LocalSourcePredicate] that always returns `false`. | 324 * Instance of [LocalSourcePredicate] that always returns `false`. |
| 330 */ | 325 */ |
| (...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 934 Source resolveAbsolute(Uri uri, [Uri actualUri]); | 929 Source resolveAbsolute(Uri uri, [Uri actualUri]); |
| 935 | 930 |
| 936 /** | 931 /** |
| 937 * Return an absolute URI that represents the given [source], or `null` if a | 932 * Return an absolute URI that represents the given [source], or `null` if a |
| 938 * valid URI cannot be computed. | 933 * valid URI cannot be computed. |
| 939 * | 934 * |
| 940 * The computation should be based solely on [source.fullName]. | 935 * The computation should be based solely on [source.fullName]. |
| 941 */ | 936 */ |
| 942 Uri restoreAbsolute(Source source) => null; | 937 Uri restoreAbsolute(Source source) => null; |
| 943 } | 938 } |
| OLD | NEW |