continue-custom/extensions/vscode/src/util/addCode.ts
2025-04-30 00:34:13 -07:00

158 lines
4 KiB
TypeScript

import { RangeInFileWithContents } from "core";
import * as os from "node:os";
import * as vscode from "vscode";
import type { VsCodeWebviewProtocol } from "../webviewProtocol";
export function getRangeInFileWithContents(
allowEmpty?: boolean,
range?: vscode.Range,
): RangeInFileWithContents | null {
const editor = vscode.window.activeTextEditor;
if (editor) {
const selection = editor.selection;
const filepath = editor.document.uri.toString();
if (range) {
const contents = editor.document.getText(range);
return {
range: {
start: {
line: range.start.line,
character: range.start.character,
},
end: {
line: range.end.line,
character: range.end.character,
},
},
filepath,
contents,
};
}
if (selection.isEmpty && !allowEmpty) {
return null;
}
let selectionRange = new vscode.Range(selection.start, selection.end);
const document = editor.document;
// Select the context from the beginning of the selection start line to the selection start position
const beginningOfSelectionStartLine = selection.start.with(undefined, 0);
const textBeforeSelectionStart = document.getText(
new vscode.Range(beginningOfSelectionStartLine, selection.start),
);
// If there are only whitespace before the start of the selection, include the indentation
if (textBeforeSelectionStart.trim().length === 0) {
selectionRange = selectionRange.with({
start: beginningOfSelectionStartLine,
});
}
const contents = editor.document.getText(selectionRange);
return {
filepath,
contents,
range: {
start: {
line: selection.start.line,
character: selection.start.character,
},
end: {
line: selection.end.line,
character: selection.end.character,
},
},
};
}
return null;
}
export async function addHighlightedCodeToContext(
webviewProtocol: VsCodeWebviewProtocol | undefined,
) {
const rangeInFileWithContents = getRangeInFileWithContents();
if (rangeInFileWithContents) {
webviewProtocol?.request("highlightedCode", {
rangeInFileWithContents,
});
}
}
export async function addEntireFileToContext(
uri: vscode.Uri,
webviewProtocol: VsCodeWebviewProtocol | undefined,
) {
// If a directory, add all files in the directory
const stat = await vscode.workspace.fs.stat(uri);
if (stat.type === vscode.FileType.Directory) {
const files = await vscode.workspace.fs.readDirectory(uri);
for (const [filename, type] of files) {
if (type === vscode.FileType.File) {
addEntireFileToContext(
vscode.Uri.joinPath(uri, filename),
webviewProtocol,
);
}
}
return;
}
// Get the contents of the file
const contents = (await vscode.workspace.fs.readFile(uri)).toString();
const rangeInFileWithContents = {
filepath: uri.toString(),
contents: contents,
range: {
start: {
line: 0,
character: 0,
},
end: {
line: contents.split(os.EOL).length - 1,
character: 0,
},
},
};
webviewProtocol?.request("highlightedCode", {
rangeInFileWithContents,
});
}
export function addCodeToContextFromRange(
range: vscode.Range,
webviewProtocol: VsCodeWebviewProtocol,
prompt?: string,
) {
const document = vscode.window.activeTextEditor?.document;
if (!document) {
return;
}
const rangeInFileWithContents = {
filepath: document.uri.toString(),
contents: document.getText(range),
range: {
start: {
line: range.start.line,
character: range.start.character,
},
end: {
line: range.end.line,
character: range.end.character,
},
},
};
webviewProtocol?.request("highlightedCode", {
rangeInFileWithContents,
prompt,
// Assume `true` since range selection is currently only used for quick actions/fixes
shouldRun: true,
});
}