Fix multiple suggestions with tabs
Since tabs are removed by marked renderer, we get content of
suggestion from comment content with substring. But that returns
always first suggestion.
To show multiple suggestions with different content, we use
index and get suggestions according to index.
Google-Bug-Id: b/371591688
Release-Notes: skip
Change-Id: I4b5eefe56d6dffefec911b2670829a6cbfd9848e
diff --git a/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts b/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts
index d183211..6e76aaa 100644
--- a/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts
+++ b/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text.ts
@@ -305,11 +305,14 @@
private convertCodeToSuggestions() {
const marks = this.renderRoot.querySelectorAll('mark');
- for (const userSuggestionMark of marks) {
+ marks.forEach((userSuggestionMark, index) => {
const userSuggestion = document.createElement('gr-user-suggestion-fix');
// Temporary workaround for bug - tabs replacement
if (this.content.includes('\t')) {
- userSuggestion.textContent = getUserSuggestionFromString(this.content);
+ userSuggestion.textContent = getUserSuggestionFromString(
+ this.content,
+ index
+ );
} else {
userSuggestion.textContent = userSuggestionMark.textContent ?? '';
}
@@ -317,7 +320,7 @@
userSuggestion,
userSuggestionMark
);
- }
+ });
}
}
diff --git a/polygerrit-ui/app/utils/comment-util.ts b/polygerrit-ui/app/utils/comment-util.ts
index c8e5173..1324061 100644
--- a/polygerrit-ui/app/utils/comment-util.ts
+++ b/polygerrit-ui/app/utils/comment-util.ts
@@ -556,12 +556,17 @@
return comment.message?.includes(USER_SUGGESTION_START_PATTERN) ?? false;
}
-export function getUserSuggestionFromString(content: string) {
- const start =
- content.indexOf(USER_SUGGESTION_START_PATTERN) +
- USER_SUGGESTION_START_PATTERN.length;
- const end = content.indexOf('\n```', start);
- return content.substring(start, end);
+export function getUserSuggestionFromString(
+ content: string,
+ suggestionIndex = 0
+) {
+ const suggestions = content.split(USER_SUGGESTION_START_PATTERN).slice(1);
+ if (suggestions.length === 0) return '';
+
+ const targetIndex = Math.min(suggestionIndex, suggestions.length - 1);
+ const targetSuggestion = suggestions[targetIndex];
+ const end = targetSuggestion.indexOf('\n```');
+ return end !== -1 ? targetSuggestion.substring(0, end) : targetSuggestion;
}
export function getUserSuggestion(comment: Comment) {
diff --git a/polygerrit-ui/app/utils/comment-util_test.ts b/polygerrit-ui/app/utils/comment-util_test.ts
index 16c8bfd..031a738 100644
--- a/polygerrit-ui/app/utils/comment-util_test.ts
+++ b/polygerrit-ui/app/utils/comment-util_test.ts
@@ -18,6 +18,7 @@
getMentionedThreads,
isNewThread,
createNew,
+ getUserSuggestionFromString,
} from './comment-util';
import {
createAccountWithEmail,
@@ -533,4 +534,69 @@
]);
});
});
+
+ suite('getUserSuggestionFromString', () => {
+ const createSuggestionContent = (suggestions: string[]) =>
+ suggestions
+ .map(s => `${USER_SUGGESTION_START_PATTERN}${s}\n\`\`\``)
+ .join('\n');
+
+ test('returns empty string for content without suggestions', () => {
+ const content = 'This is a comment without any suggestions.';
+ assert.equal(getUserSuggestionFromString(content), '');
+ });
+
+ test('returns first suggestion when no index is provided', () => {
+ const content = createSuggestionContent(['First suggestion']);
+ assert.equal(getUserSuggestionFromString(content), 'First suggestion');
+ });
+
+ test('returns correct suggestion for given index', () => {
+ const content = createSuggestionContent([
+ 'First suggestion',
+ 'Second suggestion',
+ 'Third suggestion',
+ ]);
+ assert.equal(
+ getUserSuggestionFromString(content, 1),
+ 'Second suggestion'
+ );
+ });
+
+ test('returns last suggestion when index is out of bounds', () => {
+ const content = createSuggestionContent([
+ 'First suggestion',
+ 'Second suggestion',
+ ]);
+ assert.equal(
+ getUserSuggestionFromString(content, 5),
+ 'Second suggestion'
+ );
+ });
+
+ test('handles suggestion without closing backticks', () => {
+ const content = `${USER_SUGGESTION_START_PATTERN}Unclosed suggestion`;
+ assert.equal(getUserSuggestionFromString(content), 'Unclosed suggestion');
+ });
+
+ test('handles multiple suggestions with varying content', () => {
+ const content = createSuggestionContent([
+ 'First\nMultiline\nSuggestion',
+ 'Second suggestion',
+ 'Third suggestion with `backticks`',
+ ]);
+ assert.equal(
+ getUserSuggestionFromString(content, 0),
+ 'First\nMultiline\nSuggestion'
+ );
+ assert.equal(
+ getUserSuggestionFromString(content, 1),
+ 'Second suggestion'
+ );
+ assert.equal(
+ getUserSuggestionFromString(content, 2),
+ 'Third suggestion with `backticks`'
+ );
+ });
+ });
});