Skip to content

Commit

Permalink
Fix offset for line wrapped in block
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P committed Aug 20, 2024
1 parent d4125c2 commit 30d0ca1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
8 changes: 4 additions & 4 deletions packages/parchment/lib/src/document/attributes.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:math' as math;

import 'package:collection/collection.dart';
import 'package:quiver/core.dart';

/// Scope of a style attribute, defines context in which an attribute can be
/// applied.
Expand Down Expand Up @@ -254,7 +253,7 @@ class ParchmentAttribute<T> implements ParchmentAttributeBuilder<T> {
}

@override
int get hashCode => hash3(key, scope, value);
int get hashCode => Object.hash(key, scope, value);

@override
String toString() => '$key: $value';
Expand Down Expand Up @@ -397,8 +396,9 @@ class ParchmentStyle {

@override
int get hashCode {
final hashes = _data.entries.map((entry) => hash2(entry.key, entry.value));
return hashObjects(hashes);
final hashes =
_data.entries.map((entry) => Object.hash(entry.key, entry.value));
return Object.hashAll(hashes);

Check warning on line 401 in packages/parchment/lib/src/document/attributes.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/document/attributes.dart#L400-L401

Added lines #L400 - L401 were not covered by tests
}

@override
Expand Down
10 changes: 3 additions & 7 deletions packages/parchment/lib/src/document/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ abstract base class Node extends LinkedListEntry<Node> {
void unlink() {
assert(_parent != null);
final oldParent = _parent;
_parent = null;
final oldNext = next;
_parent = null;
super.unlink();
oldNext?.invalidateOffset();
oldParent?.invalidateLength();
Expand Down Expand Up @@ -192,7 +192,7 @@ abstract base class ContainerNode<T extends Node> extends Node {
assert(node._parent == null);
node._parent = this;
_children.add(node);
node.next?.invalidateOffset();
node.invalidateOffset();
invalidateLength();
}

Expand All @@ -201,7 +201,7 @@ abstract base class ContainerNode<T extends Node> extends Node {
assert(node._parent == null);
node._parent = this;
_children.addFirst(node);
node.next?.invalidateOffset();
node.invalidateOffset();
invalidateLength();
}

Expand All @@ -227,8 +227,6 @@ abstract base class ContainerNode<T extends Node> extends Node {
newParent.add(child);
}

invalidateLength();

/// In case [newParent] already had children we need to make sure
/// combined list is optimized.
if (toBeOptimized != null) toBeOptimized.optimize();
Expand Down Expand Up @@ -283,7 +281,6 @@ abstract base class ContainerNode<T extends Node> extends Node {
final result = lookup(index);
result.node!.insert(result.offset, data, style);
}
invalidateLength();
}

@override
Expand All @@ -298,7 +295,6 @@ abstract base class ContainerNode<T extends Node> extends Node {
assert(isNotEmpty);
final res = lookup(index);
res.node!.delete(res.offset, length);
invalidateLength();
}

@override
Expand Down
9 changes: 8 additions & 1 deletion packages/parchment/test/document/line_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,17 @@ void main() {
});

test('format line', () {
root.insert(0, 'Hello world', null);
root.insert(0, 'Hello world\n', null);
root.insert(12, 'Second headline\n', null);
root.retain(11, 1, h1Style);
root.retain(11, 1, rightStyle);

final secondHeadline = root.first.next!;
expect(secondHeadline.offset, 12);

root.retain(27, 1, ParchmentStyle().merge(ParchmentAttribute.cl));
expect(secondHeadline.offset, 0);

final line = root.first as LineNode;
expect(line, hasLength(12));

Expand Down

0 comments on commit 30d0ca1

Please sign in to comment.