Skip to content

Commit 1a0e4ad

Browse files
Anemylerouxb
authored andcommitted
fix(compass-editors): fix nested field autocomplete COMPASS-6335 (#3874)
1 parent 8742030 commit 1a0e4ad

File tree

3 files changed

+115
-8
lines changed

3 files changed

+115
-8
lines changed

packages/compass-aggregations/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ If you're interested in helping with the Aggregation Builder plugin, we'd be ove
4949
- [`<ExportToLanguage />`](https://github.com/mongodb-js/compass-export-to-language) Modal plugin that connects `<Aggregation />` to `bson-transpilers` [for actual compiling to another language](https://github.com/mongodb-js/bson-transpilers)
5050
- [`mongodb-js/stage-validator`](https://github.com/mongodb-js/stage-validator) Aggregation Pipeline Stage grammar.
5151
- [`bson-transpilers`](https://github.com/mongodb-js/bson-transpilers) Read the amazing: [Compiler in JavaScript using ANTLR](https://medium.com/dailyjs/compiler-in-javascript-using-antlr-9ec53fd2780f)
52-
- [`mongodb-js/ace-mode`](https://github.com/mongodb-js/ace-mode) MongoDB highlighting rules for ACE.
53-
- [`mongodb-js/ace-autocompleter`](https://github.com/mongodb-js/ace-autocompleter) Makes ACE autocompletion aware of MongoDB Aggregation Pipeline [operators, expressions, and fields](https://github.com/mongodb-js/ace-autocompleter/tree/master/lib/constants).
52+
- [`@mongodb-js/compass-editor`](https://github.com/mongodb-js/compass/tree/main/packages/compass-editor) Reusable Compass editor component based on ace-editor with MongoDB-specific ace modes, themes, and autocompleters.
5453

5554
## Usage
5655

packages/compass-editor/src/ace/query-autocompleter.test.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,113 @@ describe('QueryAutoCompleter', function () {
124124
});
125125
});
126126

127+
context('when the query prefix matches one of the fields', function () {
128+
const { getCompletions } = setupQueryCompleter('{ pi', {
129+
serverVersion: '3.4.0',
130+
fields: [
131+
{
132+
name: 'pineapple',
133+
value: 'pineapple',
134+
score: 1,
135+
meta: 'field',
136+
version: '0.0.0',
137+
},
138+
{
139+
name: 'noMatches',
140+
value: 'noMatches',
141+
score: 1,
142+
meta: 'field',
143+
version: '0.0.0',
144+
},
145+
],
146+
});
147+
148+
it('returns all the query operators', function () {
149+
getCompletions((error, results) => {
150+
expect(error).to.equal(null);
151+
expect(results.length).to.equal(1);
152+
expect(results).to.deep.equal([
153+
{
154+
name: 'pineapple',
155+
value: 'pineapple',
156+
score: 1,
157+
meta: 'field',
158+
version: '0.0.0',
159+
},
160+
]);
161+
});
162+
});
163+
});
164+
165+
context(
166+
'when the query prefix has matching nested field names',
167+
function () {
168+
const { getCompletions } = setupQueryCompleter('{ pi', {
169+
serverVersion: '3.4.0',
170+
fields: [
171+
{
172+
name: 'pineapple',
173+
value: 'pineapple',
174+
score: 1,
175+
meta: 'field',
176+
version: '0.0.0',
177+
},
178+
{
179+
name: 'pineapple.price',
180+
value: '"pineapple.price"',
181+
score: 1,
182+
meta: 'field',
183+
version: '0.0.0',
184+
},
185+
{
186+
name: 'pineapple.fronds',
187+
value: '"pineapple.fronds"',
188+
score: 1,
189+
meta: 'field',
190+
version: '0.0.0',
191+
},
192+
{
193+
name: 'noMatches',
194+
value: 'noMatches',
195+
score: 1,
196+
meta: 'field',
197+
version: '0.0.0',
198+
},
199+
],
200+
});
201+
202+
it('returns all the query operators', function () {
203+
getCompletions((error, results) => {
204+
expect(error).to.equal(null);
205+
expect(results.length).to.equal(3);
206+
expect(results).to.deep.equal([
207+
{
208+
name: 'pineapple',
209+
value: 'pineapple',
210+
score: 1,
211+
meta: 'field',
212+
version: '0.0.0',
213+
},
214+
{
215+
name: 'pineapple.price',
216+
value: '"pineapple.price"',
217+
score: 1,
218+
meta: 'field',
219+
version: '0.0.0',
220+
},
221+
{
222+
name: 'pineapple.fronds',
223+
value: '"pineapple.fronds"',
224+
score: 1,
225+
meta: 'field',
226+
version: '0.0.0',
227+
},
228+
]);
229+
});
230+
});
231+
}
232+
);
233+
127234
context('when the version doesnt match a bson type', function () {
128235
const { getCompletions } = setupQueryCompleter('{ N', {
129236
serverVersion: '3.2.0',

packages/compass-editor/src/ace/util.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ const filter = (
88
prefix: string
99
) => {
1010
const parsedVersion = semver.parse(version);
11+
const cleanVersion = parsedVersion
12+
? [parsedVersion.major, parsedVersion.minor, parsedVersion.patch].join('.')
13+
: version;
1114
return entries.filter((e) => {
12-
const cleanVersion = parsedVersion
13-
? [parsedVersion.major, parsedVersion.minor, parsedVersion.patch].join(
14-
'.'
15-
)
16-
: version;
17-
return e.value.startsWith(prefix) && semver.gte(cleanVersion, e.version);
15+
if (!e.name) {
16+
return false;
17+
}
18+
return e.name.startsWith(prefix) && semver.gte(cleanVersion, e.version);
1819
});
1920
};
2021

0 commit comments

Comments
 (0)