Unverified Commit dd90ff42 authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Remove TextRange, export it from dart:ui (#44422)

This removes TextRange from the framework and moves it to the engine, in preparation for using it to return text ranges from the text extent APIs, and updates the APIs that use Paragraph.getWordBoundary (there was only one) to expect a TextRange or  a pair of ints temporarily until the engine side returns TextRanges, so that I can convert over without breaking the builds.
parent 9323a623
...@@ -816,8 +816,15 @@ class TextPainter { ...@@ -816,8 +816,15 @@ class TextPainter {
/// <http://www.unicode.org/reports/tr29/#Word_Boundaries>. /// <http://www.unicode.org/reports/tr29/#Word_Boundaries>.
TextRange getWordBoundary(TextPosition position) { TextRange getWordBoundary(TextPosition position) {
assert(!_needsLayout); assert(!_needsLayout);
final List<int> indices = _paragraph.getWordBoundary(position.offset); // TODO(gspencergoog): remove the List<int>-based code when the engine API
return TextRange(start: indices[0], end: indices[1]); // returns a TextRange instead of a List<int>.
final dynamic boundary = _paragraph.getWordBoundary(position.offset);
if (boundary is List<int>) {
final List<int> indices = boundary;
return TextRange(start: indices[0], end: indices[1]);
}
final TextRange range = boundary;
return range;
} }
/// Returns the full list of [LineMetrics] that describe in detail the various /// Returns the full list of [LineMetrics] that describe in detail the various
......
...@@ -2,96 +2,11 @@ ...@@ -2,96 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:ui' show hashValues, TextAffinity, TextPosition; import 'dart:ui' show hashValues, TextAffinity, TextPosition, TextRange;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
export 'dart:ui' show TextAffinity, TextPosition; export 'dart:ui' show TextAffinity, TextPosition, TextRange;
/// A range of characters in a string of text.
@immutable
class TextRange {
/// Creates a text range.
///
/// The [start] and [end] arguments must not be null. Both the [start] and
/// [end] must either be greater than or equal to zero or both exactly -1.
///
/// Instead of creating an empty text range, consider using the [empty]
/// constant.
const TextRange({
@required this.start,
@required this.end,
}) : assert(start != null && start >= -1),
assert(end != null && end >= -1);
/// A text range that starts and ends at offset.
///
/// The [offset] argument must be non-null and greater than or equal to -1.
const TextRange.collapsed(int offset)
: assert(offset != null && offset >= -1),
start = offset,
end = offset;
/// A text range that contains nothing and is not in the text.
static const TextRange empty = TextRange(start: -1, end: -1);
/// The index of the first character in the range.
///
/// If [start] and [end] are both -1, the text range is empty.
final int start;
/// The next index after the characters in this range.
///
/// If [start] and [end] are both -1, the text range is empty.
final int end;
/// Whether this range represents a valid position in the text.
bool get isValid => start >= 0 && end >= 0;
/// Whether this range is empty (but still potentially placed inside the text).
bool get isCollapsed => start == end;
/// Whether the start of this range precedes the end.
bool get isNormalized => end >= start;
/// The text before this range.
String textBefore(String text) {
assert(isNormalized);
return text.substring(0, start);
}
/// The text after this range.
String textAfter(String text) {
assert(isNormalized);
return text.substring(end);
}
/// The text inside this range.
String textInside(String text) {
assert(isNormalized);
return text.substring(start, end);
}
@override
bool operator ==(dynamic other) {
if (identical(this, other))
return true;
if (other is! TextRange)
return false;
final TextRange typedOther = other;
return typedOther.start == start
&& typedOther.end == end;
}
@override
int get hashCode => hashValues(
start.hashCode,
end.hashCode,
);
@override
String toString() => 'TextRange(start: $start, end: $end)';
}
/// A range of text that represents a selection. /// A range of text that represents a selection.
@immutable @immutable
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment