Fix how we compute the final text value after substituting a suggestion
We were using a special variable, lastMatchedString which was effectively the same as currentSearchString, except sometimes it was set as undefined. This undefinededness broke how we computed the offset of text for text to keep.
To simplify, we get rid of lastMatchedString and instead simply look at what the currentSearchString.
Google-Bug-Id: b/301931326
Release-Notes: skip
Change-Id: Iade2cf971f4926596058f8b3688a780df7206565
diff --git a/polygerrit-ui/app/elements/shared/gr-textarea/gr-textarea.ts b/polygerrit-ui/app/elements/shared/gr-textarea/gr-textarea.ts
index b8128c2..1501205 100644
--- a/polygerrit-ui/app/elements/shared/gr-textarea/gr-textarea.ts
+++ b/polygerrit-ui/app/elements/shared/gr-textarea/gr-textarea.ts
@@ -133,12 +133,6 @@
private readonly shortcuts = new ShortcutController(this);
- // Represents a snapshot of current search string for which either emoji or mention suggestions are shown.
- // Differs from currentSearchString when user moves the cursor (without typing) while dropdown is open because
- // the currentSearchString is calculated based on specialCharIndex and selectionStart.
- // private but used in tests
- lastMatchedSearchString?: string;
-
constructor() {
super();
subscribe(
@@ -430,7 +424,7 @@
const specialCharIndex = this.specialCharIndex ?? 0;
const beforeSearchString = this.text.substring(0, specialCharIndex);
const afterSearchString = this.text.substring(
- specialCharIndex + 1 + (this.lastMatchedSearchString?.length ?? 0)
+ specialCharIndex + 1 + (this.currentSearchString?.length ?? 0)
);
return beforeSearchString + value + afterSearchString;
}
@@ -484,9 +478,6 @@
}
if (searchString === this.currentSearchString) {
this.suggestions = suggestions;
- this.lastMatchedSearchString = searchString;
- } else {
- this.lastMatchedSearchString = undefined;
}
}
@@ -617,7 +608,6 @@
// hide and reset the autocomplete dropdown.
this.requestUpdate();
this.currentSearchString = '';
- this.lastMatchedSearchString = undefined;
this.closeDropdown();
this.specialCharIndex = -1;
this.focus();
diff --git a/polygerrit-ui/app/elements/shared/gr-textarea/gr-textarea_test.ts b/polygerrit-ui/app/elements/shared/gr-textarea/gr-textarea_test.ts
index 95a382c..4aef66e 100644
--- a/polygerrit-ui/app/elements/shared/gr-textarea/gr-textarea_test.ts
+++ b/polygerrit-ui/app/elements/shared/gr-textarea/gr-textarea_test.ts
@@ -84,14 +84,11 @@
assert.equal(element.specialCharIndex, 0);
assert.isFalse(element.mentionsSuggestions!.isHidden);
assert.equal(element.currentSearchString, '');
- assert.equal(element.lastMatchedSearchString, '');
element.text = '@abc@google.com';
- await waitUntil(() => element.lastMatchedSearchString !== '');
await element.updateComplete;
assert.equal(element.currentSearchString, 'abc@google.com');
- assert.equal(element.lastMatchedSearchString, 'abc@google.com');
assert.equal(element.specialCharIndex, 0);
});
@@ -422,7 +419,6 @@
assert.equal(element.specialCharIndex, 0);
assert.isTrue(!element.emojiSuggestions!.isHidden);
assert.equal(element.currentSearchString, '');
- assert.equal(element.lastMatchedSearchString, '');
});
test('emoji selector opens when a colon is typed after space', async () => {
@@ -438,7 +434,6 @@
assert.equal(element.specialCharIndex, 1);
assert.isTrue(!element.emojiSuggestions!.isHidden);
assert.equal(element.currentSearchString, '');
- assert.equal(element.lastMatchedSearchString, '');
});
test('emoji selector doesn`t open when a colon is typed after character', async () => {
@@ -471,7 +466,6 @@
assert.equal(element.specialCharIndex, 0);
assert.isTrue(!element.emojiSuggestions!.isHidden);
assert.equal(element.currentSearchString, 't');
- assert.equal(element.lastMatchedSearchString, 't');
});
test('emoji selector opens when a colon is typed in middle of text', async () => {
@@ -497,7 +491,6 @@
assert.equal(element.specialCharIndex, 0);
assert.isTrue(!element.emojiSuggestions!.isHidden);
assert.equal(element.currentSearchString, '');
- assert.equal(element.lastMatchedSearchString, '');
});
test('emoji selector closes when text changes before the colon', async () => {
@@ -523,7 +516,6 @@
await element.updateComplete;
assert.equal(element.currentSearchString, 'smi');
- assert.equal(element.lastMatchedSearchString, 'smi');
assert.isFalse(element.emojiSuggestions!.isHidden);
element.text = 'test test test :smi';
@@ -536,7 +528,6 @@
const closeSpy = sinon.spy(element, 'closeDropdown');
element.resetDropdown();
assert.equal(element.currentSearchString, '');
- assert.equal(element.lastMatchedSearchString, undefined);
assert.isTrue(element.emojiSuggestions!.isHidden);
assert.equal(element.specialCharIndex, -1);