diff --git a/core/context/providers/FileContextProvider.ts b/core/context/providers/FileContextProvider.ts index 7ce851525..b6a9cb33a 100644 --- a/core/context/providers/FileContextProvider.ts +++ b/core/context/providers/FileContextProvider.ts @@ -5,7 +5,7 @@ import { ContextSubmenuItem, LoadSubmenuItemsArgs, } from "../../index.js"; -import { getBasename, getLastNPathParts } from "../../util/index.js"; +import { getBasename, groupByLastNPathParts, getUniqueFilePath } from "../../util/index.js"; import { BaseContextProvider } from "../index.js"; const MAX_SUBMENU_ITEMS = 10_000; @@ -43,12 +43,14 @@ class FileContextProvider extends BaseContextProvider { return args.ide.listWorkspaceContents(dir); }), ); - const files = results.flat().slice(-MAX_SUBMENU_ITEMS); + const files = results.flat().slice(-MAX_SUBMENU_ITEMS); + const fileGroups = groupByLastNPathParts(files, 2); + return files.map((file) => { return { id: file, title: getBasename(file), - description: getLastNPathParts(file, 2), + description: getUniqueFilePath(file, fileGroups), }; }); } diff --git a/core/context/providers/FolderContextProvider.ts b/core/context/providers/FolderContextProvider.ts index 366abdc4a..4c2869a90 100644 --- a/core/context/providers/FolderContextProvider.ts +++ b/core/context/providers/FolderContextProvider.ts @@ -5,7 +5,7 @@ import { ContextSubmenuItem, LoadSubmenuItemsArgs, } from "../../index.js"; -import { getBasename, getLastNPathParts } from "../../util/index.js"; +import { getBasename, groupByLastNPathParts, getUniqueFilePath } from "../../util/index.js"; import { BaseContextProvider } from "../index.js"; class FolderContextProvider extends BaseContextProvider { @@ -29,11 +29,13 @@ class FolderContextProvider extends BaseContextProvider { args: LoadSubmenuItemsArgs, ): Promise { const folders = await args.ide.listFolders(); + const folderGroups = groupByLastNPathParts(folders, 2); + return folders.map((folder) => { return { id: folder, title: getBasename(folder), - description: getLastNPathParts(folder, 2), + description: getUniqueFilePath(folder, folderGroups) }; }); } diff --git a/core/util/index.ts b/core/util/index.ts index 0a6215961..bcc7eda22 100644 --- a/core/util/index.ts +++ b/core/util/index.ts @@ -99,6 +99,37 @@ export function getLastNPathParts(filepath: string, n: number): string { return filepath.split(SEP_REGEX).slice(-n).join("/"); } +export function groupByLastNPathParts(filepaths: string[], n: number): Record { + return filepaths.reduce((groups, item) => { + const lastNParts = getLastNPathParts(item, n); + if (!groups[lastNParts]) { + groups[lastNParts] = []; + } + groups[lastNParts].push(item); + return groups; + }, {} as Record); +} + +export function getUniqueFilePath(item: string, itemGroups: Record): string { + const lastTwoParts = getLastNPathParts(item, 2); + const group = itemGroups[lastTwoParts]; + + let n = 2; + if (group.length > 1) { + while ( + group.some( + (otherItem) => + otherItem !== item && + getLastNPathParts(otherItem, n) === getLastNPathParts(item, n) + ) + ) { + n++; + } + } + + return getLastNPathParts(item, n); +} + export function shortestRelativePaths(paths: string[]): string[] { if (paths.length === 0) return []; diff --git a/gui/src/hooks/useSubmenuContextProviders.tsx b/gui/src/hooks/useSubmenuContextProviders.tsx index e249c0a62..0be8d3b10 100644 --- a/gui/src/hooks/useSubmenuContextProviders.tsx +++ b/gui/src/hooks/useSubmenuContextProviders.tsx @@ -1,5 +1,5 @@ import { ContextSubmenuItem } from "core"; -import { deduplicateArray, getBasename, getLastNPathParts } from "core/util"; +import { deduplicateArray, getBasename, groupByLastNPathParts, getUniqueFilePath } from "core/util"; import MiniSearch, { SearchResult } from "minisearch"; import { useContext, useEffect, useState } from "react"; import { useSelector } from "react-redux"; @@ -34,11 +34,13 @@ function useSubmenuContextProviders() { async function getOpenFileItems() { const openFiles = await ideMessenger.ide.getOpenFiles(); + const openFileGroups = groupByLastNPathParts(openFiles, 2); + return openFiles.map((file) => { return { id: file, title: getBasename(file), - description: getLastNPathParts(file, 2), + description: getUniqueFilePath(file, openFileGroups), providerTitle: "file", }; });