|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import LanguageServerProtocol |
| 14 | +import SwiftSyntax |
| 15 | + |
| 16 | +fileprivate class StartOfIdentifierFinder: SyntaxAnyVisitor { |
| 17 | + let requestedPosition: AbsolutePosition |
| 18 | + var resolvedPosition: AbsolutePosition? |
| 19 | + |
| 20 | + init(position: AbsolutePosition) { |
| 21 | + self.requestedPosition = position |
| 22 | + super.init(viewMode: .sourceAccurate) |
| 23 | + } |
| 24 | + |
| 25 | + override func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind { |
| 26 | + if (node.position...node.endPosition).contains(requestedPosition) { |
| 27 | + return .visitChildren |
| 28 | + } else { |
| 29 | + return .skipChildren |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + override func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind { |
| 34 | + if token.tokenKind.isPunctuation || token.tokenKind == .endOfFile { |
| 35 | + return .skipChildren |
| 36 | + } |
| 37 | + if (token.positionAfterSkippingLeadingTrivia...token.endPositionBeforeTrailingTrivia).contains(requestedPosition) { |
| 38 | + self.resolvedPosition = token.positionAfterSkippingLeadingTrivia |
| 39 | + } |
| 40 | + return .skipChildren |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +extension SwiftLanguageServer { |
| 45 | + /// VS Code considers the position after an identifier as part of an identifier. Ie. if you have `let foo| = 1`, then |
| 46 | + /// it considers the cursor to be positioned at the identifier. This scenario is hit, when selecting an identifier by |
| 47 | + /// double-clicking it and then eg. performing jump-to-definition. In that case VS Code will send the position after |
| 48 | + /// the identifier. |
| 49 | + /// `sourcekitd`, on the other hand, does not consider the position after the identifier as part of the identifier. |
| 50 | + /// To bridge the gap here, normalize any positions inside, or directly after, an identifier to the identifier's |
| 51 | + /// start. |
| 52 | + private func adjustPositionToStartOfIdentifier( |
| 53 | + _ position: Position, |
| 54 | + in snapshot: DocumentSnapshot |
| 55 | + ) async -> Position { |
| 56 | + let tree = await self.syntaxTreeManager.syntaxTree(for: snapshot) |
| 57 | + guard let swiftSyntaxPosition = snapshot.position(of: position) else { |
| 58 | + return position |
| 59 | + } |
| 60 | + let visitor = StartOfIdentifierFinder(position: swiftSyntaxPosition) |
| 61 | + visitor.walk(tree) |
| 62 | + if let resolvedPosition = visitor.resolvedPosition { |
| 63 | + return snapshot.position(of: resolvedPosition) ?? position |
| 64 | + } else { |
| 65 | + return position |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public func symbolInfo(_ req: SymbolInfoRequest) async throws -> [SymbolDetails] { |
| 70 | + let uri = req.textDocument.uri |
| 71 | + let snapshot = try documentManager.latestSnapshot(uri) |
| 72 | + let position = await self.adjustPositionToStartOfIdentifier(req.position, in: snapshot) |
| 73 | + guard let cursorInfo = try await cursorInfo(uri, position..<position) else { |
| 74 | + return [] |
| 75 | + } |
| 76 | + return [cursorInfo.symbolInfo] |
| 77 | + } |
| 78 | +} |
0 commit comments