Skip to content

Commit 4069e88

Browse files
authored
fix: when autocompleting with insertText, use that rather than name (#562)
when autocompleting with insertText, use that rather than name
1 parent 12c64fb commit 4069e88

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

packages/mongodb-ts-autocomplete/src/autocompleter.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ service host, so typescript wouldn't load all the dependencies.
220220
{
221221
kind: 'property',
222222
name: 'one.two',
223-
result: 'db.one.two',
223+
result: 'db["one.two"]',
224224
},
225225
]);
226226
}

packages/ts-autocomplete/src/index.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ describe('Autocompleter', function () {
8181
});
8282

8383
expect(autoCompleter.autocomplete('myGlobalObject.')).to.deep.equal([
84+
{
85+
kind: 'property',
86+
name: 'foo.bar',
87+
result: 'myGlobalObject["foo.bar"]',
88+
},
8489
{
8590
kind: 'property',
8691
name: 'functionProp',
@@ -100,6 +105,11 @@ describe('Autocompleter', function () {
100105
});
101106

102107
expect(autoCompleter.autocomplete('myGlobalObject.str')).to.deep.equal([
108+
{
109+
kind: 'property',
110+
name: 'foo.bar',
111+
result: 'myGlobalObject["foo.bar"]',
112+
},
103113
{
104114
kind: 'property',
105115
name: 'functionProp',
@@ -121,6 +131,11 @@ describe('Autocompleter', function () {
121131
expect(
122132
autoCompleter.autocomplete('myGlobalObject.doesNotExist'),
123133
).to.deep.equal([
134+
{
135+
kind: 'property',
136+
name: 'foo.bar',
137+
result: 'myGlobalObject["foo.bar"]',
138+
},
124139
{
125140
kind: 'property',
126141
name: 'functionProp',
@@ -207,6 +222,32 @@ describe('Autocompleter', function () {
207222
},
208223
]);
209224
});
225+
226+
it('returns completions for variables with dots in their names', function () {
227+
autoCompleter.updateCode({
228+
'/code.d.ts': CODE_TS,
229+
});
230+
231+
const completions = autoCompleter.autocomplete('myGlobalObject.fo');
232+
233+
expect(completions).to.deep.equal([
234+
{
235+
kind: 'property',
236+
name: 'foo.bar',
237+
result: 'myGlobalObject["foo.bar"]',
238+
},
239+
{
240+
kind: 'property',
241+
name: 'functionProp',
242+
result: 'myGlobalObject.functionProp',
243+
},
244+
{
245+
kind: 'property',
246+
name: 'stringProp',
247+
result: 'myGlobalObject.stringProp',
248+
},
249+
]);
250+
});
210251
});
211252

212253
describe('with filter', function () {
@@ -250,6 +291,11 @@ describe('Autocompleter', function () {
250291
},
251292
]);
252293
expect(autoCompleter.autocomplete('myGlobalObject.')).to.deep.equal([
294+
{
295+
kind: 'property',
296+
name: 'foo.bar',
297+
result: 'myGlobalObject["foo.bar"]',
298+
},
253299
{
254300
kind: 'property',
255301
name: 'functionProp',

packages/ts-autocomplete/src/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,23 @@ function mapCompletions(
190190
return completions.entries
191191
.filter((entry) => filter({ trigger, kind: entry.kind, name: entry.name }))
192192
.map((entry) => {
193+
/*
194+
With the includeCompletionsWithInsertText option set, auto-completions
195+
might have an insertText property for cases like where a property has
196+
to be escaped.
197+
198+
An obvious example is autocompleting a collection name that has a dot in
199+
it. Let's say you have the text db.fo and the collection name is foo.bar.
200+
name would be 'foo.bar' and insertText would be '["foo.bar"]'.
201+
202+
In that case we also want to strip the dot from the prefix.
203+
*/
204+
const result = entry.insertText
205+
? prefix.slice(0, -1) + entry.insertText
206+
: prefix + entry.name;
207+
193208
return {
194-
result: prefix + entry.name,
209+
result,
195210
name: entry.name,
196211
kind: entry.kind,
197212
};

packages/ts-autocomplete/test/fixtures/code.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export type MyObject = {
33
stringProp: string;
44
functionProp: (p1: number) => void;
5+
'foo.bar': string;
56
};
67

78
export type MyFunctionParams = { param1: string; param2: string };

0 commit comments

Comments
 (0)