Skip to content

Commit

Permalink
chore: playground will also autocomplete collection names for default…
Browse files Browse the repository at this point in the history
… connected db
  • Loading branch information
himanshusinghs committed Dec 5, 2023
1 parent cd1db8a commit 6ff553f
Show file tree
Hide file tree
Showing 4 changed files with 804 additions and 489 deletions.
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;
}
}
}
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;
}

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.
* @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
);
});
});
});
});
Loading

0 comments on commit 6ff553f

Please sign in to comment.