Skip to content

Commit

Permalink
feat: Suggest quoted entities for MongoDB (#280)
Browse files Browse the repository at this point in the history
* feat: support collections suggestion

* feat: support user suggestions

* refactor: rename variables

* feat: suggest collections for indexInformation method
  • Loading branch information
viladimiru authored Jan 27, 2025
1 parent 71acf78 commit 4cc7db0
Show file tree
Hide file tree
Showing 12 changed files with 1,598 additions and 1,443 deletions.

Large diffs are not rendered by default.

2,911 changes: 1,478 additions & 1,433 deletions src/autocomplete/databases/mongo/generated/MongoParser.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { DatabaseIndexInformationArgument1Context } from "./MongoParser.js";
import { DatabaseIndexInformationArgument2Context } from "./MongoParser.js";
import { DatabaseRemoveUserMethodContext } from "./MongoParser.js";
import { DatabaseRemoveUserArgument1Context } from "./MongoParser.js";
import { QuotedUsernameContext } from "./MongoParser.js";
import { DatabaseRemoveUserArgument2Context } from "./MongoParser.js";
import { DatabaseCreateIndexMethodContext } from "./MongoParser.js";
import { DatabaseCreateIndexArgument3Context } from "./MongoParser.js";
Expand Down Expand Up @@ -425,6 +426,12 @@ export class MongoParserVisitor<Result> extends AbstractParseTreeVisitor<Result>
* @return the visitor result
*/
visitDatabaseRemoveUserArgument1?: (ctx: DatabaseRemoveUserArgument1Context) => Result;
/**
* Visit a parse tree produced by `MongoParser.quotedUsername`.
* @param ctx the parse tree
* @return the visitor result
*/
visitQuotedUsername?: (ctx: QuotedUsernameContext) => Result;
/**
* Visit a parse tree produced by `MongoParser.databaseRemoveUserArgument2`.
* @param ctx the parse tree
Expand Down
10 changes: 7 additions & 3 deletions src/autocomplete/databases/mongo/grammar/MongoParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ databaseIndexInformationMethod
;

databaseIndexInformationArgument1
: STRING
: quotedCollectionName
;

databaseIndexInformationArgument2
Expand All @@ -183,6 +183,10 @@ databaseRemoveUserMethod
;

databaseRemoveUserArgument1
: quotedUsername
;

quotedUsername
: STRING
;

Expand Down Expand Up @@ -225,7 +229,7 @@ databaseDropCollectionMethod
;

databaseDropCollectionArgument1
: STRING
: quotedCollectionName
;

databaseDropCollectionArgument2
Expand All @@ -239,7 +243,7 @@ databaseRenameCollectionMethod
;

databaseRenameCollectionArgument1
: STRING
: quotedCollectionName
;

databaseRenameCollectionArgument2
Expand Down
2 changes: 2 additions & 0 deletions src/autocomplete/databases/mongo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
export * from './mongo-extract-commands';

export interface MongoAutocompleteResult extends SqlAutocompleteResult {
suggestQuotedCollections?: boolean;
suggestCollections?: boolean;
suggestQuotedUsers?: boolean;
}

export function parseMongoQueryWithoutCursor(
Expand Down
22 changes: 20 additions & 2 deletions src/autocomplete/databases/mongo/mongo-autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ function getIgnoredTokens(): number[] {

const ignoredTokens = new Set(getIgnoredTokens());

const rulesToVisit = new Set([MongoParser.RULE_collectionName]);
const rulesToVisit = new Set([
MongoParser.RULE_collectionName,
MongoParser.RULE_quotedCollectionName,
MongoParser.RULE_quotedUsername,
]);

function processVisitedRules(
rules: c3.CandidatesCollection['rules'],
cursorTokenIndex: number,
): ProcessVisitedRulesResult<MongoAutocompleteResult> {
let suggestQuotedCollections;
let suggestCollections;
let suggestQuotedUsers;

for (const [ruleId, rule] of rules) {
if (!isStartingToWriteRule(cursorTokenIndex, rule)) {
Expand All @@ -50,10 +56,22 @@ function processVisitedRules(
suggestCollections = true;
break;
}
case MongoParser.RULE_quotedCollectionName: {
suggestQuotedCollections = true;
break;
}
case MongoParser.RULE_quotedUsername: {
suggestQuotedUsers = true;
break;
}
}
}

return {suggestCollections};
return {
suggestQuotedCollections,
suggestCollections,
suggestQuotedUsers,
};
}

export function getParseTree(parser: MongoParser): ParseTree {
Expand Down
8 changes: 8 additions & 0 deletions src/autocomplete/databases/mongo/tests/admin/admin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ test('should not report errors on extended admin statements', () => {
expect(autocompleteResult.errors).toHaveLength(0);
});

test('should suggest users on first removeUser argument', () => {
const autocompleteResult = parseMongoQueryWithCursor(`
db.admin().removeUser(|
`);

expect(autocompleteResult.suggestQuotedUsers).toEqual(true);
});

test('should suggest keywords after db.admin().', () => {
const autocompleteResult = parseMongoQueryWithCursor('db.admin().|');

Expand Down
18 changes: 18 additions & 0 deletions src/autocomplete/databases/mongo/tests/db/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ test('should not report errors on multiple statements', () => {
expect(autocompleteResult.errors).toHaveLength(0);
});

test('should suggest collections without quotes after db', () => {
const autocompleteResult = parseMongoQueryWithCursor(`
db.|
`);

expect(autocompleteResult.suggestCollections).toEqual(true);
expect(autocompleteResult.suggestQuotedCollections).toBeUndefined();
});

test('should suggest collections in collection method', () => {
const autocompleteResult = parseMongoQueryWithCursor(`
db.collection(|
`);

expect(autocompleteResult.suggestCollections).toBeUndefined();
expect(autocompleteResult.suggestQuotedCollections).toEqual(true);
});

test('should not report errors on three statements', () => {
const autocompleteResult = parseMongoQueryWithoutCursor(`
db.test_collection.find();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {Command, extractMongoCommandsFromQuery, parseMongoQueryWithoutCursor} from '../..';
import {
Command,
extractMongoCommandsFromQuery,
parseMongoQueryWithCursor,
parseMongoQueryWithoutCursor,
} from '../..';

test('should not report errors on dropCollection statement', () => {
const autocompleteResult = parseMongoQueryWithoutCursor(`
Expand All @@ -21,6 +26,14 @@ test('should not report errors on extended dropCollection statement', () => {
expect(autocompleteResult.errors).toHaveLength(0);
});

test('should suggest collections on first dropCollection argument', () => {
const autocompleteResult = parseMongoQueryWithCursor(`
db.dropCollection(|
`);

expect(autocompleteResult.suggestQuotedCollections).toEqual(true);
});

test('should extract dropCollection commands properly', () => {
const result = extractMongoCommandsFromQuery(`
db.dropCollection('test_collection_name');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {Command, extractMongoCommandsFromQuery, parseMongoQueryWithoutCursor} from '../..';
import {
Command,
extractMongoCommandsFromQuery,
parseMongoQueryWithCursor,
parseMongoQueryWithoutCursor,
} from '../..';

test('should not report errors on indexInformation statement', () => {
const autocompleteResult = parseMongoQueryWithoutCursor(`
Expand Down Expand Up @@ -34,6 +39,14 @@ test('should not report errors on extended indexInformation statement', () => {
expect(autocompleteResult.errors).toHaveLength(0);
});

test('should suggest quoted collections on first argument of indexInformation method in database context', () => {
const autocompleteResult = parseMongoQueryWithCursor(`
db.indexInformation(|
`);

expect(autocompleteResult.suggestQuotedCollections).toEqual(true);
});

test('should extract indexInformation commands properly', () => {
const result = extractMongoCommandsFromQuery(`
db.test_collection.indexInformation();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {Command, extractMongoCommandsFromQuery, parseMongoQueryWithoutCursor} from '../..';
import {
Command,
extractMongoCommandsFromQuery,
parseMongoQueryWithCursor,
parseMongoQueryWithoutCursor,
} from '../..';

test('should not report errors on removeUser statement', () => {
const autocompleteResult = parseMongoQueryWithoutCursor(`
Expand All @@ -21,6 +26,14 @@ test('should not report errors on extended removeUser statement', () => {
expect(autocompleteResult.errors).toHaveLength(0);
});

test('should suggest users on first removeUser argument', () => {
const autocompleteResult = parseMongoQueryWithCursor(`
db.removeUser(|
`);

expect(autocompleteResult.suggestQuotedUsers).toEqual(true);
});

test('should extract removeUser commands properly', () => {
const result = extractMongoCommandsFromQuery(`
db.removeUser('test_user');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {Command, extractMongoCommandsFromQuery, parseMongoQueryWithoutCursor} from '../..';
import {
Command,
extractMongoCommandsFromQuery,
parseMongoQueryWithCursor,
parseMongoQueryWithoutCursor,
} from '../..';

test('should not report errors on renameCollection statement', () => {
const autocompleteResult = parseMongoQueryWithoutCursor(`
Expand All @@ -8,6 +13,14 @@ test('should not report errors on renameCollection statement', () => {
expect(autocompleteResult.errors).toHaveLength(0);
});

test('should suggest collections on first renameCollection argument', () => {
const autocompleteResult = parseMongoQueryWithCursor(`
db.renameCollection(|
`);

expect(autocompleteResult.suggestQuotedCollections).toEqual(true);
});

test('should not report errors on extended renameCollection statement', () => {
const autocompleteResult = parseMongoQueryWithoutCursor(`
db.renameCollection(
Expand Down

0 comments on commit 4cc7db0

Please sign in to comment.