diff --git a/core/indexing/CodeSnippetsIndex.ts b/core/indexing/CodeSnippetsIndex.ts index 0138817f6..48d60083b 100644 --- a/core/indexing/CodeSnippetsIndex.ts +++ b/core/indexing/CodeSnippetsIndex.ts @@ -1,6 +1,5 @@ import Parser from "web-tree-sitter"; -import { getBasename, getLastNPathParts } from "../util/"; import { migrate } from "../util/paths"; import { getFullLanguageName, @@ -29,6 +28,7 @@ import type { IndexTag, IndexingProgressUpdate, } from "../"; +import { getLastNPathParts, getUriPathBasename } from "../util/uri"; type SnippetChunk = ChunkWithoutID & { title: string; signature: string }; @@ -250,7 +250,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { } yield { - desc: `Indexing ${getBasename(compute.path)}`, + desc: `Indexing ${getUriPathBasename(compute.path)}`, progress: i / results.compute.length, status: "indexing", }; @@ -353,7 +353,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { return { name: row.title, description: getLastNPathParts(row.path, 2), - content: `\`\`\`${getBasename(row.path)}\n${row.content}\n\`\`\``, + content: `\`\`\`${getUriPathBasename(row.path)}\n${row.content}\n\`\`\``, uri: { type: "file", value: row.path, diff --git a/core/promptFiles/v1/slashCommandFromPromptFile.ts b/core/promptFiles/v1/slashCommandFromPromptFile.ts index b4f670513..d89ee1082 100644 --- a/core/promptFiles/v1/slashCommandFromPromptFile.ts +++ b/core/promptFiles/v1/slashCommandFromPromptFile.ts @@ -1,6 +1,6 @@ import { ContinueSDK, SlashCommand } from "../.."; -import { getLastNPathParts } from "../../util"; import { renderChatMessage } from "../../util/messageContent"; +import { getLastNPathParts } from "../../util/uri"; import { parsePromptFileV1V2 } from "../v2/parsePromptFileV1V2"; import { getContextProviderHelpers } from "./getContextProviderHelpers"; diff --git a/core/promptFiles/v2/parse.ts b/core/promptFiles/v2/parse.ts index 334733d79..95e60230e 100644 --- a/core/promptFiles/v2/parse.ts +++ b/core/promptFiles/v2/parse.ts @@ -1,5 +1,5 @@ import * as YAML from "yaml"; -import { getLastNPathParts } from "../../util"; +import { getLastNPathParts } from "../../util/uri"; export function extractName(preamble: { name?: string }, path: string): string { return preamble.name ?? getLastNPathParts(path, 1).split(".prompt")[0]; diff --git a/core/promptFiles/v2/parsePromptFileV1V2.ts b/core/promptFiles/v2/parsePromptFileV1V2.ts index ad607f3cc..11e2b10be 100644 --- a/core/promptFiles/v2/parsePromptFileV1V2.ts +++ b/core/promptFiles/v2/parsePromptFileV1V2.ts @@ -1,5 +1,5 @@ import * as YAML from "yaml"; -import { getLastNPathParts } from "../../util"; +import { getLastNPathParts } from "../../util/uri"; export function parsePromptFileV1V2(path: string, content: string) { let [preambleRaw, prompt] = content.split("\n---\n"); diff --git a/core/util/index.ts b/core/util/index.ts index d310b3fde..04dc6d567 100644 --- a/core/util/index.ts +++ b/core/util/index.ts @@ -66,85 +66,6 @@ export function dedentAndGetCommonWhitespace(s: string): [string, string] { return [lines.map((x) => x.replace(lcp, "")).join("\n"), lcp]; } -const SEP_REGEX = /[\\/]/; - -// export function getBasename(filepath: string): string { -// return filepath.split(SEP_REGEX).pop() ?? ""; -// } - -export function getLastNPathParts(filepath: string, n: number): string { - if (n <= 0) { - return ""; - } - 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 splitPath(path: string, withRoot?: string): string[] { -// let parts = path.includes("/") ? path.split("/") : path.split("\\"); -// if (withRoot !== undefined) { -// const rootParts = splitPath(withRoot); -// parts = parts.slice(rootParts.length - 1); -// } -// return parts; -// } - -// export function getRelativePath( -// filepath: string, -// workspaceDirs: string[], -// ): string { -// for (const workspaceDir of workspaceDirs) { -// const filepathParts = splitPath(filepath); -// const workspaceDirParts = splitPath(workspaceDir); -// if ( -// filepathParts.slice(0, workspaceDirParts.length).join("/") === -// workspaceDirParts.join("/") -// ) { -// return filepathParts.slice(workspaceDirParts.length).join("/"); -// } -// } -// return splitPath(filepath).pop() ?? ""; // If the file is not in any of the workspaces, return the plain filename -// } - export function getMarkdownLanguageTagForFile(filepath: string): string { const extToLangMap: { [key: string]: string } = { py: "python", diff --git a/core/util/uri.test.ts b/core/util/uri.test.ts index 1b5a7e2e0..09cb27c6c 100644 --- a/core/util/uri.test.ts +++ b/core/util/uri.test.ts @@ -1,6 +1,5 @@ -import { getLastNPathParts } from "."; import { - getLastNUriRelativePathParts, + getLastNPathParts, getRelativePath, getUniqueUriPath, getUriPathBasename, diff --git a/core/util/uri.ts b/core/util/uri.ts index 57af61235..ef3223013 100644 --- a/core/util/uri.ts +++ b/core/util/uri.ts @@ -179,3 +179,82 @@ export function getUriFileExtension(uri: string) { const baseName = getUriPathBasename(uri); return baseName.split(".")[-1] ?? ""; } + +const SEP_REGEX = /[\\/]/; + +// export function getBasename(filepath: string): string { +// return filepath.split(SEP_REGEX).pop() ?? ""; +// } + +export function getLastNPathParts(filepath: string, n: number): string { + if (n <= 0) { + return ""; + } + 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 splitPath(path: string, withRoot?: string): string[] { +// let parts = path.includes("/") ? path.split("/") : path.split("\\"); +// if (withRoot !== undefined) { +// const rootParts = splitPath(withRoot); +// parts = parts.slice(rootParts.length - 1); +// } +// return parts; +// } + +// export function getRelativePath( +// filepath: string, +// workspaceDirs: string[], +// ): string { +// for (const workspaceDir of workspaceDirs) { +// const filepathParts = splitPath(filepath); +// const workspaceDirParts = splitPath(workspaceDir); +// if ( +// filepathParts.slice(0, workspaceDirParts.length).join("/") === +// workspaceDirParts.join("/") +// ) { +// return filepathParts.slice(workspaceDirParts.length).join("/"); +// } +// } +// return splitPath(filepath).pop() ?? ""; // If the file is not in any of the workspaces, return the plain filename +// } diff --git a/extensions/vscode/src/lang-server/promptFileCompletions.ts b/extensions/vscode/src/lang-server/promptFileCompletions.ts index 8c5c05021..ba1590685 100644 --- a/extensions/vscode/src/lang-server/promptFileCompletions.ts +++ b/extensions/vscode/src/lang-server/promptFileCompletions.ts @@ -2,8 +2,7 @@ import { IDE } from "core"; import vscode from "vscode"; import { FileSearch } from "../util/FileSearch"; -import { getUriPathBasename } from "core/util/uri"; -import { getLastNPathParts } from "core/util"; +import { getUriPathBasename, getLastNPathParts } from "core/util/uri"; class PromptFilesCompletionItemProvider implements vscode.CompletionItemProvider