File: OpenInterface.swift

package info (click to toggle)
swiftlang 6.1.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,791,644 kB
  • sloc: cpp: 9,901,738; ansic: 2,201,433; 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 (138 lines) | stat: -rw-r--r-- 4,799 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 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)
import Foundation
package import LanguageServerProtocol
import SKLogging
import SKUtilities
import SourceKitD
#else
import Foundation
import LanguageServerProtocol
import SKLogging
import SKUtilities
import SourceKitD
#endif

struct GeneratedInterfaceInfo {
  var contents: String
}

extension SwiftLanguageService {
  package func openGeneratedInterface(
    document: DocumentURI,
    moduleName: String,
    groupName: String?,
    symbolUSR symbol: String?
  ) async throws -> GeneratedInterfaceDetails? {
    // Name of interface module name with group names appended
    let name =
      if let groupName {
        "\(moduleName).\(groupName.replacing("/", with: "."))"
      } else {
        moduleName
      }
    let interfaceFilePath = self.generatedInterfacesPath.appendingPathComponent("\(name).swiftinterface")
    let interfaceDocURI = DocumentURI(interfaceFilePath)
    // has interface already been generated
    if let snapshot = try? await self.latestSnapshot(for: interfaceDocURI) {
      return await self.generatedInterfaceDetails(
        uri: interfaceDocURI,
        snapshot: snapshot,
        symbol: symbol
      )
    } else {
      let interfaceInfo = try await self.generatedInterfaceInfo(
        document: document,
        moduleName: moduleName,
        groupName: groupName,
        interfaceURI: interfaceDocURI
      )
      try interfaceInfo.contents.write(to: interfaceFilePath, atomically: true, encoding: String.Encoding.utf8)
      let snapshot = DocumentSnapshot(
        uri: interfaceDocURI,
        language: .swift,
        version: 0,
        lineTable: LineTable(interfaceInfo.contents)
      )
      let result = await self.generatedInterfaceDetails(
        uri: interfaceDocURI,
        snapshot: snapshot,
        symbol: symbol
      )
      _ = await orLog("Closing generated interface") {
        try await sendSourcekitdRequest(closeDocumentSourcekitdRequest(uri: interfaceDocURI), fileContents: nil)
      }
      return result
    }
  }

  /// Open the Swift interface for a module.
  ///
  /// - Parameters:
  ///   - document: The document whose compiler arguments should be used to generate the interface.
  ///   - moduleName: The module to generate an index for.
  ///   - groupName: The module group name.
  ///   - interfaceURI: The file where the generated interface should be written.
  ///
  /// - Important: This opens a document with name `interfaceURI.pseudoPath` in sourcekitd. The caller is responsible
  ///   for ensuring that the document will eventually get closed in sourcekitd again.
  private func generatedInterfaceInfo(
    document: DocumentURI,
    moduleName: String,
    groupName: String?,
    interfaceURI: DocumentURI
  ) async throws -> GeneratedInterfaceInfo {
    let keys = self.keys
    let skreq = sourcekitd.dictionary([
      keys.request: requests.editorOpenInterface,
      keys.moduleName: moduleName,
      keys.groupName: groupName,
      keys.name: interfaceURI.pseudoPath,
      keys.synthesizedExtension: 1,
      keys.compilerArgs: await self.buildSettings(for: document, fallbackAfterTimeout: false)?.compilerArgs
        as [SKDRequestValue]?,
    ])

    let dict = try await sendSourcekitdRequest(skreq, fileContents: nil)
    return GeneratedInterfaceInfo(contents: dict[keys.sourceText] ?? "")
  }

  private func generatedInterfaceDetails(
    uri: DocumentURI,
    snapshot: DocumentSnapshot,
    symbol: String?
  ) async -> GeneratedInterfaceDetails {
    do {
      guard let symbol = symbol else {
        return GeneratedInterfaceDetails(uri: uri, position: nil)
      }
      let keys = self.keys
      let skreq = sourcekitd.dictionary([
        keys.request: requests.editorFindUSR,
        keys.sourceFile: uri.sourcekitdSourceFile,
        keys.primaryFile: uri.primaryFile?.pseudoPath,
        keys.usr: symbol,
      ])

      let dict = try await sendSourcekitdRequest(skreq, fileContents: snapshot.text)
      if let offset: Int = dict[keys.offset] {
        return GeneratedInterfaceDetails(uri: uri, position: snapshot.positionOf(utf8Offset: offset))
      } else {
        return GeneratedInterfaceDetails(uri: uri, position: nil)
      }
    } catch {
      return GeneratedInterfaceDetails(uri: uri, position: nil)
    }
  }
}