diff --git a/package.json b/package.json index 204eac5f..24b237fc 100644 --- a/package.json +++ b/package.json @@ -229,11 +229,6 @@ "title": "Refresh scratchpads", "icon": "$(refresh)" }, - { - "category": "KX", - "command": "kdb.runScratchpad", - "title": "Run scratchpad" - }, { "category": "KX", "command": "kdb.pickConnection", @@ -395,8 +390,7 @@ "category": "KX", "command": "kdb.terminal.run", "title": "Run q file in a new q instance", - "icon": "$(run)", - "enablement": "(kdb.QHOME || config.kdb.qHomeDirectory)" + "icon": "$(run)" }, { "category": "KX", @@ -406,14 +400,14 @@ { "category": "KX", "command": "kdb.execute.selectedQuery", - "title": "Execute Current Selection" + "title": "Execute Current Selection", + "icon": "$(run)" }, { "category": "KX", "command": "kdb.execute.fileQuery", "title": "Execute Entire File", - "icon": "$(run)", - "enablement": "kdb.connected" + "icon": "$(run)" }, { "category": "KX", @@ -424,13 +418,7 @@ "category": "KX", "command": "kdb.execute.pythonFileScratchpadQuery", "title": "Execute Entire File in Insights Scratchpad", - "icon": "$(run)", - "enablement": "kdb.insightsConnected" - }, - { - "category": "KX", - "command": "kdb.execute.entireFile", - "title": "Execute Entire File" + "icon": "$(run)" }, { "command": "kdb.scratchpad.reset", @@ -448,31 +436,31 @@ "command": "kdb.execute.selectedQuery", "key": "ctrl+d", "mac": "cmd+d", - "when": "editorLangId == q && kdb.connected" + "when": "editorLangId == q" }, { "command": "kdb.execute.fileQuery", "key": "ctrl+shift+d", "mac": "cmd+shift+d", - "when": "editorLangId == q && kdb.connected" + "when": "editorLangId == q" }, { "command": "kdb.execute.pythonScratchpadQuery", "key": "ctrl+d", "mac": "cmd+d", - "when": "editorLangId == python && kdb.insightsConnected" + "when": "editorLangId == python" }, { "command": "kdb.execute.pythonFileScratchpadQuery", "key": "ctrl+shift+d", "mac": "cmd+shift+d", - "when": "editorLangId == python && kdb.insightsConnected" + "when": "editorLangId == python" }, { "command": "kdb.terminal.run", "key": "ctrl+shift+r", "mac": "cmd+shift+r", - "when": "editorLangId == q && (kdb.QHOME || config.kdb.qHomeDirectory)" + "when": "editorLangId == q" }, { "command": "kdb.scratchpad.reset", @@ -764,51 +752,54 @@ { "command": "kdb.execute.fileQuery", "group": "q@0", - "when": "resourceLangId == q" + "when": "editorLangId == q" }, { - "command": "kdb.terminal.run", + "command": "kdb.execute.selectedQuery", "group": "q@1", - "when": "resourceLangId == q" + "when": "editorLangId == q" + }, + { + "command": "kdb.terminal.run", + "group": "q@2", + "when": "editorLangId == q" }, { "command": "kdb.execute.pythonFileScratchpadQuery", "group": "q@0", - "when": "editorLangId == python && kdb.insightsConnected" + "when": "editorLangId == python" + }, + { + "command": "kdb.execute.pythonScratchpadQuery", + "group": "q@1", + "when": "editorLangId == python" } ], "editor/context": [ { - "command": "kdb.terminal.run", - "when": "resourceLangId == q && (kdb.QHOME || config.kdb.qHomeDirectory)", - "group": "q" + "command": "kdb.execute.fileQuery", + "group": "q@0", + "when": "editorLangId == q" }, { "command": "kdb.execute.selectedQuery", - "group": "q", - "when": "editorLangId == q && kdb.connected" - }, - { - "command": "kdb.execute.fileQuery", - "group": "q", - "when": "editorLangId == q && kdb.connected" + "group": "q@1", + "when": "editorLangId == q" }, { - "command": "kdb.execute.pythonScratchpadQuery", - "group": "q", - "when": "editorLangId == python && kdb.insightsConnected" + "command": "kdb.terminal.run", + "group": "q@2", + "when": "editorLangId == q" }, { "command": "kdb.execute.pythonFileScratchpadQuery", - "group": "q", - "when": "editorLangId == python && kdb.insightsConnected" - } - ], - "explorer/context": [ + "group": "q@0", + "when": "editorLangId == python" + }, { - "command": "kdb.execute.entireFile", - "group": "q", - "when": "(resourceExtname == .q && (kdb.connected || kdb.insightsConnected)) || (resourceExtname == .py && kdb.insightsConnected)" + "command": "kdb.execute.pythonScratchpadQuery", + "group": "q@1", + "when": "editorLangId == python" } ] }, diff --git a/src/commands/scratchpadCommand.ts b/src/commands/scratchpadCommand.ts index febf8e35..8a96d850 100644 --- a/src/commands/scratchpadCommand.ts +++ b/src/commands/scratchpadCommand.ts @@ -170,38 +170,45 @@ export async function pickConnection(uri: Uri) { return picked; } -export async function runScratchpad(uri: Uri) { - let server = getServerForUri(uri); +function isPython(uri: Uri) { + return uri.path.endsWith(".py"); +} - if (!server) { - server = await pickConnection(uri); - } +function isScratchpad(uri: Uri) { + return uri.path.endsWith(".kdb.q") || uri.path.endsWith(".kdb.py"); +} - if (server) { - const connection = await getConnectionForServer(server); - if (connection) { - if (!connectionService.isConnected(connection.label)) { - const action = await window.showWarningMessage( - `${connection.label} is not connected`, - "Connect", - ); - if (action === "Connect") { - await connectionService.connect(connection.label); - await waitForConnection(connection.label); +export async function runScratchpad(type: ExecutionTypes) { + if (ext.activeTextEditor) { + const uri = ext.activeTextEditor.document.uri; + if (isScratchpad(uri)) { + let server = getServerForUri(uri); + if (!server) { + server = await pickConnection(uri); + } + + if (server) { + const connection = await getConnectionForServer(server); + if (connection) { + if (!connectionService.isConnected(connection.label)) { + const action = await window.showWarningMessage( + `${connection.label} is not connected`, + "Connect", + ); + if (action === "Connect") { + await connectionService.connect(connection.label); + await waitForConnection(connection.label); + } else { + return; + } + } + connectionService.setActiveConnection(connection); } else { - return; + window.showErrorMessage(`${server} is not found`); } } - connectionService.setActiveConnection(connection); - - runQuery( - uri.path.endsWith(".py") - ? ExecutionTypes.PythonQueryFile - : ExecutionTypes.QueryFile, - ); - } else { - window.showErrorMessage(`${server} is not found`); } + runQuery(type); } } @@ -210,7 +217,9 @@ export class ConnectionLensProvider implements CodeLensProvider { const server = getServerForUri(document.uri); const top = new Range(0, 0, 0, 0); const runScratchpad = new CodeLens(top, { - command: "kdb.runScratchpad", + command: isPython(document.uri) + ? "kdb.execute.pythonFileScratchpadQuery" + : "kdb.execute.fileQuery", title: server ? `Run on ${server}` : "Run", }); const pickConnection = new CodeLens(top, { diff --git a/src/extension.ts b/src/extension.ts index 4ca2c6ae..6fac234a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -63,7 +63,6 @@ import { removeConnection, rerunQuery, resetScratchPad, - runQuery, } from "./commands/serverCommand"; import { showInstallationDetails } from "./commands/walkthroughCommand"; import { ext } from "./extensionVariables"; @@ -342,37 +341,39 @@ export async function activate(context: ExtensionContext) { checkLocalInstall(); } }), - commands.registerCommand("kdb.execute.selectedQuery", () => { - runQuery(ExecutionTypes.QuerySelection); + commands.registerCommand("kdb.runScratchpad", async () => { + if (ext.activeTextEditor) { + const path = ext.activeTextEditor.document.uri.path; + await runScratchpad( + path.endsWith(".kdb.py") + ? ExecutionTypes.PythonQueryFile + : ExecutionTypes.QuerySelection, + ); + ext.activeConnection?.update(); + } + }), + commands.registerCommand("kdb.execute.selectedQuery", async () => { + await runScratchpad(ExecutionTypes.QuerySelection); ext.activeConnection?.update(); }), - commands.registerCommand("kdb.execute.fileQuery", () => { - runQuery(ExecutionTypes.QueryFile); + commands.registerCommand("kdb.execute.fileQuery", async () => { + await runScratchpad(ExecutionTypes.QueryFile); ext.activeConnection?.update(); }), - commands.registerCommand("kdb.execute.pythonScratchpadQuery", () => { - runQuery(ExecutionTypes.PythonQuerySelection); + commands.registerCommand("kdb.execute.pythonScratchpadQuery", async () => { + await runScratchpad(ExecutionTypes.PythonQuerySelection); ext.activeConnection?.update(); }), commands.registerCommand("kdb.scratchpad.reset", async () => { await resetScratchPad(); }), - commands.registerCommand("kdb.execute.pythonFileScratchpadQuery", () => { - runQuery(ExecutionTypes.PythonQueryFile); - ext.activeConnection?.update(); - }), - // TODO MS REMOVE - commands.registerCommand("kdb.execute.entireFile", async (uri: Uri) => { - if (!uri) { - return; - } - const isPython = uri.fsPath.endsWith(".py"); - if (uri.fsPath.endsWith(".q") || isPython) { - const content = await workspace.fs.readFile(uri); - const query = content.toString(); - await executeQuery(query, undefined, isPython); - } - }), + commands.registerCommand( + "kdb.execute.pythonFileScratchpadQuery", + async () => { + await runScratchpad(ExecutionTypes.PythonQueryFile); + ext.activeConnection?.update(); + }, + ), commands.registerCommand( "kdb.createDataSource", async (item: FileTreeItem) => { @@ -418,12 +419,6 @@ export async function activate(context: ExtensionContext) { await pickConnection(editor.document.uri); } }), - commands.registerCommand("kdb.runScratchpad", async () => { - const editor = ext.activeTextEditor; - if (editor) { - await runScratchpad(editor.document.uri); - } - }), DataSourceEditorProvider.register(context),