Revert "Stable sort hunks by lines then time"

This reverts commit 94e6ed09c9.
This commit is contained in:
Mattias Granlund 2023-07-11 13:51:38 +02:00
parent e1509d69f0
commit a1b034c5d3
3 changed files with 5 additions and 43 deletions

View File

@ -1,25 +0,0 @@
/**
* Helper function to extract line numbers from the chunk header in a diff.
*
* @param diff A diff containing a chunk header
* @returns The original and modified file line numbers
*/
export function getLinesFromChunkHeader(diff: string): {
originalLineNumber: number;
currentLineNumber: number;
} {
try {
const diffLines = diff.split('\n');
const header = diffLines[0];
const lr = header.split('@@')[1].trim().split(' ');
return {
originalLineNumber: parseInt(lr[0].split(',')[0].slice(1)),
currentLineNumber: parseInt(lr[1].split(',')[0].slice(1))
};
} catch {
return {
originalLineNumber: -1,
currentLineNumber: -1
};
}
}

View File

@ -1,7 +1,6 @@
import { invoke } from '$lib/ipc';
import { plainToInstance } from 'class-transformer';
import { Branch, BranchData, Target } from './types';
import { getLinesFromChunkHeader } from '$lib/diff/utils';
export async function listVirtualBranches(params: { projectId: string }): Promise<Branch[]> {
const result = await invoke<any[]>('list_virtual_branches', params);
@ -78,20 +77,5 @@ export async function fetchFromTarget(params: { projectId: string }) {
function sortBranches(branches: Branch[]): Branch[] {
branches.sort((a, b) => a.order - b.order);
branches.forEach((branch) => {
const files = branch.files;
files.sort((a, b) => b.modifiedAt.getTime() - a.modifiedAt.getTime());
files.forEach((file) => {
const hunks = file.hunks;
// Sort by line before sorting by to prevent random order for hunks
// with the same timestamp.
hunks.sort((a, b) => {
const aln = getLinesFromChunkHeader(a.diff);
const bln = getLinesFromChunkHeader(b.diff);
return aln.currentLineNumber - bln.currentLineNumber;
});
hunks.sort((a, b) => b.modifiedAt.getTime() - a.modifiedAt.getTime());
});
});
return branches;
}

View File

@ -2,7 +2,6 @@
import { buildDiffRows, documentMap, RowType, type Row } from '$lib/components/Differ/renderer';
import { line, type DiffArray } from '$lib/diff';
import { create } from '$lib/components/Differ/CodeHighlighter';
import { getLinesFromChunkHeader } from '$lib/diff/utils';
export let diff: string;
export let filePath: string;
@ -17,7 +16,11 @@
currentLineNumber: number;
} {
const diffLines = diff.split('\n');
const { originalLineNumber, currentLineNumber } = getLinesFromChunkHeader(diff);
const header = diffLines[0];
const lr = header.split('@@')[1].trim().split(' ');
const originalLineNumber = parseInt(lr[0].split(',')[0].slice(1));
const currentLineNumber = parseInt(lr[1].split(',')[0].slice(1));
const before = diffLines
.filter((line) => line.startsWith('-'))