Skip to content

Commit d50581b

Browse files
feat: provide shell methods completions after getCollection VSCODE-390 (#498)
1 parent 0a27eea commit d50581b

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/language/visitor.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ export class Visitor {
575575
}
576576
}
577577

578-
_checkIsCollectionSymbol(node: babel.types.MemberExpression): void {
578+
_checkIsCollectionMemberExpression(node: babel.types.MemberExpression): void {
579579
if (
580580
node.object.type === 'MemberExpression' &&
581581
node.object.object.type === 'Identifier' &&
@@ -586,4 +586,24 @@ export class Visitor {
586586
this._state.isCollectionSymbol = true;
587587
}
588588
}
589+
590+
_checkIsCollectionCallExpression(node: babel.types.MemberExpression): void {
591+
if (
592+
node.object.type === 'CallExpression' &&
593+
node.object.callee.type === 'MemberExpression' &&
594+
node.object.callee.object.type === 'Identifier' &&
595+
node.object.callee.object.name === 'db' &&
596+
node.object.callee.property.type === 'Identifier' &&
597+
node.object.callee.property.name === 'getCollection' &&
598+
node.property.type === 'Identifier' &&
599+
node.property.name.includes(PLACEHOLDER)
600+
) {
601+
this._state.isCollectionSymbol = true;
602+
}
603+
}
604+
605+
_checkIsCollectionSymbol(node: babel.types.MemberExpression): void {
606+
this._checkIsCollectionMemberExpression(node);
607+
this._checkIsCollectionCallExpression(node);
608+
}
589609
}

src/test/suite/language/mongoDBService.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ suite('MongoDBService Test Suite', () => {
151151
expect(completion).to.have.property('kind', CompletionItemKind.Method);
152152
});
153153

154-
test('provide shell collection methods completion if collection name is computed property', async () => {
154+
test('provide shell collection methods completion for a collection name in a bracket notation', async () => {
155155
const result = await testMongoDBService.provideCompletionItems(
156156
['use("test");', 'db["test"].'].join('\n'),
157157
{ line: 1, character: 11 }
@@ -163,6 +163,18 @@ suite('MongoDBService Test Suite', () => {
163163
expect(completion).to.have.property('kind', CompletionItemKind.Method);
164164
});
165165

166+
test('provide shell collection methods completion for a collection name in getCollection', async () => {
167+
const result = await testMongoDBService.provideCompletionItems(
168+
['use("test");', 'db.getCollection("test").'].join('\n'),
169+
{ line: 1, character: 41 }
170+
);
171+
const completion = result.find(
172+
(item: CompletionItem) => item.label === 'find'
173+
);
174+
175+
expect(completion).to.have.property('kind', CompletionItemKind.Method);
176+
});
177+
166178
test('provide shell collection methods completion if single quotes', async () => {
167179
const result = await testMongoDBService.provideCompletionItems(
168180
["use('test');", "db['test']."].join('\n'),

0 commit comments

Comments
 (0)