From cc4fb38e50fd792a322b2d043673c3eb25b3363b Mon Sep 17 00:00:00 2001 From: Thomas Randolph Date: Wed, 11 Jun 2025 18:45:51 -0600 Subject: [PATCH 1/2] Handle situation where match line is after target line The logic for `isExpandDown` is a bit confusing, so this is a known-good fix. Changelog: fixed --- .../javascripts/diffs/stores/legacy_diffs/actions.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/assets/javascripts/diffs/stores/legacy_diffs/actions.js b/app/assets/javascripts/diffs/stores/legacy_diffs/actions.js index ae2b77ca6ba957..9b78ec4e42b3f8 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, -- GitLab From 916647e93f7e475e652bb331ebca6576f9aa03db Mon Sep 17 00:00:00 2001 From: Thomas Randolph Date: Wed, 11 Jun 2025 21:41:09 -0600 Subject: [PATCH 2/2] Add a test for when the match line is after target line --- .../diffs/stores/legacy_diffs/actions_spec.js | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/spec/frontend/diffs/stores/legacy_diffs/actions_spec.js b/spec/frontend/diffs/stores/legacy_diffs/actions_spec.js index edeef3dece4190..9db8e4a0afea3a 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', -- GitLab