Skip to content

Commit a87c734

Browse files
authored
✨ Add extension state and analysis placeholders (#29)
- Add placeholder menu items for analyzing specific files from the explorer view - Add placeholder startAnalysis command - Create utility function for getting the webview content since it is being used in panel and sidebar webviews - Create extension state to hold any new webview providers. Allows access to the state without having to change function signatures to pass in any new webviews to the registerCommand function as they are created. Signed-off-by: Ian Bolton <[email protected]>
1 parent f8eb03a commit a87c734

7 files changed

+133
-61
lines changed

vscode/package.json

+27
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
"title": "Toggle Fullscreen",
4141
"category": "Konveyor",
4242
"icon": "$(fullscreen)"
43+
},
44+
{
45+
"command": "konveyor.startAnalysis",
46+
"title": "Start Analysis",
47+
"category": "Konveyor"
48+
}
49+
],
50+
"submenus": [
51+
{
52+
"id": "konveyor.submenu",
53+
"label": "Konveyor Actions"
4354
}
4455
],
4556
"viewsContainers": {
@@ -74,6 +85,22 @@
7485
"command": "konveyor.toggleFullScreen",
7586
"group": "navigation@1",
7687
"when": "activeWebviewPanelId == konveyor.konveyorGUIView"
88+
},
89+
{
90+
"command": "konveyor.startAnalysis",
91+
"group": "navigation@1"
92+
}
93+
],
94+
"explorer/context": [
95+
{
96+
"submenu": "konveyor.submenu",
97+
"group": "navigation@1"
98+
}
99+
],
100+
"konveyor.submenu": [
101+
{
102+
"command": "konveyor.startAnalysis",
103+
"group": "navigation@1"
77104
}
78105
]
79106
}
+5-44
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from "vscode";
22
import { getNonce } from "./getNonce";
3+
import { getWebviewContent } from "./webviewContent";
34

45
export class KonveyorGUIWebviewViewProvider
56
implements vscode.WebviewViewProvider
@@ -24,6 +25,7 @@ export class KonveyorGUIWebviewViewProvider
2425
get webview() {
2526
return this._webview;
2627
}
28+
2729
onWebviewReady(callback: (webview: vscode.Webview) => void) {
2830
this.webviewReadyCallback = callback;
2931
if (this._webview) {
@@ -47,55 +49,14 @@ export class KonveyorGUIWebviewViewProvider
4749
],
4850
};
4951

50-
webviewView.webview.html = this.getSidebarContent(
52+
webviewView.webview.html = getWebviewContent(
5153
this.extensionContext,
52-
webviewView,
53-
true,
54+
webviewView.webview,
55+
true
5456
);
5557

5658
if (this.webviewReadyCallback) {
5759
this.webviewReadyCallback(webviewView.webview);
5860
}
5961
}
60-
61-
getSidebarContent(
62-
context: vscode.ExtensionContext,
63-
panel: vscode.WebviewPanel | vscode.WebviewView,
64-
isFullScreen: boolean = false,
65-
): string {
66-
const webview = panel.webview;
67-
const extensionUri = context.extensionUri;
68-
const scriptUri = webview.asWebviewUri(
69-
vscode.Uri.joinPath(extensionUri, "out", "webview", "main.wv.js"),
70-
);
71-
const styleUri = webview.asWebviewUri(
72-
vscode.Uri.joinPath(extensionUri, "media", "styles.css"),
73-
);
74-
const nonce = getNonce();
75-
76-
return `<!DOCTYPE html>
77-
<html lang="en">
78-
<head>
79-
<meta charset="UTF-8">
80-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
81-
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${
82-
webview.cspSource
83-
}; script-src 'nonce-${nonce}' ${webview.cspSource} 'unsafe-inline';">
84-
<title>Konveyor</title>
85-
<link rel="stylesheet" href="${styleUri}">
86-
</head>
87-
<body>
88-
<div id="root"></div>
89-
<script nonce="${nonce}">
90-
const vscode = acquireVsCodeApi();
91-
window.addEventListener('load', function() {
92-
vscode.postMessage({ command: 'startup', isFullScreen: ${isFullScreen} });
93-
console.log('HTML started up. Full screen:', ${isFullScreen});
94-
});
95-
</script>
96-
${`<script nonce="${nonce}" src="${scriptUri}"></script>`}
97-
</body>
98-
</html>
99-
`;
100-
}
10162
}

vscode/src/VsCodeExtension.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ import { v4 as uuidv4 } from "uuid";
33
import { KonveyorGUIWebviewViewProvider } from "./KonveyorGUIWebviewViewProvider";
44
import { registerAllCommands } from "./commands";
55
import { setupWebviewMessageListener } from "./webviewMessageHandler";
6+
import { ExtensionState } from "./extensionState";
67

78
export class VsCodeExtension {
89
private extensionContext: vscode.ExtensionContext;
9-
private sidebar: KonveyorGUIWebviewViewProvider;
1010
private windowId: string;
11+
private state: ExtensionState;
1112

1213
constructor(context: vscode.ExtensionContext) {
1314
this.extensionContext = context;
1415
this.windowId = uuidv4();
15-
this.sidebar = new KonveyorGUIWebviewViewProvider(
16+
17+
const sidebarProvider = new KonveyorGUIWebviewViewProvider(
1618
this.windowId,
1719
this.extensionContext,
1820
);
@@ -24,11 +26,15 @@ export class VsCodeExtension {
2426
);
2527
}
2628

29+
this.state = {
30+
sidebarProvider,
31+
};
32+
2733
// Sidebar
2834
context.subscriptions.push(
2935
vscode.window.registerWebviewViewProvider(
3036
"konveyor.konveyorGUIView",
31-
this.sidebar,
37+
sidebarProvider,
3238
{
3339
webviewOptions: {
3440
retainContextWhenHidden: true,
@@ -37,12 +43,11 @@ export class VsCodeExtension {
3743
),
3844
);
3945

40-
// Set up message listener when the webview is ready
41-
this.sidebar.onWebviewReady((webview) => {
46+
sidebarProvider.onWebviewReady((webview) => {
4247
setupWebviewMessageListener(webview);
4348
});
4449

4550
// Commands
46-
registerAllCommands(context, this.sidebar);
51+
registerAllCommands(this.extensionContext, this.state);
4752
}
4853
}

vscode/src/commands.ts

+40-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as vscode from "vscode";
22
import { KonveyorGUIWebviewViewProvider } from "./KonveyorGUIWebviewViewProvider";
33
import { setupWebviewMessageListener } from "./webviewMessageHandler";
4+
import { ExtensionState } from "./extensionState";
5+
import { getWebviewContent } from "./webviewContent";
46

57
let fullScreenPanel: vscode.WebviewPanel | undefined;
68

@@ -13,12 +15,39 @@ function getFullScreenTab() {
1315

1416
const commandsMap: (
1517
extensionContext: vscode.ExtensionContext,
16-
sidebar: KonveyorGUIWebviewViewProvider,
17-
) => { [command: string]: (...args: any) => any } = (
18-
extensionContext,
19-
sidebar,
18+
state: ExtensionState
2019
) => {
20+
[command: string]: (...args: any) => any;
21+
} = (extensionContext, state) => {
22+
const { sidebarProvider } = state;
2123
return {
24+
"konveyor.startAnalysis": async (resource: vscode.Uri) => {
25+
if (!resource) {
26+
vscode.window.showErrorMessage("No file selected for analysis.");
27+
return;
28+
}
29+
30+
// Get the file path
31+
const filePath = resource.fsPath;
32+
33+
// Perform your analysis logic here
34+
try {
35+
// For example, read the file content
36+
const fileContent = await vscode.workspace.fs.readFile(resource);
37+
const contentString = Buffer.from(fileContent).toString("utf8");
38+
39+
console.log(contentString, fileContent);
40+
41+
// TODO: Analyze the file content
42+
vscode.window.showInformationMessage(`Analyzing file: ${filePath}`);
43+
44+
// Call your analysis function/module
45+
// analyzeFileContent(contentString);
46+
} catch (error) {
47+
vscode.window.showErrorMessage(`Failed to analyze file: ${error}`);
48+
}
49+
},
50+
2251
"konveyor.focusKonveyorInput": async () => {
2352
const fullScreenTab = getFullScreenTab();
2453
if (!fullScreenTab) {
@@ -28,14 +57,14 @@ const commandsMap: (
2857
// focus fullscreen
2958
fullScreenPanel?.reveal();
3059
}
31-
// sidebar.webviewProtocol?.request("focusContinueInput", undefined);
60+
// sidebar.webviewProtocol?.request("focusInput", undefined);
3261
// await addHighlightedCodeToContext(sidebar.webviewProtocol);
3362
},
3463
"konveyor.toggleFullScreen": () => {
3564
// Check if full screen is already open by checking open tabs
3665
const fullScreenTab = getFullScreenTab();
3766

38-
// Check if the active editor is the Continue GUI View
67+
// Check if the active editor is the GUI View
3968
if (fullScreenTab && fullScreenTab.isActive) {
4069
//Full screen open and focused - close it
4170
vscode.commands.executeCommand("workbench.action.closeActiveEditor"); //this will trigger the onDidDispose listener below
@@ -61,10 +90,10 @@ const commandsMap: (
6190
fullScreenPanel = panel;
6291

6392
//Add content to the panel
64-
panel.webview.html = sidebar.getSidebarContent(
93+
panel.webview.html = getWebviewContent(
6594
extensionContext,
66-
panel,
67-
true,
95+
sidebarProvider?.webview || panel.webview,
96+
true
6897
);
6998

7099
setupWebviewMessageListener(panel.webview);
@@ -83,10 +112,10 @@ const commandsMap: (
83112

84113
export function registerAllCommands(
85114
context: vscode.ExtensionContext,
86-
sidebar: KonveyorGUIWebviewViewProvider
115+
state: ExtensionState
87116
) {
88117
for (const [command, callback] of Object.entries(
89-
commandsMap(context, sidebar)
118+
commandsMap(context, state)
90119
)) {
91120
context.subscriptions.push(
92121
vscode.commands.registerCommand(command, callback),

vscode/src/extensionState.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// extensionState.ts
2+
import { KonveyorGUIWebviewViewProvider } from "./KonveyorGUIWebviewViewProvider";
3+
4+
export interface ExtensionState {
5+
sidebarProvider: KonveyorGUIWebviewViewProvider;
6+
// Add other shared components as needed
7+
}

vscode/src/webview/components/App.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from "react";
22

3+
// Global variable vscode pulled from the window object.
34
interface vscode {
45
postMessage(message: any): void;
56
}

vscode/src/webviewContent.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as vscode from "vscode";
2+
import { getNonce } from "./getNonce";
3+
4+
export function getWebviewContent(
5+
context: vscode.ExtensionContext,
6+
webview: vscode.Webview,
7+
isFullScreen: boolean = false
8+
): string {
9+
const extensionUri = context.extensionUri;
10+
const scriptUri = webview.asWebviewUri(
11+
vscode.Uri.joinPath(extensionUri, "out", "webview", "main.wv.js")
12+
);
13+
const styleUri = webview.asWebviewUri(
14+
vscode.Uri.joinPath(extensionUri, "media", "styles.css")
15+
);
16+
const nonce = getNonce();
17+
18+
return `<!DOCTYPE html>
19+
<html lang="en">
20+
<head>
21+
<meta charset="UTF-8">
22+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
23+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${
24+
webview.cspSource
25+
}; script-src 'nonce-${nonce}' ${webview.cspSource} 'unsafe-inline';">
26+
<title>Konveyor</title>
27+
<link rel="stylesheet" href="${styleUri}">
28+
</head>
29+
<body>
30+
<div id="root"></div>
31+
<script nonce="${nonce}">
32+
const vscode = acquireVsCodeApi();
33+
window.addEventListener('load', function() {
34+
vscode.postMessage({ command: 'startup', isFullScreen: ${isFullScreen} });
35+
console.log('HTML started up. Full screen:', ${isFullScreen});
36+
});
37+
</script>
38+
${`<script nonce="${nonce}" src="${scriptUri}"></script>`}
39+
</body>
40+
</html>
41+
`;
42+
}

0 commit comments

Comments
 (0)