Commit b263b3f2 authored by Phil Hughes's avatar Phil Hughes Committed by Kushal Pandya

Make code suggestions aware of multi-line comments

This makes the code suggestions button be aware of the
multi-line comments.
The content in the suggestion and the indexes are based on
the multi-line comments options.

Closes https://gitlab.com/gitlab-org/gitlab/-/issues/232339
parent dcc14623
......@@ -10,7 +10,12 @@ import {
} from '../../notes/components/multiline_comment_utils';
import noteForm from '../../notes/components/note_form.vue';
import autosave from '../../notes/mixins/autosave';
import { DIFF_NOTE_TYPE, INLINE_DIFF_LINES_KEY, PARALLEL_DIFF_VIEW_TYPE } from '../constants';
import {
DIFF_NOTE_TYPE,
INLINE_DIFF_LINES_KEY,
PARALLEL_DIFF_VIEW_TYPE,
OLD_LINE_TYPE,
} from '../constants';
export default {
components: {
......@@ -113,6 +118,34 @@ export default {
const lines = getDiffLines();
return commentLineOptions(lines, this.line, this.line.line_code, side);
},
commentLines() {
if (!this.selectedCommentPosition) return [];
const lines = [];
const { start, end } = this.selectedCommentPosition;
const diffLines = this.diffFile[INLINE_DIFF_LINES_KEY];
let isAdding = false;
for (let i = 0, diffLinesLength = diffLines.length - 1; i < diffLinesLength; i += 1) {
const line = diffLines[i];
if (start.line_code === line.line_code) {
isAdding = true;
}
if (isAdding) {
if (line.type !== OLD_LINE_TYPE) {
lines.push(line);
}
if (end.line_code === line.line_code) {
break;
}
}
}
return lines;
},
},
mounted() {
if (this.isLoggedIn) {
......@@ -177,6 +210,7 @@ export default {
:is-editing="true"
:line-code="line.line_code"
:line="line"
:lines="commentLines"
:help-page-path="helpPagePath"
:diff-file="diffFile"
:show-suggest-popover="showSuggestPopover"
......
......@@ -232,7 +232,7 @@ export function insertMarkdownText({
.join('\n');
}
} else if (tag.indexOf(textPlaceholder) > -1) {
textToInsert = tag.replace(textPlaceholder, selected);
textToInsert = tag.replace(textPlaceholder, selected.replace(/\\n/g, '\n'));
} else {
textToInsert = String(startChar) + tag + selected + (wrap ? tag : '');
}
......@@ -322,7 +322,7 @@ export function updateTextForToolbarBtn($toolbarBtn) {
blockTag: $toolbarBtn.data('mdBlock'),
wrap: !$toolbarBtn.data('mdPrepend'),
select: $toolbarBtn.data('mdSelect'),
tagContent: $toolbarBtn.data('mdTagContent'),
tagContent: $toolbarBtn.attr('data-md-tag-content'),
});
}
......
......@@ -60,6 +60,11 @@ export default {
required: false,
default: null,
},
lines: {
type: Array,
required: false,
default: () => [],
},
note: {
type: Object,
required: false,
......@@ -333,6 +338,7 @@ export default {
:help-page-path="helpPagePath"
:show-suggest-popover="showSuggestPopover"
:textarea-value="updatedNoteBody"
:lines="lines"
@handleSuggestDismissed="() => $emit('handleSuggestDismissed')"
>
<template #textarea>
......
......@@ -77,6 +77,11 @@ export default {
required: false,
default: null,
},
lines: {
type: Array,
required: false,
default: () => [],
},
note: {
type: Object,
required: false,
......@@ -115,6 +120,20 @@ export default {
return this.referencedUsers.length >= referencedUsersThreshold;
},
lineContent() {
if (this.lines.length) {
return this.lines
.map((line) => {
const { rich_text: richText, text } = line;
if (text) {
return text;
}
return unescape(stripHtml(richText).replace(/\n/g, ''));
})
.join('\\n');
}
if (this.line) {
const { rich_text: richText, text } = this.line;
......@@ -241,6 +260,7 @@ export default {
:line-content="lineContent"
:can-suggest="canSuggest"
:show-suggest-popover="showSuggestPopover"
:suggestion-start-index="lines.length"
@preview-markdown="showPreviewTab"
@write-markdown="showWriteTab"
@handleSuggestDismissed="() => $emit('handleSuggestDismissed')"
......
......@@ -37,6 +37,11 @@ export default {
required: false,
default: false,
},
suggestionStartIndex: {
type: Number,
required: false,
default: 0,
},
},
data() {
return {
......@@ -54,7 +59,9 @@ export default {
].join('\n');
},
mdSuggestion() {
return ['```suggestion:-0+0', `{text}`, '```'].join('\n');
return [['```', `suggestion:-${this.suggestionStartIndex}+0`].join(''), `{text}`, '```'].join(
'\n',
);
},
isMac() {
// Accessing properties using ?. to allow tests to use
......
---
title: Code suggestions correctly add based on multi-line comments
merge_request: 57125
author:
type: added
......@@ -29,6 +29,7 @@ exports[`Snippet Description Edit component rendering matches the snapshot 1`] =
>
<markdown-header-stub
linecontent=""
suggestionstartindex="0"
/>
<div
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment