2023-04-11 14:08:56 -04:00
|
|
|
import _ from 'lodash'
|
|
|
|
import type { Nullable } from '../../../../../types/utils'
|
|
|
|
import type { FileDiff } from '../services/types/file'
|
2023-04-27 14:45:13 -04:00
|
|
|
import type { FileOperation } from '../services/types/file-operation'
|
2023-04-20 11:41:44 -04:00
|
|
|
import type { LoadedUpdate, Version } from '../services/types/update'
|
2023-05-16 04:17:57 -04:00
|
|
|
import { fileFinalPathname } from './file-diff'
|
2023-04-11 14:08:56 -04:00
|
|
|
|
|
|
|
type FileWithOps = {
|
|
|
|
pathname: FileDiff['pathname']
|
2023-04-27 14:45:13 -04:00
|
|
|
operation: FileOperation
|
2023-04-11 14:08:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getFilesWithOps(
|
2023-04-14 06:57:41 -04:00
|
|
|
files: FileDiff[],
|
2023-04-20 11:41:44 -04:00
|
|
|
toV: Version,
|
2023-04-18 10:15:01 -04:00
|
|
|
comparing: boolean,
|
2023-05-24 06:02:21 -04:00
|
|
|
updateForToV: LoadedUpdate | undefined
|
2023-04-11 14:08:56 -04:00
|
|
|
): FileWithOps[] {
|
2023-04-20 11:41:44 -04:00
|
|
|
if (toV && !comparing) {
|
2023-04-11 14:08:56 -04:00
|
|
|
const filesWithOps: FileWithOps[] = []
|
|
|
|
|
2023-05-24 06:02:21 -04:00
|
|
|
if (updateForToV) {
|
|
|
|
for (const pathname of updateForToV.pathnames) {
|
2023-04-11 14:08:56 -04:00
|
|
|
filesWithOps.push({
|
|
|
|
pathname,
|
|
|
|
operation: 'edited',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-24 06:02:21 -04:00
|
|
|
for (const op of updateForToV.project_ops) {
|
2023-04-11 14:08:56 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 14:45:13 -04:00
|
|
|
const orderedOpTypes: FileOperation[] = [
|
2023-04-11 14:08:56 -04:00
|
|
|
'edited',
|
|
|
|
'added',
|
|
|
|
'renamed',
|
|
|
|
'removed',
|
|
|
|
]
|
|
|
|
|
|
|
|
export function autoSelectFile(
|
|
|
|
files: FileDiff[],
|
2023-04-20 11:41:44 -04:00
|
|
|
toV: Version,
|
2023-04-18 10:15:01 -04:00
|
|
|
comparing: boolean,
|
2023-05-24 06:02:21 -04:00
|
|
|
updateForToV: LoadedUpdate | undefined
|
2023-04-27 14:45:13 -04:00
|
|
|
): FileDiff {
|
2023-04-11 14:08:56 -04:00
|
|
|
let fileToSelect: Nullable<FileDiff> = null
|
|
|
|
|
2023-05-24 06:02:21 -04:00
|
|
|
const filesWithOps = getFilesWithOps(files, toV, comparing, updateForToV)
|
2023-04-11 14:08:56 -04:00
|
|
|
for (const opType of orderedOpTypes) {
|
|
|
|
const fileWithMatchingOpType = _.find(filesWithOps, {
|
|
|
|
operation: opType,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (fileWithMatchingOpType != null) {
|
|
|
|
fileToSelect =
|
2023-05-16 04:17:57 -04:00
|
|
|
_.find(
|
|
|
|
files,
|
|
|
|
file => fileFinalPathname(file) === fileWithMatchingOpType.pathname
|
|
|
|
) ?? null
|
2023-04-11 14:08:56 -04:00
|
|
|
|
|
|
|
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-27 14:45:13 -04:00
|
|
|
return fileToSelect
|
2023-04-11 14:08:56 -04:00
|
|
|
}
|