Skip to content

Conversation

@a7maad-ayman
Copy link

@a7maad-ayman a7maad-ayman commented Dec 25, 2025

This PR refines jump-to-definition behavior by excluding literal values from definition lookups.

Problem

Currently, invoking jump-to-definition on literal values (strings, integers, floats, booleans, nil) triggers a definition lookup that returns no meaningful result, since literals are concrete values written directly in source code with no declaration to navigate to.

Solution

Detect when the cursor is positioned on a literal token and return an empty response early, avoiding unnecessary SourceKit queries. This aligns with how SourceKit handles non-symbol tokens.

Changes

  • Add literal token detection in SwiftLanguageService using SwiftSyntax
  • Handle string segments, integer/float literals, and boolean/nil keywords
  • Return empty definition response for literal positions in symbolInfo
  • Add tests covering various literal types

Resolves #2368

Co-Authored-By: clemo97 [email protected]

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

This looks great. Just a few comments to clean up the code slightly.

/// Literal value tokens do not support jump-to-definition because they represent
/// concrete values written directly in source code and therefore have no
/// meaningful definition location.
func isPositionOnLiteralToken(_ position: Position, in uri: DocumentURI) async -> Bool
Copy link
Member

Choose a reason for hiding this comment

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

I don’t think we need this as a requirement in LanguageService at all if we are only calling it from SwiftLanguageService.symbolInfo, right?

Comment on lines 1333 to 1337
case .stringSegment,
.integerLiteral,
.floatLiteral:
return true
case .keyword(let keyword) where keyword == .true || keyword == .false || keyword == .nil:
return true
Copy link
Member

Choose a reason for hiding this comment

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

I think this can be simplified slightly

Suggested change
case .stringSegment,
.integerLiteral,
.floatLiteral:
return true
case .keyword(let keyword) where keyword == .true || keyword == .false || keyword == .nil:
return true
case .stringSegment,
.integerLiteral,
.floatLiteral,
.keyword(.true), .keyword(.false), .keyword(.nil)::
return true

XCTAssertEqual(response?.locations?.map(\.uri), [try project.uri(for: "Test.h")])
}

func testDefinitionOnLiteralsShouldReturnEmpty() async throws {
Copy link
Member

Choose a reason for hiding this comment

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

let positions = testClient.openDocument(
"""
let name = "Ayman"
let greeting = "Hello \\(1️⃣name)"
Copy link
Member

Choose a reason for hiding this comment

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

If you start the string literal with #""" you don’t need the double backslash here.

@ahoppen
Copy link
Member

ahoppen commented Jan 3, 2026

I just realized that we have two open PRs open for this from @Clemo97 (#2394) and you, @a7maad-ayman, (#2404). First of all, thanks to both of you for working on this issue 🙏🏽. Given that both PRs contain (nearly) the same contents, you have already converged on a solution. To consolidate the two PRs, I would suggest that:

Would that work for both of you?

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Nice. Looks good to me. Just one more request: Could you add @Clemo97 as a co-author of this PR by squashing your commits and adding Co-Authored-By: clemo97 <[email protected]> to the commit message so both of you are credited properly?

@a7maad-ayman
Copy link
Author

I’ve squashed the commits and added the Co-Authored-By line for @Clemo97 so we’re both properly credited.

Thanks again for the review and for pointing that out — much appreciated.

Screenshot 2026-01-03 at 10 05 02 PM

@ahoppen
Copy link
Member

ahoppen commented Jan 5, 2026

Looks like the Co-Authored-By: line needs to be the last in your commit message. Also, the history in your branch now contains a merge commit, which I personally dislike because it creates a more convoluted git history. The best way to remedy this is probably if you just create a fresh commit with all changes in your PR and then force push it

git reset 7495f5532fdb17184d69518f46a207e596b26c64
# Commit your changes, including Co-Authored-By in the last line of the commit
git push -f

Return empty response when cursor is on literal tokens (strings,
integers, floats, booleans, nil) since they have no declaration.

Resolves swiftlang#2368

Co-Authored-By: clemo97 <[email protected]>
@ahoppen
Copy link
Member

ahoppen commented Jan 9, 2026

Sorry for not triggering a test run when you pushed your changes. There’s a merge conflict now, could you rebase your changes on top of the latest main to resolve it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do not offer jump-to-definition for literals

2 participants