Skip to content

Commit

Permalink
Merge pull request #483 from KxSystems/ee-plot
Browse files Browse the repository at this point in the history
gg plot first phase
  • Loading branch information
ecmel authored Dec 19, 2024
2 parents 3a96804 + 16684d9 commit e28a293
Show file tree
Hide file tree
Showing 18 changed files with 553 additions and 153 deletions.
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,19 @@
"dark": "./resources/dark/datasource.svg",
"light": "./resources/light/datasource.svg"
}
},
{
"id": "kdbplot",
"aliases": [
"kdbplot"
],
"extensions": [
".plot"
],
"icon": {
"dark": "./resources/dark/plot.svg",
"light": "./resources/light/plot.svg"
}
}
],
"grammars": [
Expand Down Expand Up @@ -935,6 +948,16 @@
}
],
"priority": "default"
},
{
"viewType": "kdb.chartEditor",
"displayName": "Chart Viewer",
"selector": [
{
"filenamePattern": "*.plot"
}
],
"priority": "default"
}
]
},
Expand Down
8 changes: 8 additions & 0 deletions resources/dark/plot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 10 additions & 4 deletions resources/evaluate.q
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,21 @@
(`result; ::);
(`errored; 1b);
(`error; err);
(`backtrace; .Q.sbt userCode))
(`backtrace; .Q.sbt userCode);
(`base64; 0b))
}[suffix; prefix]];
if [isLastLine or result`errored;
system "d ", cachedCtx;
: result];
index +: 1];
};
result: evalInContext[ctx; splitExpression stripTrailingSemi wrapLines removeMultilineComments code];
if [(not result `errored) and stringify;
result[`result]: toString result `result];
result: evalInContext[ctx; splitExpression stripTrailingSemi wrapLines removeMultilineComments code];
if[result `errored; :result];
if[type[result[`result]] = 99h;
if[`output in key result[`result];
if[type[result[`result][`output]] = 99h;
if[`bytes in key result[`result][`output];
result[`base64]:1b; result[`result]: .Q.btoa result[`result][`output][`bytes]; :result]]]];
if [stringify; result[`result]: toString result `result];
result
}
8 changes: 8 additions & 0 deletions resources/light/plot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/classes/localConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export class LocalConnection {
isPython?: boolean,
): Promise<any> {
let result;
let base64 = false;
await this.waitForConnection();

if (!this.connection) {
Expand Down Expand Up @@ -202,6 +203,7 @@ export class LocalConnection {
);
} else {
result = res.result === null ? "" : res.result;
base64 = res.base64 || false;
}
});

Expand All @@ -213,6 +215,10 @@ export class LocalConnection {

this.updateGlobal();

if (base64) {
return { base64, result };
}

if (ext.isResultsTabVisible && stringify) {
if (this.isError) {
this.isError = false;
Expand Down
28 changes: 27 additions & 1 deletion src/commands/serverCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ import {
ServerType,
} from "../models/connectionsModels";
import * as fs from "fs";
import { ChartEditorProvider } from "../services/chartEditorProvider";
import {
addWorkspaceFile,
openWith,
setUriContent,
workspaceHas,
} from "../utils/workspace";
import { Plot } from "../models/plot";

export async function addNewConnection(): Promise<void> {
NewConnectionPannel.close();
Expand Down Expand Up @@ -848,7 +856,25 @@ export async function executeQuery(
);
} else {
/* istanbul ignore next */
if (ext.isResultsTabVisible) {
if (results.base64) {
const active = ext.activeTextEditor;
if (active) {
const data = `data:image/png;base64,${results.result}`;
const plot = <Plot>{
charts: [{ data }],
};
const uri = await addWorkspaceFile(
active.document.uri,
"plot",
".plot",
);
if (!workspaceHas(uri)) {
await workspace.openTextDocument(uri);
await openWith(uri, ChartEditorProvider.viewType, ViewColumn.Beside);
}
await setUriContent(uri, JSON.stringify(plot));
}
} else if (ext.isResultsTabVisible) {
writeQueryResultsToView(
results,
query,
Expand Down
86 changes: 42 additions & 44 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ import {
ConfigurationTarget,
EventEmitter,
ExtensionContext,
Range,
TextDocumentContentProvider,
Uri,
WorkspaceEdit,
commands,
extensions,
languages,
Expand Down Expand Up @@ -93,7 +91,6 @@ import { DataSourceEditorProvider } from "./services/dataSourceEditorProvider";
import {
FileTreeItem,
WorkspaceTreeProvider,
addWorkspaceFile,
} from "./services/workspaceTreeProvider";
import {
ConnectionLensProvider,
Expand All @@ -102,6 +99,7 @@ import {
importOldDSFiles,
pickConnection,
runActiveEditor,
setServerForUri,
} from "./commands/workspaceCommand";
import { createDefaultDataSourceFile } from "./models/dataSource";
import { connectBuildTools, lintCommand } from "./commands/buildToolsCommand";
Expand All @@ -116,13 +114,19 @@ import {
renameLabel,
setLabelColor,
} from "./utils/connLabel";
import { activateTextDocument } from "./utils/workspace";
import {
activateTextDocument,
addWorkspaceFile,
openWith,
setUriContent,
} from "./utils/workspace";
import {
InsightDetails,
Insights,
Server,
ServerDetails,
} from "./models/connectionsModels";
import { ChartEditorProvider } from "./services/chartEditorProvider";

let client: LanguageClient;

Expand Down Expand Up @@ -472,26 +476,19 @@ export async function activate(context: ExtensionContext) {
"kdb.createDataSource",
async (item: FileTreeItem) => {
if (hasWorkspaceOrShowOption("adding datasources")) {
const uri = await addWorkspaceFile(item, "datasource", ".kdb.json");

if (uri) {
const edit = new WorkspaceEdit();

edit.replace(
uri,
new Range(0, 0, 1, 0),
JSON.stringify(createDefaultDataSourceFile(), null, 2),
);

workspace.applyEdit(edit);

await commands.executeCommand(
"vscode.openWith",
uri,
DataSourceEditorProvider.viewType,
);
await commands.executeCommand("workbench.action.files.save", uri);
}
const uri = await addWorkspaceFile(
item ? item.resourceUri : undefined,
"datasource",
".kdb.json",
);
await workspace.openTextDocument(uri);
await setUriContent(
uri,
JSON.stringify(createDefaultDataSourceFile(), null, 2),
);
await openWith(uri, DataSourceEditorProvider.viewType);
await commands.executeCommand("workbench.action.files.save", uri);
await setServerForUri(uri, undefined);
}
},
),
Expand All @@ -502,23 +499,31 @@ export async function activate(context: ExtensionContext) {
"kdb.createScratchpad",
async (item: FileTreeItem) => {
if (hasWorkspaceOrShowOption("adding workbooks")) {
const uri = await addWorkspaceFile(item, "workbook", ".kdb.q");
if (uri) {
await window.showTextDocument(uri);
await commands.executeCommand("workbench.action.files.save", uri);
}
const uri = await addWorkspaceFile(
item ? item.resourceUri : undefined,
"workbook",
".kdb.q",
);
await workspace.openTextDocument(uri);
await window.showTextDocument(uri);
await commands.executeCommand("workbench.action.files.save", uri);
await setServerForUri(uri, undefined);
}
},
),
commands.registerCommand(
"kdb.createPythonScratchpad",
async (item: FileTreeItem) => {
if (hasWorkspaceOrShowOption("adding workbooks")) {
const uri = await addWorkspaceFile(item, "workbook", ".kdb.py");
if (uri) {
await window.showTextDocument(uri);
await commands.executeCommand("workbench.action.files.save", uri);
}
const uri = await addWorkspaceFile(
item ? item.resourceUri : undefined,
"workbook",
".kdb.py",
);
await workspace.openTextDocument(uri);
await window.showTextDocument(uri);
await commands.executeCommand("workbench.action.files.save", uri);
await setServerForUri(uri, undefined);
}
},
),
Expand All @@ -534,11 +539,7 @@ export async function activate(context: ExtensionContext) {
commands.registerCommand("kdb.renameFile", async (item: FileTreeItem) => {
if (item && item.resourceUri) {
if (item.resourceUri.path.endsWith(".kdb.json")) {
await commands.executeCommand(
"vscode.openWith",
item.resourceUri,
DataSourceEditorProvider.viewType,
);
await openWith(item.resourceUri, DataSourceEditorProvider.viewType);
} else {
const document = await workspace.openTextDocument(item.resourceUri);
await window.showTextDocument(document);
Expand All @@ -550,11 +551,7 @@ export async function activate(context: ExtensionContext) {
commands.registerCommand("kdb.deleteFile", async (item: FileTreeItem) => {
if (item && item.resourceUri) {
if (item.resourceUri.path.endsWith(".kdb.json")) {
await commands.executeCommand(
"vscode.openWith",
item.resourceUri,
DataSourceEditorProvider.viewType,
);
await openWith(item.resourceUri, DataSourceEditorProvider.viewType);
} else {
const document = await workspace.openTextDocument(item.resourceUri);
await window.showTextDocument(document);
Expand All @@ -565,6 +562,7 @@ export async function activate(context: ExtensionContext) {
}),

DataSourceEditorProvider.register(context),
ChartEditorProvider.register(context),

languages.registerCodeLensProvider(
{ pattern: "**/*.kdb.{q,py}" },
Expand Down
20 changes: 20 additions & 0 deletions src/models/plot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 1998-2023 Kx Systems Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

export interface Chart {
data: string;
}

export interface Plot {
charts: Chart[];
}
1 change: 1 addition & 0 deletions src/models/queryResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type QueryResult = {
text: string;
index: number;
}[];
base64?: boolean;
};

export enum QueryResultType {
Expand Down
Loading

0 comments on commit e28a293

Please sign in to comment.