-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
+343
−5
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
tests/cases/fourslash/jsxAttributeSnippetCompletionClosed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/// <reference path="fourslash.ts" /> | ||
//@Filename: file.tsx | ||
////interface NestedInterface { | ||
//// Foo: NestedInterface; | ||
//// (props: {className?: string}): any; | ||
////} | ||
//// | ||
////declare const Foo: NestedInterface; | ||
//// | ||
////function fn1() { | ||
//// return <Foo> | ||
//// <Foo /*1*/ /> | ||
//// </Foo> | ||
////} | ||
////function fn2() { | ||
//// return <Foo> | ||
//// <Foo.Foo /*2*/ /> | ||
//// </Foo> | ||
////} | ||
////function fn3() { | ||
//// return <Foo> | ||
//// <Foo.Foo cla/*3*/ /> | ||
//// </Foo> | ||
////} | ||
////function fn4() { | ||
//// return <Foo> | ||
//// <Foo.Foo cla/*4*/ something /> | ||
//// </Foo> | ||
////} | ||
////function fn5() { | ||
//// return <Foo> | ||
//// <Foo.Foo something /*5*/ /> | ||
//// </Foo> | ||
////} | ||
////function fn6() { | ||
//// return <Foo> | ||
//// <Foo.Foo something cla/*6*/ /> | ||
//// </Foo> | ||
////} | ||
////function fn7() { | ||
//// return <Foo /*7*/ /> | ||
////} | ||
////function fn8() { | ||
//// return <Foo cla/*8*/ /> | ||
////} | ||
////function fn9() { | ||
//// return <Foo cla/*9*/ something /> | ||
////} | ||
////function fn10() { | ||
//// return <Foo something /*10*/ /> | ||
////} | ||
////function fn11() { | ||
//// return <Foo something cla/*11*/ /> | ||
////} | ||
|
||
var preferences: FourSlashInterface.UserPreferences = { | ||
jsxAttributeCompletionStyle: "braces", | ||
includeCompletionsWithSnippetText: true, | ||
includeCompletionsWithInsertText: true, | ||
}; | ||
|
||
verify.completions( | ||
{ marker: "1", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "2", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "3", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "4", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "5", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "6", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "7", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "8", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "9", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "10", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "11", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
) |
74 changes: 74 additions & 0 deletions
74
tests/cases/fourslash/jsxAttributeSnippetCompletionUnclosed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/// <reference path="fourslash.ts" /> | ||
//@Filename: file.tsx | ||
////interface NestedInterface { | ||
//// Foo: NestedInterface; | ||
//// (props: {className?: string}): any; | ||
////} | ||
//// | ||
////declare const Foo: NestedInterface; | ||
//// | ||
////function fn1() { | ||
//// return <Foo> | ||
//// <Foo /*1*/ | ||
//// </Foo> | ||
////} | ||
////function fn2() { | ||
//// return <Foo> | ||
//// <Foo.Foo /*2*/ | ||
//// </Foo> | ||
////} | ||
////function fn3() { | ||
//// return <Foo> | ||
//// <Foo.Foo cla/*3*/ | ||
//// </Foo> | ||
////} | ||
////function fn4() { | ||
//// return <Foo> | ||
//// <Foo.Foo cla/*4*/ something | ||
//// </Foo> | ||
////} | ||
////function fn5() { | ||
//// return <Foo> | ||
//// <Foo.Foo something /*5*/ | ||
//// </Foo> | ||
////} | ||
////function fn6() { | ||
//// return <Foo> | ||
//// <Foo.Foo something cla/*6*/ | ||
//// </Foo> | ||
////} | ||
////function fn7() { | ||
//// return <Foo /*7*/ | ||
////} | ||
////function fn8() { | ||
//// return <Foo cla/*8*/ | ||
////} | ||
////function fn9() { | ||
//// return <Foo cla/*9*/ something | ||
////} | ||
////function fn10() { | ||
//// return <Foo something /*10*/ | ||
////} | ||
////function fn11() { | ||
//// return <Foo something cla/*11*/ | ||
////} | ||
|
||
var preferences: FourSlashInterface.UserPreferences = { | ||
jsxAttributeCompletionStyle: "braces", | ||
includeCompletionsWithSnippetText: true, | ||
includeCompletionsWithInsertText: true, | ||
}; | ||
|
||
verify.completions( | ||
{ marker: "1", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "2", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "3", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "4", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "5", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "6", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "7", preferences, includes: { name: "className", insertText: "className={$1}", text: "(property) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "8", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "9", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "10", preferences, includes: { name: "className", insertText: "className={$1}", text: "(property) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
{ marker: "11", preferences, includes: { name: "className", insertText: "className={$1}", text: "(JSX attribute) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } }, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/// <reference path="fourslash.ts" /> | ||
//@Filename: file.tsx | ||
////interface NestedInterface { | ||
//// Foo: NestedInterface; | ||
//// (props: {}): any; | ||
////} | ||
//// | ||
////declare const Foo: NestedInterface; | ||
//// | ||
////function fn1() { | ||
//// return <Foo> | ||
//// </*1*/ /> | ||
//// </Foo> | ||
////} | ||
////function fn2() { | ||
//// return <Foo> | ||
//// <Fo/*2*/ /> | ||
//// </Foo> | ||
////} | ||
////function fn3() { | ||
//// return <Foo> | ||
//// <Foo./*3*/ /> | ||
//// </Foo> | ||
////} | ||
////function fn4() { | ||
//// return <Foo> | ||
//// <Foo.F/*4*/ /> | ||
//// </Foo> | ||
////} | ||
////function fn5() { | ||
//// return <Foo> | ||
//// <Foo.Foo./*5*/ /> | ||
//// </Foo> | ||
////} | ||
////function fn6() { | ||
//// return <Foo> | ||
//// <Foo.Foo.F/*6*/ /> | ||
//// </Foo> | ||
////} | ||
|
||
var preferences: FourSlashInterface.UserPreferences = { | ||
jsxAttributeCompletionStyle: "braces", | ||
includeCompletionsWithSnippetText: true, | ||
includeCompletionsWithInsertText: true, | ||
}; | ||
|
||
verify.completions( | ||
{ marker: "1", preferences, includes: { name: "Foo", text: "const Foo: NestedInterface" } }, | ||
{ marker: "2", preferences, includes: { name: "Foo", text: "const Foo: NestedInterface" } }, | ||
{ marker: "3", preferences, includes: { name: "Foo", text: "(JSX attribute) NestedInterface.Foo: NestedInterface" } }, | ||
{ marker: "4", preferences, includes: { name: "Foo", text: "(property) NestedInterface.Foo: NestedInterface" } }, | ||
{ marker: "5", preferences, includes: { name: "Foo", text: "(JSX attribute) NestedInterface.Foo: NestedInterface" } }, | ||
{ marker: "6", preferences, includes: { name: "Foo", text: "(property) NestedInterface.Foo: NestedInterface" } }, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/// <reference path="fourslash.ts" /> | ||
//@Filename: file.tsx | ||
////interface NestedInterface { | ||
//// Foo: NestedInterface; | ||
//// (props: {}): any; | ||
////} | ||
//// | ||
////declare const Foo: NestedInterface; | ||
//// | ||
////function fn1() { | ||
//// return <Foo> | ||
//// </*1*/ | ||
//// </Foo> | ||
////} | ||
////function fn2() { | ||
//// return <Foo> | ||
//// <Fo/*2*/ | ||
//// </Foo> | ||
////} | ||
////function fn3() { | ||
//// return <Foo> | ||
//// <Foo./*3*/ | ||
//// </Foo> | ||
////} | ||
////function fn4() { | ||
//// return <Foo> | ||
//// <Foo.F/*4*/ | ||
//// </Foo> | ||
////} | ||
////function fn5() { | ||
//// return <Foo> | ||
//// <Foo.Foo./*5*/ | ||
//// </Foo> | ||
////} | ||
////function fn6() { | ||
//// return <Foo> | ||
//// <Foo.Foo.F/*6*/ | ||
//// </Foo> | ||
////} | ||
|
||
var preferences: FourSlashInterface.UserPreferences = { | ||
jsxAttributeCompletionStyle: "braces", | ||
includeCompletionsWithSnippetText: true, | ||
includeCompletionsWithInsertText: true, | ||
}; | ||
|
||
verify.completions( | ||
{ marker: "1", preferences, includes: { name: "Foo", text: "const Foo: NestedInterface" } }, | ||
{ marker: "2", preferences, includes: { name: "Foo", text: "const Foo: NestedInterface" } }, | ||
{ marker: "3", preferences, includes: { name: "Foo", text: "(JSX attribute) NestedInterface.Foo: NestedInterface" } }, | ||
{ marker: "4", preferences, includes: { name: "Foo", text: "(property) NestedInterface.Foo: NestedInterface" } }, | ||
{ marker: "5", preferences, includes: { name: "Foo", text: "(JSX attribute) NestedInterface.Foo: NestedInterface" } }, | ||
{ marker: "6", preferences, includes: { name: "Foo", text: "(property) NestedInterface.Foo: NestedInterface" } }, | ||
) |
35 changes: 35 additions & 0 deletions
35
tests/cases/fourslash/jsxTagNameCompletionUnderElementClosed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/// <reference path="fourslash.ts" /> | ||
//@Filename: file.tsx | ||
////declare namespace JSX { | ||
//// interface IntrinsicElements { | ||
//// button: any; | ||
//// div: any; | ||
//// } | ||
////} | ||
////function fn() { | ||
//// return <> | ||
//// <butto/*1*/ /> | ||
//// </>; | ||
////} | ||
////function fn2() { | ||
//// return <> | ||
//// preceding junk <butto/*2*/ /> | ||
//// </>; | ||
////} | ||
////function fn3() { | ||
//// return <> | ||
//// <butto/*3*/ style="" /> | ||
//// </>; | ||
////} | ||
|
||
var preferences: FourSlashInterface.UserPreferences = { | ||
jsxAttributeCompletionStyle: "braces", | ||
includeCompletionsWithSnippetText: true, | ||
includeCompletionsWithInsertText: true, | ||
}; | ||
|
||
verify.completions( | ||
{ marker: "1", preferences, includes: { name: "button", text: "(JSX attribute) JSX.IntrinsicElements.button: any" } }, | ||
{ marker: "2", preferences, includes: { name: "button", text: "(JSX attribute) JSX.IntrinsicElements.button: any" } }, | ||
{ marker: "3", preferences, includes: { name: "button", text: "(JSX attribute) JSX.IntrinsicElements.button: any" } }, | ||
) |
35 changes: 35 additions & 0 deletions
35
tests/cases/fourslash/jsxTagNameCompletionUnderElementUnclosed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/// <reference path="fourslash.ts" /> | ||
//@Filename: file.tsx | ||
////declare namespace JSX { | ||
//// interface IntrinsicElements { | ||
//// button: any; | ||
//// div: any; | ||
//// } | ||
////} | ||
////function fn() { | ||
//// return <> | ||
//// <butto/*1*/ | ||
//// </>; | ||
////} | ||
////function fn2() { | ||
//// return <> | ||
//// preceding junk <butto/*2*/ | ||
//// </>; | ||
////} | ||
////function fn3() { | ||
//// return <> | ||
//// <butto/*3*/ style="" | ||
//// </>; | ||
////} | ||
|
||
var preferences: FourSlashInterface.UserPreferences = { | ||
jsxAttributeCompletionStyle: "braces", | ||
includeCompletionsWithSnippetText: true, | ||
includeCompletionsWithInsertText: true, | ||
}; | ||
|
||
verify.completions( | ||
{ marker: "1", preferences, includes: { name: "button", text: "(JSX attribute) JSX.IntrinsicElements.button: any" } }, | ||
{ marker: "2", preferences, includes: { name: "button", text: "(JSX attribute) JSX.IntrinsicElements.button: any" } }, | ||
{ marker: "3", preferences, includes: { name: "button", text: "(JSX attribute) JSX.IntrinsicElements.button: any" } }, | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 onMaybe 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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.