feat: Add select range functionality

- Implement selectRange function in UnofficialEndpoints
- Add select_range endpoint to functionRegistry
- Import vscode module for text editor operations
This commit is contained in:
bobo.yang 2024-07-18 14:13:38 +08:00
parent af2292c065
commit 01555650ef
2 changed files with 34 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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,
}
};