Coverage report for lib/src/source_code.dart

Line coverage: 22 / 22 (100.0%)

All files > lib/src/source_code.dart

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
3
// BSD-style license that can be found in the LICENSE file.
4
5
library dart_style.src.source_code;
6
7
/// Describes a chunk of source code that is to be formatted or has been
8
/// formatted.
9
class SourceCode {
10
  /// The [uri] where the source code is from.
11
  ///
12
  /// Used in error messages if the code cannot be parsed.
13
  final String uri;
14
15
  /// The Dart source code text.
16
  final String text;
17
18
  /// Whether the source is a compilation unit or a bare statement.
19
  final bool isCompilationUnit;
20
21
  /// The offset in [text] where the selection begins, or `null` if there is
22
  /// no selection.
23
  final int selectionStart;
24
25
  /// The number of selected characters or `null` if there is no selection.
26
  final int selectionLength;
27
28
  /// Gets the source code before the beginning of the selection.
29
  ///
30
  /// If there is no selection, returns [text].
311
  String get textBeforeSelection {
322
    if (selectionStart == null) return text;
333
    return text.substring(0, selectionStart);
34
  }
35
36
  /// Gets the selected source code, if any.
37
  ///
38
  /// If there is no selection, returns an empty string.
391
  String get selectedText {
401
    if (selectionStart == null) return "";
416
    return text.substring(selectionStart, selectionStart + selectionLength);
42
  }
43
44
  /// Gets the source code following the selection.
45
  ///
46
  /// If there is no selection, returns an empty string.
471
  String get textAfterSelection {
481
    if (selectionStart == null) return "";
495
    return text.substring(selectionStart + selectionLength);
50
  }
51
524
  SourceCode(this.text,
53
      {this.uri,
54
      this.isCompilationUnit: true,
55
      this.selectionStart,
56
      this.selectionLength}) {
57
    // Must either provide both selection bounds or neither.
5812
    if ((selectionStart == null) != (selectionLength == null)) {
591
      throw new ArgumentError(
60
          "Is selectionStart is provided, selectionLength must be too.");
61
    }
62
634
    if (selectionStart != null) {
644
      if (selectionStart < 0) {
651
        throw new ArgumentError("selectionStart must be non-negative.");
66
      }
67
688
      if (selectionStart > text.length) {
691
        throw new ArgumentError("selectionStart must be within text.");
70
      }
71
    }
72
734
    if (selectionLength != null) {
744
      if (selectionLength < 0) {
751
        throw new ArgumentError("selectionLength must be non-negative.");
76
      }
77
7812
      if (selectionStart + selectionLength > text.length) {
791
        throw new ArgumentError("selectionLength must end within text.");
80
      }
81
    }
82
  }
83
}