2023-04-11 14:08:56 -04:00
|
|
|
import _ from 'lodash'
|
|
|
|
import type { Nullable } from '../../../../../types/utils'
|
|
|
|
import type { HistoryContextValue } from '../context/types/history-context-value'
|
|
|
|
import type { FileDiff } from '../services/types/file'
|
|
|
|
import type { DiffOperation } from '../services/types/diff-operation'
|
2023-04-18 10:15:01 -04:00
|
|
|
import type { LoadedUpdate, UpdateRange } from '../services/types/update'
|
2023-04-11 14:08:56 -04:00
|
|
|
|
|
|
|
function getUpdateForVersion(
|
2023-04-18 10:15:01 -04:00
|
|
|
version: LoadedUpdate['toV'],
|
2023-04-11 14:08:56 -04:00
|
|
|
updates: HistoryContextValue['updates']
|
2023-04-18 10:15:01 -04:00
|
|
|
): Nullable<LoadedUpdate> {
|
2023-04-11 14:08:56 -04:00
|
|
|
return updates.filter(update => update.toV === version)?.[0] ?? null
|
|
|
|
}
|
|
|
|
|
|
|
|
type FileWithOps = {
|
|
|
|
pathname: FileDiff['pathname']
|
|
|
|
operation: DiffOperation
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFilesWithOps(
|
2023-04-14 06:57:41 -04:00
|
|
|
files: FileDiff[],
|
2023-04-18 10:15:01 -04:00
|
|
|
updateRange: UpdateRange,
|
|
|
|
comparing: boolean,
|
2023-04-11 14:08:56 -04:00
|
|
|
updates: HistoryContextValue['updates']
|
|
|
|
): FileWithOps[] {
|
2023-04-18 10:15:01 -04:00
|
|
|
if (updateRange.toV && !comparing) {
|
2023-04-11 14:08:56 -04:00
|
|
|
const filesWithOps: FileWithOps[] = []
|
2023-04-18 10:15:01 -04:00
|
|
|
const currentUpdate = getUpdateForVersion(updateRange.toV, updates)
|
2023-04-11 14:08:56 -04:00
|
|
|
|
|
|
|
if (currentUpdate !== null) {
|
|
|
|
for (const pathname of currentUpdate.pathnames) {
|
|
|
|
filesWithOps.push({
|
|
|
|
pathname,
|
|
|
|
operation: 'edited',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const op of currentUpdate.project_ops) {
|
|
|
|
let fileWithOps: Nullable<FileWithOps> = null
|
|
|
|
|
|
|
|
if (op.add) {
|
|
|
|
fileWithOps = {
|
|
|
|
pathname: op.add.pathname,
|
|
|
|
operation: 'added',
|
|
|
|
}
|
|
|
|
} else if (op.remove) {
|
|
|
|
fileWithOps = {
|
|
|
|
pathname: op.remove.pathname,
|
|
|
|
operation: 'removed',
|
|
|
|
}
|
|
|
|
} else if (op.rename) {
|
|
|
|
fileWithOps = {
|
|
|
|
pathname: op.rename.newPathname,
|
|
|
|
operation: 'renamed',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileWithOps !== null) {
|
|
|
|
filesWithOps.push(fileWithOps)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filesWithOps
|
|
|
|
} else {
|
|
|
|
const filesWithOps = _.reduce(
|
2023-04-14 06:57:41 -04:00
|
|
|
files,
|
2023-04-11 14:08:56 -04:00
|
|
|
(curFilesWithOps, file) => {
|
|
|
|
if ('operation' in file) {
|
|
|
|
curFilesWithOps.push({
|
|
|
|
pathname: file.pathname,
|
|
|
|
operation: file.operation,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return curFilesWithOps
|
|
|
|
},
|
|
|
|
<FileWithOps[]>[]
|
|
|
|
)
|
|
|
|
|
|
|
|
return filesWithOps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const orderedOpTypes: DiffOperation[] = [
|
|
|
|
'edited',
|
|
|
|
'added',
|
|
|
|
'renamed',
|
|
|
|
'removed',
|
|
|
|
]
|
|
|
|
|
|
|
|
export function autoSelectFile(
|
|
|
|
files: FileDiff[],
|
2023-04-18 10:15:01 -04:00
|
|
|
updateRange: UpdateRange,
|
|
|
|
comparing: boolean,
|
2023-04-11 14:08:56 -04:00
|
|
|
updates: HistoryContextValue['updates']
|
|
|
|
) {
|
|
|
|
let fileToSelect: Nullable<FileDiff> = null
|
|
|
|
|
2023-04-18 10:15:01 -04:00
|
|
|
const filesWithOps = getFilesWithOps(files, updateRange, comparing, updates)
|
2023-04-11 14:08:56 -04:00
|
|
|
for (const opType of orderedOpTypes) {
|
|
|
|
const fileWithMatchingOpType = _.find(filesWithOps, {
|
|
|
|
operation: opType,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (fileWithMatchingOpType != null) {
|
|
|
|
fileToSelect =
|
|
|
|
_.find(files, {
|
|
|
|
pathname: fileWithMatchingOpType.pathname,
|
|
|
|
}) ?? null
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fileToSelect) {
|
|
|
|
const mainFile = _.find(files, function (file) {
|
|
|
|
return /main\.tex$/.test(file.pathname)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (mainFile) {
|
|
|
|
fileToSelect = mainFile
|
|
|
|
} else {
|
|
|
|
const anyTeXFile = _.find(files, function (file) {
|
|
|
|
return /\.tex$/.test(file.pathname)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (anyTeXFile) {
|
|
|
|
fileToSelect = anyTeXFile
|
|
|
|
} else {
|
|
|
|
fileToSelect = files[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 06:57:41 -04:00
|
|
|
return fileToSelect.pathname
|
2023-04-11 14:08:56 -04:00
|
|
|
}
|