Commit ebf8b3cd authored by Phil Hughes's avatar Phil Hughes

Merge branch 'tor/defect/correct-suggestion-line' into 'master'

Correct suggestions for multiple comment boxes

See merge request gitlab-org/gitlab!72599
parents cdfd75cb e18e015a
...@@ -29,6 +29,11 @@ export default { ...@@ -29,6 +29,11 @@ export default {
required: false, required: false,
default: false, default: false,
}, },
lineRange: {
type: Object,
required: false,
default: null,
},
linePosition: { linePosition: {
type: String, type: String,
required: false, required: false,
...@@ -59,6 +64,7 @@ export default { ...@@ -59,6 +64,7 @@ export default {
<diff-line-note-form <diff-line-note-form
:diff-file-hash="diffFileHash" :diff-file-hash="diffFileHash"
:line="line" :line="line"
:range="lineRange"
:note-target-line="line" :note-target-line="line"
:help-page-path="helpPagePath" :help-page-path="helpPagePath"
:line-position="linePosition" :line-position="linePosition"
......
...@@ -32,6 +32,11 @@ export default { ...@@ -32,6 +32,11 @@ export default {
type: Object, type: Object,
required: true, required: true,
}, },
range: {
type: Object,
required: false,
default: null,
},
linePosition: { linePosition: {
type: String, type: String,
required: false, required: false,
...@@ -49,6 +54,7 @@ export default { ...@@ -49,6 +54,7 @@ export default {
}, },
data() { data() {
return { return {
lines: null,
commentLineStart: { commentLineStart: {
line_code: this.line.line_code, line_code: this.line.line_code,
type: this.line.type, type: this.line.type,
...@@ -116,10 +122,8 @@ export default { ...@@ -116,10 +122,8 @@ export default {
return commentLineOptions(lines, this.line, this.line.line_code, side); return commentLineOptions(lines, this.line, this.line.line_code, side);
}, },
commentLines() { commentLines() {
if (!this.selectedCommentPosition) return [];
const lines = []; const lines = [];
const { start, end } = this.selectedCommentPosition; const { start, end } = this.lines;
const diffLines = this.diffFile[INLINE_DIFF_LINES_KEY]; const diffLines = this.diffFile[INLINE_DIFF_LINES_KEY];
let isAdding = false; let isAdding = false;
...@@ -144,6 +148,9 @@ export default { ...@@ -144,6 +148,9 @@ export default {
return lines; return lines;
}, },
}, },
created() {
this.lines = { ...this.range };
},
mounted() { mounted() {
if (this.isLoggedIn) { if (this.isLoggedIn) {
const keys = [ const keys = [
...@@ -189,6 +196,9 @@ export default { ...@@ -189,6 +196,9 @@ export default {
this.handleCancelCommentForm(), this.handleCancelCommentForm(),
); );
}, },
updateStartLine(line) {
this.lines.start = line;
},
}, },
}; };
</script> </script>
...@@ -199,7 +209,9 @@ export default { ...@@ -199,7 +209,9 @@ export default {
<multiline-comment-form <multiline-comment-form
v-model="commentLineStart" v-model="commentLineStart"
:line="line" :line="line"
:line-range="lines"
:comment-line-options="commentLineOptions" :comment-line-options="commentLineOptions"
@input="updateStartLine"
/> />
</div> </div>
<note-form <note-form
......
...@@ -6,6 +6,7 @@ import draftCommentsMixin from '~/diffs/mixins/draft_comments'; ...@@ -6,6 +6,7 @@ import draftCommentsMixin from '~/diffs/mixins/draft_comments';
import { getCommentedLines } from '~/notes/components/multiline_comment_utils'; import { getCommentedLines } from '~/notes/components/multiline_comment_utils';
import { hide } from '~/tooltips'; import { hide } from '~/tooltips';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin'; import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { pickDirection } from '../utils/diff_line';
import DiffCommentCell from './diff_comment_cell.vue'; import DiffCommentCell from './diff_comment_cell.vue';
import DiffExpansionCell from './diff_expansion_cell.vue'; import DiffExpansionCell from './diff_expansion_cell.vue';
import DiffRow from './diff_row.vue'; import DiffRow from './diff_row.vue';
...@@ -106,6 +107,16 @@ export default { ...@@ -106,6 +107,16 @@ export default {
}); });
this.idState.dragStart = null; this.idState.dragStart = null;
}, },
singleLineComment(code, line) {
const lineDir = pickDirection({ line, code });
this.idState.updatedLineRange = {
start: lineDir,
end: lineDir,
};
this.showCommentForm({ lineCode: lineDir.line_code, fileHash: this.diffFile.file_hash });
},
isHighlighted(line) { isHighlighted(line) {
return isHighlighted( return isHighlighted(
this.highlightedRow, this.highlightedRow,
...@@ -169,7 +180,7 @@ export default { ...@@ -169,7 +180,7 @@ export default {
:index="index" :index="index"
:is-highlighted="isHighlighted(line)" :is-highlighted="isHighlighted(line)"
:file-line-coverage="fileLineCoverage" :file-line-coverage="fileLineCoverage"
@showCommentForm="(lineCode) => showCommentForm({ lineCode, fileHash: diffFile.file_hash })" @showCommentForm="(code) => singleLineComment(code, line)"
@setHighlightedRow="setHighlightedRow" @setHighlightedRow="setHighlightedRow"
@toggleLineDiscussions=" @toggleLineDiscussions="
({ lineCode, expanded }) => ({ lineCode, expanded }) =>
...@@ -193,6 +204,7 @@ export default { ...@@ -193,6 +204,7 @@ export default {
<diff-comment-cell <diff-comment-cell
v-if="line.left && (line.left.renderDiscussion || line.left.hasCommentForm)" v-if="line.left && (line.left.renderDiscussion || line.left.hasCommentForm)"
:line="line.left" :line="line.left"
:line-range="idState.updatedLineRange"
:diff-file-hash="diffFile.file_hash" :diff-file-hash="diffFile.file_hash"
:help-page-path="helpPagePath" :help-page-path="helpPagePath"
line-position="left" line-position="left"
...@@ -206,6 +218,7 @@ export default { ...@@ -206,6 +218,7 @@ export default {
<diff-comment-cell <diff-comment-cell
v-if="line.right && (line.right.renderDiscussion || line.right.hasCommentForm)" v-if="line.right && (line.right.renderDiscussion || line.right.hasCommentForm)"
:line="line.right" :line="line.right"
:line-range="idState.updatedLineRange"
:diff-file-hash="diffFile.file_hash" :diff-file-hash="diffFile.file_hash"
:line-index="index" :line-index="index"
:help-page-path="helpPagePath" :help-page-path="helpPagePath"
......
export function pickDirection({ line, code } = {}) {
const { left, right } = line;
let direction = left || right;
if (right?.line_code === code) {
direction = right;
}
return direction;
}
<script> <script>
import { GlFormSelect, GlSprintf } from '@gitlab/ui'; import { GlFormSelect, GlSprintf } from '@gitlab/ui';
import { mapActions, mapState } from 'vuex'; import { mapActions } from 'vuex';
import { getSymbol, getLineClasses } from './multiline_comment_utils'; import { getSymbol, getLineClasses } from './multiline_comment_utils';
export default { export default {
...@@ -27,13 +27,12 @@ export default { ...@@ -27,13 +27,12 @@ export default {
}; };
}, },
computed: { computed: {
...mapState({ selectedCommentPosition: ({ notes }) => notes.selectedCommentPosition }),
lineNumber() { lineNumber() {
return this.commentLineOptions[this.commentLineOptions.length - 1].text; return this.commentLineOptions[this.commentLineOptions.length - 1].text;
}, },
}, },
created() { created() {
const line = this.selectedCommentPosition?.start || this.lineRange?.start || this.line; const line = this.lineRange?.start || this.line;
this.commentLineStart = { this.commentLineStart = {
line_code: line.line_code, line_code: line.line_code,
...@@ -42,7 +41,6 @@ export default { ...@@ -42,7 +41,6 @@ export default {
new_line: line.new_line, new_line: line.new_line,
}; };
if (this.selectedCommentPosition) return;
this.highlightSelection(); this.highlightSelection();
}, },
destroyed() { destroyed() {
......
...@@ -334,13 +334,13 @@ export default { ...@@ -334,13 +334,13 @@ export default {
:markdown-docs-path="markdownDocsPath" :markdown-docs-path="markdownDocsPath"
:quick-actions-docs-path="quickActionsDocsPath" :quick-actions-docs-path="quickActionsDocsPath"
:line="line" :line="line"
:lines="lines"
:note="discussionNote" :note="discussionNote"
:can-suggest="canSuggest" :can-suggest="canSuggest"
:add-spacing-classes="false" :add-spacing-classes="false"
: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>
......
...@@ -26,8 +26,9 @@ describe('DiffLineNoteForm', () => { ...@@ -26,8 +26,9 @@ describe('DiffLineNoteForm', () => {
propsData: { propsData: {
diffFileHash: diffFile.file_hash, diffFileHash: diffFile.file_hash,
diffLines, diffLines,
line: diffLines[0], line: diffLines[1],
noteTargetLine: diffLines[0], range: { start: diffLines[0], end: diffLines[1] },
noteTargetLine: diffLines[1],
}, },
}); });
}; };
...@@ -67,7 +68,7 @@ describe('DiffLineNoteForm', () => { ...@@ -67,7 +68,7 @@ describe('DiffLineNoteForm', () => {
expect(window.confirm).not.toHaveBeenCalled(); expect(window.confirm).not.toHaveBeenCalled();
wrapper.vm.$nextTick(() => { wrapper.vm.$nextTick(() => {
expect(wrapper.vm.cancelCommentForm).toHaveBeenCalledWith({ expect(wrapper.vm.cancelCommentForm).toHaveBeenCalledWith({
lineCode: diffLines[0].line_code, lineCode: diffLines[1].line_code,
fileHash: wrapper.vm.diffFileHash, fileHash: wrapper.vm.diffFileHash,
}); });
...@@ -88,13 +89,13 @@ describe('DiffLineNoteForm', () => { ...@@ -88,13 +89,13 @@ describe('DiffLineNoteForm', () => {
start: { start: {
line_code: wrapper.vm.commentLineStart.line_code, line_code: wrapper.vm.commentLineStart.line_code,
type: wrapper.vm.commentLineStart.type, type: wrapper.vm.commentLineStart.type,
new_line: 1, new_line: 2,
old_line: null, old_line: null,
}, },
end: { end: {
line_code: wrapper.vm.line.line_code, line_code: wrapper.vm.line.line_code,
type: wrapper.vm.line.type, type: wrapper.vm.line.type,
new_line: 1, new_line: 2,
old_line: null, old_line: null,
}, },
}; };
...@@ -120,7 +121,7 @@ describe('DiffLineNoteForm', () => { ...@@ -120,7 +121,7 @@ describe('DiffLineNoteForm', () => {
describe('mounted', () => { describe('mounted', () => {
it('should init autosave', () => { it('should init autosave', () => {
const key = 'autosave/Note/Issue/98//DiffNote//1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_1'; const key = 'autosave/Note/Issue/98//DiffNote//1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_2';
wrapper = createComponent(); wrapper = createComponent();
expect(wrapper.vm.autosave).toBeDefined(); expect(wrapper.vm.autosave).toBeDefined();
......
import { pickDirection } from '~/diffs/utils/diff_line';
describe('diff_line utilities', () => {
describe('pickDirection', () => {
const left = {
line_code: 'left',
};
const right = {
line_code: 'right',
};
const defaultLine = {
left,
right,
};
it.each`
code | pick | line | pickDescription
${'left'} | ${left} | ${defaultLine} | ${'the left line'}
${'right'} | ${right} | ${defaultLine} | ${'the right line'}
${'junk'} | ${left} | ${defaultLine} | ${'the default: the left line'}
${'junk'} | ${right} | ${{ right }} | ${"the right line if there's no left line to default to"}
${'right'} | ${left} | ${{ left }} | ${"the left line when there isn't a right line to match"}
`(
'when provided a line and a line code `$code`, picks $pickDescription',
({ code, line, pick }) => {
expect(pickDirection({ line, code })).toBe(pick);
},
);
});
});
...@@ -50,18 +50,6 @@ describe('MultilineCommentForm', () => { ...@@ -50,18 +50,6 @@ describe('MultilineCommentForm', () => {
expect(wrapper.vm.commentLineStart).toEqual(lineRange.start); expect(wrapper.vm.commentLineStart).toEqual(lineRange.start);
expect(setSelectedCommentPosition).toHaveBeenCalled(); expect(setSelectedCommentPosition).toHaveBeenCalled();
}); });
it('sets commentLineStart to selectedCommentPosition', () => {
const notes = {
selectedCommentPosition: {
start: { ...testLine },
},
};
const wrapper = createWrapper({}, { notes });
expect(wrapper.vm.commentLineStart).toEqual(wrapper.vm.selectedCommentPosition.start);
expect(setSelectedCommentPosition).not.toHaveBeenCalled();
});
}); });
describe('destroyed', () => { describe('destroyed', () => {
......
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