-
Notifications
You must be signed in to change notification settings - Fork 13.2k
feat: comprehensive Type Hierarchy LSP support #63052
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
Open
kbrilla
wants to merge
8
commits into
microsoft:main
Choose a base branch
from
kbrilla:feat/type-hierarchy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
97df10d
feat: Add Type Hierarchy LSP support
kbrilla c94cf47
chore: Accept public API baseline with Type Hierarchy additions
kbrilla e7f105f
feat: comprehensive Type Hierarchy LSP support
kbrilla ac0d3ce
refactor: address Copilot review comments
kbrilla 29f6b36
docs: link issue #45877 in PR description
kbrilla 09e063c
feat: add configurable typeHierarchyMaxResults preference
kbrilla 22bcb2f
chore: remove PR_DESCRIPTION.md from repo
kbrilla cd68bd4
docs: add comments explaining mapped type subtype limitations
kbrilla 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 hidden or 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 |
|---|---|---|
|
|
@@ -64,3 +64,4 @@ package-lock.json | |
| .eslintcache | ||
| *v8.log | ||
| /lib/ | ||
| PR_DESCRIPTION.md | ||
This file contains hidden or 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 hidden or 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 hidden or 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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -211,6 +211,12 @@ const enum CallHierarchyItemDirection { | |||||||||||||||||||||
| Outgoing, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const enum TypeHierarchyItemDirection { | ||||||||||||||||||||||
| Root, | ||||||||||||||||||||||
| Supertypes, | ||||||||||||||||||||||
| Subtypes, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| interface RealizedDiagnostic { | ||||||||||||||||||||||
| message: string; | ||||||||||||||||||||||
| start: number; | ||||||||||||||||||||||
|
|
@@ -4421,6 +4427,119 @@ export class TestState { | |||||||||||||||||||||
| this.baseline("Call Hierarchy", text, ".callHierarchy.txt"); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| private formatLibFileHierarchyItemSpan(item: ts.TypeHierarchyItem, prefix: string, trailingPrefix: string): string { | ||||||||||||||||||||||
| // For lib files, we don't have the source available in the test | ||||||||||||||||||||||
| let text = ""; | ||||||||||||||||||||||
| text += `${prefix}╭ ${item.file} (lib file)\n`; | ||||||||||||||||||||||
| text += `${prefix}│ <source not available>\n`; | ||||||||||||||||||||||
| text += `${trailingPrefix}╰\n`; | ||||||||||||||||||||||
| return text; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| private formatTypeHierarchyItemSpan(file: FourSlashFile | undefined, item: ts.TypeHierarchyItem, span: ts.TextSpan, prefix: string, trailingPrefix = prefix) { | ||||||||||||||||||||||
| if (!file) { | ||||||||||||||||||||||
| return this.formatLibFileHierarchyItemSpan(item, prefix, trailingPrefix); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return this.formatCallHierarchyItemSpan(file, span, prefix, trailingPrefix); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| private formatTypeHierarchyItem(file: FourSlashFile | undefined, item: ts.TypeHierarchyItem, direction: TypeHierarchyItemDirection, seen: Map<string, boolean>, prefix: string, trailingPrefix: string = prefix): string { | ||||||||||||||||||||||
| const key = `${item.file}|${JSON.stringify(item.span)}|${direction}`; | ||||||||||||||||||||||
| const alreadySeen = seen.has(key); | ||||||||||||||||||||||
| seen.set(key, true); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const supertypes = direction === TypeHierarchyItemDirection.Subtypes ? { result: "skip" } as const : | ||||||||||||||||||||||
| alreadySeen ? { result: "seen" } as const : | ||||||||||||||||||||||
| { result: "show", values: this.languageService.provideTypeHierarchySupertypes(item.file, item.selectionSpan.start) } as const; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const subtypes = direction === TypeHierarchyItemDirection.Supertypes ? { result: "skip" } as const : | ||||||||||||||||||||||
| alreadySeen ? { result: "seen" } as const : | ||||||||||||||||||||||
| { result: "show", values: this.languageService.provideTypeHierarchySubtypes(item.file, item.selectionSpan.start) } as const; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| let text = ""; | ||||||||||||||||||||||
| text += `${prefix}╭ name: ${item.name}\n`; | ||||||||||||||||||||||
| text += `${prefix}├ kind: ${item.kind}\n`; | ||||||||||||||||||||||
| if (item.kindModifiers) { | ||||||||||||||||||||||
| text += `${prefix}├ kindModifiers: ${item.kindModifiers}\n`; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if (item.containerName) { | ||||||||||||||||||||||
| text += `${prefix}├ containerName: ${item.containerName}\n`; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| text += `${prefix}├ file: ${item.file}\n`; | ||||||||||||||||||||||
| text += `${prefix}├ span:\n`; | ||||||||||||||||||||||
| text += this.formatTypeHierarchyItemSpan(file, item, item.span, `${prefix}│ `); | ||||||||||||||||||||||
| text += `${prefix}├ selectionSpan:\n`; | ||||||||||||||||||||||
| text += this.formatTypeHierarchyItemSpan( | ||||||||||||||||||||||
| file, | ||||||||||||||||||||||
| item, | ||||||||||||||||||||||
| item.selectionSpan, | ||||||||||||||||||||||
| `${prefix}│ `, | ||||||||||||||||||||||
| supertypes.result !== "skip" || subtypes.result !== "skip" ? `${prefix}│ ` : `${trailingPrefix}╰ `, | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (supertypes.result === "seen") { | ||||||||||||||||||||||
| if (subtypes.result === "skip") { | ||||||||||||||||||||||
| text += `${trailingPrefix}╰ supertypes: ...\n`; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| else { | ||||||||||||||||||||||
| text += `${prefix}├ supertypes: ...\n`; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| else if (supertypes.result === "show") { | ||||||||||||||||||||||
| if (!ts.some(supertypes.values)) { | ||||||||||||||||||||||
| if (subtypes.result === "skip") { | ||||||||||||||||||||||
| text += `${trailingPrefix}╰ supertypes: none\n`; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| else { | ||||||||||||||||||||||
| text += `${prefix}├ supertypes: none\n`; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| else { | ||||||||||||||||||||||
| text += `${prefix}├ supertypes:\n`; | ||||||||||||||||||||||
| for (let i = 0; i < supertypes.values.length; i++) { | ||||||||||||||||||||||
| const supertype = supertypes.values[i]; | ||||||||||||||||||||||
| const supertypeFile = this.tryFindFileWorker(supertype.file).file; | ||||||||||||||||||||||
| text += `${prefix}│ ╭\n`; | ||||||||||||||||||||||
| text += this.formatTypeHierarchyItem(supertypeFile, supertype, TypeHierarchyItemDirection.Supertypes, seen, `${prefix}│ │ `, i < supertypes.values.length - 1 ? `${prefix}│ ╰ ` : subtypes.result !== "skip" ? `${prefix}│ ╰ ` : `${trailingPrefix}╰ ╰ `); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (subtypes.result === "seen") { | ||||||||||||||||||||||
| text += `${trailingPrefix}╰ subtypes: ...\n`; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| else if (subtypes.result === "show") { | ||||||||||||||||||||||
| if (!ts.some(subtypes.values)) { | ||||||||||||||||||||||
| text += `${trailingPrefix}╰ subtypes: none\n`; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| else { | ||||||||||||||||||||||
| text += `${prefix}├ subtypes:\n`; | ||||||||||||||||||||||
| for (let i = 0; i < subtypes.values.length; i++) { | ||||||||||||||||||||||
| const subtype = subtypes.values[i]; | ||||||||||||||||||||||
| const subtypeFile = this.tryFindFileWorker(subtype.file).file; | ||||||||||||||||||||||
| text += `${prefix}│ ╭\n`; | ||||||||||||||||||||||
| text += this.formatTypeHierarchyItem(subtypeFile, subtype, TypeHierarchyItemDirection.Subtypes, seen, `${prefix}│ │ `, i < subtypes.values.length - 1 ? `${prefix}│ ╰ ` : `${trailingPrefix}╰ ╰ `); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return text; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| private formatTypeHierarchy(item: ts.TypeHierarchyItem | undefined): string { | ||||||||||||||||||||||
| let text = ""; | ||||||||||||||||||||||
| if (item) { | ||||||||||||||||||||||
| const file = this.tryFindFileWorker(item.file).file; | ||||||||||||||||||||||
| text += this.formatTypeHierarchyItem(file, item, TypeHierarchyItemDirection.Root, new Map(), ""); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return text; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public baselineTypeHierarchy(): void { | ||||||||||||||||||||||
| const item = this.languageService.prepareTypeHierarchy(this.activeFile.fileName, this.currentCaretPosition); | ||||||||||||||||||||||
| const text = item ? ts.mapOneOrMany(item, hierarchyItem => this.formatTypeHierarchy(hierarchyItem), result => result.join("")) : "none"; | ||||||||||||||||||||||
|
||||||||||||||||||||||
| const text = item ? ts.mapOneOrMany(item, hierarchyItem => this.formatTypeHierarchy(hierarchyItem), result => result.join("")) : "none"; | |
| let text: string; | |
| if (item) { | |
| const formatHierarchyItem = (hierarchyItem: ts.TypeHierarchyItem) => this.formatTypeHierarchy(hierarchyItem); | |
| const concatenateResults = (results: string[]) => results.join(""); | |
| text = ts.mapOneOrMany(item, formatHierarchyItem, concatenateResults); | |
| } | |
| else { | |
| text = "none"; | |
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| export * from "../typeHierarchy.js"; |
Oops, something went wrong.
Oops, something went wrong.
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.
Using
JSON.stringifyfor creating cache keys can be expensive when called frequently. Consider using a simpler concatenation like${item.file}|${item.span.start}|${item.span.length}|${direction}for better performance.