continue-custom/core/util/index.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

182 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export function removeQuotesAndEscapes(output: string): string {
output = output.trim();
// Replace smart quotes
output = output.replace("“", '"');
output = output.replace("”", '"');
output = output.replace("‘", "'");
output = output.replace("’", "'");
// Remove escapes
output = output.replace('\\"', '"');
output = output.replace("\\'", "'");
output = output.replace("\\n", "\n");
output = output.replace("\\t", "\t");
output = output.replace("\\\\", "\\");
while (
(output.startsWith('"') && output.endsWith('"')) ||
(output.startsWith("'") && output.endsWith("'"))
) {
output = output.slice(1, -1);
}
while (output.startsWith("`") && output.endsWith("`")) {
output = output.slice(1, -1);
}
return output;
}
export function proxyFetch(url: string, init?: RequestInit): Promise<Response> {
if (!(window as any)._fetch) {
throw new Error("Proxy fetch not initialized");
}
if (!(url.startsWith("http://") || url.startsWith("https://"))) {
// Relative URL
const fullUrl = `${window.vscMediaUrl}/${url}`;
return (window as any)._fetch(fullUrl, init);
}
const proxyServerUrl =
(window as any).proxyServerUrl || "http://localhost:65433";
const headers = new Headers(init?.headers);
headers.append("x-continue-url", url);
return (window as any)._fetch(proxyServerUrl, {
...init,
headers,
});
}
export function dedentAndGetCommonWhitespace(s: string): [string, string] {
const lines = s.split("\n");
if (lines.length === 0 || (lines[0].trim() === "" && lines.length === 1)) {
return ["", ""];
}
// Longest common whitespace prefix
let lcp = lines[0].split(lines[0].trim())[0];
// Iterate through the lines
for (let i = 1; i < lines.length; i++) {
// Empty lines are wildcards
if (lines[i].trim() === "") {
continue; // hey that's us!
}
if (lcp === undefined) {
lcp = lines[i].split(lines[i].trim())[0];
}
// Iterate through the leading whitespace characters of the current line
for (let j = 0; j < lcp.length; j++) {
// If it doesn't have the same whitespace as lcp, then update lcp
if (j >= lines[i].length || lcp[j] !== lines[i][j]) {
lcp = lcp.slice(0, j);
if (lcp === "") {
return [s, ""];
}
break;
}
}
}
if (lcp === undefined) {
return [s, ""];
}
return [lines.map((x) => x.replace(lcp, "")).join("\n"), lcp];
}
export function getBasename(filepath: string, n = 1): string {
return filepath.split(/[\\/]/).pop() ?? "";
}
export function getLastNPathParts(filepath: string, n: number): string {
return filepath.split(/[\\/]/).slice(-n).join("/");
}
export function getMarkdownLanguageTagForFile(filepath: string): string {
const ext = filepath.split(".").pop();
switch (ext) {
case "py":
return "python";
case "js":
return "javascript";
case "jsx":
return "jsx";
case "tsx":
return "tsx";
case "ts":
return "typescript";
case "java":
return "java";
case "go":
return "go";
case "rb":
return "ruby";
case "rs":
return "rust";
case "c":
return "c";
case "cpp":
return "cpp";
case "cs":
return "csharp";
case "php":
return "php";
case "scala":
return "scala";
case "swift":
return "swift";
case "kt":
return "kotlin";
case "md":
return "markdown";
case "json":
return "json";
case "html":
return "html";
case "css":
return "css";
case "sh":
return "shell";
case "yaml":
return "yaml";
case "toml":
return "toml";
case "tex":
return "latex";
case "sql":
return "sql";
case "ps1":
return "powershell";
default:
return ext ?? "";
}
}
export function copyOf(obj: any): any {
if (obj === null || obj === undefined) {
return obj;
}
return JSON.parse(JSON.stringify(obj));
}
export function deduplicateArray<T>(
array: T[],
equal: (a: T, b: T) => boolean,
): T[] {
const result: T[] = [];
for (const item of array) {
if (!result.some((existingItem) => equal(existingItem, item))) {
result.push(item);
}
}
return result;
}
export type TODO = any;