Suggest edit works on multiline comments
This feature is under feature flag. This worked only on 1 line
comments. After this change it works on multi-line comments.
It copy whole line for suggestion.
Google-Bug-Id: b/241898563
Release-Notes: skip
Change-Id: Id48f1e49192972b266b1d80ed9a55f4612b7e801
diff --git a/polygerrit-ui/app/utils/comment-util.ts b/polygerrit-ui/app/utils/comment-util.ts
index f26abd6..fbdceb8 100644
--- a/polygerrit-ui/app/utils/comment-util.ts
+++ b/polygerrit-ui/app/utils/comment-util.ts
@@ -513,15 +513,16 @@
return comment.message.substring(start, end);
}
-/**
- * Currently it works only on 1 line.
- * TODO(milutin): Extend for multiline comments
- */
export function getContentInCommentRange(
fileContent: string,
comment: Comment
) {
- return fileContent.split('\n')[comment.line! - 1];
+ const lines = fileContent.split('\n');
+ if (comment.range) {
+ const range = comment.range;
+ return lines.slice(range.start_line - 1, range.end_line).join('\n');
+ }
+ return lines[comment.line! - 1];
}
export function createUserFixSuggestion(
@@ -529,6 +530,7 @@
line: string,
replacement: string
): FixSuggestionInfo[] {
+ const lastLine = line.split('\n').pop();
return [
{
fix_id: USER_SUGGEST_EDIT_FIX_ID,
@@ -537,10 +539,10 @@
{
path: comment.path!,
range: {
- start_line: comment.line!,
+ start_line: comment.range?.start_line ?? comment.line!,
start_character: 0,
- end_line: comment.line!,
- end_character: line.length,
+ end_line: comment.range?.end_line ?? comment.line!,
+ end_character: lastLine!.length,
},
replacement,
},
diff --git a/polygerrit-ui/app/utils/comment-util_test.ts b/polygerrit-ui/app/utils/comment-util_test.ts
index dd57042..46f4c38 100644
--- a/polygerrit-ui/app/utils/comment-util_test.ts
+++ b/polygerrit-ui/app/utils/comment-util_test.ts
@@ -247,40 +247,103 @@
assert.equal(getUserSuggestion(comment), suggestion);
});
- test('getContentInCommentRange', () => {
- const comment = {
- ...createComment(),
- line: 1,
- };
- const content = 'line1\nline2\nline3';
- assert.equal(getContentInCommentRange(content, comment), 'line1');
+ suite('getContentInCommentRange', () => {
+ test('one line', () => {
+ const comment = {
+ ...createComment(),
+ line: 1,
+ };
+ const content = 'line1\nline2\nline3';
+ assert.equal(getContentInCommentRange(content, comment), 'line1');
+ });
+
+ test('multi line', () => {
+ const comment = {
+ ...createComment(),
+ line: 3,
+ range: {
+ start_line: 1,
+ start_character: 5,
+ end_line: 3,
+ end_character: 39,
+ },
+ };
+ const selectedText =
+ ' * Examples:\n' +
+ ' * Acknowledge/Dismiss, Delete, Report a bug, Report as not useful,\n' +
+ ' * Make blocking, Downgrade severity.';
+ const content = `${selectedText}\n`;
+ assert.equal(getContentInCommentRange(content, comment), selectedText);
+ });
});
- test('createUserFixSuggestion', () => {
- const comment = {
- ...createComment(),
- line: 1,
- path: 'abc.txt',
- };
- const line = 'lane1';
- const replacement = 'line1';
- assert.deepEqual(createUserFixSuggestion(comment, line, replacement), [
- {
- fix_id: USER_SUGGEST_EDIT_FIX_ID,
- description: 'User suggestion',
- replacements: [
- {
- path: 'abc.txt',
- range: {
- start_line: 1,
- start_character: 0,
- end_line: 1,
- end_character: line.length,
+ suite('createUserFixSuggestion', () => {
+ test('one line', () => {
+ const comment = {
+ ...createComment(),
+ line: 1,
+ path: 'abc.txt',
+ };
+ const line = 'lane1';
+ const replacement = 'line1';
+ assert.deepEqual(createUserFixSuggestion(comment, line, replacement), [
+ {
+ fix_id: USER_SUGGEST_EDIT_FIX_ID,
+ description: 'User suggestion',
+ replacements: [
+ {
+ path: 'abc.txt',
+ range: {
+ start_line: 1,
+ start_character: 0,
+ end_line: 1,
+ end_character: line.length,
+ },
+ replacement,
},
- replacement,
- },
- ],
- },
- ]);
+ ],
+ },
+ ]);
+ });
+
+ test('multiline', () => {
+ const comment = {
+ ...createComment(),
+ line: 3,
+ range: {
+ start_line: 1,
+ start_character: 5,
+ end_line: 3,
+ end_character: 39,
+ },
+ path: 'abc.txt',
+ };
+ const line =
+ ' * Examples:\n' +
+ ' * Acknowledge/Dismiss, Delete, Report a bug, Report as not useful,\n' +
+ ' * Make blocking, Downgrade severity.';
+ const replacement =
+ ' - Examples:\n' +
+ ' - Acknowledge/Dismiss, Delete, Report a bug, Report as not useful,\n' +
+ ' - Make blocking, Downgrade severity.';
+ assert.deepEqual(createUserFixSuggestion(comment, line, replacement), [
+ {
+ fix_id: USER_SUGGEST_EDIT_FIX_ID,
+ description: 'User suggestion',
+ replacements: [
+ {
+ path: 'abc.txt',
+ range: {
+ start_line: 1,
+ start_character: 0,
+ end_line: 3,
+ end_character: 42,
+ },
+ replacement,
+ },
+ ],
+ },
+ ]);
+ });
});
});