getShortestUniqueRelativeUriPaths
This commit is contained in:
parent
19802493eb
commit
7621abcdcc
6 changed files with 72 additions and 225 deletions
|
|
@ -8,9 +8,8 @@ import {
|
|||
} from "../../";
|
||||
import { walkDirInWorkspaces } from "../../indexing/walkDir";
|
||||
import {
|
||||
getUniqueUriPath,
|
||||
getUriPathBasename,
|
||||
groupByLastNPathParts,
|
||||
getShortestUniqueRelativeUriPaths,
|
||||
} from "../../util/uri";
|
||||
|
||||
const MAX_SUBMENU_ITEMS = 10_000;
|
||||
|
|
@ -53,13 +52,16 @@ class FileContextProvider extends BaseContextProvider {
|
|||
workspaceDirs,
|
||||
);
|
||||
const files = results.flat().slice(-MAX_SUBMENU_ITEMS);
|
||||
const fileGroups = groupByLastNPathParts(workspaceDirs, files, 2);
|
||||
const withUniquePaths = getShortestUniqueRelativeUriPaths(
|
||||
files,
|
||||
workspaceDirs,
|
||||
);
|
||||
|
||||
return files.map((uri) => {
|
||||
return withUniquePaths.map((file) => {
|
||||
return {
|
||||
id: uri,
|
||||
title: getUriPathBasename(uri),
|
||||
description: getUniqueUriPath(uri, fileGroups),
|
||||
id: file.uri,
|
||||
title: getUriPathBasename(file.uri),
|
||||
description: file.uniquePath,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,9 +7,8 @@ import {
|
|||
} from "../../index.js";
|
||||
import { walkDirInWorkspaces } from "../../indexing/walkDir.js";
|
||||
import {
|
||||
getUniqueUriPath,
|
||||
getShortestUniqueRelativeUriPaths,
|
||||
getUriPathBasename,
|
||||
groupByLastNPathParts,
|
||||
} from "../../util/uri.js";
|
||||
import { BaseContextProvider } from "../index.js";
|
||||
import { retrieveContextItemsFromEmbeddings } from "../retrieval/retrieval.js";
|
||||
|
|
@ -40,13 +39,16 @@ class FolderContextProvider extends BaseContextProvider {
|
|||
},
|
||||
workspaceDirs,
|
||||
);
|
||||
const folderGroups = groupByLastNPathParts(workspaceDirs, folders, 2);
|
||||
const withUniquePaths = getShortestUniqueRelativeUriPaths(
|
||||
folders,
|
||||
workspaceDirs,
|
||||
);
|
||||
|
||||
return folders.map((folder) => {
|
||||
return withUniquePaths.map((folder) => {
|
||||
return {
|
||||
id: folder,
|
||||
title: getUriPathBasename(folder),
|
||||
description: getUniqueUriPath(folder, folderGroups),
|
||||
id: folder.uri,
|
||||
title: getUriPathBasename(folder.uri),
|
||||
description: folder.uniquePath,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,9 +9,8 @@ import {
|
|||
import { walkDirInWorkspaces } from "../../indexing/walkDir";
|
||||
import generateRepoMap from "../../util/generateRepoMap";
|
||||
import {
|
||||
getUniqueUriPath,
|
||||
getShortestUniqueRelativeUriPaths,
|
||||
getUriPathBasename,
|
||||
groupByLastNPathParts,
|
||||
} from "../../util/uri";
|
||||
|
||||
const ENTIRE_PROJECT_ITEM: ContextSubmenuItem = {
|
||||
|
|
@ -54,14 +53,17 @@ class RepoMapContextProvider extends BaseContextProvider {
|
|||
},
|
||||
workspaceDirs,
|
||||
);
|
||||
const folderGroups = groupByLastNPathParts(workspaceDirs, folders, 2);
|
||||
const withUniquePaths = getShortestUniqueRelativeUriPaths(
|
||||
folders,
|
||||
workspaceDirs,
|
||||
);
|
||||
|
||||
return [
|
||||
ENTIRE_PROJECT_ITEM,
|
||||
...folders.map((folder) => ({
|
||||
id: folder,
|
||||
title: getUriPathBasename(folder),
|
||||
description: getUniqueUriPath(folder, folderGroups),
|
||||
...withUniquePaths.map((folder) => ({
|
||||
id: folder.uri,
|
||||
title: getUriPathBasename(folder.uri),
|
||||
description: folder.uniquePath,
|
||||
})),
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,5 @@
|
|||
import { TEST_DIR } from "../test/testDir";
|
||||
import {
|
||||
getLastNPathParts,
|
||||
getUniqueUriPath,
|
||||
getUriPathBasename,
|
||||
groupByLastNPathParts,
|
||||
} from "./uri";
|
||||
import { getLastNPathParts, getUriPathBasename } from "./uri";
|
||||
|
||||
describe("getUriPathBasename", () => {
|
||||
it("should return the base name of a Unix-style path", () => {
|
||||
|
|
@ -32,64 +27,6 @@ describe("getUriPathBasename", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("groupByLastNPathParts", () => {
|
||||
const workspaceDirs = [TEST_DIR];
|
||||
it("should group filepaths by their last N parts", () => {
|
||||
const filepaths = [
|
||||
"/a/b/c/d/file1.txt",
|
||||
"/x/y/z/file1.txt",
|
||||
"/a/b/c/d/file2.txt",
|
||||
];
|
||||
const output = groupByLastNPathParts(workspaceDirs, filepaths, 2);
|
||||
expect(output).toEqual({
|
||||
"d/file1.txt": ["/a/b/c/d/file1.txt"],
|
||||
"z/file1.txt": ["/x/y/z/file1.txt"],
|
||||
"d/file2.txt": ["/a/b/c/d/file2.txt"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle an empty array", () => {
|
||||
const filepaths: string[] = [];
|
||||
const output = groupByLastNPathParts(workspaceDirs, filepaths, 2);
|
||||
expect(output).toEqual({});
|
||||
});
|
||||
|
||||
it("should handle N greater than path parts", () => {
|
||||
const filepaths = ["/file.txt"];
|
||||
const output = groupByLastNPathParts(workspaceDirs, filepaths, 5);
|
||||
expect(output).toEqual({ "/file.txt": ["/file.txt"] });
|
||||
});
|
||||
});
|
||||
|
||||
describe("getUniqueUriPath", () => {
|
||||
it("should return a unique file path within the group", () => {
|
||||
const item = "/a/b/c/file.txt";
|
||||
const itemGroups = {
|
||||
"c/file.txt": ["/a/b/c/file.txt", "/x/y/c/file.txt"],
|
||||
};
|
||||
const output = getUniqueUriPath(item, itemGroups);
|
||||
expect(output).toBe("b/c/file.txt");
|
||||
});
|
||||
|
||||
it("should return the last two parts if unique", () => {
|
||||
const item = "/a/b/c/file.txt";
|
||||
const itemGroups = {
|
||||
"c/file.txt": ["/a/b/c/file.txt"],
|
||||
};
|
||||
const output = getUniqueUriPath(item, itemGroups);
|
||||
expect(output).toBe("c/file.txt");
|
||||
});
|
||||
|
||||
it("should handle when additional parts are needed to make it unique", () => {
|
||||
const item = "/a/b/c/d/e/file.txt";
|
||||
const itemGroups = {
|
||||
"e/file.txt": ["/a/b/c/d/e/file.txt", "/x/y/z/e/file.txt"],
|
||||
};
|
||||
const output = getUniqueUriPath(item, itemGroups);
|
||||
expect(output).toBe("d/e/file.txt");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getLastNPathParts", () => {
|
||||
test("returns the last N parts of a filepath with forward slashes", () => {
|
||||
const filepath = "home/user/documents/project/file.txt";
|
||||
|
|
@ -128,43 +65,3 @@ describe("getLastNPathParts", () => {
|
|||
expect(getLastNPathParts(filepath, 2)).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("shortestRelativePaths", () => {
|
||||
it("should return shortest unique paths", () => {
|
||||
const paths = [
|
||||
"/a/b/c/file.txt",
|
||||
"/a/b/d/file.txt",
|
||||
"/a/b/d/file2.txt",
|
||||
"/x/y/z/file.txt",
|
||||
];
|
||||
const output = shortestRelativePaths(paths);
|
||||
expect(output).toEqual([
|
||||
"c/file.txt",
|
||||
"d/file.txt",
|
||||
"file2.txt",
|
||||
"z/file.txt",
|
||||
]);
|
||||
});
|
||||
|
||||
it("should handle empty array", () => {
|
||||
const paths: string[] = [];
|
||||
const output = shortestRelativePaths(paths);
|
||||
expect(output).toEqual([]);
|
||||
});
|
||||
|
||||
it("should handle paths with same base names", () => {
|
||||
const paths = [
|
||||
"/a/b/c/d/file.txt",
|
||||
"/a/b/c/e/file.txt",
|
||||
"/a/b/f/g/h/file.txt",
|
||||
];
|
||||
const output = shortestRelativePaths(paths);
|
||||
expect(output).toEqual(["d/file.txt", "e/file.txt", "h/file.txt"]);
|
||||
});
|
||||
|
||||
it("should handle paths where entire path is needed", () => {
|
||||
const paths = ["/a/b/c/file.txt", "/a/b/c/file.txt", "/a/b/c/file.txt"];
|
||||
const output = shortestRelativePaths(paths);
|
||||
expect(output).toEqual(["file.txt", "file.txt", "file.txt"]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
128
core/util/uri.ts
128
core/util/uri.ts
|
|
@ -33,6 +33,7 @@ export function findUriInDirs(
|
|||
uri: string,
|
||||
dirUriCandidates: string[],
|
||||
): {
|
||||
uri: string;
|
||||
relativePathOrBasename: string;
|
||||
foundInDir: string | null;
|
||||
} {
|
||||
|
|
@ -72,6 +73,7 @@ export function findUriInDirs(
|
|||
}
|
||||
if (allDirPartsMatch) {
|
||||
return {
|
||||
uri,
|
||||
relativePathOrBasename: uriPathParts
|
||||
.slice(dirPathParts.length)
|
||||
.join("/"),
|
||||
|
|
@ -82,6 +84,7 @@ export function findUriInDirs(
|
|||
// Not found
|
||||
console.warn("Directory not found for uri", uri, dirUriCandidates);
|
||||
return {
|
||||
uri,
|
||||
relativePathOrBasename: getUriPathBasename(uri),
|
||||
foundInDir: null,
|
||||
};
|
||||
|
|
@ -138,112 +141,51 @@ export function joinPathsToUri(uri: string, ...pathSegments: string[]) {
|
|||
return URI.serialize(components);
|
||||
}
|
||||
|
||||
// Note, will show workspace
|
||||
export function groupByLastNPathParts(
|
||||
workspaceDirs: string[],
|
||||
uris: string[],
|
||||
n: number,
|
||||
): Record<string, string[]> {
|
||||
return uris.reduce(
|
||||
(groups, uri) => {
|
||||
const lastNParts = getLastNUriRelativePathParts(workspaceDirs, uri, n);
|
||||
if (!groups[lastNParts]) {
|
||||
groups[lastNParts] = [];
|
||||
}
|
||||
groups[lastNParts].push(uri);
|
||||
return groups;
|
||||
},
|
||||
{} as Record<string, string[]>,
|
||||
);
|
||||
}
|
||||
|
||||
export function getUniqueUriPath(
|
||||
item: string,
|
||||
itemGroups: Record<string, string[]>,
|
||||
): string {
|
||||
return "hi";
|
||||
}
|
||||
|
||||
export function shortestRelativeUriPaths(
|
||||
export function getShortestUniqueRelativeUriPaths(
|
||||
uris: string[],
|
||||
dirUriCandidates: string[],
|
||||
): string[] {
|
||||
if (uris.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const relativeUris;
|
||||
): {
|
||||
uri: string;
|
||||
uniquePath: string;
|
||||
}[] {
|
||||
// Split all URIs into segments and count occurrences of each suffix combination
|
||||
const segmentCombinationsMap = new Map<string, number>();
|
||||
const segmentsInfo = uris.map((uri) => {
|
||||
const { relativePathOrBasename } = findUriInDirs(uri, dirUriCandidates);
|
||||
const segments = relativePathOrBasename.split("/");
|
||||
const suffixes: string[] = [];
|
||||
|
||||
const partsLengths = uris.map((x) => x.split("/").length);
|
||||
const currentRelativeUris = uris.map(getB);
|
||||
const currentNumParts = uris.map(() => 1);
|
||||
const isDuplicated = currentRelativeUris.map(
|
||||
(x, i) =>
|
||||
currentRelativeUris.filter((y, j) => y === x && paths[i] !== paths[j])
|
||||
.length > 1,
|
||||
);
|
||||
// Generate all possible suffix combinations
|
||||
for (let i = segments.length - 1; i >= 0; i--) {
|
||||
const suffix = segments.slice(i).join("/");
|
||||
suffixes.unshift(suffix);
|
||||
|
||||
while (isDuplicated.some(Boolean)) {
|
||||
const firstDuplicatedPath = currentRelativeUris.find(
|
||||
(x, i) => isDuplicated[i],
|
||||
);
|
||||
if (!firstDuplicatedPath) {
|
||||
break;
|
||||
// Count occurrences of each suffix
|
||||
segmentCombinationsMap.set(
|
||||
suffix,
|
||||
(segmentCombinationsMap.get(suffix) || 0) + 1,
|
||||
);
|
||||
}
|
||||
|
||||
currentRelativeUris.forEach((x, i) => {
|
||||
if (x === firstDuplicatedPath) {
|
||||
currentNumParts[i] += 1;
|
||||
currentRelativeUris[i] = getLastNUriRelativePathParts(
|
||||
paths[i],
|
||||
currentNumParts[i],
|
||||
);
|
||||
}
|
||||
});
|
||||
return { uri, segments, suffixes, relativePathOrBasename };
|
||||
});
|
||||
|
||||
isDuplicated.forEach((x, i) => {
|
||||
if (x) {
|
||||
isDuplicated[i] =
|
||||
// Once we've used up all the parts, we can't make it longer
|
||||
currentNumParts[i] < partsLengths[i] &&
|
||||
currentRelativeUris.filter((y) => y === currentRelativeUris[i])
|
||||
.length > 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
// Find shortest unique path for each URI
|
||||
return segmentsInfo.map(({ uri, suffixes, relativePathOrBasename }) => {
|
||||
// Find the first (shortest) unique suffix
|
||||
const uniquePath =
|
||||
suffixes.find((suffix) => segmentCombinationsMap.get(suffix) === 1) ??
|
||||
relativePathOrBasename; // case where not unique - perhaps basename
|
||||
|
||||
return currentRelativeUris;
|
||||
return { uri, uniquePath };
|
||||
});
|
||||
}
|
||||
|
||||
// export function getBasename(filepath: string): string {
|
||||
// return filepath.split(SEP_REGEX).pop() ?? "";
|
||||
// }
|
||||
|
||||
// Only used when working with system paths and relative paths
|
||||
// Since doesn't account for URI segements before workspace
|
||||
export function getLastNPathParts(filepath: string, n: number): string {
|
||||
if (n <= 0) {
|
||||
return "";
|
||||
}
|
||||
return filepath.split(/[\\/]/).slice(-n).join("/");
|
||||
}
|
||||
|
||||
// export function getUniqueFilePath(
|
||||
// item: string,
|
||||
// itemGroups: Record<string, string[]>,
|
||||
// ): 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);
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -15,9 +15,8 @@ import { selectContextProviderDescriptions } from "../redux/selectors";
|
|||
import { useWebviewListener } from "../hooks/useWebviewListener";
|
||||
import { useAppSelector } from "../redux/hooks";
|
||||
import {
|
||||
getUniqueUriPath,
|
||||
getShortestUniqueRelativeUriPaths,
|
||||
getUriPathBasename,
|
||||
groupByLastNPathParts,
|
||||
} from "core/util/uri";
|
||||
|
||||
const MINISEARCH_OPTIONS = {
|
||||
|
|
@ -90,12 +89,15 @@ export const SubmenuContextProvidersProvider = ({
|
|||
const getOpenFilesItems = useCallback(async () => {
|
||||
const openFiles = await ideMessenger.ide.getOpenFiles();
|
||||
const workspaceDirs = await ideMessenger.ide.getWorkspaceDirs();
|
||||
const openFileGroups = groupByLastNPathParts(workspaceDirs, openFiles, 2);
|
||||
const withUniquePaths = getShortestUniqueRelativeUriPaths(
|
||||
openFiles,
|
||||
workspaceDirs,
|
||||
);
|
||||
|
||||
return openFiles.map((file) => ({
|
||||
id: file,
|
||||
title: getUriPathBasename(file),
|
||||
description: getUniqueUriPath(file, openFileGroups),
|
||||
return withUniquePaths.map((file) => ({
|
||||
id: file.uri,
|
||||
title: getUriPathBasename(file.uri),
|
||||
description: file.uniquePath,
|
||||
providerTitle: "file",
|
||||
}));
|
||||
}, [ideMessenger]);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue