Skip to content

Commit

Permalink
Updating various parts of super_editor to support AttributedSpans wit…
Browse files Browse the repository at this point in the history
…h exclusive end positions
  • Loading branch information
jmatth committed Jun 24, 2022
1 parent 79b9e84 commit ddb1af5
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 50 deletions.
4 changes: 2 additions & 2 deletions attributed_text/lib/src/attributed_spans.dart
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ class AttributedSpans {
required AttributedSpans other,
required int index,
}) {
if (markers.isNotEmpty && markers.last.offset >= index) {
if (markers.isNotEmpty && markers.last.offset > index) {
throw Exception(
'Another AttributedSpans can only be appended after the final marker in this AttributedSpans. Final marker: ${markers.last}');
}
Expand Down Expand Up @@ -605,7 +605,7 @@ class AttributedSpans {
// Look for any compatible attributions at
// `mergePoint - 1` and `mergePoint` and combine them.
final endAtMergePointMarkers =
attributions.where((marker) => marker.isEnd && marker.offset == mergePoint - 1).toList();
attributions.where((marker) => marker.isEnd && marker.offset == mergePoint).toList();
final startAtMergePointMarkers =
attributions.where((marker) => marker.isStart && marker.offset == mergePoint).toList();
for (final startMarker in startAtMergePointMarkers) {
Expand Down
8 changes: 3 additions & 5 deletions attributed_text/lib/src/attributed_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,14 @@ class AttributedText {
AttributedText copyText(int startOffset, [int? endOffset]) {
_log.fine('start: $startOffset, end: $endOffset');

// Note: -1 because copyText() uses an exclusive `start` and `end` but
// _copyAttributionRegion() uses an inclusive `start` and `end`.
final startCopyOffset = startOffset < text.length ? startOffset : text.length - 1;
int endCopyOffset;
if (endOffset == startOffset) {
endCopyOffset = startCopyOffset;
} else if (endOffset != null) {
endCopyOffset = endOffset - 1;
endCopyOffset = endOffset;
} else {
endCopyOffset = text.length - 1;
endCopyOffset = text.length;
}
_log.fine('offsets, start: $startCopyOffset, end: $endCopyOffset');

Expand Down Expand Up @@ -269,7 +267,7 @@ class AttributedText {
final insertedText = AttributedText(
text: textToInsert,
);
final insertTextRange = SpanRange(start: 0, end: textToInsert.length - 1);
final insertTextRange = SpanRange(start: 0, end: textToInsert.length);
for (dynamic attribution in applyAttributions) {
insertedText.addAttribution(attribution, insertTextRange);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class AttributedTextEditingController with ChangeNotifier {
for (final attribution in attributions) {
_text.toggleAttribution(
attribution,
SpanRange(start: selection.start, end: selection.end - 1),
SpanRange(start: selection.start, end: selection.end),
);
}

Expand All @@ -133,7 +133,7 @@ class AttributedTextEditingController with ChangeNotifier {
}

_text.clearAttributions(
SpanRange(start: selection.start, end: selection.end - 1),
SpanRange(start: selection.start, end: selection.end),
);

notifyListeners();
Expand Down
22 changes: 11 additions & 11 deletions super_editor/test/src/attributed_text_styles_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: ExpectedSpans.bold, offset: 0, markerType: SpanMarkerType.start),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 9, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 10, markerType: SpanMarkerType.end),
],
),
);
Expand All @@ -38,7 +38,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: ExpectedSpans.bold, offset: 1, markerType: SpanMarkerType.start),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 1, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 2, markerType: SpanMarkerType.end),
],
),
);
Expand All @@ -61,7 +61,7 @@ void main() {
// Notice that the markers are provided in reverse order:
// end then start. Order shouldn't matter within a single
// position index. This test ensures that.
const SpanMarker(attribution: ExpectedSpans.bold, offset: 1, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 2, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 1, markerType: SpanMarkerType.start),
],
),
Expand All @@ -79,7 +79,7 @@ void main() {

test('add single character style', () {
final text = AttributedText(text: 'abcdefghij');
text.addAttribution(ExpectedSpans.bold, const SpanRange(start: 1, end: 1));
text.addAttribution(ExpectedSpans.bold, const SpanRange(start: 1, end: 2));
final textSpan = text.computeTextSpan(_styleBuilder);

expect(textSpan.text, null);
Expand All @@ -97,7 +97,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: ExpectedSpans.bold, offset: 2, markerType: SpanMarkerType.start),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 7, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 8, markerType: SpanMarkerType.end),
],
),
);
Expand All @@ -117,7 +117,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: ExpectedSpans.bold, offset: 9, markerType: SpanMarkerType.start),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 9, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 10, markerType: SpanMarkerType.end),
],
),
);
Expand All @@ -127,7 +127,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: ExpectedSpans.bold, offset: 0, markerType: SpanMarkerType.start),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 0, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 1, markerType: SpanMarkerType.end),
],
),
));
Expand All @@ -152,9 +152,9 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: ExpectedSpans.bold, offset: 0, markerType: SpanMarkerType.start),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 4, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 5, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.italics, offset: 5, markerType: SpanMarkerType.start),
const SpanMarker(attribution: ExpectedSpans.italics, offset: 9, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.italics, offset: 10, markerType: SpanMarkerType.end),
],
),
);
Expand All @@ -177,8 +177,8 @@ void main() {
attributions: [
const SpanMarker(attribution: ExpectedSpans.bold, offset: 2, markerType: SpanMarkerType.start),
const SpanMarker(attribution: ExpectedSpans.italics, offset: 4, markerType: SpanMarkerType.start),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 5, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.italics, offset: 7, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 6, markerType: SpanMarkerType.end),
const SpanMarker(attribution: ExpectedSpans.italics, offset: 8, markerType: SpanMarkerType.end),
],
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 7, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 9, markerType: SpanMarkerType.end),
],
),
),
Expand All @@ -105,7 +105,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 7, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 9, markerType: SpanMarkerType.end),
],
),
),
Expand Down Expand Up @@ -404,7 +404,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 0, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 0, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 1, markerType: SpanMarkerType.end),
],
),
),
Expand Down Expand Up @@ -500,7 +500,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 0, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 10, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 11, markerType: SpanMarkerType.end),
],
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 7, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 9, markerType: SpanMarkerType.end),
],
),
),
Expand Down Expand Up @@ -221,7 +221,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 7, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 9, markerType: SpanMarkerType.end),
],
),
),
Expand Down
10 changes: 4 additions & 6 deletions super_editor_markdown/lib/src/markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -381,23 +381,23 @@ class _InlineMarkdownToDocument implements md.NodeVisitor {
boldAttribution,
SpanRange(
start: 0,
end: styledText.text.length - 1,
end: styledText.text.length,
),
);
} else if (element.tag == 'em') {
styledText.addAttribution(
italicsAttribution,
SpanRange(
start: 0,
end: styledText.text.length - 1,
end: styledText.text.length,
),
);
} else if (element.tag == 'a') {
styledText.addAttribution(
LinkAttribution(url: Uri.parse(element.attributes['href']!)),
SpanRange(
start: 0,
end: styledText.text.length - 1,
end: styledText.text.length,
),
);
}
Expand Down Expand Up @@ -478,10 +478,8 @@ extension on AttributedText {
..write(markdownStyles);
break;
case AttributionVisitEvent.end:
// +1 on end index because this visitor has inclusive indices
// whereas substring() expects an exclusive ending index.
buffer
..write(fullText.text.substring(spanStart, index + 1))
..write(fullText.text.substring(spanStart, index))
..write(markdownStyles)
..write(linkMarker);
break;
Expand Down
36 changes: 18 additions & 18 deletions super_editor_markdown/test/super_editor_markdown_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:super_editor/super_editor.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:super_editor/super_editor.dart';
import 'package:super_editor_markdown/super_editor_markdown.dart';

void main() {
Expand Down Expand Up @@ -41,7 +41,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 3, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 9, markerType: SpanMarkerType.end),
],
),
),
Expand Down Expand Up @@ -73,7 +73,7 @@ void main() {
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 10, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 19, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 20, markerType: SpanMarkerType.end),
],
),
),
Expand Down Expand Up @@ -122,7 +122,7 @@ This is some code
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 5, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 9, markerType: SpanMarkerType.end),
],
),
),
Expand All @@ -141,9 +141,9 @@ This is some code
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 5, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 9, markerType: SpanMarkerType.end),
const SpanMarker(attribution: italicsAttribution, offset: 5, markerType: SpanMarkerType.start),
const SpanMarker(attribution: italicsAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: italicsAttribution, offset: 9, markerType: SpanMarkerType.end),
],
),
),
Expand All @@ -162,9 +162,9 @@ This is some code
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 5, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 9, markerType: SpanMarkerType.end),
const SpanMarker(attribution: italicsAttribution, offset: 5, markerType: SpanMarkerType.start),
const SpanMarker(attribution: italicsAttribution, offset: 18, markerType: SpanMarkerType.end),
const SpanMarker(attribution: italicsAttribution, offset: 19, markerType: SpanMarkerType.end),
],
),
),
Expand All @@ -184,9 +184,9 @@ This is some code
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 5, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 9, markerType: SpanMarkerType.end),
const SpanMarker(attribution: codeAttribution, offset: 5, markerType: SpanMarkerType.start),
const SpanMarker(attribution: codeAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: codeAttribution, offset: 9, markerType: SpanMarkerType.end),
],
),
),
Expand All @@ -210,7 +210,7 @@ This is some code
markerType: SpanMarkerType.start),
SpanMarker(
attribution: LinkAttribution(url: Uri.https('example.org', '')),
offset: 18,
offset: 19,
markerType: SpanMarkerType.end),
],
),
Expand All @@ -235,10 +235,10 @@ This is some code
markerType: SpanMarkerType.start),
SpanMarker(
attribution: LinkAttribution(url: Uri.https('example.org', '')),
offset: 18,
offset: 19,
markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 10, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 18, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 19, markerType: SpanMarkerType.end),
],
),
),
Expand All @@ -265,7 +265,7 @@ This is some code
offset: 18,
markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 5, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 9, markerType: SpanMarkerType.end),
],
),
),
Expand Down Expand Up @@ -350,7 +350,7 @@ This is some code
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 0, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 8, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 9, markerType: SpanMarkerType.end),
],
),
),
Expand Down Expand Up @@ -412,7 +412,7 @@ This is some code
spans: AttributedSpans(
attributions: [
const SpanMarker(attribution: boldAttribution, offset: 0, markerType: SpanMarkerType.start),
const SpanMarker(attribution: boldAttribution, offset: 6, markerType: SpanMarkerType.end),
const SpanMarker(attribution: boldAttribution, offset: 7, markerType: SpanMarkerType.end),
],
),
),
Expand Down Expand Up @@ -566,8 +566,8 @@ This is some code
expect(styledText.getAllAttributionsAt(0).isEmpty, true);
expect(styledText.getAllAttributionsAt(8).contains(boldAttribution), true);
expect(styledText.getAllAttributionsAt(13).containsAll([boldAttribution, italicsAttribution]), true);
expect(styledText.getAllAttributionsAt(19).isEmpty, true);
expect(styledText.getAllAttributionsAt(40).single, LinkAttribution(url: Uri.https('example.org', '')));
expect(styledText.getAllAttributionsAt(20).isEmpty, true);
expect(styledText.getAllAttributionsAt(41).single, LinkAttribution(url: Uri.https('example.org', '')));
});

test('link within multiple styles', () {
Expand Down

0 comments on commit ddb1af5

Please sign in to comment.