Skip to content

Commit 2e2ae18

Browse files
committed
Add VariableTypeInfo structure
1 parent 5eaee0f commit 2e2ae18

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Sources/SourceKitLSP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ target_sources(SourceKitLSP PRIVATE
3030
Swift/SourceKitD+ResponseError.swift
3131
Swift/SwiftCommand.swift
3232
Swift/SwiftLanguageServer.swift)
33+
Swift/VariableTypeInfo.swift
3334
set_target_properties(SourceKitLSP PROPERTIES
3435
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
3536
# TODO(compnerd) reduce the exposure here, why is everything PUBLIC-ly linked?
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
24+
init?(_ dict: SKDResponseDictionary, in snapshot: DocumentSnapshot) {
25+
let keys = dict.sourcekitd.keys
26+
27+
guard let offset: Int = dict[keys.variable_offset],
28+
let length: Int = dict[keys.variable_length],
29+
let startIndex = snapshot.positionOf(utf8Offset: offset),
30+
let endIndex = snapshot.positionOf(utf8Offset: offset + length),
31+
let printedType: String = dict[keys.variable_type] else {
32+
return nil
33+
}
34+
35+
self.range = startIndex..<endIndex
36+
self.printedType = printedType
37+
}
38+
}

0 commit comments

Comments
 (0)