2023-04-11 14:08:56 -04:00
|
|
|
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-30 06:15:38 -04:00
|
|
|
import type { Selection } from '../services/types/selection'
|
2023-05-25 06:20:57 -04:00
|
|
|
import { fileFinalPathname, isFileEditable } from './file-diff'
|
2023-04-11 14:08:56 -04:00
|
|
|
|
|
|
|
type FileWithOps = {
|
|
|
|
pathname: FileDiff['pathname']
|
2023-05-25 06:20:57 -04:00
|
|
|
editable: boolean
|
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) {
|
2023-05-25 06:20:57 -04:00
|
|
|
const filesByPathname = new Map<string, FileDiff>()
|
|
|
|
for (const file of files) {
|
|
|
|
const pathname = fileFinalPathname(file)
|
|
|
|
filesByPathname.set(pathname, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
const isEditable = (pathname: string) => {
|
|
|
|
const fileDiff = filesByPathname.get(pathname)
|
|
|
|
if (!fileDiff) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return isFileEditable(fileDiff)
|
|
|
|
}
|
|
|
|
|
2023-05-24 06:02:21 -04:00
|
|
|
for (const pathname of updateForToV.pathnames) {
|
2023-04-11 14:08:56 -04:00
|
|
|
filesWithOps.push({
|
|
|
|
pathname,
|
2023-05-25 06:20:57 -04:00
|
|
|
editable: isEditable(pathname),
|
2023-04-11 14:08:56 -04:00
|
|
|
operation: 'edited',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-24 06:02:21 -04:00
|
|
|
for (const op of updateForToV.project_ops) {
|
2023-05-25 06:20:57 -04:00
|
|
|
let pathAndOp: Nullable<Pick<FileWithOps, 'pathname' | 'operation'>> =
|
|
|
|
null
|
2023-04-11 14:08:56 -04:00
|
|
|
|
|
|
|
if (op.add) {
|
2023-05-25 06:20:57 -04:00
|
|
|
pathAndOp = {
|
2023-04-11 14:08:56 -04:00
|
|
|
pathname: op.add.pathname,
|
|
|
|
operation: 'added',
|
|
|
|
}
|
|
|
|
} else if (op.remove) {
|
2023-05-25 06:20:57 -04:00
|
|
|
pathAndOp = {
|
2023-04-11 14:08:56 -04:00
|
|
|
pathname: op.remove.pathname,
|
|
|
|
operation: 'removed',
|
|
|
|
}
|
|
|
|
} else if (op.rename) {
|
2023-05-25 06:20:57 -04:00
|
|
|
pathAndOp = {
|
2023-04-11 14:08:56 -04:00
|
|
|
pathname: op.rename.newPathname,
|
|
|
|
operation: 'renamed',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 06:20:57 -04:00
|
|
|
if (pathAndOp !== null) {
|
|
|
|
filesWithOps.push({
|
|
|
|
editable: isEditable(pathAndOp.pathname),
|
|
|
|
...pathAndOp,
|
|
|
|
})
|
2023-04-11 14:08:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filesWithOps
|
|
|
|
} else {
|
2023-05-25 06:20:57 -04:00
|
|
|
const filesWithOps = files.reduce((curFilesWithOps, file) => {
|
|
|
|
if ('operation' in file) {
|
|
|
|
curFilesWithOps.push({
|
|
|
|
pathname: file.pathname,
|
|
|
|
editable: isFileEditable(file),
|
|
|
|
operation: file.operation,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return curFilesWithOps
|
|
|
|
}, <FileWithOps[]>[])
|
2023-04-11 14:08:56 -04:00
|
|
|
|
|
|
|
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-30 06:15:38 -04:00
|
|
|
updateForToV: LoadedUpdate | undefined,
|
|
|
|
previouslySelectedPathname: Selection['previouslySelectedPathname']
|
2023-04-27 14:45:13 -04:00
|
|
|
): FileDiff {
|
2023-05-24 06:02:21 -04:00
|
|
|
const filesWithOps = getFilesWithOps(files, toV, comparing, updateForToV)
|
2023-05-30 06:15:38 -04:00
|
|
|
const previouslySelectedFile = files.find(file => {
|
|
|
|
return file.pathname === previouslySelectedPathname
|
|
|
|
})
|
|
|
|
const previouslySelectedFileHasOp = filesWithOps.some(file => {
|
|
|
|
return file.pathname === previouslySelectedPathname
|
|
|
|
})
|
|
|
|
|
|
|
|
if (previouslySelectedFile && previouslySelectedFileHasOp) {
|
|
|
|
return previouslySelectedFile
|
|
|
|
}
|
|
|
|
|
2023-04-11 14:08:56 -04:00
|
|
|
for (const opType of orderedOpTypes) {
|
2023-05-25 06:20:57 -04:00
|
|
|
const fileWithMatchingOpType = filesWithOps.find(
|
|
|
|
file => file.operation === opType && file.editable
|
|
|
|
)
|
2023-04-11 14:08:56 -04:00
|
|
|
|
2023-05-25 06:20:57 -04:00
|
|
|
if (fileWithMatchingOpType) {
|
|
|
|
const fileToSelect = files.find(
|
|
|
|
file => fileFinalPathname(file) === fileWithMatchingOpType.pathname
|
|
|
|
)
|
|
|
|
if (fileToSelect) {
|
|
|
|
return fileToSelect
|
2023-04-11 14:08:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 06:20:57 -04:00
|
|
|
return (
|
2023-05-30 06:15:38 -04:00
|
|
|
previouslySelectedFile ||
|
2023-05-25 06:20:57 -04:00
|
|
|
files.find(file => /main\.tex$/.test(file.pathname)) ||
|
|
|
|
files.find(file => /\.tex$/.test(file.pathname)) ||
|
|
|
|
files[0]
|
|
|
|
)
|
2023-04-11 14:08:56 -04:00
|
|
|
}
|