continue-custom/core/autocomplete/charStream.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

94 lines
2.7 KiB
TypeScript

export async function* onlyWhitespaceAfterEndOfLine(
stream: AsyncGenerator<string>,
endOfLine: string[],
): AsyncGenerator<string> {
let pending = "";
for await (let chunk of stream) {
chunk = pending + chunk;
pending = "";
for (let i = 0; i < chunk.length - 1; i++) {
if (
endOfLine.includes(chunk[i]) &&
chunk[i + 1].trim() === chunk[i + 1]
) {
yield chunk.slice(0, i + 1);
return;
}
}
if (endOfLine.includes(chunk[chunk.length - 1])) {
pending = chunk[chunk.length - 1];
yield chunk.slice(0, chunk.length - 1);
} else {
yield chunk;
}
}
yield pending;
}
export async function* noFirstCharNewline(stream: AsyncGenerator<string>) {
let first = true;
for await (const char of stream) {
if (first) {
first = false;
if (char === "\n") {
return;
}
}
yield char;
}
}
const BRACKETS: { [key: string]: string } = { "(": ")", "{": "}", "[": "]" };
const BRACKETS_REVERSE: { [key: string]: string } = {
")": "(",
"}": "{",
"]": "[",
};
export async function* stopOnUnmatchedClosingBracket(
stream: AsyncGenerator<string>,
suffix: string,
): AsyncGenerator<string> {
// Add corresponding open brackets from suffix to stack
const stack: string[] = [];
for (let i = 0; i < suffix.length; i++) {
if (suffix[i] === " ") continue;
const openBracket = BRACKETS_REVERSE[suffix[i]];
if (!openBracket) break;
stack.unshift(openBracket);
}
let all = "";
let seenNonWhitespaceOrClosingBracket = false;
for await (let chunk of stream) {
// Allow closing brackets before any non-whitespace characters
if (!seenNonWhitespaceOrClosingBracket) {
const firstNonWhitespaceOrClosingBracketIndex =
chunk.search(/[^\s\)\}\]]/);
if (firstNonWhitespaceOrClosingBracketIndex !== -1) {
yield chunk.slice(0, firstNonWhitespaceOrClosingBracketIndex);
chunk = chunk.slice(firstNonWhitespaceOrClosingBracketIndex);
seenNonWhitespaceOrClosingBracket = true;
} else {
yield chunk;
continue;
}
}
all += chunk;
for (let i = 0; i < chunk.length; i++) {
const char = chunk[i];
if (Object.values(BRACKETS).includes(char)) {
// It's a closing bracket
if (stack.length === 0 || BRACKETS[stack.pop()!] !== char) {
// If the stack is empty or the top of the stack doesn't match the current closing bracket
yield chunk.slice(0, i);
return; // Stop the generator if the closing bracket doesn't have a matching opening bracket in the stream
}
} else if (Object.keys(BRACKETS).includes(char)) {
// It's an opening bracket
stack.push(char);
}
}
yield chunk;
}
}