Skip to content

Commit

Permalink
Merge pull request #318 from KxSystems/KXI-46682
Browse files Browse the repository at this point in the history
KXI-46682: Datasources and Workbooks remain when .kx folder is deleted
  • Loading branch information
ecmel authored May 23, 2024
2 parents 0e8f0ab + c5a5047 commit f9f1f47
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 17 deletions.
62 changes: 47 additions & 15 deletions src/commands/workspaceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,10 @@ import { runQuery } from "./serverCommand";
import { ExecutionTypes } from "../models/execution";
import { importOldDsFiles, oldFilesExists } from "../utils/dataSource";
import { offerConnectAction } from "../utils/core";
import Path from "path";

const connectionService = new ConnectionManagementService();

/* istanbul ignore next */
function updateViews() {
ext.dataSourceTreeProvider.reload();
ext.scratchpadTreeProvider.reload();
}

/* istanbul ignore next */
function setRealActiveTextEditor(editor?: TextEditor | undefined) {
if (editor) {
Expand Down Expand Up @@ -196,12 +191,22 @@ export async function pickConnection(uri: Uri) {
return picked;
}

function isPython(uri: Uri) {
return uri.path.endsWith(".py");
/* istanbul ignore next */
function isPython(uri: Uri | undefined) {
return uri && uri.path.endsWith(".py");
}

function isScratchpad(uri: Uri | undefined) {
return uri && (uri.path.endsWith(".kdb.q") || uri.path.endsWith(".kdb.py"));
}

function isScratchpad(uri: Uri) {
return uri.path.endsWith(".kdb.q") || uri.path.endsWith(".kdb.py");
function isDataSource(uri: Uri | undefined) {
return uri && uri.path.endsWith(".kdb.json");
}

/* istanbul ignore next */
function isKxFolder(uri: Uri | undefined) {
return uri && Path.basename(uri.path) === ".kx";
}

/* istanbul ignore next */
Expand Down Expand Up @@ -261,6 +266,14 @@ export async function runActiveEditor(type?: ExecutionTypes) {
}
}

function update(uri: Uri) {
if (isDataSource(uri)) {
ext.dataSourceTreeProvider.reload();
} else if (isScratchpad(uri)) {
ext.scratchpadTreeProvider.reload();
}
}

export class ConnectionLensProvider implements CodeLensProvider {
provideCodeLenses(document: TextDocument): ProviderResult<CodeLens[]> {
const server = getServerForUri(document.uri);
Expand Down Expand Up @@ -294,11 +307,30 @@ export function connectWorkspaceCommands() {
};

const watcher = workspace.createFileSystemWatcher("**/*.kdb.{json,q,py}");
watcher.onDidDelete((uri) =>
setServerForUri(uri, undefined).then(() => updateViews()),
);
watcher.onDidCreate(updateViews);
workspace.onDidChangeWorkspaceFolders(updateViews);
watcher.onDidCreate(update);
watcher.onDidDelete(update);
/* istanbul ignore next */
workspace.onDidDeleteFiles((event) => {
for (const uri of event.files) {
if (isKxFolder(uri)) {
ext.dataSourceTreeProvider.reload();
ext.scratchpadTreeProvider.reload();
break;
}
}
});
/* istanbul ignore next */
workspace.onDidRenameFiles(async (event) => {
for (const { oldUri, newUri } of event.files) {
await setServerForUri(newUri, getServerForUri(oldUri));
await setServerForUri(oldUri, undefined);
}
});
/* istanbul ignore next */
workspace.onDidChangeWorkspaceFolders(() => {
ext.dataSourceTreeProvider.reload();
ext.scratchpadTreeProvider.reload();
});
window.onDidChangeActiveTextEditor(activeEditorChanged);
activeEditorChanged(window.activeTextEditor);
}
Expand Down
7 changes: 7 additions & 0 deletions src/services/dataSourceEditorProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ export class DataSourceEditorProvider implements CustomTextEditorProvider {
});
};

/* istanbul ignore next */
workspace.onDidChangeConfiguration((event) => {
if ((event.affectsConfiguration("kdb.connectionMap"), document)) {
updateWebview();
}
});

const changeDocumentSubscription = workspace.onDidChangeTextDocument(
(event) => {
if (event.document.uri.toString() === document.uri.toString()) {
Expand Down
6 changes: 5 additions & 1 deletion src/services/workspaceTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
} from "vscode";
import Path from "path";
import { getWorkspaceIconsState } from "../utils/core";
import { getConnectionForUri } from "../commands/workspaceCommand";
import {
getConnectionForUri,
setServerForUri,
} from "../commands/workspaceCommand";
import { ext } from "../extensionVariables";

export class WorkspaceTreeProvider implements TreeDataProvider<FileTreeItem> {
Expand Down Expand Up @@ -165,6 +168,7 @@ export async function addWorkspaceFile(
});

await workspace.openTextDocument(uri);
await setServerForUri(uri, undefined);
return uri;
}
} else {
Expand Down
22 changes: 21 additions & 1 deletion test/suite/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { ConnectionManagementService } from "../../src/services/connectionManage
import { InsightsConnection } from "../../src/classes/insightsConnection";
import * as workspaceCommand from "../../src/commands/workspaceCommand";
import { MetaObject } from "../../src/models/meta";
import { WorkspaceTreeProvider } from "../../src/services/workspaceTreeProvider";

describe("dataSourceCommand", () => {
afterEach(() => {
Expand Down Expand Up @@ -1778,8 +1779,27 @@ describe("workspaceCommand", () => {
sinon.restore();
});
describe("connectWorkspaceCommands", () => {
it("should connect listeners", () => {
it("should update views on delete and create", () => {
let cb1, cb2, dsTree, wbTree;
sinon.stub(vscode.workspace, "createFileSystemWatcher").value(() => ({
onDidCreate: (cb) => (cb1 = cb),
onDidDelete: (cb) => (cb2 = cb),
}));
ext.dataSourceTreeProvider = <WorkspaceTreeProvider>{
reload() {
dsTree = true;
},
};
ext.scratchpadTreeProvider = <WorkspaceTreeProvider>{
reload() {
wbTree = true;
},
};
workspaceCommand.connectWorkspaceCommands();
cb1(vscode.Uri.file("test.kdb.json"));
assert.strictEqual(dsTree, true);
cb2(vscode.Uri.file("test.kdb.q"));
assert.strictEqual(wbTree, true);
});
});
describe("activateConnectionForServer", () => {
Expand Down

0 comments on commit f9f1f47

Please sign in to comment.