-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathsortUtils.pure.ts
More file actions
72 lines (61 loc) · 2.84 KB
/
Copy pathsortUtils.pure.ts
File metadata and controls
72 lines (61 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import type * as vscode from 'vscode';
import { sort as _sortByPath } from 'cross-path-sort';
import { FoundRefT } from '../types';
export type GetMTimeOfFile = (uri: vscode.Uri) => Promise<number>;
type PickedFoundRefT = {
location: Pick<FoundRefT['location'], 'uri'>;
};
const sortByPath = async (referencesByPath: Record<string, PickedFoundRefT[]>) => {
// sort keys by path
return _sortByPath(Object.keys(referencesByPath), { shallowFirst: true });
};
const sortByAlphabetical = async (referencesByPath: Record<string, PickedFoundRefT[]>) => {
// sort keys by alphabetical
const pathsSorted = Object.keys(referencesByPath).sort();
return pathsSorted;
};
const getMTimeOfLastModifiedRef =
(getMTimeOfFile: GetMTimeOfFile) => async (refs: PickedFoundRefT[]) => {
// find the last modified ref's mtime in refs
const refsWithModified = await Promise.all(refs.map((ref) => getMTimeOfFile(ref.location.uri)));
return Math.max(...refsWithModified);
};
const withModifiedTime =
(getMTimeOfFile: GetMTimeOfFile, file: (path: string) => vscode.Uri) =>
(type: SortPathsByModifiedType) =>
async ([path, ref]: [string, PickedFoundRefT[]]): Promise<Readonly<[number, string]>> => {
const lastModified =
type === 'last-modified'
? await getMTimeOfFile(file(path))
: await getMTimeOfLastModifiedRef(getMTimeOfFile)(ref);
return [lastModified, path] as const;
};
const sortByLastModified =
(getMTimeOfFile: GetMTimeOfFile, file: (path: string) => vscode.Uri) =>
(type: SortPathsByModifiedType) =>
async (referencesByPath: Record<string, PickedFoundRefT[]>): Promise<string[]> => {
// sort keys by last modified
const refsWithModifiedTime = await Promise.all(
Object.entries(referencesByPath).map(withModifiedTime(getMTimeOfFile, file)(type)),
);
const pathsSorted = refsWithModifiedTime.sort(([a], [b]) => b - a).map(([, path]) => path);
return pathsSorted;
};
export type SortFunction = (
referencesByPath: Record<string, PickedFoundRefT[]>,
) => Promise<string[]>;
export type SortPathsType = 'path' | 'alphabet' | SortPathsByModifiedType;
export type SortPathsByModifiedType = 'last-modified' | 'last-modified-refs';
export type SortPathsDefaultType = typeof sortPathsDefaultType;
export const sortPathsDefaultType = 'path' satisfies SortPathsType;
export const _sortPaths = (getMTimeOfFile: GetMTimeOfFile, file: (path: string) => vscode.Uri) => {
const sortMap = {
path: sortByPath,
alphabet: sortByAlphabetical,
'last-modified': sortByLastModified(getMTimeOfFile, file)('last-modified'),
'last-modified-refs': sortByLastModified(getMTimeOfFile, file)('last-modified-refs'),
} as const satisfies Record<SortPathsType, SortFunction>;
return (order: SortPathsType) => (referencesByPath: Record<string, PickedFoundRefT[]>) => {
return sortMap[order](referencesByPath);
};
};