-
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 1 commit
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 documentSymbolHighlightHelper( | ||
| at: req.position, | ||
| in: snapshot | ||
| ) { | ||
| return highlights | ||
| } | ||
|
|
||
| let relatedIdentifiers = try await self.relatedIdentifiers( | ||
| at: req.position, | ||
|
|
@@ -889,6 +898,67 @@ extension SwiftLanguageService { | |
| } | ||
| } | ||
|
|
||
| private func documentSymbolHighlightHelper( | ||
| 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 targetStructure = Syntax(tokenSyntax).parent!.lookupControlStructure() else { | ||
lokesh-tr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 { | ||
|
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. This could be a |
||
| highlights.append( | ||
| DocumentHighlight( | ||
| range: snapshot.absolutePositionRange(of: node.positionAfterSkippingLeadingTrivia..<node.endPosition), | ||
lokesh-tr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.