Skip to content

Conversation

@lokesh-tr
Copy link
Contributor

@lokesh-tr lokesh-tr commented Dec 22, 2025

⚠️ Under Progress ⚠️

  • Explanation:
    Upgrades textDocument/documentHighlight to highlight various return / throw statements corresponding to a function / closure / accessor / init / deinit, and break / continue statements corresponding to a loop, thereby allowing us to not wonder about which statements could cause an exit from a function or loop.

This also makes use of a new API introduced in SwiftLexicalLookup Library in Swift Syntax.

…e/accessor/init/deinit) and break / continue (for/while/repeat...while)
@lokesh-tr lokesh-tr changed the title Implement Document Highlight for all return / throws (function/closure/accessor/init/deinit) and break / continue (for/while/repeat...while) 🚥 Implement Document Highlight for all return / throws (function/closure/accessor/init/deinit) and break / continue (for/while/repeat...while) Dec 22, 2025
@lokesh-tr
Copy link
Contributor Author

This is how it looks:

Inner function return statements are highlighted Outer function return statements are highlighted

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.

Very nice and excited for this. 😍

A preliminary review, as you already noted in the PR description, a few tests would be good 😉

if node.lookupControlStructure() == targetStructure {
highlights.append(
DocumentHighlight(
range: snapshot.absolutePositionRange(of: node.positionAfterSkippingLeadingTrivia..<node.endPosition),
Copy link
Member

Choose a reason for hiding this comment

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

Should we just highlight the control flow keyword (eg. return). I think that might look nicer, especially if the returned expression spans multiple lines.

return nil
}

guard let targetStructure = Syntax(tokenSyntax).parent!.lookupControlStructure() else {
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn’t force unwrap the parent here.

}
}

private func documentSymbolHighlightHelper(
Copy link
Member

Choose a reason for hiding this comment

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

I would give this a more descriptive name which highlights that this is about control flow keyword highlighting.

}
}

override func visit(_ node: BreakStmtSyntax) -> SyntaxVisitorContinueKind {
Copy link
Member

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 repeat the list of control flow nodes if this is a SyntaxAnyVisitor and you add a visitAny override.

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.

Highlight related identifiers for control flow keywords

2 participants