From 5f16f62191cf36e77dd41f04f2d545b211296a84 Mon Sep 17 00:00:00 2001 From: amitjoshi438 <54068463+amitjoshi438@users.noreply.github.com> Date: Wed, 8 May 2024 14:17:23 +0530 Subject: [PATCH] feat: Add getActiveEditorContent function for retrieving active editor data (#929) The code changes add a new function called getActiveEditorContent to the Utils module. This function retrieves the content and parameters of the active editor in Visual Studio Code. It is used in the PowerPagesCopilot module to get the active file content and its associated parameters. Co-authored-by: amitjoshi --- src/common/Utils.ts | 29 ++++++++++++++++++++- src/common/copilot/PowerPagesCopilot.ts | 34 +++---------------------- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/common/Utils.ts b/src/common/Utils.ts index 0eddc9e2..30e7c934 100644 --- a/src/common/Utils.ts +++ b/src/common/Utils.ts @@ -8,7 +8,8 @@ import * as vscode from "vscode"; import { EXTENSION_ID, EXTENSION_NAME, SETTINGS_EXPERIMENTAL_STORE_NAME } from "../client/constants"; import { CUSTOM_TELEMETRY_FOR_POWER_PAGES_SETTING_NAME } from "./OneDSLoggerTelemetry/telemetryConstants"; import { PacWrapper } from "../client/pac/PacWrapper"; -import { AUTH_CREATE_FAILED, AUTH_CREATE_MESSAGE, PAC_SUCCESS } from "./copilot/constants"; +import { AUTH_CREATE_FAILED, AUTH_CREATE_MESSAGE, DataverseEntityNameMap, EntityFieldMap, FieldTypeMap, PAC_SUCCESS } from "./copilot/constants"; +import { IActiveFileData, IActiveFileParams } from "./copilot/model"; export function getSelectedCode(editor: vscode.TextEditor): string { if (!editor) { @@ -152,3 +153,29 @@ export async function createAuthProfileExp(pacWrapper: PacWrapper | undefined) { return; } } + +export function getActiveEditorContent(): IActiveFileData { + const activeEditor = vscode.window.activeTextEditor; + const activeFileData: IActiveFileData = { + activeFileContent: '', + activeFileParams: { + dataverseEntity: '', + entityField: '', + fieldType: '' + } as IActiveFileParams + }; + if (activeEditor) { + const document = activeEditor.document; + const fileName = document.fileName; + const relativeFileName = vscode.workspace.asRelativePath(fileName); + + const activeFileParams: string[] = getLastThreePartsOfFileName(relativeFileName); + + activeFileData.activeFileContent = document.getText(); + activeFileData.activeFileParams.dataverseEntity = DataverseEntityNameMap.get(activeFileParams[0]) || ""; + activeFileData.activeFileParams.entityField = EntityFieldMap.get(activeFileParams[1]) || ""; + activeFileData.activeFileParams.fieldType = FieldTypeMap.get(activeFileParams[2]) || ""; + } + + return activeFileData; +} diff --git a/src/common/copilot/PowerPagesCopilot.ts b/src/common/copilot/PowerPagesCopilot.ts index e5049451..3df7c01d 100644 --- a/src/common/copilot/PowerPagesCopilot.ts +++ b/src/common/copilot/PowerPagesCopilot.ts @@ -10,9 +10,9 @@ import { dataverseAuthentication, intelligenceAPIAuthentication } from "../Authe import { v4 as uuidv4 } from 'uuid' import { PacWrapper } from "../../client/pac/PacWrapper"; import { ITelemetry } from "../../client/telemetry/ITelemetry"; -import { ADX_ENTITYFORM, ADX_ENTITYLIST, AUTH_CREATE_FAILED, AUTH_CREATE_MESSAGE, AuthProfileNotFound, COPILOT_UNAVAILABLE, CopilotDisclaimer, CopilotStylePathSegments, DataverseEntityNameMap, EXPLAIN_CODE, EntityFieldMap, FieldTypeMap, PAC_SUCCESS, SELECTED_CODE_INFO, SELECTED_CODE_INFO_ENABLED, THUMBS_DOWN, THUMBS_UP, UserPrompt, WebViewMessage, sendIconSvg } from "./constants"; -import { IActiveFileParams, IActiveFileData, IOrgInfo } from './model'; -import { createAuthProfileExp, escapeDollarSign, getLastThreePartsOfFileName, getNonce, getSelectedCode, getSelectedCodeLineRange, getUserName, openWalkthrough, showConnectedOrgMessage, showInputBoxAndGetOrgUrl, showProgressWithNotification } from "../Utils"; +import { ADX_ENTITYFORM, ADX_ENTITYLIST, AUTH_CREATE_FAILED, AUTH_CREATE_MESSAGE, AuthProfileNotFound, COPILOT_UNAVAILABLE, CopilotDisclaimer, CopilotStylePathSegments, EXPLAIN_CODE, PAC_SUCCESS, SELECTED_CODE_INFO, SELECTED_CODE_INFO_ENABLED, THUMBS_DOWN, THUMBS_UP, UserPrompt, WebViewMessage, sendIconSvg } from "./constants"; +import { IActiveFileParams, IOrgInfo } from './model'; +import { createAuthProfileExp, escapeDollarSign, getActiveEditorContent, getNonce, getSelectedCode, getSelectedCodeLineRange, getUserName, openWalkthrough, showConnectedOrgMessage, showInputBoxAndGetOrgUrl, showProgressWithNotification } from "../Utils"; import { CESUserFeedback } from "./user-feedback/CESSurvey"; import { ActiveOrgOutput } from "../../client/pac/PacTypes"; import { CopilotWalkthroughEvent, CopilotCopyCodeToClipboardEvent, CopilotInsertCodeToEditorEvent, CopilotLoadedEvent, CopilotOrgChangedEvent, CopilotUserFeedbackThumbsDownEvent, CopilotUserFeedbackThumbsUpEvent, CopilotUserPromptedEvent, CopilotCodeLineCountEvent, CopilotClearChatEvent, CopilotNotAvailable, CopilotExplainCode, CopilotExplainCodeSize, CopilotNotAvailableECSConfig } from "./telemetry/telemetryConstants"; @@ -230,7 +230,7 @@ export class PowerPagesCopilot implements vscode.WebviewViewProvider { sendTelemetryEvent(this.telemetry, { eventName: CopilotUserPromptedEvent, copilotSessionId: sessionID, aibEndpoint: this.aibEndpoint ?? '', orgId: orgID, isSuggestedPrompt: String(data.value.isSuggestedPrompt) }); //TODO: Add active Editor info orgID ? (async () => { - const { activeFileParams } = this.getActiveEditorContent(); + const { activeFileParams } = getActiveEditorContent(); await this.authenticateAndSendAPIRequest(data.value.userPrompt, activeFileParams, orgID, this.telemetry); })() : (() => { @@ -426,32 +426,6 @@ export class PowerPagesCopilot implements vscode.WebviewViewProvider { } } - private getActiveEditorContent(): IActiveFileData { - const activeEditor = vscode.window.activeTextEditor; - const activeFileData: IActiveFileData = { - activeFileContent: '', - activeFileParams: { - dataverseEntity: '', - entityField: '', - fieldType: '' - } as IActiveFileParams - }; - if (activeEditor) { - const document = activeEditor.document; - const fileName = document.fileName; - const relativeFileName = vscode.workspace.asRelativePath(fileName); - - const activeFileParams: string[] = getLastThreePartsOfFileName(relativeFileName); - - activeFileData.activeFileContent = document.getText(); - activeFileData.activeFileParams.dataverseEntity = DataverseEntityNameMap.get(activeFileParams[0]) || ""; - activeFileData.activeFileParams.entityField = EntityFieldMap.get(activeFileParams[1]) || ""; - activeFileData.activeFileParams.fieldType = FieldTypeMap.get(activeFileParams[2]) || ""; - } - - return activeFileData; - } - public sendMessageToWebview(message: WebViewMessage) { if (this._view) { this._view.webview.postMessage(message);