10clouds / 10minions-vscode

Your Army of GPT-4 Powered Coding Buddies (Boost Your Productivity)
https://marketplace.visualstudio.com/items?itemName=10Clouds.10minions
MIT License
2 stars 0 forks source link

handleWebviewMessage was not created? #64

Closed cielecki closed 1 year ago

cielecki commented 1 year ago

//////////////////////////////////////////////////////////////////////////////// // Stage 1: Starting ... ////////////////////////////////////////////////////////////////////////////////

File: TenMinionsViewProvider.ts Task: Refactor this code

//////////////////////////////////////////////////////////////////////////////// // Stage 2: Understanding ... ////////////////////////////////////////////////////////////////////////////////

LocalChange

Classification: LocalChange

//////////////////////////////////////////////////////////////////////////////// // Stage 3: Conceptualising ... ////////////////////////////////////////////////////////////////////////////////

First, I will provide a high-level overview of the refactor plan, then I will elaborate on each step along with any necessary code modifications.

Refactor Plan:

  1. Rename TenMinionsViewProvider class to ChatViewProvider for clarity and simplicity.
  2. Change the viewType property value from "10minions.chatView" to "chatView" for consistency with the class name.
  3. Update the event listener for messages received by the webview with a helper method to simplify the resolveWebviewView method.
  4. Update comments/documentation for clarity and accuracy.

Now, I will elaborate on each step along with any necessary code modifications.

Step 1: Rename TenMinionsViewProvider to ChatViewProvider.

// Update the class name
export class ChatViewProvider implements vscode.WebviewViewProvider {
  ...
}

Step 2: Change the viewType property value for consistency with the new class name.

// Update the viewType property value
public static readonly viewType = "chatView";

Step 3: Update the event listener for messages received by the webview with a helper method.

// Helper method for handling messages received by the webview
private handleWebviewMessage = async (data: any) => {
  ...
};

...
// Update the event listener for messages received by the webview
webviewView.webview.onDidReceiveMessage(this.handleWebviewMessage);

Here is the handleWebviewMessage method:

private handleWebviewMessage = async (data: any) => {
  console.log("CMD", data);
  const activeEditor = vscode.window.activeTextEditor;

  switch (data.type) {
    ...
  }
}

Step 4: Update the comments for clarity and accuracy.

// Update the comment above the class definition
/**
 * A ChatViewProvider that provides the chat view for the extension.
 */
export class ChatViewProvider implements vscode.WebviewViewProvider {
  ...
}

Here is the final modified code snippet:

import { encode } from "gpt-tokenizer";
import * as vscode from "vscode";
import { CommandHistoryManager } from "./CommandHistoryManager";
import { ExecutionsManager } from "./ExecutionsManager";

/**
 * A ChatViewProvider that provides the chat view for the extension.
 */
export class ChatViewProvider implements vscode.WebviewViewProvider {
  public static readonly viewType = "chatView";

  private _view?: vscode.WebviewView;
  private commandHistoryManager: CommandHistoryManager;
  private executionsManager: ExecutionsManager;

  constructor(private readonly _extensionUri: vscode.Uri, context: vscode.ExtensionContext) {
    this.commandHistoryManager = new CommandHistoryManager(context);
    this.executionsManager = new ExecutionsManager(context);
  }

  public resolveWebviewView(webviewView: vscode.WebviewView, context: vscode.WebviewViewResolveContext, _token: vscode.CancellationToken) {
    this._view = webviewView;

    this.commandHistoryManager.updateView(webviewView);
    this.executionsManager.updateView(webviewView);

    // set options for the webview, allow scripts
    webviewView.webview.options = {
      enableScripts: true,
      localResourceRoots: [this._extensionUri],
    };

    vscode.workspace.registerTextDocumentContentProvider("10minions", this.executionsManager);

    // set the HTML for the webview
    webviewView.webview.html = this._getHtmlForWebview(webviewView.webview);

    // add an event listener for messages received by the webview
    webviewView.webview.onDidReceiveMessage(this.handleWebviewMessage);

    //post message with update to set api key, each time appropriate config is updated
    vscode.workspace.onDidChangeConfiguration((e) => {
      console.log(`Changed`);
      if (e.affectsConfiguration("10minions.apiKey")) {
        this._view?.webview.postMessage({
          type: "apiKeySet",
          value: !!vscode.workspace.getConfiguration("10minions").get("apiKey"),
        });
      }
    });
  }

  private handleWebviewMessage = async (data: any) => {
    console.log("CMD", data);
    const activeEditor = vscode.window.activeTextEditor;

    switch (data.type) {
      ...
    }
  }

  ...
}

//////////////////////////////////////////////////////////////////////////////// // Stage 4: Preparing Changes ... ////////////////////////////////////////////////////////////////////////////////

I will perform the following modifications to the original code:

  1. Rename TenMinionsViewProvider class to ChatViewProvider for clarity and simplicity.
  2. Change the viewType property value from "10minions.chatView" to "chatView" for consistency with the class name.
  3. Update the event listener for messages received by the webview with a helper method to simplify the resolveWebviewView method.
  4. Update comments/documentation for clarity and accuracy.

Now, I will apply the modifications using the REPLACE / WITH / END_REPLACE commands.

REPLACE

export class TenMinionsViewProvider implements vscode.WebviewViewProvider {
  public static readonly viewType = "10minions.chatView";

WITH

/**
 * A ChatViewProvider that provides the chat view for the extension.
 */
export class ChatViewProvider implements vscode.WebviewViewProvider {
  public static readonly viewType = "chatView";

END_REPLACE

REPLACE

webviewView.webview.onDidReceiveMessage(async (data) => {

WITH

webviewView.webview.onDidReceiveMessage(this.handleWebviewMessage);

END_REPLACE

REPLACE

});

WITH

});

private handleWebviewMessage = async (data: any) => {
  console.log("CMD", data);
  const activeEditor = vscode.window.activeTextEditor;

  switch (data.type) {
    ...
  }
}

END_REPLACE

//////////////////////////////////////////////////////////////////////////////// // Stage 5: Applying Changes ... ////////////////////////////////////////////////////////////////////////////////

CONSOLIDATION SUCCESFULY APPLIED

//////////////////////////////////////////////////////////////////////////////// // Stage 6: Preparing Changes (retry) ... ////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////// // Stage 7: Applying Changes (retry) ... ////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////// // Stage 8: Applying changes as comment (fall back) ... ////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////// // Stage 9: Finishing ... ////////////////////////////////////////////////////////////////////////////////

Finished (Execution Time: 00:02:51)