|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 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 Dispatch |
| 14 | +import LanguageServerProtocol |
| 15 | +import SourceKitD |
| 16 | + |
| 17 | +/// A typed variable as returned by sourcekitd's CollectVariableType. |
| 18 | +struct VariableTypeInfo { |
| 19 | + /// Range of the variable identifier in the source file. |
| 20 | + var range: Range<Position> |
| 21 | + /// The printed type of the variable. |
| 22 | + var printedType: String |
| 23 | + /// Whether the variable has an explicit type annotation in the source file. |
| 24 | + var hasExplicitType: Bool |
| 25 | + |
| 26 | + init?(_ dict: SKDResponseDictionary, in snapshot: DocumentSnapshot) { |
| 27 | + let keys = dict.sourcekitd.keys |
| 28 | + |
| 29 | + guard let offset: Int = dict[keys.variable_offset], |
| 30 | + let length: Int = dict[keys.variable_length], |
| 31 | + let startIndex = snapshot.positionOf(utf8Offset: offset), |
| 32 | + let endIndex = snapshot.positionOf(utf8Offset: offset + length), |
| 33 | + let printedType: String = dict[keys.variable_type], |
| 34 | + let hasExplicitType: Bool = dict[keys.variable_type_explicit] else { |
| 35 | + return nil |
| 36 | + } |
| 37 | + |
| 38 | + self.range = startIndex..<endIndex |
| 39 | + self.printedType = printedType |
| 40 | + self.hasExplicitType = hasExplicitType |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +enum VariableTypeInfoError: Error, Equatable { |
| 45 | + /// The given URL is not a known document. |
| 46 | + case unknownDocument(DocumentURI) |
| 47 | + |
| 48 | + /// The underlying sourcekitd request failed with the given error. |
| 49 | + case responseError(ResponseError) |
| 50 | +} |
| 51 | + |
| 52 | +extension SwiftLanguageServer { |
| 53 | + /// Must be called on self.queue. |
| 54 | + private func _variableTypeInfos( |
| 55 | + _ uri: DocumentURI, |
| 56 | + _ range: Range<Position>? = nil, |
| 57 | + _ completion: @escaping (Swift.Result<[VariableTypeInfo], VariableTypeInfoError>) -> Void |
| 58 | + ) { |
| 59 | + dispatchPrecondition(condition: .onQueue(queue)) |
| 60 | + |
| 61 | + guard let snapshot = documentManager.latestSnapshot(uri) else { |
| 62 | + return completion(.failure(.unknownDocument(uri))) |
| 63 | + } |
| 64 | + |
| 65 | + let keys = self.keys |
| 66 | + |
| 67 | + let skreq = SKDRequestDictionary(sourcekitd: sourcekitd) |
| 68 | + skreq[keys.request] = requests.variable_type |
| 69 | + skreq[keys.sourcefile] = snapshot.document.uri.pseudoPath |
| 70 | + |
| 71 | + if let range = range, |
| 72 | + let start = snapshot.utf8Offset(of: range.lowerBound), |
| 73 | + let end = snapshot.utf8Offset(of: range.upperBound) { |
| 74 | + skreq[keys.offset] = start |
| 75 | + skreq[keys.length] = end - start |
| 76 | + } |
| 77 | + |
| 78 | + // FIXME: SourceKit should probably cache this for us |
| 79 | + if let compileCommand = self.commandsByFile[uri] { |
| 80 | + skreq[keys.compilerargs] = compileCommand.compilerArgs |
| 81 | + } |
| 82 | + |
| 83 | + let handle = self.sourcekitd.send(skreq, self.queue) { result in |
| 84 | + guard let dict = result.success else { |
| 85 | + return completion(.failure(.responseError(ResponseError(result.failure!)))) |
| 86 | + } |
| 87 | + |
| 88 | + guard let skVariableTypeInfos: SKDResponseArray = dict[keys.variable_type_list] else { |
| 89 | + return completion(.success([])) |
| 90 | + } |
| 91 | + |
| 92 | + var variableTypeInfos: [VariableTypeInfo] = [] |
| 93 | + variableTypeInfos.reserveCapacity(skVariableTypeInfos.count) |
| 94 | + |
| 95 | + skVariableTypeInfos.forEach { (_, skVariableTypeInfo) -> Bool in |
| 96 | + guard let info = VariableTypeInfo(skVariableTypeInfo, in: snapshot) else { |
| 97 | + assertionFailure("VariableTypeInfo failed to deserialize") |
| 98 | + return true |
| 99 | + } |
| 100 | + variableTypeInfos.append(info) |
| 101 | + return true |
| 102 | + } |
| 103 | + |
| 104 | + completion(.success(variableTypeInfos)) |
| 105 | + } |
| 106 | + |
| 107 | + // FIXME: cancellation |
| 108 | + _ = handle |
| 109 | + } |
| 110 | + |
| 111 | + /// Provides typed variable declarations in a document. |
| 112 | + /// |
| 113 | + /// - Parameters: |
| 114 | + /// - url: Document URL in which to perform the request. Must be an open document. |
| 115 | + /// - completion: Completion block to asynchronously receive the VariableTypeInfos, or error. |
| 116 | + func variableTypeInfos( |
| 117 | + _ uri: DocumentURI, |
| 118 | + _ range: Range<Position>? = nil, |
| 119 | + _ completion: @escaping (Swift.Result<[VariableTypeInfo], VariableTypeInfoError>) -> Void |
| 120 | + ) { |
| 121 | + queue.async { |
| 122 | + self._variableTypeInfos(uri, range, completion) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments