-
Notifications
You must be signed in to change notification settings - Fork 332
🚥 Implement Document Highlight for all return / throws (function/closure/accessor/init/deinit) and break / continue (for/while/repeat...while) #2403
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
base: main
Are you sure you want to change the base?
Changes from all commits
29f4e1e
912eb60
aae3543
11124e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,8 @@ package import SwiftSyntax | |
| package import ToolchainRegistry | ||
| @_spi(SourceKitLSP) import ToolsProtocolsSwiftExtensions | ||
|
|
||
| @_spi(Experimental) import SwiftLexicalLookup | ||
|
|
||
| #if os(Windows) | ||
| import WinSDK | ||
| #endif | ||
|
|
@@ -875,6 +877,13 @@ extension SwiftLanguageService { | |
|
|
||
| package func documentSymbolHighlight(_ req: DocumentHighlightRequest) async throws -> [DocumentHighlight]? { | ||
| let snapshot = try await self.latestSnapshot(for: req.textDocument.uri) | ||
|
|
||
| if let highlights = try await controlFlowExitKeywordHighlight( | ||
| at: req.position, | ||
| in: snapshot | ||
| ) { | ||
| return highlights | ||
| } | ||
|
|
||
| let relatedIdentifiers = try await self.relatedIdentifiers( | ||
| at: req.position, | ||
|
|
@@ -889,6 +898,67 @@ extension SwiftLanguageService { | |
| } | ||
| } | ||
|
|
||
| private func controlFlowExitKeywordHighlight( | ||
| at position: Position, | ||
| in snapshot: DocumentSnapshot | ||
| ) async throws -> [DocumentHighlight]? { | ||
| let syntaxTree = await syntaxTreeManager.syntaxTree(for: snapshot) | ||
|
|
||
| guard let tokenSyntax = syntaxTree.token(at: snapshot.absolutePosition(of: position)) else { | ||
| return nil | ||
| } | ||
|
|
||
| guard let parent = Syntax(tokenSyntax).parent, let targetStructure = parent.lookupControlStructure() else { | ||
| return nil | ||
| } | ||
| class ControlFlowHighlighter: SyntaxVisitor { | ||
| var highlights: [DocumentHighlight] = [] | ||
| let targetStructure: Syntax | ||
| let snapshot: DocumentSnapshot | ||
|
|
||
| init(targetStructure: Syntax, snapshot: DocumentSnapshot) { | ||
| self.targetStructure = targetStructure | ||
| self.snapshot = snapshot | ||
| super.init(viewMode: .sourceAccurate) | ||
| } | ||
|
|
||
| private func addHighlightIfMatches(_ node: some SyntaxProtocol) { | ||
| if node.lookupControlStructure() == targetStructure { | ||
| highlights.append( | ||
| DocumentHighlight( | ||
| range: snapshot.absolutePositionRange(of: node.firstToken(viewMode: .sourceAccurate).map { $0.positionAfterSkippingLeadingTrivia..<$0.endPositionBeforeTrailingTrivia } ?? node.positionAfterSkippingLeadingTrivia..<node.endPositionBeforeTrailingTrivia), | ||
|
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 run swift-format on your changes?
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. While currently the
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. Instead of |
||
| kind: .read | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| override func visit(_ node: BreakStmtSyntax) -> SyntaxVisitorContinueKind { | ||
|
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. You shouldn’t need to repeat the list of control flow nodes if this is a |
||
| addHighlightIfMatches(node) | ||
| return .skipChildren | ||
| } | ||
|
|
||
| override func visit(_ node: ContinueStmtSyntax) -> SyntaxVisitorContinueKind { | ||
| addHighlightIfMatches(node) | ||
| return .skipChildren | ||
| } | ||
|
|
||
| override func visit(_ node: ReturnStmtSyntax) -> SyntaxVisitorContinueKind { | ||
| addHighlightIfMatches(node) | ||
| return .skipChildren | ||
| } | ||
|
|
||
| override func visit(_ node: ThrowStmtSyntax) -> SyntaxVisitorContinueKind { | ||
| addHighlightIfMatches(node) | ||
| return .skipChildren | ||
| } | ||
| } | ||
|
|
||
| let highlighter = ControlFlowHighlighter(targetStructure: targetStructure, snapshot: snapshot) | ||
| highlighter.walk(targetStructure) | ||
| return highlighter.highlights.isEmpty ? nil : highlighter.highlights | ||
| } | ||
|
|
||
| package func codeAction(_ req: CodeActionRequest) async throws -> CodeActionRequestResponse? { | ||
| if (try? ReferenceDocumentURL(from: req.textDocument.uri)) != nil { | ||
| // Do not show code actions in reference documents | ||
|
|
||
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.
This could be a
guardto avoid the nesting level.