Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
"type": "string",
"default": null,
"description": "Specifies a custom location to use when discovering tours."
},
"codetour.sortBy": {
"type": "string",
"enum": [
"title",
"dateModified",
"dateCreated"
],
"default": "title",
"description": "Specifies how to sort tours in the Start Tour dialog."
}
}
},
Expand Down
48 changes: 45 additions & 3 deletions src/store/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,52 @@ export async function discoverTours(): Promise<void> {
})
);

const flatTours = tours.flat();
const sortBy = vscode.workspace
.getConfiguration(EXTENSION_NAME)
.get<string>("sortBy", "title");

// Pre-fetch file stats for date-based sorting
const tourStats = new Map<string, vscode.FileStat>();
if (sortBy === "dateModified" || sortBy === "dateCreated") {
await Promise.all(
flatTours.map(async tour => {
try {
const stat = await vscode.workspace.fs.stat(vscode.Uri.parse(tour.id));
tourStats.set(tour.id, stat);
} catch {
// Ignore errors - will fall back to title sorting for this tour
}
})
);
}

const sortFn = (a: CodeTour, b: CodeTour) => {
switch (sortBy) {
case "dateModified": {
const aStat = tourStats.get(a.id);
const bStat = tourStats.get(b.id);
if (aStat && bStat) {
return bStat.mtime - aStat.mtime; // newest first
}
return a.title.localeCompare(b.title);
}
case "dateCreated": {
const aStat = tourStats.get(a.id);
const bStat = tourStats.get(b.id);
if (aStat && bStat) {
return bStat.ctime - aStat.ctime; // newest first
}
return a.title.localeCompare(b.title);
}
default:
return a.title.localeCompare(b.title);
}
};

runInAction(() => {
store.tours = tours
.flat()
.sort((a, b) => a.title.localeCompare(b.title))
store.tours = flatTours
.sort(sortFn)
.filter(tour => !tour.when || jexl.evalSync(tour.when, TOUR_CONTEXT));

if (store.activeTour) {
Expand Down