Skip to content

fix: when autocompleting with insertText, use that rather than name #562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/mongodb-ts-autocomplete/src/autocompleter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ service host, so typescript wouldn't load all the dependencies.
{
kind: 'property',
name: 'one.two',
result: 'db.one.two',
result: 'db["one.two"]',
},
]);
}
Expand Down
46 changes: 46 additions & 0 deletions packages/ts-autocomplete/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ describe('Autocompleter', function () {
});

expect(autoCompleter.autocomplete('myGlobalObject.')).to.deep.equal([
{
kind: 'property',
name: 'foo.bar',
result: 'myGlobalObject["foo.bar"]',
},
{
kind: 'property',
name: 'functionProp',
Expand All @@ -100,6 +105,11 @@ describe('Autocompleter', function () {
});

expect(autoCompleter.autocomplete('myGlobalObject.str')).to.deep.equal([
{
kind: 'property',
name: 'foo.bar',
result: 'myGlobalObject["foo.bar"]',
},
{
kind: 'property',
name: 'functionProp',
Expand All @@ -121,6 +131,11 @@ describe('Autocompleter', function () {
expect(
autoCompleter.autocomplete('myGlobalObject.doesNotExist'),
).to.deep.equal([
{
kind: 'property',
name: 'foo.bar',
result: 'myGlobalObject["foo.bar"]',
},
{
kind: 'property',
name: 'functionProp',
Expand Down Expand Up @@ -207,6 +222,32 @@ describe('Autocompleter', function () {
},
]);
});

it('returns completions for variables with dots in their names', function () {
autoCompleter.updateCode({
'/code.d.ts': CODE_TS,
});

const completions = autoCompleter.autocomplete('myGlobalObject.fo');

expect(completions).to.deep.equal([
{
kind: 'property',
name: 'foo.bar',
result: 'myGlobalObject["foo.bar"]',
},
{
kind: 'property',
name: 'functionProp',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... why are we getting these completions? Shouldn't they be filtered out since they don't start with fo?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function filterStartingWith({
  name,
  trigger,
}: {
  name: string;
  trigger: string;
}): boolean {
  name = name.toLocaleLowerCase();
  trigger = trigger.toLocaleLowerCase();

  if (name.startsWith(trigger)) {
    console.log({ name, trigger });
    return true;
  };
  return false;
}

trigger is a blank string in this case:

  Autocompleter
    without filter
      ✓ returns the global scope for a global variable that does not exist
      ✓ returns completions for global variables
      ✓ returns nothing for a member of a variable that does not exist
      ✓ returns matches for a member of a variable that exists
      ✓ returns matches for part of a member of a variable that exists
      ✓ returns matches for an unknown member of a variable that exists
      ✓ returns the global scope for object parameters of a function that does not exist
      ✓ returns matches for object parameters of a function that exists
      ✓ returns matches for part of an object parameter of a function that exists
      ✓ returns matches for an unknown object parameter of a function that exists
      ✓ returns completions for variables with dots in their names
    with filter
      ✓ returns nothing when it has no code
{ name: 'myglobalfunction', trigger: 'myglobalfunct' }
      ✓ returns completions for global variables
{ name: 'myglobalobject', trigger: 'myglobalobject' }
{ name: 'foo.bar', trigger: '' }
{ name: 'functionprop', trigger: '' }
{ name: 'stringprop', trigger: '' }
{ name: 'functionprop', trigger: 'functionpr' }
      ✓ returns completions for members of global variables
{ name: 'param1', trigger: 'par' }
{ name: 'param2', trigger: 'par' }
      ✓ returns completions for the fields of object function parameters


  15 passing (60ms)

result: 'myGlobalObject.functionProp',
},
{
kind: 'property',
name: 'stringProp',
result: 'myGlobalObject.stringProp',
},
]);
});
});

describe('with filter', function () {
Expand Down Expand Up @@ -250,6 +291,11 @@ describe('Autocompleter', function () {
},
]);
expect(autoCompleter.autocomplete('myGlobalObject.')).to.deep.equal([
{
kind: 'property',
name: 'foo.bar',
result: 'myGlobalObject["foo.bar"]',
},
{
kind: 'property',
name: 'functionProp',
Expand Down
17 changes: 16 additions & 1 deletion packages/ts-autocomplete/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,23 @@ function mapCompletions(
return completions.entries
.filter((entry) => filter({ trigger, kind: entry.kind, name: entry.name }))
.map((entry) => {
/*
With the includeCompletionsWithInsertText option set, auto-completions
might have an insertText property for cases like where a property has
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What other cases are there and should we add tests for those as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't think of any. Once I do I'll add regression tests ;)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably dashes, spaces, quotes..

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, will be interesting to see some corner cases involving quotes - e.g. a collection called foo"bar' fails with the current autocompleter - kind of hoping it will work out of the box with the new one

to be escaped.

An obvious example is autocompleting a collection name that has a dot in
it. Let's say you have the text db.fo and the collection name is foo.bar.
name would be 'foo.bar' and insertText would be '["foo.bar"]'.

In that case we also want to strip the dot from the prefix.
*/
const result = entry.insertText
? prefix.slice(0, -1) + entry.insertText
: prefix + entry.name;

return {
result: prefix + entry.name,
result,
name: entry.name,
kind: entry.kind,
};
Expand Down
1 change: 1 addition & 0 deletions packages/ts-autocomplete/test/fixtures/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export type MyObject = {
stringProp: string;
functionProp: (p1: number) => void;
'foo.bar': string;
};

export type MyFunctionParams = { param1: string; param2: string };
Expand Down