From b0b83f0b0ba385a22841b3f4a15dd4422edb96f2 Mon Sep 17 00:00:00 2001 From: amitjoshi Date: Wed, 27 Sep 2023 10:46:09 +0530 Subject: [PATCH 1/7] getting info about selected code --- src/common/Utils.ts | 120 +++++++++++++----------- src/common/copilot/PowerPagesCopilot.ts | 21 ++++- 2 files changed, 87 insertions(+), 54 deletions(-) diff --git a/src/common/Utils.ts b/src/common/Utils.ts index 7ce9c19f..47962463 100644 --- a/src/common/Utils.ts +++ b/src/common/Utils.ts @@ -7,94 +7,108 @@ import * as vscode from "vscode"; import { EXTENSION_ID } from "../client/constants"; -export function getSelectedSnippet(): string { - const editor = vscode.window.activeTextEditor; - if (!editor) { - return ""; - } - const selection = editor.selection; - const text = editor.document.getText(selection); - return text; +export function getSelectedCode(editor: vscode.TextEditor): string { + if (!editor) { + return ""; + } + const selection = editor.selection; + const text = editor.document.getText(selection); + return text; } +export function getSelectedCodeLineRange(editor: vscode.TextEditor): { start: number, end: number } { + if (!editor) { + return { start: 0, end: 0 }; + } + // Get the selection(s) in the editor + const selection = editor.selection; + + const startLine = selection.start.line; + const endLine = selection.end.line; + console.log(`Selected lines: ${startLine + 1}-${endLine + 1}`); + + return { start: startLine, end: endLine }; + +} // Get the organization ID from the user during login export async function getOrgID(): Promise { - const orgID = await vscode.window.showInputBox({ - placeHolder: vscode.l10n.t("Enter Organization ID") - }); - if (!orgID) { - throw new Error("Organization ID is required"); - } - return Promise.resolve(orgID); + const orgID = await vscode.window.showInputBox({ + placeHolder: vscode.l10n.t("Enter Organization ID") + }); + if (!orgID) { + throw new Error("Organization ID is required"); + } + return Promise.resolve(orgID); } export function getNonce() { - let text = ''; - const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; - for (let i = 0; i < 32; i++) { - text += possible.charAt(Math.floor(Math.random() * possible.length)); - } - return text; + let text = ''; + const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + for (let i = 0; i < 32; i++) { + text += possible.charAt(Math.floor(Math.random() * possible.length)); + } + return text; } export function getUserName(user: string) { - const parts = user.split(" - "); - return parts[0]; + const parts = user.split(" - "); + return parts[0]; } export function getLastThreePartsOfFileName(string: string): string[] { - const parts: string[] = string.split('.'); - if (parts.length >= 3) { - return parts.slice(-3); - } else { - return parts; - } + const parts: string[] = string.split('.'); + if (parts.length >= 3) { + return parts.slice(-3); + } else { + return parts; + } } export function escapeDollarSign(paragraph: string): string { - return paragraph.replace(/\$/g, "\\$"); + return paragraph.replace(/\$/g, "\\$"); } //TODO: Take message as a parameter export function showConnectedOrgMessage(environmentName: string, orgUrl: string) { - vscode.window.showInformationMessage( - vscode.l10n.t({ - message: "Power Pages Copilot is now connected to the environment: {0} : {1}", - args: [environmentName, orgUrl], - comment: ["{0} represents the environment name"] - }) - ); + vscode.window.showInformationMessage( + vscode.l10n.t({ + message: "Power Pages Copilot is now connected to the environment: {0} : {1}", + args: [environmentName, orgUrl], + comment: ["{0} represents the environment name"] + }) + ); } export async function showInputBoxAndGetOrgUrl() { - return vscode.window.showInputBox({ - placeHolder: vscode.l10n.t("Enter the environment URL"), - prompt: vscode.l10n.t("Active auth profile is not found or has expired. To create a new auth profile, enter the environment URL.") - }); + return vscode.window.showInputBox({ + placeHolder: vscode.l10n.t("Enter the environment URL"), + prompt: vscode.l10n.t("Active auth profile is not found or has expired. To create a new auth profile, enter the environment URL.") + }); } export async function showProgressWithNotification(title: string, task: () => Promise): Promise { - return await vscode.window.withProgress({ - location: vscode.ProgressLocation.Notification, - title: title, - cancellable: false - }, async () => { - return await task(); - }); + return await vscode.window.withProgress({ + location: vscode.ProgressLocation.Notification, + title: title, + cancellable: false + }, async () => { + return await task(); + }); } export function getExtensionVersion(): string { - const extension = vscode.extensions.getExtension(EXTENSION_ID); - return extension ? extension.packageJSON.version : ""; + const extension = vscode.extensions.getExtension(EXTENSION_ID); + return extension ? extension.packageJSON.version : ""; } export function getExtensionType(): string { - return vscode.env.uiKind === vscode.UIKind.Desktop ? "Desktop" : "Web"; + return vscode.env.uiKind === vscode.UIKind.Desktop ? "Desktop" : "Web"; } export function openWalkthrough(extensionUri: vscode.Uri) { - const walkthroughUri = vscode.Uri.joinPath(extensionUri, 'src', 'common', 'copilot', 'assets', 'walkthrough', 'Copilot-In-PowerPages.md'); - vscode.commands.executeCommand("markdown.showPreview", walkthroughUri); + const walkthroughUri = vscode.Uri.joinPath(extensionUri, 'src', 'common', 'copilot', 'assets', 'walkthrough', 'Copilot-In-PowerPages.md'); + vscode.commands.executeCommand("markdown.showPreview", walkthroughUri); } + diff --git a/src/common/copilot/PowerPagesCopilot.ts b/src/common/copilot/PowerPagesCopilot.ts index 0a66a666..f061d20e 100644 --- a/src/common/copilot/PowerPagesCopilot.ts +++ b/src/common/copilot/PowerPagesCopilot.ts @@ -12,7 +12,7 @@ import { PacWrapper } from "../../client/pac/PacWrapper"; import { ITelemetry } from "../../client/telemetry/ITelemetry"; import { AUTH_CREATE_FAILED, AUTH_CREATE_MESSAGE, AuthProfileNotFound, COPILOT_UNAVAILABLE, CopilotDisclaimer, CopilotStylePathSegments, DataverseEntityNameMap, EntityFieldMap, FieldTypeMap, PAC_SUCCESS, WebViewMessage, sendIconSvg } from "./constants"; import { IActiveFileParams, IActiveFileData, IOrgInfo } from './model'; -import { escapeDollarSign, getLastThreePartsOfFileName, getNonce, getUserName, openWalkthrough, showConnectedOrgMessage, showInputBoxAndGetOrgUrl, showProgressWithNotification } from "../Utils"; +import { escapeDollarSign, getLastThreePartsOfFileName, getNonce, getSelectedCode, getSelectedCodeLineRange, getUserName, openWalkthrough, showConnectedOrgMessage, showInputBoxAndGetOrgUrl, showProgressWithNotification } from "../Utils"; import { CESUserFeedback } from "./user-feedback/CESSurvey"; import { GetAuthProfileWatchPattern } from "../../client/lib/AuthPanelView"; import { ActiveOrgOutput } from "../../client/pac/PacTypes"; @@ -66,6 +66,25 @@ export class PowerPagesCopilot implements vscode.WebviewViewProvider { ) ); + this._disposables.push( + vscode.window.onDidChangeTextEditorSelection(async () => { + const editor = vscode.window.activeTextEditor; + if (!editor) { + return; + } + const selectedCode = getSelectedCode(editor); + const selectedCodeLineRange = getSelectedCodeLineRange(editor); + if(selectedCodeLineRange.start === 0) { + return; + } + console.log(selectedCode); + vscode.window.showInformationMessage("selectedCodeLineRange: " + selectedCodeLineRange.start + " " + selectedCodeLineRange.end); + this.sendMessageToWebview({ type: "selectedCodeInfo", value: {start: selectedCodeLineRange.start, end: selectedCodeLineRange.end, selectedCode: selectedCode} }); + + }) + ); + + if (this._pacWrapper) { this.setupFileWatcher(); } From 59cb3b507c33465ebe63b92e55cc05adbc0fa6ba Mon Sep 17 00:00:00 2001 From: amitjoshi Date: Fri, 29 Sep 2023 12:16:47 +0530 Subject: [PATCH 2/7] showing label for selected lines of code --- src/common/copilot/PowerPagesCopilot.ts | 9 +++++---- src/common/copilot/assets/scripts/copilot.js | 16 ++++++++++++++-- src/common/copilot/assets/styles/copilot.css | 12 ++++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/common/copilot/PowerPagesCopilot.ts b/src/common/copilot/PowerPagesCopilot.ts index f061d20e..d3267d06 100644 --- a/src/common/copilot/PowerPagesCopilot.ts +++ b/src/common/copilot/PowerPagesCopilot.ts @@ -74,11 +74,11 @@ export class PowerPagesCopilot implements vscode.WebviewViewProvider { } const selectedCode = getSelectedCode(editor); const selectedCodeLineRange = getSelectedCodeLineRange(editor); - if(selectedCodeLineRange.start === 0) { - return; - } + // if(selectedCodeLineRange.start === 0) { + // return; + // } console.log(selectedCode); - vscode.window.showInformationMessage("selectedCodeLineRange: " + selectedCodeLineRange.start + " " + selectedCodeLineRange.end); + // vscode.window.showInformationMessage("selectedCodeLineRange: " + selectedCodeLineRange.start + " " + selectedCodeLineRange.end); this.sendMessageToWebview({ type: "selectedCodeInfo", value: {start: selectedCodeLineRange.start, end: selectedCodeLineRange.end, selectedCode: selectedCode} }); }) @@ -436,6 +436,7 @@ export class PowerPagesCopilot implements vscode.WebviewViewProvider {
+