Skip to content

Commit

Permalink
support for all run commands added for ssratchpads
Browse files Browse the repository at this point in the history
  • Loading branch information
ecmel committed May 1, 2024
1 parent 4faa7bb commit 032bbb0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 103 deletions.
85 changes: 38 additions & 47 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,6 @@
"title": "Refresh scratchpads",
"icon": "$(refresh)"
},
{
"category": "KX",
"command": "kdb.runScratchpad",
"title": "Run scratchpad"
},
{
"category": "KX",
"command": "kdb.pickConnection",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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"
}
]
},
Expand Down
63 changes: 36 additions & 27 deletions src/commands/scratchpadCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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, {
Expand Down
53 changes: 24 additions & 29 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import {
removeConnection,
rerunQuery,
resetScratchPad,
runQuery,
} from "./commands/serverCommand";
import { showInstallationDetails } from "./commands/walkthroughCommand";
import { ext } from "./extensionVariables";
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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),

Expand Down

0 comments on commit 032bbb0

Please sign in to comment.