continue-custom/core/context/providers/FileTreeContextProvider.ts
Nate Sesti 53b61288ec
🎨 Refactor core (#1281)
* 🎨 refactor out react context

* 🎨 refactor local storage

* 🎨 refactor IdeMessenger

* 🔥 removing unused files

* 🚚 move scripts

* ✅ setup VSCode integration testing

* 🎨 fix test-related stuff

* 🚧 testing

* 🎨 tweak terminal cmd+L

* 🧑‍💻 install biome in core

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 run biome check in binary

* 🧑‍💻 global biome config

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 fix a few biome warnings

* 🚧 WIP on protocols

* 🚧 progress, organizing, about to remove webviewCore protocol

* 🚧 now running

* 🚧 move indexing code to core

* 🚧 WIP on JetBrains indexing implementation

* 🎨 finish listDir

* 🚧 gui fixes

* 🏷️ update IMessenger.on return type

* 🐛 fix jetbrains types + IDE detection

* 🧑‍💻 set up debugging for binary with TcpMessenger

* 👷 fix prepackage.js

* 🧑‍💻 turn off debug mode for intellij

* 🐛 merge fixes

* 🐛 fixes after refactor

* 🐛 merge fixes

* 💄 increase font size in JB

* 🔀 merge changes

* 🩹 small fixes

* 🐛 fix jetbrains build

* 🩹 more jetbrains fixes

* 🎨 jetbrains improvements

* 🐛 fix lancedb prob in jetbrains build

* 👷 update jetbrains build process

* ✨ intellij problems context provider

* 👷 add script utils file

* 💚 fix jetbrains build regression

* 🎨 dynamic import transformers.js

* 🩹 small jetbrains updates

* ✨ folder context provider in jetbrains

* 🎨 many more jetbrains improvements

* 🔀 merge fixes

* ⚡️ small improvements

* 🎨 tell users transformers.js not supported

* 🎨 trial updates

* 📝 update jetbrains readmej

* ⚡️ only use smaller context for local autocomplete models

* ⚡️ improve bracket filter

* ✨ global .continue/.prompts folder

* 💄 improved model setup process

* 🥅 display VSCode req errs
2024-05-24 19:38:13 -07:00

95 lines
2.4 KiB
TypeScript

import {
ContextItem,
ContextProviderDescription,
ContextProviderExtras,
} from "../../index.js";
import { BaseContextProvider } from "../index.js";
interface Directory {
name: string;
files: string[];
directories: Directory[];
}
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;
}
function formatFileTree(tree: Directory, indentation = ""): string {
let result = "";
for (const file of tree.files) {
result += `${indentation}${file}\n`;
}
for (const directory of tree.directories) {
result += `${indentation}${directory.name}/\n`;
result += formatFileTree(directory, `${indentation} `);
}
return result;
}
class FileTreeContextProvider extends BaseContextProvider {
static description: ContextProviderDescription = {
title: "tree",
displayTitle: "File Tree",
description: "Attach a representation of the file tree",
type: "normal",
};
async getContextItems(
query: string,
extras: ContextProviderExtras,
): Promise<ContextItem[]> {
const workspaceDirs = await extras.ide.getWorkspaceDirs();
const trees = [];
for (const workspaceDir of workspaceDirs) {
const contents = await extras.ide.listWorkspaceContents(workspaceDir);
const subDirTree: Directory = {
name: splitPath(workspaceDir).pop() ?? "",
files: [],
directories: [],
};
for (const file of contents) {
const parts = splitPath(file, workspaceDir);
let currentTree = subDirTree;
for (const part of parts.slice(0, -1)) {
if (!currentTree.directories.some((d) => d.name === part)) {
currentTree.directories.push({
name: part,
files: [],
directories: [],
});
}
currentTree = currentTree.directories.find((d) => d.name === part)!;
}
currentTree.files.push(parts.pop()!);
}
trees.push(formatFileTree(subDirTree));
}
return [
{
content: `Here is a file tree of the current workspace:\n\n${trees.join(
"\n\n",
)}`,
name: "File Tree",
description: "File Tree",
},
];
}
}
export default FileTreeContextProvider;