From 902421085252adbfd118936ecf812ac51d0d32d0 Mon Sep 17 00:00:00 2001 From: Jitendra Gundaniya Date: Tue, 20 Aug 2024 11:45:17 +0100 Subject: [PATCH 1/3] Kedro viz server removed Signed-off-by: Jitendra Gundaniya --- bundled/tool/lsp_server.py | 17 +++++++++++++++++ package.json | 1 - src/common/commands.ts | 17 ++++++++++++++++- src/common/utilities.ts | 8 -------- src/extension.ts | 23 +++++++++------------- src/webview/goToDefinition.ts | 3 ++- src/webview/vizServer.ts | 36 ----------------------------------- src/webview/vizWebView.ts | 8 -------- webview/src/App.jsx | 2 +- 9 files changed, 45 insertions(+), 70 deletions(-) delete mode 100644 src/webview/vizServer.ts diff --git a/bundled/tool/lsp_server.py b/bundled/tool/lsp_server.py index 4543f11..af2b476 100644 --- a/bundled/tool/lsp_server.py +++ b/bundled/tool/lsp_server.py @@ -83,6 +83,8 @@ ) from pygls.server import LanguageServer +from kedro_viz.server import load_and_populate_data +from kedro_viz.api.rest.responses import get_json_data class KedroLanguageServer(LanguageServer): """Store Kedro-specific information in the language server.""" @@ -553,6 +555,21 @@ def definition_from_flowchart(ls, word): result = definition(LSP_SERVER, params=None, word=word) return result +@LSP_SERVER.command("kedro.getProjectData") +def get_porject_data_from_viz(lsClient): + """Get project data from kedro viz + """ + data = None + try: + load_and_populate_data(Path.cwd()) + data = get_json_data() + return data + except Exception as e: + print(f"An error occurred: {e}") + finally: + print("Execution completed.") + return data + ### End of kedro-lsp diff --git a/package.json b/package.json index db15a1c..6c57823 100644 --- a/package.json +++ b/package.json @@ -174,7 +174,6 @@ "dependencies": { "@vscode/python-extension": "^1.0.5", "fs-extra": "^11.2.0", - "node-fetch": "^3.3.2", "vscode-languageclient": "^8.1.0" }, "devDependencies": { diff --git a/src/common/commands.ts b/src/common/commands.ts index bdb4539..3c6c755 100644 --- a/src/common/commands.ts +++ b/src/common/commands.ts @@ -83,7 +83,6 @@ export async function executeServerDefinitionCommand(lsClient: LanguageClient | const result: any[] | undefined = await vscode.commands.executeCommand( commandName /* if your command accepts arguments you can pass them here */, target, - ); logger.info(`${commandName} result: ${JSON.stringify(result, undefined, 2)}`); if (result && result.length > 0) { @@ -97,3 +96,19 @@ export async function executeServerDefinitionCommand(lsClient: LanguageClient | }); } } + +export async function executeGetProjectDataCommand(lsClient: LanguageClient | undefined) { + if (!lsClient || lsClient.state !== State.Running) { + await vscode.window.showErrorMessage('There is no language server running.'); + return; + } + if (!lsClient.initializeResult) { + await vscode.window.showErrorMessage('The Language Server fail to initialise.'); + return; + } + + const commandName = 'kedro.getProjectData'; + logger.info(`executing command: '${commandName}'`); + const result = await vscode.commands.executeCommand(commandName); + return result; +} diff --git a/src/common/utilities.ts b/src/common/utilities.ts index a3f92b7..ec6b6c4 100644 --- a/src/common/utilities.ts +++ b/src/common/utilities.ts @@ -6,7 +6,6 @@ import * as path from 'path'; import { LogLevel, Uri, WorkspaceFolder } from 'vscode'; import { Trace } from 'vscode-jsonrpc/node'; import { getWorkspaceFolders } from './vscodeapi'; -import fetch from 'node-fetch'; import KedroVizPanel from '../webview/vizWebView'; function logLevelToTrace(logLevel: LogLevel): Trace { @@ -67,10 +66,3 @@ export async function getProjectRoot(): Promise { return rootWorkspace; } } - -export async function fetchAndUpdateProjectData(): Promise { - fetch('http://127.0.0.1:3131/api/main') - .then((response: { text: () => any }) => response.text()) - .then((data: string) => KedroVizPanel.currentPanel?.updateData(data)) - .catch((err: { message: string }) => console.error('Error: ' + err.message)); -} diff --git a/src/extension.ts b/src/extension.ts index fd47b75..bf7c333 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,6 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -import { selectEnvironment, executeServerCommand, executeServerDefinitionCommand } from './common/commands'; +import { + selectEnvironment, + executeServerCommand, + executeServerDefinitionCommand, + executeGetProjectDataCommand, +} from './common/commands'; import * as vscode from 'vscode'; import { LanguageClient } from 'vscode-languageclient/node'; import { registerLogger, traceError, traceLog, traceVerbose } from './common/log/logging'; @@ -23,10 +28,8 @@ import { loadServerDefaults } from './common/setup'; import { getLSClientTraceLevel, getProjectRoot } from './common/utilities'; import { createOutputChannel, onDidChangeConfiguration, registerCommand } from './common/vscodeapi'; import KedroVizPanel from './webview/vizWebView'; -import { runKedroVizServer } from './webview/vizServer'; let lsClient: LanguageClient | undefined; -let kedroVizProcess: any; export async function activate(context: vscode.ExtensionContext): Promise { // This is required to get server name and module. This should be @@ -58,8 +61,10 @@ export async function activate(context: vscode.ExtensionContext): Promise ); context.subscriptions.push( - vscode.commands.registerCommand('kedro.runKedroViz', () => { + vscode.commands.registerCommand('kedro.runKedroViz', async () => { KedroVizPanel.createOrShow(context.extensionUri, lsClient); + const projectData = await executeGetProjectDataCommand(lsClient); + KedroVizPanel.currentPanel?.updateData(projectData); }), ); @@ -88,12 +93,6 @@ export async function activate(context: vscode.ExtensionContext): Promise console.log(interpreterDetails); console.log('===============DEBUG============'); - // Start kedro viz server - if (kedroVizProcess) { - process.kill(-kedroVizProcess.pid); - } - kedroVizProcess = await runKedroVizServer(); - if (interpreterDetails.path) { traceVerbose(`Using interpreter from Python extension: ${interpreterDetails.path.join(' ')}`); lsClient = await restartServer(serverId, serverName, outputChannel, lsClient, env); @@ -168,8 +167,4 @@ export async function deactivate(): Promise { if (lsClient) { await lsClient.stop(); } - if (kedroVizProcess) { - process.kill(-kedroVizProcess.pid); - kedroVizProcess = null; // Reset the reference after killing the process - } } diff --git a/src/webview/goToDefinition.ts b/src/webview/goToDefinition.ts index db94844..35069a2 100644 --- a/src/webview/goToDefinition.ts +++ b/src/webview/goToDefinition.ts @@ -11,7 +11,8 @@ export async function goToDefinition(message: Message) { let filePattern = '**/*.yml'; if (message.type === 'task') { - filePattern = '**/*.py'; + // Looking only in pipelines folders + filePattern = '**/pipelines/**/*.py'; } const files = await vscode.workspace.findFiles(filePattern); diff --git a/src/webview/vizServer.ts b/src/webview/vizServer.ts deleted file mode 100644 index 189b616..0000000 --- a/src/webview/vizServer.ts +++ /dev/null @@ -1,36 +0,0 @@ -import * as vscode from 'vscode'; -import { getWorkspaceFolders } from '../common/vscodeapi'; -import { fetchAndUpdateProjectData } from '../common/utilities'; -import { getInterpreterDetails } from '../common/python'; -const { spawn } = require('child_process'); - -async function getActivePythonInterpreter(): Promise { - const interpreterDetails = await getInterpreterDetails(); - if (interpreterDetails?.path) { - return interpreterDetails.path[0]; - } - vscode.window.showErrorMessage('Python interpreter could not be resolved.'); - return undefined; -} - -export async function runKedroVizServer() { - const pythonPath = await getActivePythonInterpreter(); - if (!pythonPath) return; - - const workspaceFolders = getWorkspaceFolders(); - if (!workspaceFolders.length) { - vscode.window.showErrorMessage('No workspace folder is open.'); - return; - } - const workspacePath = workspaceFolders[0].uri.fsPath; - - const command = ['-m', 'kedro', 'viz', '--no-browser', '-a', '--port=3131']; - const kedroVizProcess = spawn(pythonPath, command, { cwd: workspacePath, detached: true }); - - kedroVizProcess.stdout.on('data', (data: any) => { - console.log('Kedro Viz: ', data.toString()); - fetchAndUpdateProjectData(); - }); - - return kedroVizProcess; -} diff --git a/src/webview/vizWebView.ts b/src/webview/vizWebView.ts index b214d2f..af0206d 100644 --- a/src/webview/vizWebView.ts +++ b/src/webview/vizWebView.ts @@ -1,7 +1,5 @@ import * as vscode from 'vscode'; -import fetch from 'node-fetch'; import { goToDefinition } from './goToDefinition'; -import { fetchAndUpdateProjectData } from '../common/utilities'; import { LanguageClient } from 'vscode-languageclient/node'; import { executeServerDefinitionCommand } from '../common/commands'; @@ -37,7 +35,6 @@ export default class KedroVizPanel { }); KedroVizPanel.currentPanel = new KedroVizPanel(panel, extensionUri, lsClient); - fetchAndUpdateProjectData(); } public static revive(panel: vscode.WebviewPanel, extensionUri: vscode.Uri) { @@ -84,11 +81,6 @@ export default class KedroVizPanel { ); } - public updateTheme() { - // Send a message to the webview. - this._panel.webview.postMessage({ command: 'updateTheme', theme: 'light' }); - } - public updateData(data: any) { // Send a message to the webview. this._panel.webview.postMessage({ command: 'updateData', data }); diff --git a/webview/src/App.jsx b/webview/src/App.jsx index 39a07ac..891981b 100644 --- a/webview/src/App.jsx +++ b/webview/src/App.jsx @@ -14,7 +14,7 @@ function App() { const message = event.data; switch (message.command) { case "updateData": - setData(JSON.parse(message.data)); + setData(message.data); setLoading(false); break; default: From b0e10a4dfd9806665a696bf0432beed6dcb1088a Mon Sep 17 00:00:00 2001 From: Jitendra Gundaniya Date: Tue, 20 Aug 2024 13:55:54 +0100 Subject: [PATCH 2/3] get_json_data renamed to get_kedro_project_json_data Signed-off-by: Jitendra Gundaniya --- bundled/tool/lsp_server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bundled/tool/lsp_server.py b/bundled/tool/lsp_server.py index af2b476..8bbbf33 100644 --- a/bundled/tool/lsp_server.py +++ b/bundled/tool/lsp_server.py @@ -84,7 +84,7 @@ from pygls.server import LanguageServer from kedro_viz.server import load_and_populate_data -from kedro_viz.api.rest.responses import get_json_data +from kedro_viz.api.rest.responses import get_kedro_project_json_data class KedroLanguageServer(LanguageServer): """Store Kedro-specific information in the language server.""" @@ -562,7 +562,7 @@ def get_porject_data_from_viz(lsClient): data = None try: load_and_populate_data(Path.cwd()) - data = get_json_data() + data = get_kedro_project_json_data() return data except Exception as e: print(f"An error occurred: {e}") From a295fa6e32e8302c21a58476589c898a322d46d0 Mon Sep 17 00:00:00 2001 From: Nok Lam Chan Date: Wed, 21 Aug 2024 18:38:04 +0100 Subject: [PATCH 3/3] fix reload lsp without disconnect webview Signed-off-by: Nok Lam Chan --- src/extension.ts | 17 +++++++++++------ src/webview/vizWebView.ts | 9 +++++---- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index bf7c333..5f64863 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -31,6 +31,11 @@ import KedroVizPanel from './webview/vizWebView'; let lsClient: LanguageClient | undefined; +export async function getlsClient() { + return lsClient; +} + + export async function activate(context: vscode.ExtensionContext): Promise { // This is required to get server name and module. This should be // the first thing that we do in this extension. @@ -62,7 +67,7 @@ export async function activate(context: vscode.ExtensionContext): Promise context.subscriptions.push( vscode.commands.registerCommand('kedro.runKedroViz', async () => { - KedroVizPanel.createOrShow(context.extensionUri, lsClient); + KedroVizPanel.createOrShow(context.extensionUri); const projectData = await executeGetProjectDataCommand(lsClient); KedroVizPanel.currentPanel?.updateData(projectData); }), @@ -101,9 +106,9 @@ export async function activate(context: vscode.ExtensionContext): Promise traceError( 'Python interpreter missing:\r\n' + - '[Option 1] Select python interpreter using the ms-python.python.\r\n' + - `[Option 2] Set an interpreter using "${serverId}.interpreter" setting.\r\n` + - 'Please use Python 3.8 or greater.', + '[Option 1] Select python interpreter using the ms-python.python.\r\n' + + `[Option 2] Set an interpreter using "${serverId}.interpreter" setting.\r\n` + + 'Please use Python 3.8 or greater.', ); }; @@ -145,8 +150,8 @@ export async function activate(context: vscode.ExtensionContext): Promise registerCommand('pygls.server.executeCommand', async () => { await executeServerCommand(lsClient); }), - registerCommand('kedro.sendDefinitionRequest', async () => { - await executeServerDefinitionCommand(lsClient); + registerCommand('kedro.sendDefinitionRequest', async (word) => { + await executeServerDefinitionCommand(lsClient, word); }), ); diff --git a/src/webview/vizWebView.ts b/src/webview/vizWebView.ts index af0206d..84f5409 100644 --- a/src/webview/vizWebView.ts +++ b/src/webview/vizWebView.ts @@ -19,7 +19,7 @@ export default class KedroVizPanel { private readonly _extensionUri: vscode.Uri; private _disposables: vscode.Disposable[] = []; - public static createOrShow(extensionUri: vscode.Uri, lsClient: LanguageClient | undefined) { + public static createOrShow(extensionUri: vscode.Uri) { const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined; // If we already have a panel, show it. @@ -34,14 +34,14 @@ export default class KedroVizPanel { retainContextWhenHidden: true, }); - KedroVizPanel.currentPanel = new KedroVizPanel(panel, extensionUri, lsClient); + KedroVizPanel.currentPanel = new KedroVizPanel(panel, extensionUri); } public static revive(panel: vscode.WebviewPanel, extensionUri: vscode.Uri) { KedroVizPanel.currentPanel = new KedroVizPanel(panel, extensionUri); } - private constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri, lsClient?: LanguageClient) { + private constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri) { this._panel = panel; this._extensionUri = extensionUri; @@ -69,7 +69,8 @@ export default class KedroVizPanel { switch (message.command) { case 'fromWebview': if (message.node.type === 'data') { - await executeServerDefinitionCommand(lsClient, message.node.text); + // await executeServerDefinitionCommand(getlsClient(), message.node.text); + await vscode.commands.executeCommand('kedro.sendDefinitionRequest', message.node.text) } else { await goToDefinition(message.node); }