diff --git a/app/assets/javascripts/diffs/stores/legacy_diffs/actions.js b/app/assets/javascripts/diffs/stores/legacy_diffs/actions.js index ae2b77ca6ba9571656b72bb90ea6ad14866377fc..9b78ec4e42b3f87ee44020e08be3ebb73c214984 100644 --- a/app/assets/javascripts/diffs/stores/legacy_diffs/actions.js +++ b/app/assets/javascripts/diffs/stores/legacy_diffs/actions.js @@ -1130,6 +1130,7 @@ export function fetchLinkedExpandedLine({ fileHash, oldLine, newLine }) { const matchLine = findClosestMatchLine(lines, newLine); const { new_pos: matchNewPosition, old_pos: matchOldPosition } = matchLine.meta_data; const matchLineIndex = lines.indexOf(matchLine); + const targetIsBeforeMatch = newLine < matchNewPosition; const linesInBetween = countLinesInBetween(lines, matchLineIndex); const isExpandBoth = linesInBetween !== -1 && linesInBetween < 20; const previousLine = lines[matchLineIndex - 1]; @@ -1164,6 +1165,15 @@ export function fetchLinkedExpandedLine({ fileHash, oldLine, newLine }) { }); } + if (targetIsBeforeMatch && isExpandDown && previousLine && newLine < previousLine.new_line) { + return loadLines({ + unfold: true, + since: newLine, + to: matchNewPosition - 1, + bottom: isLastMatchLine, + }); + } + if (!isExpandDown) { return loadLines({ unfold: true, diff --git a/spec/frontend/diffs/stores/legacy_diffs/actions_spec.js b/spec/frontend/diffs/stores/legacy_diffs/actions_spec.js index edeef3dece419022c3371df0e2043f5c9177a6d0..9db8e4a0afea3ad046c0b109a5446e9ed109c0e9 100644 --- a/spec/frontend/diffs/stores/legacy_diffs/actions_spec.js +++ b/spec/frontend/diffs/stores/legacy_diffs/actions_spec.js @@ -2523,6 +2523,46 @@ describe('legacyDiffs actions', () => { }); }); + it('expands lines upwards when the match line is after the target line', async () => { + const linkedFile = { + file_hash: 'abc123', + context_lines_path: `${TEST_HOST}/linked-file`, + highlighted_diff_lines: [ + { old_line: 250, new_line: 250 }, + { meta_data: { old_pos: 300, new_pos: 300 } }, + { old_line: 300, new_line: 300 }, + ], + }; + + store.$patch({ + diffFiles: [linkedFile], + linkedFileHash: linkedFile.file_hash, + }); + + await store.fetchLinkedExpandedLine({ + fileHash: linkedFile.file_hash, + oldLine: 286, + newLine: 286, + }); + + expect(store.loadMoreLines).toHaveBeenCalledWith({ + endpoint: linkedFile.context_lines_path, + fileHash: linkedFile.file_hash, + isExpandDown: false, + lineNumbers: { + oldLineNumber: 300, + newLineNumber: 300, + }, + params: { + bottom: false, + offset: 0, + since: 286, + to: 299, + unfold: true, + }, + }); + }); + it('expands lines at the very bottom', async () => { const linkedFile = { file_hash: 'abc123',