Commit a076b958 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'ph/232339/codeSuggestionsMultiLineComment' into 'master'

Make code suggestions aware of multi-line comments

See merge request gitlab-org/gitlab!57125
parents 3d4de77d b263b3f2
...@@ -10,7 +10,12 @@ import { ...@@ -10,7 +10,12 @@ import {
} from '../../notes/components/multiline_comment_utils'; } from '../../notes/components/multiline_comment_utils';
import noteForm from '../../notes/components/note_form.vue'; import noteForm from '../../notes/components/note_form.vue';
import autosave from '../../notes/mixins/autosave'; 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 { export default {
components: { components: {
...@@ -113,6 +118,34 @@ export default { ...@@ -113,6 +118,34 @@ export default {
const lines = getDiffLines(); const lines = getDiffLines();
return commentLineOptions(lines, this.line, this.line.line_code, side); 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() { mounted() {
if (this.isLoggedIn) { if (this.isLoggedIn) {
...@@ -177,6 +210,7 @@ export default { ...@@ -177,6 +210,7 @@ export default {
:is-editing="true" :is-editing="true"
:line-code="line.line_code" :line-code="line.line_code"
:line="line" :line="line"
:lines="commentLines"
:help-page-path="helpPagePath" :help-page-path="helpPagePath"
:diff-file="diffFile" :diff-file="diffFile"
:show-suggest-popover="showSuggestPopover" :show-suggest-popover="showSuggestPopover"
......
...@@ -232,7 +232,7 @@ export function insertMarkdownText({ ...@@ -232,7 +232,7 @@ export function insertMarkdownText({
.join('\n'); .join('\n');
} }
} else if (tag.indexOf(textPlaceholder) > -1) { } else if (tag.indexOf(textPlaceholder) > -1) {
textToInsert = tag.replace(textPlaceholder, selected); textToInsert = tag.replace(textPlaceholder, selected.replace(/\\n/g, '\n'));
} else { } else {
textToInsert = String(startChar) + tag + selected + (wrap ? tag : ''); textToInsert = String(startChar) + tag + selected + (wrap ? tag : '');
} }
...@@ -322,7 +322,7 @@ export function updateTextForToolbarBtn($toolbarBtn) { ...@@ -322,7 +322,7 @@ export function updateTextForToolbarBtn($toolbarBtn) {
blockTag: $toolbarBtn.data('mdBlock'), blockTag: $toolbarBtn.data('mdBlock'),
wrap: !$toolbarBtn.data('mdPrepend'), wrap: !$toolbarBtn.data('mdPrepend'),
select: $toolbarBtn.data('mdSelect'), select: $toolbarBtn.data('mdSelect'),
tagContent: $toolbarBtn.data('mdTagContent'), tagContent: $toolbarBtn.attr('data-md-tag-content'),
}); });
} }
......
...@@ -60,6 +60,11 @@ export default { ...@@ -60,6 +60,11 @@ export default {
required: false, required: false,
default: null, default: null,
}, },
lines: {
type: Array,
required: false,
default: () => [],
},
note: { note: {
type: Object, type: Object,
required: false, required: false,
...@@ -333,6 +338,7 @@ export default { ...@@ -333,6 +338,7 @@ export default {
:help-page-path="helpPagePath" :help-page-path="helpPagePath"
:show-suggest-popover="showSuggestPopover" :show-suggest-popover="showSuggestPopover"
:textarea-value="updatedNoteBody" :textarea-value="updatedNoteBody"
:lines="lines"
@handleSuggestDismissed="() => $emit('handleSuggestDismissed')" @handleSuggestDismissed="() => $emit('handleSuggestDismissed')"
> >
<template #textarea> <template #textarea>
......
...@@ -77,6 +77,11 @@ export default { ...@@ -77,6 +77,11 @@ export default {
required: false, required: false,
default: null, default: null,
}, },
lines: {
type: Array,
required: false,
default: () => [],
},
note: { note: {
type: Object, type: Object,
required: false, required: false,
...@@ -115,6 +120,20 @@ export default { ...@@ -115,6 +120,20 @@ export default {
return this.referencedUsers.length >= referencedUsersThreshold; return this.referencedUsers.length >= referencedUsersThreshold;
}, },
lineContent() { 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) { if (this.line) {
const { rich_text: richText, text } = this.line; const { rich_text: richText, text } = this.line;
...@@ -241,6 +260,7 @@ export default { ...@@ -241,6 +260,7 @@ export default {
:line-content="lineContent" :line-content="lineContent"
:can-suggest="canSuggest" :can-suggest="canSuggest"
:show-suggest-popover="showSuggestPopover" :show-suggest-popover="showSuggestPopover"
:suggestion-start-index="lines.length"
@preview-markdown="showPreviewTab" @preview-markdown="showPreviewTab"
@write-markdown="showWriteTab" @write-markdown="showWriteTab"
@handleSuggestDismissed="() => $emit('handleSuggestDismissed')" @handleSuggestDismissed="() => $emit('handleSuggestDismissed')"
......
...@@ -37,6 +37,11 @@ export default { ...@@ -37,6 +37,11 @@ export default {
required: false, required: false,
default: false, default: false,
}, },
suggestionStartIndex: {
type: Number,
required: false,
default: 0,
},
}, },
data() { data() {
return { return {
...@@ -54,7 +59,9 @@ export default { ...@@ -54,7 +59,9 @@ export default {
].join('\n'); ].join('\n');
}, },
mdSuggestion() { mdSuggestion() {
return ['```suggestion:-0+0', `{text}`, '```'].join('\n'); return [['```', `suggestion:-${this.suggestionStartIndex}+0`].join(''), `{text}`, '```'].join(
'\n',
);
}, },
isMac() { isMac() {
// Accessing properties using ?. to allow tests to use // 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`] = ...@@ -29,6 +29,7 @@ exports[`Snippet Description Edit component rendering matches the snapshot 1`] =
> >
<markdown-header-stub <markdown-header-stub
linecontent="" linecontent=""
suggestionstartindex="0"
/> />
<div <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