microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.4k stars 12.41k forks source link

Special member completions not work after a comment. #58815

Open Jack-Works opened 3 months ago

Jack-Works commented 3 months ago

🔎 Search Terms

completion comment

🕗 Version & Regression Information

⏯ Playground Link

Not reproducible (totally broken) in playground

💻 Code

const x = {
    o: 1, 'a b': 2
}

 x. ;
// ^ trigger completion after dot gives you 2 completion items
x./**/ ;
//    ^ trigger completion after comment gives you 1 completion item

🙁 Actual behavior

Not the same

🙂 Expected behavior

Behave the same

Additional information about the issue

This style (completion after a comment) is common in the four-slash test in TS, so I believe there are some problems with the current test infrastructure.

Andarist commented 4 weeks ago

This style (completion after a comment) is common in the four-slash test in TS, so I believe there are some problems with the current test infrastructure.

Those are not actual comments but position markers parsed out of the source file text.


As to the problem itself... it isn't a TS problem (at least not in full). This completion is returned just OK by TS. It can be tested with:

/// <reference path="fourslash.ts" />

//// const x = {
////   o: 1,
////   "a b": 2,
//// };
////
//// x./*1*/;
//// x./*^comment*//*2*/;

verify.completions({
  marker: ["1", "2"],
  includes: [{ name: "a b", insertText: '["a b"]' }],
  preferences: {
    includeCompletionsWithInsertText: true
  }
});

*note how I put an actual block comment into this source text by using a value for it that is not a valid marker name.

It's the VS Code that rejects this completion item here. The problem here is that word contains the comment ("./*^comment*/") and it clashes~ with the provided filterText (".a b").

I'm not sure what exactly filterText is meant to be, it's defined here as follows:

A string that should be used when filtering a set of completion items.

This doesn't quite tell me how it's meant to be used. I'm pretty sure this could be fixed by including the comment in the filterText of the returned completion items. I'm not sure though if it would be the best solution here. Maybe it's the VS Code that should ignore comments when matching this text against the current word?

That said... this completion needs to convert . into []. This in turn requires returning a replacement range. I assume that the property access should be inserted at the cursor position so the output should look like this:

x/*^comment*/['a b']

It means that... the insertText should include the comment. So maybe filterText should contain it too?