-
-
Notifications
You must be signed in to change notification settings - Fork 200
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
(feat) event completion/hover for components with a type definition #1057
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d445130
get events info from type checker
jasonlyu123 f1433c3
lint & test
jasonlyu123 ced63f7
event documentation
jasonlyu123 5033f4c
fix build
jasonlyu123 bd04dc3
Merge branch 'master' of https://github.com/sveltejs/language-tools i…
jasonlyu123 05c6c88
cache ComponentInfoProvider in the js/ts snapshot
jasonlyu123 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
72 changes: 72 additions & 0 deletions
72
packages/language-server/src/plugins/typescript/ComponentInfoProvider.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,72 @@ | ||
import { ComponentEvents } from 'svelte2tsx'; | ||
import ts from 'typescript'; | ||
import { isNotNullOrUndefined } from '../../utils'; | ||
import { findContainingNode } from './features/utils'; | ||
|
||
type ComponentEventInfo = ReturnType<ComponentEvents['getAll']>; | ||
|
||
export class JsOrTsComponentInfoProvider implements ComponentInfoProvider { | ||
private constructor( | ||
private readonly typeChecker: ts.TypeChecker, | ||
private readonly classType: ts.Type | ||
) {} | ||
|
||
getEvents(): ComponentEventInfo { | ||
const symbol = this.classType.getProperty('$$events_def'); | ||
if (!symbol) { | ||
return []; | ||
} | ||
|
||
const declaration = symbol.valueDeclaration; | ||
if (!declaration) { | ||
return []; | ||
} | ||
|
||
const eventType = this.typeChecker.getTypeOfSymbolAtLocation(symbol, declaration); | ||
|
||
return eventType | ||
.getProperties() | ||
.map((prop) => { | ||
if (!prop.valueDeclaration) { | ||
return; | ||
} | ||
|
||
return { | ||
name: prop.name, | ||
type: this.typeChecker.typeToString( | ||
this.typeChecker.getTypeOfSymbolAtLocation(prop, prop.valueDeclaration) | ||
), | ||
doc: ts.displayPartsToString(prop.getDocumentationComment(this.typeChecker)) | ||
}; | ||
}) | ||
.filter(isNotNullOrUndefined); | ||
} | ||
|
||
static create(lang: ts.LanguageService, def: ts.DefinitionInfo): ComponentInfoProvider | null { | ||
const program = lang.getProgram(); | ||
const sourceFile = program?.getSourceFile(def.fileName); | ||
|
||
if (!program || !sourceFile) { | ||
return null; | ||
} | ||
|
||
const defClass = findContainingNode(sourceFile, def.textSpan, ts.isClassDeclaration); | ||
|
||
if (!defClass) { | ||
return null; | ||
} | ||
|
||
const typeChecker = program.getTypeChecker(); | ||
const classType = typeChecker.getTypeAtLocation(defClass); | ||
|
||
if (!classType) { | ||
return null; | ||
} | ||
|
||
return new JsOrTsComponentInfoProvider(typeChecker, classType); | ||
} | ||
} | ||
|
||
export interface ComponentInfoProvider { | ||
getEvents(): ComponentEventInfo; | ||
} |
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
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
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
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
14 changes: 14 additions & 0 deletions
14
packages/language-server/test/plugins/typescript/testfiles/completions/ComponentDef.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,14 @@ | ||
/// <reference lib="dom" /> | ||
import { SvelteComponentTyped } from 'svelte'; | ||
|
||
export class ComponentDef extends SvelteComponentTyped< | ||
{}, | ||
{ | ||
event1: CustomEvent<null>; | ||
/** | ||
* documentation for event2 | ||
*/ | ||
event2: CustomEvent<string>; | ||
}, | ||
{} | ||
> {} |
5 changes: 5 additions & 0 deletions
5
...r/test/plugins/typescript/testfiles/completions/component-events-completion-ts-def.svelte
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,5 @@ | ||
<script> | ||
import { ComponentDef } from './ComponentDef'; | ||
</script> | ||
|
||
<ComponentDef on:></ComponentDef> |
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.
What do you think about making the instance of this a property on TsOrJsDocumentSnapshot? Then it could be cached and the cache would only be deleted on updates to the snapshot.
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.
Not sure about the performance implications, but could this be something we could also use for the svelte2tsx-components? Then we could ditch our own implementation of creating the "events" property on the svelte2tsx return type.
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.
Cached in the snapshot seems like a good idea.
Yes. Tried with svelte2tsx-component. The biggest difference between this and the svelte2tsx approach is the fallback Event in the svelte2tsx.