Skip to content

Commit f93f111

Browse files
chore(playground): codelens for active connection will inform about default connected database VSCODE-316 (#621)
* chore: codelens for active connection also inform about default db in playground * chore: playground will also autocomplete collection names for default connected db * chore: added tests for playground run default context * chore: refactored duplicate code as a helper function * Update src/language/mongoDBService.ts Co-authored-by: Alena Khineika <[email protected]> --------- Co-authored-by: Alena Khineika <[email protected]>
1 parent 64c7473 commit f93f111

File tree

6 files changed

+945
-490
lines changed

6 files changed

+945
-490
lines changed

src/editors/activeConnectionCodeLensProvider.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { TextEditor } from 'vscode';
33
import EXTENSION_COMMANDS from '../commands';
44
import type ConnectionController from '../connectionController';
55
import { isPlayground } from '../utils/playground';
6+
import { getDBFromConnectionString } from '../utils/connection-string-db';
67

78
export default class ActiveConnectionCodeLensProvider
89
implements vscode.CodeLensProvider
@@ -48,7 +49,14 @@ export default class ActiveConnectionCodeLensProvider
4849
if (this._connectionController.isConnecting()) {
4950
message = 'Connecting...';
5051
} else if (this._connectionController.getActiveDataService()) {
51-
message = `Currently connected to ${this._connectionController.getActiveConnectionName()}. Click here to change connection.`;
52+
const connectionString =
53+
this._connectionController.getMongoClientConnectionOptions()?.url;
54+
const defaultDB = connectionString
55+
? getDBFromConnectionString(connectionString)
56+
: null;
57+
message = defaultDB
58+
? `Currently connected to ${this._connectionController.getActiveConnectionName()} with default database ${defaultDB}. Click here to change connection.`
59+
: `Currently connected to ${this._connectionController.getActiveConnectionName()}. Click here to change connection.`;
5260
} else {
5361
message = 'Disconnected. Click here to connect.';
5462
}

src/language/mongoDBService.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ import type {
3535
} from '../types/playgroundType';
3636
import type { ClearCompletionsCache } from '../types/completionsCache';
3737
import { Visitor } from './visitor';
38-
import type { CompletionState } from './visitor';
38+
import type { CompletionState, NamespaceState } from './visitor';
3939
import LINKS from '../utils/links';
4040

4141
import DIAGNOSTIC_CODES from './diagnosticCodes';
42+
import { getDBFromConnectionString } from '../utils/connection-string-db';
4243

4344
const PROJECT = '$project';
4445

@@ -445,6 +446,25 @@ export default class MongoDBService {
445446
return /^(?![0-9])[a-zA-Z0-9$_]+$/.test(str);
446447
}
447448

449+
/**
450+
* @param state The state returned from Visitor.
451+
* @returns The state with the default connected database, if available, if
452+
* and only if the state returned from visitor does not already mention a
453+
* database
454+
*/
455+
withDefaultDatabase<T extends NamespaceState | CompletionState>(state: T): T {
456+
const defaultDB = this.connectionString
457+
? getDBFromConnectionString(this.connectionString)
458+
: null;
459+
if (state.databaseName === null && defaultDB !== null) {
460+
return {
461+
...state,
462+
databaseName: defaultDB,
463+
};
464+
}
465+
return state;
466+
}
467+
448468
/**
449469
* Parse code from a playground to identify
450470
* a context in which export to language action is being called.
@@ -473,7 +493,9 @@ export default class MongoDBService {
473493
params: PlaygroundTextAndSelection
474494
): ExportToLanguageNamespace {
475495
try {
476-
const state = this._visitor.parseASTForNamespace(params);
496+
const state = this.withDefaultDatabase(
497+
this._visitor.parseASTForNamespace(params)
498+
);
477499
return {
478500
databaseName: state.databaseName,
479501
collectionName: state.collectionName,
@@ -917,9 +939,8 @@ export default class MongoDBService {
917939
`Provide completion items for a position: ${util.inspect(position)}`
918940
);
919941

920-
const state = this._visitor.parseASTForCompletion(
921-
document?.getText(),
922-
position
942+
const state = this.withDefaultDatabase(
943+
this._visitor.parseASTForCompletion(document?.getText(), position)
923944
);
924945
this._connection.console.log(
925946
`VISITOR completion state: ${util.inspect(state)}`

src/test/suite/editors/activeConnectionCodeLensProvider.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { StatusView } from '../../../views';
1010
import { StorageController } from '../../../storage';
1111
import { ExtensionContextStub } from '../stubs';
1212
import TelemetryService from '../../../telemetry/telemetryService';
13+
import { TEST_DATABASE_URI } from '../dbTestHelper';
1314

1415
const expect = chai.expect;
1516

@@ -100,6 +101,13 @@ suite('Active Connection CodeLens Provider Test Suite', () => {
100101
});
101102

102103
test('show active connection in code lenses', () => {
104+
sandbox.replace(
105+
testConnectionController,
106+
'getMongoClientConnectionOptions',
107+
sandbox.fake.returns({
108+
url: TEST_DATABASE_URI,
109+
})
110+
);
103111
const codeLens = testCodeLensProvider.provideCodeLenses();
104112

105113
expect(codeLens).to.be.an('array');
@@ -113,6 +121,27 @@ suite('Active Connection CodeLens Provider Test Suite', () => {
113121
'mdb.changeActiveConnection'
114122
);
115123
});
124+
125+
test('show active connection and default database in code lenses, when connected to a default database', () => {
126+
sandbox.replace(
127+
testConnectionController,
128+
'getMongoClientConnectionOptions',
129+
sandbox.fake.returns({
130+
url: `${TEST_DATABASE_URI}/fakeDBName`,
131+
})
132+
);
133+
const codeLens = testCodeLensProvider.provideCodeLenses();
134+
expect(codeLens).to.be.an('array');
135+
expect(codeLens.length).to.be.equal(1);
136+
expect(codeLens[0].command?.title).to.be.equal(
137+
'Currently connected to fakeName with default database fakeDBName. Click here to change connection.'
138+
);
139+
expect(codeLens[0].range.start.line).to.be.equal(0);
140+
expect(codeLens[0].range.end.line).to.be.equal(0);
141+
expect(codeLens[0].command?.command).to.be.equal(
142+
'mdb.changeActiveConnection'
143+
);
144+
});
116145
});
117146
});
118147

0 commit comments

Comments
 (0)