-
Notifications
You must be signed in to change notification settings - Fork 373
add go-to-definition for inlay hints (#2318) #2436
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
Merged
ahoppen
merged 12 commits into
swiftlang:main
from
loveucifer:inlay-hint-go-to-definition
Jan 16, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8e300ee
add go-to-definition for inlay hints (#2318)
loveucifer f46a31b
Use new cursorInfo type declaration fields for inlay hints
loveucifer 27cf04e
Implement textDocument/typeDefinition request (#548)
loveucifer 564a45b
Add go-to-definition for inlay type hints
loveucifer 3630ab9
Address review feedback for inlay hint go-to-definition
loveucifer 9e12af1
Add cross-module inlay hint resolve test
loveucifer 81610b3
Simplify InlayHintResolveData using Position LSPAnyCodable
loveucifer 22e7ac0
Add generated interface support for SDK types in inlay hints
loveucifer 0695d3c
Remove typeDefinition remnants
loveucifer ceaf87b
Run swift-format
loveucifer 0b02672
Restore sourcekitd_uids from main
loveucifer 45b1598
Add InlayHintResolve.swift to CMakeLists.txt
loveucifer 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
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
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add this file to CMakeLists.txt to make the tests pass on Windows? |
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,113 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2014 - 2026 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import Foundation | ||
| import IndexStoreDB | ||
| @_spi(SourceKitLSP) package import LanguageServerProtocol | ||
| import SemanticIndex | ||
| import SourceKitD | ||
| import SourceKitLSP | ||
|
|
||
| extension SwiftLanguageService { | ||
| /// Resolves an inlay hint by looking up the type definition location. | ||
| package func inlayHintResolve(_ req: InlayHintResolveRequest) async throws -> InlayHint { | ||
| let hint = req.inlayHint | ||
|
|
||
| guard hint.kind == .type, | ||
| let resolveData = InlayHintResolveData(fromLSPAny: hint.data) | ||
| else { | ||
| return hint | ||
| } | ||
|
|
||
| // Fail if document version has changed since the hint was created | ||
| let currentSnapshot = try await self.latestSnapshot(for: resolveData.uri) | ||
| guard currentSnapshot.version == resolveData.version else { | ||
| return hint | ||
| } | ||
|
|
||
| let typeLocation = try await lookupTypeDefinitionLocation( | ||
| snapshot: currentSnapshot, | ||
| position: resolveData.position | ||
| ) | ||
|
|
||
| guard let typeLocation else { | ||
| return hint | ||
| } | ||
|
|
||
| if case .string(let labelText) = hint.label { | ||
| return InlayHint( | ||
| position: hint.position, | ||
| label: .parts([InlayHintLabelPart(value: labelText, location: typeLocation)]), | ||
| kind: hint.kind, | ||
| textEdits: hint.textEdits, | ||
| tooltip: hint.tooltip, | ||
| paddingLeft: hint.paddingLeft, | ||
| paddingRight: hint.paddingRight, | ||
| data: hint.data | ||
| ) | ||
| } | ||
|
|
||
| return hint | ||
| } | ||
|
|
||
| /// Looks up the definition location for the type at the given position. | ||
| /// | ||
| /// This is used by inlay hint resolution to enable go-to-definition on type hints. | ||
| /// For SDK types, this returns a location in the generated interface. | ||
| func lookupTypeDefinitionLocation( | ||
| snapshot: DocumentSnapshot, | ||
| position: Position | ||
| ) async throws -> Location? { | ||
| let compileCommand = await self.compileCommand(for: snapshot.uri, fallbackAfterTimeout: false) | ||
|
|
||
| let skreq = sourcekitd.dictionary([ | ||
| keys.cancelOnSubsequentRequest: 0, | ||
| keys.offset: snapshot.utf8Offset(of: position), | ||
| keys.sourceFile: snapshot.uri.sourcekitdSourceFile, | ||
| keys.primaryFile: snapshot.uri.primaryFile?.pseudoPath, | ||
| keys.compilerArgs: compileCommand?.compilerArgs as [any SKDRequestValue]?, | ||
| ]) | ||
|
|
||
| let dict = try await send(sourcekitdRequest: \.cursorInfo, skreq, snapshot: snapshot) | ||
|
|
||
| guard let typeUsr: String = dict[keys.typeUsr] else { | ||
| return nil | ||
| } | ||
|
|
||
| guard let typeInfo = try await cursorInfoFromTypeUSR(typeUsr, in: snapshot) else { | ||
| return nil | ||
| } | ||
|
|
||
| // For local types, return the local declaration | ||
| if let location = typeInfo.symbolInfo.bestLocalDeclaration { | ||
| return location | ||
| } | ||
|
|
||
| // For SDK types, fall back to generated interface | ||
| if typeInfo.symbolInfo.isSystem ?? false, | ||
| let systemModule = typeInfo.symbolInfo.systemModule | ||
| { | ||
| let interfaceDetails = try await self.openGeneratedInterface( | ||
| document: snapshot.uri, | ||
| moduleName: systemModule.moduleName, | ||
| groupName: systemModule.groupName, | ||
| symbolUSR: typeInfo.symbolInfo.usr | ||
| ) | ||
| if let details = interfaceDetails { | ||
| let position = details.position ?? Position(line: 0, utf16index: 0) | ||
| return Location(uri: details.uri, range: Range(position)) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } |
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
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.
You shouldn't need to do this, cursor info should be able to handle a type USR with it
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.
Hmm but the tests fail without the D stripping.
Uh oh!
There was an error while loading. Please reload this page.
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.
Oh interesting, I saw handling of
Demangle::Node::Kind::Typein the code and assumed that was referring toDbut it's actually a different kind. I guess this is okay for now but we really ought to fix this on the sourcekitd side, ideally at the same time as the$s->s:change.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.
okei
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.
Just to add my 2cts, while I’m not a huge fan, I’m fine with having the stripping of
Das a temporary workaround for now as long as we try to adjust sourcekitd to work without this hack in the future.