Skip to content

Commit

Permalink
chore(playground): codelens for active connection will inform about d…
Browse files Browse the repository at this point in the history
…efault 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]>
  • Loading branch information
himanshusinghs and alenakhineika authored Dec 7, 2023
1 parent 64c7473 commit f93f111
Show file tree
Hide file tree
Showing 6 changed files with 945 additions and 490 deletions.
10 changes: 9 additions & 1 deletion src/editors/activeConnectionCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { TextEditor } from 'vscode';
import EXTENSION_COMMANDS from '../commands';
import type ConnectionController from '../connectionController';
import { isPlayground } from '../utils/playground';
import { getDBFromConnectionString } from '../utils/connection-string-db';

export default class ActiveConnectionCodeLensProvider
implements vscode.CodeLensProvider
Expand Down Expand Up @@ -48,7 +49,14 @@ export default class ActiveConnectionCodeLensProvider
if (this._connectionController.isConnecting()) {
message = 'Connecting...';
} else if (this._connectionController.getActiveDataService()) {
message = `Currently connected to ${this._connectionController.getActiveConnectionName()}. Click here to change connection.`;
const connectionString =
this._connectionController.getMongoClientConnectionOptions()?.url;
const defaultDB = connectionString
? getDBFromConnectionString(connectionString)
: null;
message = defaultDB
? `Currently connected to ${this._connectionController.getActiveConnectionName()} with default database ${defaultDB}. Click here to change connection.`
: `Currently connected to ${this._connectionController.getActiveConnectionName()}. Click here to change connection.`;
} else {
message = 'Disconnected. Click here to connect.';
}
Expand Down
31 changes: 26 additions & 5 deletions src/language/mongoDBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ import type {
} from '../types/playgroundType';
import type { ClearCompletionsCache } from '../types/completionsCache';
import { Visitor } from './visitor';
import type { CompletionState } from './visitor';
import type { CompletionState, NamespaceState } from './visitor';
import LINKS from '../utils/links';

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

const PROJECT = '$project';

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

/**
* @param state The state returned from Visitor.
* @returns The state with the default connected database, if available, if
* and only if the state returned from visitor does not already mention a
* database
*/
withDefaultDatabase<T extends NamespaceState | CompletionState>(state: T): T {
const defaultDB = this.connectionString
? getDBFromConnectionString(this.connectionString)
: null;
if (state.databaseName === null && defaultDB !== null) {
return {
...state,
databaseName: defaultDB,
};
}
return state;
}

/**
* Parse code from a playground to identify
* a context in which export to language action is being called.
Expand Down Expand Up @@ -473,7 +493,9 @@ export default class MongoDBService {
params: PlaygroundTextAndSelection
): ExportToLanguageNamespace {
try {
const state = this._visitor.parseASTForNamespace(params);
const state = this.withDefaultDatabase(
this._visitor.parseASTForNamespace(params)
);
return {
databaseName: state.databaseName,
collectionName: state.collectionName,
Expand Down Expand Up @@ -917,9 +939,8 @@ export default class MongoDBService {
`Provide completion items for a position: ${util.inspect(position)}`
);

const state = this._visitor.parseASTForCompletion(
document?.getText(),
position
const state = this.withDefaultDatabase(
this._visitor.parseASTForCompletion(document?.getText(), position)
);
this._connection.console.log(
`VISITOR completion state: ${util.inspect(state)}`
Expand Down
29 changes: 29 additions & 0 deletions src/test/suite/editors/activeConnectionCodeLensProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { StatusView } from '../../../views';
import { StorageController } from '../../../storage';
import { ExtensionContextStub } from '../stubs';
import TelemetryService from '../../../telemetry/telemetryService';
import { TEST_DATABASE_URI } from '../dbTestHelper';

const expect = chai.expect;

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

test('show active connection in code lenses', () => {
sandbox.replace(
testConnectionController,
'getMongoClientConnectionOptions',
sandbox.fake.returns({
url: TEST_DATABASE_URI,
})
);
const codeLens = testCodeLensProvider.provideCodeLenses();

expect(codeLens).to.be.an('array');
Expand All @@ -113,6 +121,27 @@ suite('Active Connection CodeLens Provider Test Suite', () => {
'mdb.changeActiveConnection'
);
});

test('show active connection and default database in code lenses, when connected to a default database', () => {
sandbox.replace(
testConnectionController,
'getMongoClientConnectionOptions',
sandbox.fake.returns({
url: `${TEST_DATABASE_URI}/fakeDBName`,
})
);
const codeLens = testCodeLensProvider.provideCodeLenses();
expect(codeLens).to.be.an('array');
expect(codeLens.length).to.be.equal(1);
expect(codeLens[0].command?.title).to.be.equal(
'Currently connected to fakeName with default database fakeDBName. Click here to change connection.'
);
expect(codeLens[0].range.start.line).to.be.equal(0);
expect(codeLens[0].range.end.line).to.be.equal(0);
expect(codeLens[0].command?.command).to.be.equal(
'mdb.changeActiveConnection'
);
});
});
});

Expand Down
Loading

0 comments on commit f93f111

Please sign in to comment.