diff --git a/src/commands/workspaceCommand.ts b/src/commands/workspaceCommand.ts index 609ce94d..879014fe 100644 --- a/src/commands/workspaceCommand.ts +++ b/src/commands/workspaceCommand.ts @@ -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) { @@ -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 */ @@ -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 { const server = getServerForUri(document.uri); @@ -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); } diff --git a/src/services/dataSourceEditorProvider.ts b/src/services/dataSourceEditorProvider.ts index 90caa2d8..a98b86ae 100644 --- a/src/services/dataSourceEditorProvider.ts +++ b/src/services/dataSourceEditorProvider.ts @@ -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()) { diff --git a/src/services/workspaceTreeProvider.ts b/src/services/workspaceTreeProvider.ts index 422b6726..208e97cf 100644 --- a/src/services/workspaceTreeProvider.ts +++ b/src/services/workspaceTreeProvider.ts @@ -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 { @@ -165,6 +168,7 @@ export async function addWorkspaceFile( }); await workspace.openTextDocument(uri); + await setServerForUri(uri, undefined); return uri; } } else { diff --git a/test/suite/commands.test.ts b/test/suite/commands.test.ts index 900b7cfe..92a692cd 100644 --- a/test/suite/commands.test.ts +++ b/test/suite/commands.test.ts @@ -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(() => { @@ -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 = { + reload() { + dsTree = true; + }, + }; + ext.scratchpadTreeProvider = { + 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", () => {