Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(playground): codelens for active connection will inform about default connected database VSCODE-316 #621

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1122,4 +1122,17 @@ export default class ConnectionController {
selectedQuickPickItem.data.connectionId
);
}

getActiveConnectionDefaultDB(): string | null {
try {
const connectionString = new ConnectionString(
this.getActiveConnectionString()
);
return connectionString.pathname !== '/'
? connectionString.pathname.substring(1)
: null;
} catch {
return null;
}
}
}
6 changes: 5 additions & 1 deletion src/editors/activeConnectionCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ 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 defaultDB =
this._connectionController.getActiveConnectionDefaultDB();
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
38 changes: 33 additions & 5 deletions src/language/mongoDBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
MarkupContent,
Diagnostic,
} from 'vscode-languageserver/node';
import ConnectionString from 'mongodb-connection-string-url';
import { CliServiceProvider } from '@mongosh/service-provider-server';
import type { Document } from '@mongosh/service-provider-core';
import { getFilteredCompletions } from '@mongodb-js/mongodb-constants';
Expand All @@ -35,7 +36,7 @@ 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';
Expand Down Expand Up @@ -96,6 +97,16 @@ export default class MongoDBService {
return this._currentConnectionOptions;
}

get defaultConnectedDB(): string | null {
if (!this.connectionString) {
return null;
}
const connectionString = new ConnectionString(this.connectionString);
return connectionString.pathname !== '/'
? connectionString.pathname.substring(1)
: null;
Anemy marked this conversation as resolved.
Show resolved Hide resolved
}

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

/**
* @param state The state returned from Visitor.
himanshusinghs marked this conversation as resolved.
Show resolved Hide resolved
* @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 {
if (state.databaseName === null && this.defaultConnectedDB !== null) {
return {
...state,
databaseName: this.defaultConnectedDB,
};
}
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 +500,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 +946,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
40 changes: 40 additions & 0 deletions src/test/suite/connectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
TEST_DATABASE_URI_USER,
TEST_USER_USERNAME,
TEST_USER_PASSWORD,
TEST_DB_NAME,
} from './dbTestHelper';
import KeytarStub from './keytarStub';
import { ext } from '../../extensionConstants';
Expand Down Expand Up @@ -1852,4 +1853,43 @@ suite('Connection Controller Test Suite', function () {
]);
});
});

suite('getActiveConnectionDefaultDB', () => {
suite('when connected to a connection and a default database', () => {
test('it should return the default connected database', async () => {
const succesfullyConnected =
await testConnectionController.addNewConnectionStringAndConnect(
`${TEST_DATABASE_URI}/${TEST_DB_NAME}`
);
assert.strictEqual(succesfullyConnected, true);
assert.strictEqual(
testConnectionController.getActiveConnectionDefaultDB(),
TEST_DB_NAME
);
});
});

suite('when connected to a connection without a default database', () => {
test('it should return the null', async () => {
const succesfullyConnected =
await testConnectionController.addNewConnectionStringAndConnect(
`${TEST_DATABASE_URI}`
);
assert.strictEqual(succesfullyConnected, true);
assert.strictEqual(
testConnectionController.getActiveConnectionDefaultDB(),
null
);
});
});

suite('when not connected to a connection', () => {
test('it should return the null', () => {
assert.strictEqual(
testConnectionController.getActiveConnectionDefaultDB(),
null
);
});
});
});
});
19 changes: 19 additions & 0 deletions src/test/suite/editors/activeConnectionCodeLensProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,25 @@ 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,
'getActiveConnectionDefaultDB',
sandbox.fake.returns('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
Loading