| 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].
|
| 31 | 1 | String get textBeforeSelection {
|
| 32 | 2 | if (selectionStart == null) return text;
|
| 33 | 3 | 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.
|
| 39 | 1 | String get selectedText {
|
| 40 | 1 | if (selectionStart == null) return "";
|
| 41 | 6 | 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.
|
| 47 | 1 | String get textAfterSelection {
|
| 48 | 1 | if (selectionStart == null) return "";
|
| 49 | 5 | return text.substring(selectionStart + selectionLength);
|
| 50 | | }
|
| 51 | |
|
| 52 | 4 | 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.
|
| 58 | 12 | if ((selectionStart == null) != (selectionLength == null)) {
|
| 59 | 1 | throw new ArgumentError(
|
| 60 | | "Is selectionStart is provided, selectionLength must be too.");
|
| 61 | | }
|
| 62 | |
|
| 63 | 4 | if (selectionStart != null) {
|
| 64 | 4 | if (selectionStart < 0) {
|
| 65 | 1 | throw new ArgumentError("selectionStart must be non-negative.");
|
| 66 | | }
|
| 67 | |
|
| 68 | 8 | if (selectionStart > text.length) {
|
| 69 | 1 | throw new ArgumentError("selectionStart must be within text.");
|
| 70 | | }
|
| 71 | | }
|
| 72 | |
|
| 73 | 4 | if (selectionLength != null) {
|
| 74 | 4 | if (selectionLength < 0) {
|
| 75 | 1 | throw new ArgumentError("selectionLength must be non-negative.");
|
| 76 | | }
|
| 77 | |
|
| 78 | 12 | if (selectionStart + selectionLength > text.length) {
|
| 79 | 1 | throw new ArgumentError("selectionLength must end within text.");
|
| 80 | | }
|
| 81 | | }
|
| 82 | | }
|
| 83 | | }
|