Skip to content

Commit

Permalink
fixed composintag issue with consecutive trigger characters (#3)
Browse files Browse the repository at this point in the history
* fixed compositag issue with consecutive trigger characters + added corresponding test

* removed useless import
  • Loading branch information
BazinC authored Mar 9, 2024
1 parent b0d08dd commit 6a9db1d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ class TagFinder {
final charactersAfter = rawText.substring(splitIndex).characters;
final iteratorDownstream = charactersAfter.iterator;

var movedBack = iteratorUpstream.moveBack();

if (iteratorUpstream.current != tagRule.trigger) {
while (iteratorUpstream.moveBack()) {
while (movedBack) {
final current = iteratorUpstream.current;
if (tagRule.excludedCharacters.contains(current)) {
// The upstream character isn't allowed to appear in a tag. Break before moving
Expand All @@ -55,7 +57,10 @@ class TagFinder {
iteratorUpstream.moveBack();
break;
}
movedBack = iteratorUpstream.moveBack();
}
} else if (movedBack) {
iteratorUpstream.moveNext();
}

while (iteratorDownstream.moveNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ void main() {
),
],
),
const TagRule(trigger: "@"),
plugin: StableTagPlugin(
tagRule: const TagRule(trigger: '@'),
),
);

// Place the caret at "before |"
Expand Down Expand Up @@ -435,6 +437,47 @@ void main() {
final text = SuperEditorInspector.findTextInComponent("1");
expect(text.text, "@💙");
});

testWidgetsOnAllPlatforms("composing tag with consecutive trigger characters", (tester) async {
final plugin = StableTagPlugin();
await _pumpTestEditor(
tester,
singleParagraphEmptyDoc(),
plugin: plugin,
);
await tester.placeCaretInParagraph("1", 0);

// Type two consecutive trigger characters.
await tester.typeImeText("@@");

final composingStableTag = plugin.tagIndex.composingStableTag.value!;

// Ensure the composing tag is correct is placed on the second @, with empty token.
expect(
composingStableTag,
const ComposingStableTag(
DocumentRange(
start: DocumentPosition(
nodeId: '1',
nodePosition: TextNodePosition(offset: 2),
),
end: DocumentPosition(
nodeId: '1',
nodePosition: TextNodePosition(offset: 2),
),
),
''),
);

final commitedTags = plugin.tagIndex.getCommittedTagsInTextNode('1');

expect(commitedTags.length, 1);

final commitTag = commitedTags.first;

// Ensure the committed tag is correct is the first @
expect(commitTag, const IndexedTag(Tag('@', ''), '1', 0));
});
});

group("commits >", () {
Expand Down Expand Up @@ -1091,14 +1134,15 @@ void main() {

Future<TestDocumentContext> _pumpTestEditor(
WidgetTester tester,
MutableDocument document, [
TagRule tagRule = userTagRule,
]) async {
MutableDocument document, {
SuperEditorPlugin? plugin,
}) async {
return await tester
.createDocument()
.withCustomContent(document)
.withPlugin(StableTagPlugin(
tagRule: tagRule,
))
.withPlugin(plugin ??
StableTagPlugin(
tagRule: userTagRule,
))
.pump();
}

0 comments on commit 6a9db1d

Please sign in to comment.