File: SKDRequestDictionary.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (115 lines) | stat: -rw-r--r-- 4,026 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#if compiler(>=6)
package import Csourcekitd
import SKLogging
#else
import Csourcekitd
import SKLogging
#endif

#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(CRT)
import CRT
#elseif canImport(Bionic)
import Bionic
#endif

/// Values that can be stored in a `SKDRequestDictionary`.
///
/// - Warning: `SKDRequestDictionary.subscript` and `SKDRequestArray.append`
///   switch exhaustively over this protocol.
///   Do not add new conformances without adding a new case in the subscript and
///   `append` function.
package protocol SKDRequestValue {}

extension String: SKDRequestValue {}
extension Int: SKDRequestValue {}
extension sourcekitd_api_uid_t: SKDRequestValue {}
extension SKDRequestDictionary: SKDRequestValue {}
extension SKDRequestArray: SKDRequestValue {}
extension Array<SKDRequestValue>: SKDRequestValue {}
extension Dictionary<sourcekitd_api_uid_t, SKDRequestValue>: SKDRequestValue {}
extension Optional: SKDRequestValue where Wrapped: SKDRequestValue {}

extension SourceKitD {
  /// Create a `SKDRequestDictionary` from the given dictionary.
  package func dictionary(_ dict: [sourcekitd_api_uid_t: SKDRequestValue]) -> SKDRequestDictionary {
    let result = SKDRequestDictionary(sourcekitd: self)
    for (key, value) in dict {
      result.set(key, to: value)
    }
    return result
  }
}

package final class SKDRequestDictionary: Sendable {
  nonisolated(unsafe) let dict: sourcekitd_api_object_t
  private let sourcekitd: SourceKitD

  package init(_ dict: sourcekitd_api_object_t? = nil, sourcekitd: SourceKitD) {
    self.dict = dict ?? sourcekitd.api.request_dictionary_create(nil, nil, 0)!
    self.sourcekitd = sourcekitd
  }

  deinit {
    sourcekitd.api.request_release(dict)
  }

  package func set(_ key: sourcekitd_api_uid_t, to newValue: SKDRequestValue) {
    switch newValue {
    case let newValue as String:
      sourcekitd.api.request_dictionary_set_string(dict, key, newValue)
    case let newValue as Int:
      sourcekitd.api.request_dictionary_set_int64(dict, key, Int64(newValue))
    case let newValue as sourcekitd_api_uid_t:
      sourcekitd.api.request_dictionary_set_uid(dict, key, newValue)
    case let newValue as SKDRequestDictionary:
      sourcekitd.api.request_dictionary_set_value(dict, key, newValue.dict)
    case let newValue as SKDRequestArray:
      sourcekitd.api.request_dictionary_set_value(dict, key, newValue.array)
    case let newValue as Array<SKDRequestValue>:
      self.set(key, to: sourcekitd.array(newValue))
    case let newValue as Dictionary<sourcekitd_api_uid_t, SKDRequestValue>:
      self.set(key, to: sourcekitd.dictionary(newValue))
    case let newValue as Optional<SKDRequestValue>:
      if let newValue {
        self.set(key, to: newValue)
      }
    default:
      preconditionFailure("Unknown type conforming to SKDRequestValue")
    }
  }
}

extension SKDRequestDictionary: CustomStringConvertible {
  package var description: String {
    let ptr = sourcekitd.api.request_description_copy(dict)!
    defer { free(ptr) }
    return String(cString: ptr)
  }
}

extension SKDRequestDictionary: CustomLogStringConvertible {
  package var redactedDescription: String {
    // TODO: Implement a better redacted log that contains keys, number of
    // elements in an array but not the data itself.
    // (https://github.com/swiftlang/sourcekit-lsp/issues/1598)
    return "<\(description.filter(\.isNewline).count) lines>"
  }
}