Skip to content

Commit

Permalink
feat: Add getActiveEditorContent function for retrieving active edito…
Browse files Browse the repository at this point in the history
…r 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 <[email protected]>
  • Loading branch information
amitjoshi438 and amitjoshi authored May 8, 2024
1 parent 09bf6e8 commit 5f16f62
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
29 changes: 28 additions & 1 deletion src/common/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
34 changes: 4 additions & 30 deletions src/common/copilot/PowerPagesCopilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
})()
: (() => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5f16f62

Please sign in to comment.