move around getlastnpathparts

This commit is contained in:
Dallin Romney 2024-12-12 00:53:51 -08:00
parent 4bddb19366
commit 500b7219f4
8 changed files with 87 additions and 89 deletions

View file

@ -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,

View file

@ -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";

View file

@ -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];

View file

@ -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");

View file

@ -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<string, string[]> {
// return filepaths.reduce(
// (groups, item) => {
// const lastNParts = getLastNPathParts(item, n);
// if (!groups[lastNParts]) {
// groups[lastNParts] = [];
// }
// groups[lastNParts].push(item);
// return groups;
// },
// {} as Record<string, string[]>,
// );
// }
// 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);
// }
// 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",

View file

@ -1,6 +1,5 @@
import { getLastNPathParts } from ".";
import {
getLastNUriRelativePathParts,
getLastNPathParts,
getRelativePath,
getUniqueUriPath,
getUriPathBasename,

View file

@ -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<string, string[]> {
// return filepaths.reduce(
// (groups, item) => {
// const lastNParts = getLastNPathParts(item, n);
// if (!groups[lastNParts]) {
// groups[lastNParts] = [];
// }
// groups[lastNParts].push(item);
// return groups;
// },
// {} as Record<string, string[]>,
// );
// }
// 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);
// }
// 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
// }

View file

@ -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