diff --git a/src/ide_services/endpoints/unofficial.ts b/src/ide_services/endpoints/unofficial.ts index 750b89e..c6bdde5 100644 --- a/src/ide_services/endpoints/unofficial.ts +++ b/src/ide_services/endpoints/unofficial.ts @@ -1,5 +1,5 @@ import { applyCodeWithDiff, applyEditCodeWithDiff } from "../../handler/diffHandler"; - +import * as vscode from 'vscode'; export namespace UnofficialEndpoints { export async function diffApply(filepath: string, content: string, autoedit: boolean = false) { @@ -19,4 +19,33 @@ export namespace UnofficialEndpoints { const res = eval(evalCode); return res; } + + export async function selectRange(fileName: string, startLine: number, startColumn: number, endLine: number, endColumn: number) { + let editor = vscode.window.activeTextEditor; + + // If the file is not open or not the active editor, open it + if (!editor || editor.document.fileName !== fileName) { + const document = await vscode.workspace.openTextDocument(fileName); + editor = await vscode.window.showTextDocument(document); + } + + if (editor) { + if (startLine === -1) { + // Cancel selection + editor.selection = new vscode.Selection(editor.selection.active, editor.selection.active); + } else { + // Select range + const selection = new vscode.Selection( + new vscode.Position(startLine, startColumn), + new vscode.Position(endLine, endColumn) + ); + editor.selection = selection; + + // Reveal the selection + editor.revealRange(selection, vscode.TextEditorRevealType.InCenter); + } + return true; + } + return false; + } } diff --git a/src/ide_services/services.ts b/src/ide_services/services.ts index f70f81f..f97c27a 100644 --- a/src/ide_services/services.ts +++ b/src/ide_services/services.ts @@ -100,6 +100,10 @@ const functionRegistry: any = { "/get_collapsed_code": { keys: ["fileName", "startLine", "endLine"], handler: getCollapsedCode, + }, + "/select_range": { + keys: ["fileName", "startLine", "startColumn", "endLine", "endColumn"], + handler: UnofficialEndpoints.selectRange, } };