Skip to content

Commit db87e39

Browse files
committed
add preset settings
1 parent 172bc53 commit db87e39

File tree

9 files changed

+167
-77
lines changed

9 files changed

+167
-77
lines changed

.vscode/settings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@
1616
"out": true
1717
},
1818
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
19-
"typescript.tsc.autoDetect": "off"
19+
"typescript.tsc.autoDetect": "off",
20+
"mdb.presetSavedConnections": [
21+
{
22+
"name": "Local Test",
23+
"connectionString": "mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000"
24+
}
25+
]
2026
}

package.json

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@
320320
"command": "mdb.copyConnectionString",
321321
"title": "Copy Connection String"
322322
},
323+
{
324+
"command": "mdb.openWorkspaceSettingsFile",
325+
"title": "Open Workspace Settings"
326+
},
323327
{
324328
"command": "mdb.renameConnection",
325329
"title": "Rename Connection..."
@@ -499,32 +503,32 @@
499503
"view/item/context": [
500504
{
501505
"command": "mdb.addDatabase",
502-
"when": "view == mongoDBConnectionExplorer && viewItem == connectedConnectionTreeItem && mdb.isAtlasStreams == false",
506+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem) && mdb.isAtlasStreams == false",
503507
"group": "inline"
504508
},
505509
{
506510
"command": "mdb.addDatabase",
507-
"when": "view == mongoDBConnectionExplorer && viewItem == connectedConnectionTreeItem && mdb.isAtlasStreams == false",
511+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem) && mdb.isAtlasStreams == false",
508512
"group": "1@1"
509513
},
510514
{
511515
"command": "mdb.addStreamProcessor",
512-
"when": "view == mongoDBConnectionExplorer && viewItem == connectedConnectionTreeItem && mdb.isAtlasStreams == true",
516+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem) && mdb.isAtlasStreams == true",
513517
"group": "inline"
514518
},
515519
{
516520
"command": "mdb.addStreamProcessor",
517-
"when": "view == mongoDBConnectionExplorer && viewItem == connectedConnectionTreeItem && mdb.isAtlasStreams == true",
521+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem) && mdb.isAtlasStreams == true",
518522
"group": "1@1"
519523
},
520524
{
521525
"command": "mdb.refreshConnection",
522-
"when": "view == mongoDBConnectionExplorer && viewItem == connectedConnectionTreeItem",
526+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem)",
523527
"group": "1@2"
524528
},
525529
{
526530
"command": "mdb.treeViewOpenMongoDBShell",
527-
"when": "view == mongoDBConnectionExplorer && viewItem == connectedConnectionTreeItem",
531+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem)",
528532
"group": "2@1"
529533
},
530534
{
@@ -537,14 +541,19 @@
537541
"when": "view == mongoDBConnectionExplorer && viewItem == connectedConnectionTreeItem",
538542
"group": "3@2"
539543
},
544+
{
545+
"command": "mdb.openWorkspaceSettingsFile",
546+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedImmutableConnectionTreeItem)",
547+
"group": "3@2"
548+
},
540549
{
541550
"command": "mdb.copyConnectionString",
542-
"when": "view == mongoDBConnectionExplorer && viewItem == connectedConnectionTreeItem",
551+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem)",
543552
"group": "4@1"
544553
},
545554
{
546555
"command": "mdb.disconnectFromConnectionTreeItem",
547-
"when": "view == mongoDBConnectionExplorer && viewItem == connectedConnectionTreeItem",
556+
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedConnectionTreeItem || viewItem == connectedImmutableConnectionTreeItem)",
548557
"group": "5@1"
549558
},
550559
{
@@ -559,7 +568,7 @@
559568
},
560569
{
561570
"command": "mdb.connectToConnectionTreeItem",
562-
"when": "view == mongoDBConnectionExplorer && viewItem == disconnectedConnectionTreeItem",
571+
"when": "view == mongoDBConnectionExplorer && (viewItem == disconnectedConnectionTreeItem || viewItem == disconnectedImmutableConnectionTreeItem)",
563572
"group": "1@1"
564573
},
565574
{
@@ -572,9 +581,14 @@
572581
"when": "view == mongoDBConnectionExplorer && viewItem == disconnectedConnectionTreeItem",
573582
"group": "2@2"
574583
},
584+
{
585+
"command": "mdb.openWorkspaceSettingsFile",
586+
"when": "view == mongoDBConnectionExplorer && (viewItem == disconnectedImmutableConnectionTreeItem)",
587+
"group": "2@2"
588+
},
575589
{
576590
"command": "mdb.copyConnectionString",
577-
"when": "view == mongoDBConnectionExplorer && viewItem == disconnectedConnectionTreeItem",
591+
"when": "view == mongoDBConnectionExplorer && (viewItem == disconnectedConnectionTreeItem || viewItem == disconnectedImmutableConnectionTreeItem)",
578592
"group": "3@1"
579593
},
580594
{
@@ -1171,6 +1185,28 @@
11711185
"type": "string",
11721186
"default": "",
11731187
"description": "Specify a shell command that is run to start the browser for authenticating with the OIDC identity provider for the server connection. Leave this empty for default browser."
1188+
},
1189+
"mdb.presetSavedConnections": {
1190+
"scope": "window",
1191+
"type": "array",
1192+
"description": "Defines preset saved connections.",
1193+
"items": {
1194+
"type": "object",
1195+
"properties": {
1196+
"name": {
1197+
"type": "string",
1198+
"description": "Name of the connection."
1199+
},
1200+
"connectionString": {
1201+
"type": "string",
1202+
"description": "Connection string. Do not store sensitive credentials here."
1203+
}
1204+
},
1205+
"required": [
1206+
"name",
1207+
"connectionString"
1208+
]
1209+
}
11741210
}
11751211
}
11761212
},

src/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +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',
4748
MDB_REMOVE_CONNECTION_TREE_VIEW = 'mdb.treeItemRemoveConnection',
4849
MDB_RENAME_CONNECTION = 'mdb.renameConnection',
4950
MDB_ADD_DATABASE = 'mdb.addDatabase',

src/explorer/connectionTreeItem.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ import { getImagesPath } from '../extensionConstants';
1212
import type TreeItemParent from './treeItemParentInterface';
1313
import StreamProcessorTreeItem from './streamProcessorTreeItem';
1414

15-
export enum ConnectionItemContextValues {
16-
disconnected = 'disconnectedConnectionTreeItem',
17-
connected = 'connectedConnectionTreeItem',
18-
}
15+
export type ConnectionItemContextValue = `${'disconnected' | 'connected'}${
16+
| ''
17+
| 'Immutable'}ConnectionTreeItem`;
1918

2019
function getIconPath(isActiveConnection: boolean): {
2120
light: string;
@@ -39,7 +38,7 @@ export default class ConnectionTreeItem
3938
extends vscode.TreeItem
4039
implements TreeItemParent, vscode.TreeDataProvider<ConnectionTreeItem>
4140
{
42-
contextValue = ConnectionItemContextValues.disconnected;
41+
contextValue: ConnectionItemContextValue = 'disconnectedConnectionTreeItem';
4342

4443
private _childrenCache: {
4544
[key: string]: DatabaseTreeItem | StreamProcessorTreeItem;
@@ -50,6 +49,7 @@ export default class ConnectionTreeItem
5049
connectionId: string;
5150

5251
isExpanded: boolean;
52+
isMutable: boolean;
5353

5454
constructor({
5555
connectionId,
@@ -58,6 +58,7 @@ export default class ConnectionTreeItem
5858
connectionController,
5959
cacheIsUpToDate,
6060
childrenCache,
61+
isMutable,
6162
}: {
6263
connectionId: string;
6364
collapsibleState: vscode.TreeItemCollapsibleState;
@@ -67,21 +68,24 @@ export default class ConnectionTreeItem
6768
childrenCache: {
6869
[key: string]: DatabaseTreeItem | StreamProcessorTreeItem;
6970
}; // Existing cache.
71+
isMutable: boolean;
7072
}) {
7173
super(
7274
connectionController.getSavedConnectionName(connectionId),
7375
collapsibleState
7476
);
7577

76-
if (
78+
const isConnected =
7779
connectionController.getActiveConnectionId() === connectionId &&
7880
!connectionController.isDisconnecting() &&
79-
!connectionController.isConnecting()
80-
) {
81-
this.contextValue = ConnectionItemContextValues.connected;
82-
}
81+
!connectionController.isConnecting();
82+
83+
this.contextValue = `${isConnected ? 'connected' : 'disconnected'}${
84+
isMutable ? '' : 'Immutable'
85+
}ConnectionTreeItem`;
8386

8487
this.connectionId = connectionId;
88+
this.isMutable = isMutable;
8589
this._connectionController = connectionController;
8690
this.isExpanded = isExpanded;
8791
this._childrenCache = childrenCache;

src/explorer/explorerTreeController.ts

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DOCUMENT_ITEM } from './documentTreeItem';
88
import { DOCUMENT_LIST_ITEM, CollectionTypes } from './documentListTreeItem';
99
import EXTENSION_COMMANDS from '../commands';
1010
import { sortTreeItemsByLabel } from './treeItemUtils';
11+
import type { LoadedConnection } from '../storage/connectionStorage';
1112

1213
const log = createLogger('explorer tree controller');
1314

@@ -130,6 +131,46 @@ export default class ExplorerTreeController
130131
return element;
131132
}
132133

134+
private _getConnectionExpandedState(connection: LoadedConnection): {
135+
collapsibleState: vscode.TreeItemCollapsibleState;
136+
isExpanded: boolean;
137+
} {
138+
const pastConnectionTreeItems = this._connectionTreeItems;
139+
const isActiveConnection =
140+
connection.id === this._connectionController.getActiveConnectionId();
141+
const isBeingConnectedTo =
142+
this._connectionController.isConnecting() &&
143+
connection.id === this._connectionController.getConnectingConnectionId();
144+
145+
let collapsibleState = isActiveConnection
146+
? vscode.TreeItemCollapsibleState.Expanded
147+
: vscode.TreeItemCollapsibleState.Collapsed;
148+
149+
if (
150+
pastConnectionTreeItems[connection.id] &&
151+
!pastConnectionTreeItems[connection.id].isExpanded
152+
) {
153+
// Connection was manually collapsed while being active.
154+
collapsibleState = vscode.TreeItemCollapsibleState.Collapsed;
155+
}
156+
if (isActiveConnection && this._connectionController.isDisconnecting()) {
157+
// Don't show a collapsable state when the connection is being disconnected from.
158+
collapsibleState = vscode.TreeItemCollapsibleState.None;
159+
}
160+
if (isBeingConnectedTo) {
161+
// Don't show a collapsable state when the connection is being connected to.
162+
collapsibleState = vscode.TreeItemCollapsibleState.None;
163+
}
164+
return {
165+
collapsibleState,
166+
// Set expanded when we're connecting to a connection so that it
167+
// expands when it's connected.
168+
isExpanded:
169+
isBeingConnectedTo ||
170+
collapsibleState === vscode.TreeItemCollapsibleState.Expanded,
171+
};
172+
}
173+
133174
getChildren(element?: any): Thenable<any[]> {
134175
// When no element is present we are at the root.
135176
if (!element) {
@@ -139,45 +180,14 @@ export default class ExplorerTreeController
139180

140181
// Create new connection tree items, using cached children wherever possible.
141182
connections.forEach((connection) => {
142-
const isActiveConnection =
143-
connection.id === this._connectionController.getActiveConnectionId();
144-
const isBeingConnectedTo =
145-
this._connectionController.isConnecting() &&
146-
connection.id ===
147-
this._connectionController.getConnectingConnectionId();
148-
149-
let connectionExpandedState = isActiveConnection
150-
? vscode.TreeItemCollapsibleState.Expanded
151-
: vscode.TreeItemCollapsibleState.Collapsed;
152-
153-
if (
154-
pastConnectionTreeItems[connection.id] &&
155-
!pastConnectionTreeItems[connection.id].isExpanded
156-
) {
157-
// Connection was manually collapsed while being active.
158-
connectionExpandedState = vscode.TreeItemCollapsibleState.Collapsed;
159-
}
160-
if (
161-
isActiveConnection &&
162-
this._connectionController.isDisconnecting()
163-
) {
164-
// Don't show a collapsable state when the connection is being disconnected from.
165-
connectionExpandedState = vscode.TreeItemCollapsibleState.None;
166-
}
167-
if (isBeingConnectedTo) {
168-
// Don't show a collapsable state when the connection is being connected to.
169-
connectionExpandedState = vscode.TreeItemCollapsibleState.None;
170-
}
183+
const { collapsibleState, isExpanded } =
184+
this._getConnectionExpandedState(connection);
171185

172186
this._connectionTreeItems[connection.id] = new ConnectionTreeItem({
173187
connectionId: connection.id,
174-
collapsibleState: connectionExpandedState,
175-
// Set expanded when we're connecting to a connection so that it
176-
// expands when it's connected.
177-
isExpanded:
178-
isBeingConnectedTo ||
179-
connectionExpandedState ===
180-
vscode.TreeItemCollapsibleState.Expanded,
188+
collapsibleState,
189+
isExpanded,
190+
isMutable: connection.isMutable ?? true,
181191
connectionController: this._connectionController,
182192
cacheIsUpToDate: pastConnectionTreeItems[connection.id]
183193
? pastConnectionTreeItems[connection.id].cacheIsUpToDate

src/mdbExtensionController.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,16 @@ export default class MDBExtensionController implements vscode.Disposable {
480480
return true;
481481
}
482482
);
483+
this.registerCommand(
484+
EXTENSION_COMMANDS.MDB_OPEN_WORKSPACE_SETTINGS_FILE,
485+
async () => {
486+
await vscode.commands.executeCommand(
487+
'workbench.action.openWorkspaceSettingsFile'
488+
);
489+
490+
return true;
491+
}
492+
);
483493
this.registerCommand(
484494
EXTENSION_COMMANDS.MDB_COPY_CONNECTION_STRING,
485495
async (element: ConnectionTreeItem): Promise<boolean> => {

0 commit comments

Comments
 (0)