Skip to content
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

Rewrite logic for JSX attribute completion detection #47412

Merged
merged 2 commits into from Jan 14, 2022

Conversation

jakebailey
Copy link
Member

Fixes #47090
Fixes #47280

This is the first part of #47096, which simply fixes the snippet issue by not using SymbolDisplay.getSymbolKind (which is broken; note the oddity of the kinds in the test cases) to detect that we are completing a JSX attribute.

src/services/completions.ts Outdated Show resolved Hide resolved
src/services/completions.ts Outdated Show resolved Hide resolved
@@ -744,8 +744,46 @@ namespace ts.Completions {
}
}

const kind = SymbolDisplay.getSymbolKind(typeChecker, symbol, location);
if (kind === ScriptElementKind.jsxAttribute && preferences.includeCompletionsWithSnippetText && preferences.jsxAttributeCompletionStyle && preferences.jsxAttributeCompletionStyle !== "none") {
const isJSXAttributeCompletion = contextToken && forEachAncestor(contextToken, (n) => {
Copy link
Member

Choose a reason for hiding this comment

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

createCompletionEntry is a very hot path, so we should take care to (a) make this function as efficient as possible, and more importantly (b) not run it unless we need to. I would suggest moving it to its own function so it’s ergonomic to guard on

  • the necessary preferences checks right after this function
  • are we even in a TSX/JSX file

Maybe there’s also a function that ran earlier that already detected that we’re in a likely JSX attribute position (after all, it had to get the props type of the component/element in order to come up with the right list of symbols, so this seems likely) and we can save that state to CompletionData for use here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good points, I'll take a look. Since this is a pure function of contextToken and location, there's also no reason to calculate it more than once, either, yet I do it per entry.

Copy link
Member

Choose a reason for hiding this comment

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

Doh, that’s the easy answer here. But it does make me think, we basically already had to know we were getting props for JSX attributes, so maybe a lot of this can go away anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

I feel really silly now. This is easily achievable with two flags that are set up earlier, just not passed down. Now it's both fast and not duplicated. Pushed now.

If only I had noticed much, much sooner.

PR Backlog automation moved this from Waiting on reviewers to Waiting on author Jan 14, 2022
@jakebailey jakebailey moved this from Waiting on author to Waiting on reviewers in PR Backlog Jan 14, 2022
PR Backlog automation moved this from Waiting on reviewers to Needs merge Jan 14, 2022
@jakebailey jakebailey merged commit 16bbddb into microsoft:main Jan 14, 2022
PR Backlog automation moved this from Needs merge to Done Jan 14, 2022
@jakebailey jakebailey deleted the fix-47090-2 branch January 14, 2022 17:57
@0xorial
Copy link

0xorial commented Nov 22, 2022

Is attributes completion supposed to work only for intrinsic elements (i.e. html tags) or for all components?

@jakebailey
Copy link
Member Author

It should work for all components if they are typed. If you're having a problem, I suggest filling a bug (this is a 10 month old closed PR).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
PR Backlog
  
Done
5 participants