From d9397d870f4de3a09c0aed40a81cc80485430441 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 28 Oct 2024 17:17:26 -0700 Subject: [PATCH] change CodeSelection type --- extensions/void/src/common/shared_types.ts | 7 +--- extensions/void/src/extension.ts | 41 +++++++------------- extensions/void/src/webviews/ctrlk/CtrlK.tsx | 7 ++++ 3 files changed, 22 insertions(+), 33 deletions(-) diff --git a/extensions/void/src/common/shared_types.ts b/extensions/void/src/common/shared_types.ts index dfe72b46..9fc6f820 100644 --- a/extensions/void/src/common/shared_types.ts +++ b/extensions/void/src/common/shared_types.ts @@ -2,10 +2,7 @@ import * as vscode from 'vscode'; import { PartialVoidConfig } from '../webviews/common/contextForConfig' - - -// a selection is a frozen snapshot -type CodeSelection = { selectionStr: string, selectionRange: vscode.Range, filePath: vscode.Uri } +type CodeSelection = { selectionStr: string, filePath: vscode.Uri } type File = { filepath: vscode.Uri, content: string } @@ -37,7 +34,7 @@ type Diff = { // editor -> sidebar type MessageToSidebar = ( - | { type: 'ctrl+l', selection: CodeSelection } // user presses ctrl+l in the editor + | { type: 'ctrl+l', selection: CodeSelection } // user presses ctrl+l in the editor. selection and path are frozen snapshots | { type: 'ctrl+k', selection: CodeSelection } | { type: 'files', files: { filepath: vscode.Uri, content: string }[] } | { type: 'partialVoidConfig', partialVoidConfig: PartialVoidConfig } diff --git a/extensions/void/src/extension.ts b/extensions/void/src/extension.ts index 8b24ed70..581b03b0 100644 --- a/extensions/void/src/extension.ts +++ b/extensions/void/src/extension.ts @@ -25,10 +25,18 @@ declare module 'vscode' { } const roundRangeToLines = (selection: vscode.Selection) => { - let endLine = selection.end.character === 0 ? selection.end.line - 1 : selection.end.line // triple clicking selects column=0, line=line through column=0, line=line+1 + let endLine = selection.end.character === 0 ? selection.end.line - 1 : selection.end.line // e.g. if the user triple clicks, it selects column=0, line=line -> column=0, line=line+1 return new vscode.Range(selection.start.line, 0, endLine, Number.MAX_SAFE_INTEGER) } +const getSelection = (editor: vscode.TextEditor) => { + // get the range of the selection and the file the user is in + const selectionRange = roundRangeToLines(editor.selection); + const selectionStr = editor.document.getText(selectionRange).trim(); + const filePath = editor.document.uri; + return { selectionStr, filePath } +} + export function activate(context: vscode.ExtensionContext) { // 1. Mount the chat sidebar @@ -45,30 +53,14 @@ export function activate(context: vscode.ExtensionContext) { const editor = vscode.window.activeTextEditor if (!editor) return - - // const inset = vscode.window.createWebviewTextEditorInset(editor, 10, 10, {}) - // inset.webview.html = ` - // - // Hello World! - // - // `; - - // show the sidebar vscode.commands.executeCommand('workbench.view.extension.voidViewContainer'); // vscode.commands.executeCommand('vscode.moveViewToPanel', CustomViewProvider.viewId); // move to aux bar - // get the range of the selection - const selectionRange = roundRangeToLines(editor.selection); - - // get the text the user is selecting - const selectionStr = editor.document.getText(selectionRange); - - // get the file the user is in - const filePath = editor.document.uri; + const { selectionStr, filePath } = getSelection(editor) // send message to the webview (Sidebar.tsx) - sidebarWebviewProvider.webview.then(webview => webview.postMessage({ type: 'ctrl+l', selection: { selectionStr, selectionRange, filePath } } satisfies MessageToSidebar)); + sidebarWebviewProvider.webview.then(webview => webview.postMessage({ type: 'ctrl+l', selection: { selectionStr, filePath } } satisfies MessageToSidebar)); }) ); @@ -79,17 +71,10 @@ export function activate(context: vscode.ExtensionContext) { const editor = vscode.window.activeTextEditor if (!editor) return - // get the range of the selection - const selectionRange = roundRangeToLines(editor.selection); - - // get the text the user is selecting - const selectionStr = editor.document.getText(selectionRange); - - // get the file the user is in - const filePath = editor.document.uri; + const { selectionStr, filePath } = getSelection(editor) // send message to the webview (Sidebar.tsx) - sidebarWebviewProvider.webview.then(webview => webview.postMessage({ type: 'ctrl+k', selection: { selectionStr, selectionRange, filePath } } satisfies MessageToSidebar)); + sidebarWebviewProvider.webview.then(webview => webview.postMessage({ type: 'ctrl+k', selection: { selectionStr, filePath } } satisfies MessageToSidebar)); }) ); diff --git a/extensions/void/src/webviews/ctrlk/CtrlK.tsx b/extensions/void/src/webviews/ctrlk/CtrlK.tsx index 09250fab..ac167005 100644 --- a/extensions/void/src/webviews/ctrlk/CtrlK.tsx +++ b/extensions/void/src/webviews/ctrlk/CtrlK.tsx @@ -11,6 +11,13 @@ export const CtrlK = () => { sx('Pressed ctrl+k') }) + // const inset = vscode.window.createWebviewTextEditorInset(editor, 10, 10, {}) + // inset.webview.html = ` + // + // Hello World! + // + // `; + return <>
{x}