From ed9e126a4a46d0099d3dd0a55b20d6e62be0a45f Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 30 Oct 2024 23:16:36 -0700 Subject: [PATCH] remove duplicate extension.ts --- extensions/void/src/extension.ts | 196 ------------------ .../{ctrlL.ts => applyDiffLazily.ts} | 6 +- extensions/void/src/extension/extension.ts | 47 ++--- 3 files changed, 23 insertions(+), 226 deletions(-) delete mode 100644 extensions/void/src/extension.ts rename extensions/void/src/extension/{ctrlL.ts => applyDiffLazily.ts} (94%) diff --git a/extensions/void/src/extension.ts b/extensions/void/src/extension.ts deleted file mode 100644 index 581b03b0..00000000 --- a/extensions/void/src/extension.ts +++ /dev/null @@ -1,196 +0,0 @@ -import * as vscode from 'vscode'; - -import { DiffArea, ChatThreads, MessageFromSidebar, MessageToSidebar } from './common/shared_types'; -import { v4 as uuidv4 } from 'uuid' -import { AbortRef } from './common/sendLLMMessage'; -import { DiffProvider } from './extension/DiffProvider'; -import { SidebarWebviewProvider } from './extension/providers/SidebarWebviewProvider'; -import { getVoidConfigFromPartial } from './webviews/common/contextForConfig'; -import { applyDiffLazily } from './extension/ctrlL'; -import { readFileContentOfUri } from './extension/extensionLib/readFileContentOfUri'; - -// this comes from vscode.proposed.editorInsets.d.ts -declare module 'vscode' { - export interface WebviewEditorInset { - readonly editor: vscode.TextEditor; - readonly line: number; - readonly height: number; - readonly webview: vscode.Webview; - readonly onDidDispose: Event; - dispose(): void; - } - export namespace window { - export function createWebviewTextEditorInset(editor: vscode.TextEditor, line: number, height: number, options?: vscode.WebviewOptions): WebviewEditorInset; - } -} - -const roundRangeToLines = (selection: vscode.Selection) => { - 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 - const sidebarWebviewProvider = new SidebarWebviewProvider(context); - context.subscriptions.push( - vscode.window.registerWebviewViewProvider(SidebarWebviewProvider.viewId, sidebarWebviewProvider, { webviewOptions: { retainContextWhenHidden: true } }) - ); - - - - // 2. ctrl+l - context.subscriptions.push( - vscode.commands.registerCommand('void.ctrl+l', () => { - const editor = vscode.window.activeTextEditor - if (!editor) return - - // show the sidebar - vscode.commands.executeCommand('workbench.view.extension.voidViewContainer'); - // vscode.commands.executeCommand('vscode.moveViewToPanel', CustomViewProvider.viewId); // move to aux bar - - const { selectionStr, filePath } = getSelection(editor) - - // send message to the webview (Sidebar.tsx) - sidebarWebviewProvider.webview.then(webview => webview.postMessage({ type: 'ctrl+l', selection: { selectionStr, filePath } } satisfies MessageToSidebar)); - }) - ); - - // 2.5: ctrl+k - context.subscriptions.push( - vscode.commands.registerCommand('void.ctrl+k', () => { - console.log('CTRLK PRESSED') - const editor = vscode.window.activeTextEditor - if (!editor) return - - const { selectionStr, filePath } = getSelection(editor) - - // send message to the webview (Sidebar.tsx) - sidebarWebviewProvider.webview.then(webview => webview.postMessage({ type: 'ctrl+k', selection: { selectionStr, filePath } } satisfies MessageToSidebar)); - }) - ); - - // 3. Show an approve/reject codelens above each change - const diffProvider = new DiffProvider(); - context.subscriptions.push(vscode.languages.registerCodeLensProvider('*', diffProvider)); - - // 4. Add approve/reject commands - context.subscriptions.push(vscode.commands.registerCommand('void.acceptDiff', async (params) => { - diffProvider.acceptDiff(params) - })); - context.subscriptions.push(vscode.commands.registerCommand('void.rejectDiff', async (params) => { - diffProvider.rejectDiff(params) - })); - - // 5. Receive messages from sidebar - sidebarWebviewProvider.webview.then( - webview => { - - // top navigation bar commands - context.subscriptions.push(vscode.commands.registerCommand('void.startNewThread', async () => { - webview.postMessage({ type: 'startNewThread' } satisfies MessageToSidebar) - })) - context.subscriptions.push(vscode.commands.registerCommand('void.toggleThreadSelector', async () => { - webview.postMessage({ type: 'toggleThreadSelector' } satisfies MessageToSidebar) - })) - context.subscriptions.push(vscode.commands.registerCommand('void.toggleSettings', async () => { - webview.postMessage({ type: 'toggleSettings' } satisfies MessageToSidebar) - })); - - // Receive messages in the extension from the sidebar webview (messages are sent using `postMessage`) - webview.onDidReceiveMessage(async (m: MessageFromSidebar) => { - - const abortApplyRef: AbortRef = { current: null } - - if (m.type === 'requestFiles') { - - // get contents of all file paths - const files = await Promise.all( - m.filepaths.map(async (filepath) => ({ filepath, content: await readFileContentOfUri(filepath) })) - ) - - // send contents to webview - webview.postMessage({ type: 'files', files, } satisfies MessageToSidebar) - - } - else if (m.type === 'applyChanges') { - - const editor = vscode.window.activeTextEditor - if (!editor) { - vscode.window.showInformationMessage('No active editor!') - return - } - // create an area to show diffs - const partialDiffArea: Omit = { - startLine: 0, // in ctrl+L the start and end lines are the full document - endLine: editor.document.lineCount, - originalStartLine: 0, - originalEndLine: editor.document.lineCount, - sweepIndex: null, - } - const diffArea = diffProvider.createDiffArea(editor.document.uri, partialDiffArea, await readFileContentOfUri(editor.document.uri)) - - const docUri = editor.document.uri - const fileStr = await readFileContentOfUri(docUri) - const voidConfig = getVoidConfigFromPartial(context.globalState.get('partialVoidConfig') ?? {}) - - await applyDiffLazily({ docUri, oldFileStr: fileStr, diffRepr: m.diffRepr, voidConfig, diffProvider, diffArea, abortRef: abortApplyRef }) - } - else if (m.type === 'getPartialVoidConfig') { - const partialVoidConfig = context.globalState.get('partialVoidConfig') ?? {} - webview.postMessage({ type: 'partialVoidConfig', partialVoidConfig } satisfies MessageToSidebar) - } - else if (m.type === 'persistPartialVoidConfig') { - const partialVoidConfig = m.partialVoidConfig - context.globalState.update('partialVoidConfig', partialVoidConfig) - } - else if (m.type === 'getAllThreads') { - const threads: ChatThreads = context.workspaceState.get('allThreads') ?? {} - webview.postMessage({ type: 'allThreads', threads } satisfies MessageToSidebar) - } - else if (m.type === 'persistThread') { - const threads: ChatThreads = context.workspaceState.get('allThreads') ?? {} - const updatedThreads: ChatThreads = { ...threads, [m.thread.id]: m.thread } - context.workspaceState.update('allThreads', updatedThreads) - } - else if (m.type === 'getDeviceId') { - let deviceId = context.globalState.get('void_deviceid') - if (!deviceId || typeof deviceId !== 'string') { - deviceId = uuidv4() - context.globalState.update('void_deviceid', deviceId) - } - webview.postMessage({ type: 'deviceId', deviceId: deviceId as string } satisfies MessageToSidebar) - } - else { - console.error('unrecognized command', m) - } - }) - } - ) - - - - - // Gets called when user presses ctrl + k (mounts ctrl+k-style codelens) - // TODO need to build this - // const ctrlKCodeLensProvider = new CtrlKCodeLensProvider(); - // context.subscriptions.push(vscode.languages.registerCodeLensProvider('*', ctrlKCodeLensProvider)); - // context.subscriptions.push( - // vscode.commands.registerCommand('void.ctrl+k', () => { - // const editor = vscode.window.activeTextEditor; - // if (!editor) - // return - // ctrlKCodeLensProvider.addNewCodeLens(editor.document, editor.selection); - // // vscode.commands.executeCommand('editor.action.showHover'); // apparently this refreshes the codelenses by having the internals call provideCodeLenses - // }) - // ) - -} diff --git a/extensions/void/src/extension/ctrlL.ts b/extensions/void/src/extension/applyDiffLazily.ts similarity index 94% rename from extensions/void/src/extension/ctrlL.ts rename to extensions/void/src/extension/applyDiffLazily.ts index 2837638f..9b350082 100644 --- a/extensions/void/src/extension/ctrlL.ts +++ b/extensions/void/src/extension/applyDiffLazily.ts @@ -134,7 +134,7 @@ Return \`true\` if ANY part of the chunk should be modified, and \`false\` if it // lazily applies the diff to the file // we chunk the text in the file, and ask an LLM whether it should edit each chunk -const applyDiffLazily = async ({ docUri, oldFileStr, voidConfig, abortRef, diffRepr, diffProvider, diffArea }: { docUri: vscode.Uri, oldFileStr: string, diffRepr: string, voidConfig: VoidConfig, diffProvider: DiffProvider, diffArea: DiffArea, abortRef: AbortRef }) => { +export const applyDiffLazily = async ({ docUri, oldFileStr, voidConfig, abortRef, diffRepr, diffProvider, diffArea }: { docUri: vscode.Uri, oldFileStr: string, diffRepr: string, voidConfig: VoidConfig, diffProvider: DiffProvider, diffArea: DiffArea, abortRef: AbortRef }) => { // stateful variables @@ -179,7 +179,3 @@ const applyDiffLazily = async ({ docUri, oldFileStr, voidConfig, abortRef, diffR } - - - -export { applyDiffLazily } diff --git a/extensions/void/src/extension/extension.ts b/extensions/void/src/extension/extension.ts index 9cba5293..042b5dac 100644 --- a/extensions/void/src/extension/extension.ts +++ b/extensions/void/src/extension/extension.ts @@ -1,13 +1,14 @@ import * as vscode from 'vscode'; -import { applyDiffLazily } from './ctrlL'; -import { readFileContentOfUri } from './extensionLib/readFileContentOfUri'; -import { MessageToSidebar, MessageFromSidebar, DiffArea, ChatThreads } from '../common/shared_types'; -import { DiffProvider } from './DiffProvider'; -import { getVoidConfigFromPartial } from '../webviews/common/contextForConfig'; -import { CtrlKWebviewProvider } from './providers/CtrlKWebviewProvider'; -import { SidebarWebviewProvider } from './providers/SidebarWebviewProvider'; + import { v4 as uuidv4 } from 'uuid' import { AbortRef } from '../common/sendLLMMessage'; +import { MessageToSidebar, MessageFromSidebar, DiffArea, ChatThreads } from '../common/shared_types'; +import { getVoidConfigFromPartial } from '../webviews/common/contextForConfig'; +import { applyDiffLazily } from './applyDiffLazily'; +import { DiffProvider } from './DiffProvider'; +import { readFileContentOfUri } from './extensionLib/readFileContentOfUri'; +import { SidebarWebviewProvider } from './providers/SidebarWebviewProvider'; +import { CtrlKWebviewProvider } from './providers/CtrlKWebviewProvider'; // this comes from vscode.proposed.editorInsets.d.ts declare module 'vscode' { @@ -25,7 +26,16 @@ declare module 'vscode' { } const roundRangeToLines = (selection: vscode.Selection) => { - return new vscode.Range(selection.start.line, 0, selection.end.line, Number.MAX_SAFE_INTEGER) + 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) { @@ -50,14 +60,7 @@ export function activate(context: vscode.ExtensionContext) { 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, filePath } } satisfies MessageToSidebar)); @@ -71,17 +74,11 @@ 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) - ctrlKWebviewProvider.onPressCtrlK() + // ctrlKWebviewProvider.onPressCtrlK() + // sidebarWebviewProvider.webview.then(webview => webview.postMessage({ type: 'ctrl+k', selection: { selectionStr, filePath } } satisfies MessageToSidebar)); }) );