From fc97cae7b761ea6a4088c155a1ee8538a79db754 Mon Sep 17 00:00:00 2001 From: ecmel Date: Tue, 6 Aug 2024 13:30:10 +0300 Subject: [PATCH] fixes files cannot be executed from entity tree --- package.json | 12 ++++++++++++ src/extension.ts | 11 +++++++++-- src/utils/workspace.ts | 18 ++++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 065330d2..d9ffa42f 100644 --- a/package.json +++ b/package.json @@ -849,6 +849,18 @@ "group": "q@1", "when": "editorLangId == python && (resourceFilename =~ /.kdb.py/ || kdb.insightsConnected)" } + ], + "explorer/context": [ + { + "command": "kdb.execute.fileQuery", + "group": "q", + "when": "resourceExtname == .q" + }, + { + "command": "kdb.execute.pythonFileScratchpadQuery", + "group": "q", + "when": "resourceExtname == .py" + } ] }, "customEditors": [ diff --git a/src/extension.ts b/src/extension.ts index e0bcd150..392b355b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -112,6 +112,7 @@ import { renameLabel, setLabelColor, } from "./utils/connLabel"; +import { activateTextDocument } from "./utils/workspace"; let client: LanguageClient; @@ -390,7 +391,10 @@ export async function activate(context: ExtensionContext) { commands.registerCommand("kdb.execute.selectedQuery", async () => { await runActiveEditor(ExecutionTypes.QuerySelection); }), - commands.registerCommand("kdb.execute.fileQuery", async () => { + commands.registerCommand("kdb.execute.fileQuery", async (item) => { + if (item instanceof Uri) { + await activateTextDocument(item); + } await runActiveEditor(ExecutionTypes.QueryFile); }), commands.registerCommand("kdb.execute.pythonScratchpadQuery", async () => { @@ -402,7 +406,10 @@ export async function activate(context: ExtensionContext) { // }), commands.registerCommand( "kdb.execute.pythonFileScratchpadQuery", - async () => { + async (item) => { + if (item instanceof Uri) { + await activateTextDocument(item); + } await runActiveEditor(ExecutionTypes.PythonQueryFile); }, ), diff --git a/src/utils/workspace.ts b/src/utils/workspace.ts index da344976..8ca042e1 100644 --- a/src/utils/workspace.ts +++ b/src/utils/workspace.ts @@ -11,10 +11,10 @@ * specific language governing permissions and limitations under the License. */ -import { workspace } from "vscode"; +import { Uri, window, workspace } from "vscode"; export function getWorkspaceRoot( - ignoreException: boolean = false + ignoreException: boolean = false, ): string | undefined { const workspaceRoot = workspace.workspaceFolders && workspace.workspaceFolders[0].uri.fsPath; @@ -32,3 +32,17 @@ export function isWorkspaceOpen(): boolean { workspace.workspaceFolders && workspace.workspaceFolders[0].uri.fsPath ); } + +export async function activateTextDocument(item: Uri) { + if (item.fsPath) { + let document = workspace.textDocuments.find( + (doc) => doc.uri.fsPath === item.fsPath, + ); + if (!document) { + document = await workspace.openTextDocument(item); + } + if (document) { + await window.showTextDocument(document); + } + } +}