Skip to content

Commit

Permalink
VSCODE-93: db and collection names completion (#69)
Browse files Browse the repository at this point in the history
* feat: provide db and collection names completion

* test: add tests for snippets, db and collection names completions

* refactor: remove unused code

* test: modify long-running tests

* docs: update read me

* refactor: update function names

* refactor: get fields completion as early as possible

* refactor: wait for collection names and fields

* feat: use shell-api directly

* feat: insert valid object names

* feat: remove trigger doc when replace collection name with dash
  • Loading branch information
alenakhineika authored Apr 29, 2020
1 parent 3c1abb6 commit adea744
Show file tree
Hide file tree
Showing 11 changed files with 766 additions and 302 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Open VS Code, launch the Commmand Palette (⌘+Shift+P on MacOS, Ctrl+Shift+P on

![Playgrounds](resources/screenshots/playground.png)

> Make sure you are connected to a cluster before using a playground. You can't run a playground and you won't get completions if you are not connected.
### Quick access to the MongoDB Shell

- Launch the MongoDB Shell from the command palette to quickly connect to the same cluster you have active in VS Code
Expand All @@ -70,6 +72,22 @@ Open VS Code, launch the Commmand Palette (⌘+Shift+P on MacOS, Ctrl+Shift+P on

![Settings](resources/screenshots/settings.png)

## Additional Settings

> These settings affect not only MongoDB extension but all installed extensions. If you don't want to change default settings, you can force VS Code to trigger suggestions by clicking `Ctrl+Space` inside a snippet or string literal.
`editor.suggest.snippetsPreventQuickSuggestions`: By default, VS Code prevents code completion in the snippet mode (editing placeholders in inserted code). Setting this option to `false` stops that and allows for the `db.collection.aggregate()` expression having both snippets completion (eg. `$match`, `$addFields`) and fields completion based on the document schema.

`editor.quickSuggestions`: By default, VS Code prevents code completion inside string literals. To enable database names completions for `use('')` expression use the following setting:

```
"editor.quickSuggestions": {
"other": true,
"comments": false,
"strings": true
}
```

## Telemetry

MongoDB for VS Code collects usage data and sends it to MongoDB to help improve our products and services. Read our [privacy policy](https://www.mongodb.com/legal/privacy-policy) to learn more. If you don’t wish to send usage data to MongoDB, you can opt out in the extension's settings.
Expand Down
5 changes: 0 additions & 5 deletions languages/mongodb-language-configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,5 @@
"start": "^\\s*//\\s*#?region\\b",
"end": "^\\s*//\\s*#?endregion\\b"
}
},
"wordPattern": "(-?\\d*\\.\\d\\w*)|([^\\`\\~\\!\\@\\#\\%\\^\\&\\*\\(\\)\\-\\=\\+\\[\\{\\]\\}\\\\\\|\\;\\:\\'\\\"\\,\\.\\<\\>\\/\\?\\s]+)",
"indentationRules": {
"increaseIndentPattern": "^((?!.*?\\/\\*).*\\*/)?\\s*[\\}\\]].*$",
"decreaseIndentPattern": "^((?!\\/\\/).)*(\\{[^}\"'`]*|\\([^)\"'`]*|\\[[^\\]\"'`]*)$"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@
"@leafygreen-ui/toggle": "^3.0.0",
"@mongosh/browser-runtime-electron": "0.0.1-alpha.12",
"@mongosh/service-provider-server": "0.0.1-alpha.12",
"@mongosh/shell-api": "0.0.1-alpha.12",
"analytics-node": "^3.4.0-beta.1",
"bson": "^4.0.3",
"classnames": "^2.2.6",
Expand Down
2 changes: 1 addition & 1 deletion scripts/update-snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const snippetTemplate = (
// but the variable is not known.
// The solution is to escape this symbol before building the stage body.
body = comment
? [...comment.replace(re, '\\$').split('\n'), ...body]
? [...comment.trim().replace(re, '\\$').split('\n'), ...body]
: [...body];

return { prefix, body, description };
Expand Down
27 changes: 0 additions & 27 deletions snippets/stage-autocompleter.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
" * newField: The new field name.",
" * expression: The new field expression.",
" */",
"",
"\\$addFields: {",
" ${1:newField}: ${2:expression}, ${3:...}",
"}"
Expand All @@ -24,7 +23,6 @@
" * outputN: Optional. The output object may contain a single or numerous field names used to accumulate values per bucket.",
" * }",
" */",
"",
"\\$bucket: {",
" groupBy: ${1:expression},",
" boundaries: [ ${2:lowerbound}, ${3:...} ],",
Expand All @@ -47,7 +45,6 @@
" * }",
" * granularity: Optional number series",
" */",
"",
"\\$bucketAuto: {",
" groupBy: ${1:expression},",
" buckets: ${2:number},",
Expand All @@ -66,7 +63,6 @@
" * histograms: Optional latency histograms.",
" * storageStats: Optional storage stats.",
"*/",
"",
"\\$collStats: {",
" latencyStats: {",
" histograms: ${1:boolean}",
Expand All @@ -82,7 +78,6 @@
"/**",
" * Provide the field name for the count.",
" */",
"",
"\\$count: '${1:string}'"
],
"description": "Returns a count of the number of documents at this stage of the aggregation pipeline."
Expand All @@ -94,7 +89,6 @@
" * outputFieldN: The first output field.",
" * stageN: The first aggregation stage.",
" */",
"",
"\\$facet: {",
" ${1:outputFieldN}: [ ${2:stageN}, ${3:...} ]",
"}"
Expand All @@ -107,7 +101,6 @@
"/**",
" * options: The geo query options.",
" */",
"",
"\\$geoNear: {",
" near: The point to search near.",
" distanceField: The calculated distance.",
Expand All @@ -133,7 +126,6 @@
" * depthField: Optional Name of the depth field.",
" * restrictSearchWithMatch: Optional query.",
" */",
"",
"\\$graphLookup: {",
" from: '${1:string}',",
" startWith: ${2:expression},",
Expand All @@ -154,7 +146,6 @@
" * _id: The id of the group.",
" * fieldN: The first field name.",
" */",
"",
"\\$group: {",
" _id: ${1:expression},",
" ${2:fieldN}: {",
Expand All @@ -170,7 +161,6 @@
"/**",
" * No parameters.",
" */",
"",
"\\$indexStats: {}"
],
"description": "Returns statistics regarding the use of each index for the collection."
Expand All @@ -181,7 +171,6 @@
"/**",
" * Provide the number of documents to limit.",
" */",
"",
"\\$limit: ${1:number}"
],
"description": "Limits the number of documents that flow into subsequent stages."
Expand All @@ -197,7 +186,6 @@
" * pipeline: The pipeline to run on the joined collection.",
" * let: Optional variables to use in the pipeline field stages.",
" */",
"",
"\\$lookup: {",
" from: '${1:string}',",
" localField: '${2:string}',",
Expand All @@ -213,7 +201,6 @@
"/**",
" * query: The query in MQL.",
" */",
"",
"\\$match: {",
" ${1:query}",
"}"
Expand All @@ -230,7 +217,6 @@
" * whenMatched: Action for matching docs.",
" * whenNotMatched: Action for non-matching docs.",
" */",
"",
"\\$merge: {",
" into: '${1:string}',",
" on: '${2:string}',",
Expand All @@ -247,7 +233,6 @@
"/**",
" * Provide the name of the output collection.",
" */",
"",
"\\$out: '${1:string}'"
],
"description": "Writes the result of a pipeline to a new or existing collection."
Expand All @@ -259,7 +244,6 @@
" * specifications: The fields to",
" * include or exclude.",
" */",
"",
"\\$project: {",
" ${1:specification(s)}",
"}"
Expand All @@ -273,7 +257,6 @@
" * expression: Any valid expression that",
" * evaluates to \\$\\$DESCEND, \\$\\$PRUNE, or \\$\\$KEEP.",
" */",
"",
"\\$redact: {",
" ${1:expression}",
"}"
Expand All @@ -286,7 +269,6 @@
"/**",
" * replacementDocument: A document or string.",
" */",
"",
"\\$replaceWith: {",
" newWith: ${1:replacementDocument}",
"}"
Expand All @@ -299,7 +281,6 @@
"/**",
" * replacementDocument: A document or string.",
" */",
"",
"\\$replaceRoot: {",
" newRoot: ${1:replacementDocument}",
"}"
Expand All @@ -312,7 +293,6 @@
"/**",
" * size: The number of documents to sample.",
" */",
"",
"\\$sample: {",
" size: ${1:number}",
"}"
Expand All @@ -327,7 +307,6 @@
" * search: The search spec.",
" * highlight: Search with highlights.",
" */",
"",
"\\$searchBeta: {",
" index: '${1:string}',",
" search: '${2:specification}',",
Expand All @@ -343,7 +322,6 @@
" * field: The field name",
" * expression: The expression.",
" */",
"",
"\\$set: {",
" ${1:field}: ${2:expression}",
"}"
Expand All @@ -356,7 +334,6 @@
"/**",
" * Provide the number of documents to skip.",
" */",
"",
"\\$skip: ${1:number}"
],
"description": "Skips a specified number of documents before advancing to the next stage."
Expand All @@ -367,7 +344,6 @@
"/**",
" * Provide any number of field/order pairs.",
" */",
"",
"\\$sort: {",
" ${1:field1}: ${2:sortOrder}",
"}"
Expand All @@ -380,7 +356,6 @@
"/**",
" * expression: Grouping expression or string.",
" */",
"",
"\\$sortByCount: {",
" ${1:expression}",
"}"
Expand All @@ -393,7 +368,6 @@
"/**",
" * fields: The field name(s).",
" */",
"",
"\\$unset: {",
" ${1:field}",
"}"
Expand All @@ -409,7 +383,6 @@
" * preserveNullAndEmptyArrays: Optional",
" * toggle to unwind null and empty values.",
" */",
"",
"\\$unwind: {",
" path: ${1:path},",
" includeArrayIndex: '${2:string}',",
Expand Down
Loading

0 comments on commit adea744

Please sign in to comment.