Skip to content

Commit 35794f7

Browse files
lrlnaaddaleax
andauthored
chore(cli-repl): MONGOSH-377 separate out autocompleter into its own lerna package (#389)
Co-authored-by: Anna Henningsen <[email protected]>
1 parent 06c781f commit 35794f7

File tree

16 files changed

+1224
-23
lines changed

16 files changed

+1224
-23
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
lib/

packages/autocomplete/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('../../config/eslintrc.base');

packages/autocomplete/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib/

packages/autocomplete/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# `@mongosh/autocomplete`
2+
3+
Package for [MongoDB Shell](mongosh)
4+
5+
## Usage
6+
7+
```js
8+
const autocomplete = require('@mongosh/autocomplete');
9+
const serverVersion = '4.4.0';
10+
const line = 'db.coll.fin';
11+
const completions = autocomplete(serverVersion, line);
12+
if (!completions || !completions.length) {
13+
return [];
14+
}
15+
const entries = completions[0].map((completion) => {
16+
return {
17+
completion
18+
};
19+
});
20+
```
21+
### API
22+
23+
#### completions = autocomplete(serverVersion, line)
24+
__serverVersion:__ current version of MongoDB
25+
__line:__ current line to autcomplete
26+
27+
Returns an array of completions, and the line we were autocompleting. For
28+
example:
29+
30+
```js
31+
const autocomplete = require('@mongosh/autocomplete');
32+
const serverVersion = '4.4.0';
33+
const line = 'db.coll.re';
34+
const completions = autocomplete(serverVersion, line);
35+
// returns:
36+
// [
37+
// [ 'db.coll.renameCollection', 'db.coll.replaceOne', 'db.coll.reIndex' ],
38+
// 'db.coll.re'
39+
// ]
40+
```
41+
42+
Autocomplete is based on currently implemeted APIs in [@mongosh/shell-api](https://www.npmjs.com/package/@mongosh/shell-api)
43+
44+
45+
## Installation
46+
```shell
47+
npm install -S @mongosh/errors
48+
```
49+
50+
[mongosh]: https://github.com/mongodb-js/mongosh

packages/cli-repl/src/completer.spec.ts renamed to packages/autocomplete/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import completer from './completer';
1+
import completer from './';
22
import { signatures as shellSignatures } from '@mongosh/shell-api';
33

44
import { expect } from 'chai';

packages/cli-repl/src/completer.ts renamed to packages/autocomplete/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const BASE_COMPLETIONS = EXPRESSION_OPERATORS.concat(
1717
const MATCH_COMPLETIONS = QUERY_OPERATORS.concat(BSON_TYPES);
1818

1919
/**
20-
* The proect stage operator.
20+
* The project stage operator.
2121
*/
2222
const PROJECT = '$project';
2323

@@ -29,7 +29,8 @@ const GROUP = '$group';
2929
/**
3030
* Return complete suggestions given currently typed line
3131
*
32-
* @param {string} Line - Current user input.
32+
* @param {string} mdbVersion - Current mongoDB version.
33+
* @param {string} line - Current user input.
3334
*
3435
* @returns {array} Matching Completions, Current User Input.
3536
*/
@@ -78,7 +79,7 @@ function completer(mdbVersion: string, line: string): [string[], string] {
7879
expressions = BASE_COMPLETIONS.concat(getStageAccumulators(
7980
elToComplete, mdbVersion));
8081
} else {
81-
// collection quering just needs MATCH COMPLETIONS
82+
// collection querying just needs MATCH COMPLETIONS
8283
expressions = MATCH_COMPLETIONS;
8384
}
8485
// split on {, as a stage/query will always follow an open curly brace
@@ -128,8 +129,7 @@ function filterQueries(mdbVersion: string, completions: any, prefix: string, spl
128129
return e.name.startsWith(prefix) && semver.gte(mdbVersion, e.version);
129130
});
130131

131-
const adjusted = hits.map(h => `${split}${h.name}`);
132-
return adjusted;
132+
return hits.map(h => `${split}${h.name}`);
133133
}
134134

135135
function filterShellAPI(mdbVersion: string, completions: object, prefix: string, split?: string[]): any {
@@ -141,8 +141,7 @@ function filterShellAPI(mdbVersion: string, completions: object, prefix: string,
141141
});
142142

143143
if (split) {
144-
const adjusted = hits.map(h => `${split.slice(0, -1).join('.')}.${h}`);
145-
return adjusted;
144+
return hits.map(h => `${split.slice(0, -1).join('.')}.${h}`);
146145
}
147146

148147
return hits;

0 commit comments

Comments
 (0)