Skip to content

Commit bf9cd64

Browse files
committed
feat(tree-explorer): automatically derive workspace or global settings
Now when one clicks edit preset connections, we'll automatically redirect to the correct settings.json (workspace or global).
1 parent d2d58e6 commit bf9cd64

File tree

9 files changed

+80
-54
lines changed

9 files changed

+80
-54
lines changed

package.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@
321321
"title": "Copy Connection String"
322322
},
323323
{
324-
"command": "mdb.openWorkspaceSettingsFile",
325-
"title": "Open Workspace Settings"
324+
"command": "mdb.editPresetConnections",
325+
"title": "Edit Preset Connections..."
326326
},
327327
{
328328
"command": "mdb.renameConnection",
@@ -503,32 +503,32 @@
503503
"view/item/context": [
504504
{
505505
"command": "mdb.addDatabase",
506-
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem) && mdb.isAtlasStreams == false",
506+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedPresetConnectionTreeItem) && mdb.isAtlasStreams == false",
507507
"group": "inline"
508508
},
509509
{
510510
"command": "mdb.addDatabase",
511-
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem) && mdb.isAtlasStreams == false",
511+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedPresetConnectionTreeItem) && mdb.isAtlasStreams == false",
512512
"group": "1@1"
513513
},
514514
{
515515
"command": "mdb.addStreamProcessor",
516-
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem) && mdb.isAtlasStreams == true",
516+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedPresetConnectionTreeItem) && mdb.isAtlasStreams == true",
517517
"group": "inline"
518518
},
519519
{
520520
"command": "mdb.addStreamProcessor",
521-
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem) && mdb.isAtlasStreams == true",
521+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedPresetConnectionTreeItem) && mdb.isAtlasStreams == true",
522522
"group": "1@1"
523523
},
524524
{
525525
"command": "mdb.refreshConnection",
526-
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem)",
526+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedPresetConnectionTreeItem)",
527527
"group": "1@2"
528528
},
529529
{
530530
"command": "mdb.treeViewOpenMongoDBShell",
531-
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem)",
531+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedPresetConnectionTreeItem)",
532532
"group": "2@1"
533533
},
534534
{
@@ -542,18 +542,18 @@
542542
"group": "3@2"
543543
},
544544
{
545-
"command": "mdb.openWorkspaceSettingsFile",
546-
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedImmutableConnectionTreeItem)",
545+
"command": "mdb.editPresetConnections",
546+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedPresetConnectionTreeItem)",
547547
"group": "3@2"
548548
},
549549
{
550550
"command": "mdb.copyConnectionString",
551-
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem)",
551+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedPresetConnectionTreeItem)",
552552
"group": "4@1"
553553
},
554554
{
555555
"command": "mdb.disconnectFromConnectionTreeItem",
556-
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem)",
556+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedPresetConnectionTreeItem)",
557557
"group": "5@1"
558558
},
559559
{
@@ -568,7 +568,7 @@
568568
},
569569
{
570570
"command": "mdb.connectToConnectionTreeItem",
571-
"when": "view == mongoDBConnectionExplorer && (viewItem == disconnectedConnectionTreeItem || viewItem == disconnectedImmutableConnectionTreeItem)",
571+
"when": "view == mongoDBConnectionExplorer && (viewItem == disconnectedConnectionTreeItem || viewItem == disconnectedPresetConnectionTreeItem)",
572572
"group": "1@1"
573573
},
574574
{
@@ -582,13 +582,13 @@
582582
"group": "2@2"
583583
},
584584
{
585-
"command": "mdb.openWorkspaceSettingsFile",
586-
"when": "view == mongoDBConnectionExplorer && (viewItem == disconnectedImmutableConnectionTreeItem)",
585+
"command": "mdb.editPresetConnections",
586+
"when": "view == mongoDBConnectionExplorer && (viewItem == disconnectedPresetConnectionTreeItem)",
587587
"group": "2@2"
588588
},
589589
{
590590
"command": "mdb.copyConnectionString",
591-
"when": "view == mongoDBConnectionExplorer && (viewItem == disconnectedConnectionTreeItem || viewItem == disconnectedImmutableConnectionTreeItem)",
591+
"when": "view == mongoDBConnectionExplorer && (viewItem == disconnectedConnectionTreeItem || viewItem == disconnectedPresetConnectionTreeItem)",
592592
"group": "3@1"
593593
},
594594
{

src/commands/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ enum EXTENSION_COMMANDS {
4444
MDB_EDIT_CONNECTION = 'mdb.editConnection',
4545
MDB_REFRESH_CONNECTION = 'mdb.refreshConnection',
4646
MDB_COPY_CONNECTION_STRING = 'mdb.copyConnectionString',
47-
MDB_OPEN_WORKSPACE_SETTINGS_FILE = 'mdb.openWorkspaceSettingsFile',
47+
MDB_EDIT_PRESET_CONNECTIONS = 'mdb.editPresetConnections',
4848
MDB_REMOVE_CONNECTION_TREE_VIEW = 'mdb.treeItemRemoveConnection',
4949
MDB_RENAME_CONNECTION = 'mdb.renameConnection',
5050
MDB_ADD_DATABASE = 'mdb.addDatabase',

src/explorer/connectionTreeItem.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import formatError from '../utils/formatError';
1111
import { getImagesPath } from '../extensionConstants';
1212
import type TreeItemParent from './treeItemParentInterface';
1313
import StreamProcessorTreeItem from './streamProcessorTreeItem';
14+
import type { ConnectionSource } from '../storage/connectionStorage';
1415

1516
export type ConnectionItemContextValue = `${'disconnected' | 'connected'}${
1617
| ''
17-
| 'Immutable'}ConnectionTreeItem`;
18+
| 'Preset'}ConnectionTreeItem`;
1819

1920
function getIconPath(isActiveConnection: boolean): {
2021
light: string;
@@ -49,7 +50,7 @@ export default class ConnectionTreeItem
4950
connectionId: string;
5051

5152
isExpanded: boolean;
52-
isMutable: boolean;
53+
source: ConnectionSource;
5354

5455
constructor({
5556
connectionId,
@@ -58,7 +59,7 @@ export default class ConnectionTreeItem
5859
connectionController,
5960
cacheIsUpToDate,
6061
childrenCache,
61-
isMutable,
62+
source,
6263
}: {
6364
connectionId: string;
6465
collapsibleState: vscode.TreeItemCollapsibleState;
@@ -68,7 +69,7 @@ export default class ConnectionTreeItem
6869
childrenCache: {
6970
[key: string]: DatabaseTreeItem | StreamProcessorTreeItem;
7071
}; // Existing cache.
71-
isMutable: boolean;
72+
source: ConnectionSource;
7273
}) {
7374
super(
7475
connectionController.getSavedConnectionName(connectionId),
@@ -81,11 +82,11 @@ export default class ConnectionTreeItem
8182
!connectionController.isConnecting();
8283

8384
this.contextValue = `${isConnected ? 'connected' : 'disconnected'}${
84-
isMutable ? '' : 'Immutable'
85+
source === 'user' ? '' : 'Preset'
8586
}ConnectionTreeItem`;
8687

8788
this.connectionId = connectionId;
88-
this.isMutable = isMutable;
89+
this.source = source;
8990
this._connectionController = connectionController;
9091
this.isExpanded = isExpanded;
9192
this._childrenCache = childrenCache;

src/explorer/explorerTreeController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export default class ExplorerTreeController
187187
connectionId: connection.id,
188188
collapsibleState,
189189
isExpanded,
190-
isMutable: connection.isMutable ?? true,
190+
source: connection.source ?? 'user',
191191
connectionController: this._connectionController,
192192
cacheIsUpToDate: pastConnectionTreeItems[connection.id]
193193
? pastConnectionTreeItems[connection.id].cacheIsUpToDate

src/mdbExtensionController.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,17 @@ export default class MDBExtensionController implements vscode.Disposable {
491491
}
492492
);
493493
this.registerCommand(
494-
EXTENSION_COMMANDS.MDB_OPEN_WORKSPACE_SETTINGS_FILE,
495-
async () => {
496-
await vscode.commands.executeCommand(
497-
'workbench.action.openWorkspaceSettingsFile'
498-
);
499-
494+
EXTENSION_COMMANDS.MDB_EDIT_PRESET_CONNECTIONS,
495+
async (element: ConnectionTreeItem) => {
496+
if (element.source === 'workspaceSettings') {
497+
await vscode.commands.executeCommand(
498+
'workbench.action.openWorkspaceSettingsFile'
499+
);
500+
} else if (element.source === 'globalSettings') {
501+
await vscode.commands.executeCommand(
502+
'workbench.action.openSettingsJson'
503+
);
504+
}
500505
return true;
501506
}
502507
);

src/storage/connectionStorage.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import { v4 as uuidv4 } from 'uuid';
1919

2020
const log = createLogger('connection storage');
2121

22+
export type ConnectionSource = 'globalSettings' | 'workspaceSettings' | 'user';
2223
export interface StoreConnectionInfo {
2324
id: string; // Connection model id or a new uuid.
2425
name: string; // Possibly user given name, not unique.
2526
storageLocation: StorageLocation;
2627
secretStorageLocation?: SecretStorageLocationType;
2728
connectionOptions?: ConnectionOptions;
28-
isMutable?: boolean;
29+
source?: ConnectionSource;
2930
lastUsed?: Date; // Date and time when the connection was last used, i.e. connected with.
3031
}
3132

@@ -34,6 +35,10 @@ export type PresetSavedConnection = {
3435
connectionString: string;
3536
};
3637

38+
export type PresetSavedConnectionWithSource = PresetSavedConnection & {
39+
source: ConnectionSource;
40+
};
41+
3742
type StoreConnectionInfoWithConnectionOptions = StoreConnectionInfo &
3843
Required<Pick<StoreConnectionInfo, 'connectionOptions'>>;
3944

@@ -64,7 +69,7 @@ export class ConnectionStorage {
6469
return {
6570
id: connectionId,
6671
name,
67-
isMutable: true,
72+
source: 'user',
6873
storageLocation: this.getPreferredStorageLocationFromConfiguration(),
6974
secretStorageLocation: 'vscode.SecretStorage',
7075
connectionOptions: connectionOptions,
@@ -176,20 +181,33 @@ export class ConnectionStorage {
176181
}
177182

178183
_loadPresetSavedConnections(): LoadedConnection[] {
179-
const presetSavedConnections: PresetSavedConnection[] | undefined =
180-
vscode.workspace.getConfiguration('mdb').get('presetSavedConnections');
184+
const configuration = vscode.workspace.getConfiguration('mdb');
185+
const presetSavedConnectionsInfo = configuration.inspect<
186+
PresetSavedConnection[]
187+
>('presetSavedConnections');
181188

182-
if (!presetSavedConnections) {
189+
if (!presetSavedConnectionsInfo) {
183190
return [];
184191
}
185192

186-
return presetSavedConnections.map((presetConnection) => ({
193+
const combinedPresetSavedConnections: PresetSavedConnectionWithSource[] = [
194+
...(presetSavedConnectionsInfo?.workspaceValue ?? []).map((preset) => ({
195+
...preset,
196+
source: 'workspaceSettings' as const,
197+
})),
198+
...(presetSavedConnectionsInfo?.globalValue ?? []).map((preset) => ({
199+
...preset,
200+
source: 'globalSettings' as const,
201+
})),
202+
];
203+
204+
return combinedPresetSavedConnections.map((presetConnection) => ({
187205
id: uuidv4(),
188206
name: presetConnection.name,
189207
connectionOptions: {
190208
connectionString: presetConnection.connectionString,
191209
},
192-
isMutable: false,
210+
source: presetConnection.source,
193211
storageLocation: StorageLocation.NONE,
194212
secretStorageLocation: SecretStorageLocation.SecretStorage,
195213
}));

src/test/suite/explorer/connectionTreeItem.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function getTestConnectionTreeItem(): ConnectionTreeItem {
2121
mdbTestExtension.testExtensionController._connectionController,
2222
cacheIsUpToDate: false,
2323
childrenCache: {},
24-
isMutable: true,
24+
source: 'user',
2525
});
2626
}
2727

src/test/suite/mdbExtensionController.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function getTestConnectionTreeItem(
4242
mdbTestExtension.testExtensionController._connectionController,
4343
cacheIsUpToDate: false,
4444
childrenCache: {},
45-
isMutable: true,
45+
source: 'user',
4646
...options,
4747
});
4848
}

src/test/suite/storage/connectionStorage.test.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,18 @@ suite('Connection Storage Test Suite', function () {
323323
});
324324

325325
suite('when there are preset connections', () => {
326-
const presetConnections = [
327-
{
328-
name: 'Preset Connection 1',
329-
connectionString: 'mongodb://localhost:27017/',
330-
},
331-
{
332-
name: 'Preset Connection 2',
333-
connectionString: 'mongodb://localhost:27018/',
334-
},
335-
];
326+
const presetConnections = {
327+
workspaceValue: [
328+
{
329+
name: 'Preset Connection 1',
330+
connectionString: 'mongodb://localhost:27017/',
331+
},
332+
{
333+
name: 'Preset Connection 2',
334+
connectionString: 'mongodb://localhost:27018/',
335+
},
336+
],
337+
};
336338

337339
let getConfigurationStub: sinon.SinonStub<
338340
[
@@ -354,7 +356,7 @@ suite('Connection Storage Test Suite', function () {
354356
'getConfiguration'
355357
);
356358
getConfigurationStub.returns({
357-
get: getPresetSavedConnectionsStub,
359+
inspect: getPresetSavedConnectionsStub,
358360
} as any);
359361

360362
getPresetSavedConnectionsStub
@@ -372,7 +374,7 @@ suite('Connection Storage Test Suite', function () {
372374
expect(connection.connectionOptions.connectionString).equals(
373375
presetConnection.connectionString
374376
);
375-
expect(connection.isMutable).equals(false);
377+
expect(connection.source).equals('workspaceSettings');
376378
}
377379
});
378380

@@ -396,14 +398,14 @@ suite('Connection Storage Test Suite', function () {
396398

397399
expect(loadedConnections.length).equals(3);
398400

399-
for (let i = 0; i < presetConnections.length; i++) {
401+
for (let i = 0; i < presetConnections.workspaceValue.length; i++) {
400402
const connection = loadedConnections[i];
401403
const presetConnection = presetConnections[i];
402404
expect(connection.name).equals(presetConnection.name);
403405
expect(connection.connectionOptions.connectionString).equals(
404406
presetConnection.connectionString
405407
);
406-
expect(connection.isMutable).equals(false);
408+
expect(connection.source).equals('workspaceSettings');
407409
}
408410

409411
const savedLoadedConnection = loadedConnections[2];
@@ -412,7 +414,7 @@ suite('Connection Storage Test Suite', function () {
412414
expect(
413415
savedLoadedConnection.connectionOptions.connectionString
414416
).contains(savedConnection.connectionOptions.connectionString);
415-
expect(savedLoadedConnection.isMutable).equals(true);
417+
expect(savedLoadedConnection.source).equals('workspaceSettings');
416418
});
417419
});
418420

0 commit comments

Comments
 (0)