File: SymbolInfoRequest.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (159 lines) | stat: -rw-r--r-- 5,852 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 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
//
//===----------------------------------------------------------------------===//

/// Request for semantic information about the symbol at a given location **(LSP Extension)**.
///
/// This request looks up the symbol (if any) at a given text document location and returns
/// SymbolDetails for that location, including information such as the symbol's USR. The symbolInfo
/// request is not primarily designed for editors, but instead as an implementation detail of how
/// one LSP implementation (e.g. SourceKit) gets information from another (e.g. clangd) to use in
/// performing index queries or otherwise implementing the higher level requests such as definition.
///
/// - Parameters:
///   - textDocument: The document in which to lookup the symbol location.
///   - position: The document location at which to lookup symbol information.
///
/// - Returns: `[SymbolDetails]` for the given location, which may have multiple elements if there are
///   multiple references, or no elements if there is no symbol at the given location.
///
/// ### LSP Extension
///
/// This request is an extension to LSP supported by SourceKit-LSP and clangd. It does *not* require
/// any additional client or server capabilities to use.
public struct SymbolInfoRequest: TextDocumentRequest, Hashable {
  public static let method: String = "textDocument/symbolInfo"
  public typealias Response = [SymbolDetails]

  /// The document in which to lookup the symbol location.
  public var textDocument: TextDocumentIdentifier

  /// The document location at which to lookup symbol information.
  public var position: Position

  public init(textDocument: TextDocumentIdentifier, position: Position) {
    self.textDocument = textDocument
    self.position = position
  }
}

/// Detailed information about a symbol, such as the response to a `SymbolInfoRequest`
/// **(LSP Extension)**.
public struct SymbolDetails: ResponseType, Hashable {
  public struct ModuleInfo: Codable, Hashable, Sendable {
    /// The name of the module in which the symbol is defined.
    public let moduleName: String

    /// If the symbol is defined within a subgroup of a module, the name of the group. Otherwise `nil`.
    public let groupName: String?

    public init(moduleName: String, groupName: String? = nil) {
      self.moduleName = moduleName
      self.groupName = groupName
    }
  }

  /// The name of the symbol, if any.
  public var name: String?

  /// The name of the containing type for the symbol, if any.
  ///
  /// For example, in the following snippet, the `containerName` of `foo()` is `C`.
  ///
  /// ```c++
  /// class C {
  ///   void foo() {}
  /// }
  /// ```
  public var containerName: String?

  /// The USR of the symbol, if any.
  public var usr: String?

  /// An opaque identifier in a format known only to clangd.
  // public var id: String?

  /// Best known declaration or definition location without global knowledge.
  ///
  /// For a local or private variable, this is generally the canonical definition location -
  /// appropriate as a response to a `textDocument/definition` request. For global symbols this is
  /// the best known location within a single compilation unit. For example, in C++ this might be
  /// the declaration location from a header as opposed to the definition in some other
  /// translation unit.
  public var bestLocalDeclaration: Location? = nil

  /// The kind of the symbol
  public var kind: SymbolKind?

  /// Whether the symbol is a dynamic call for which it isn't known which method will be invoked at runtime. This is
  /// the case for protocol methods and class functions.
  ///
  /// Optional because `clangd` does not return whether a symbol is dynamic.
  public var isDynamic: Bool?

  /// Whether this symbol is defined in the SDK or standard library.
  ///
  /// This property only applies to Swift symbols.
  public var isSystem: Bool?

  /// If the symbol is dynamic, the USRs of the types that might be called.
  ///
  /// This is relevant in the following cases
  /// ```swift
  /// class A {
  ///   func doThing() {}
  /// }
  /// class B: A {}
  /// class C: B {
  ///   override func doThing() {}
  /// }
  /// class D: A {
  ///   override func doThing() {}
  /// }
  /// func test(value: B) {
  ///   value.doThing()
  /// }
  /// ```
  ///
  /// The USR of the called function in `value.doThing` is `A.doThing` (or its
  /// mangled form) but it can never call `D.doThing`. In this case, the
  /// receiver USR would be `B`, indicating that only overrides of subtypes in
  /// `B` may be called dynamically.
  public var receiverUsrs: [String]?

  /// If the symbol is defined in a module that doesn't have source information associated with it, the name and group
  /// and group name that defines this symbol.
  ///
  /// This property only applies to Swift symbols.
  public var systemModule: ModuleInfo?

  public init(
    name: String?,
    containerName: String?,
    usr: String?,
    bestLocalDeclaration: Location?,
    kind: SymbolKind?,
    isDynamic: Bool?,
    isSystem: Bool?,
    receiverUsrs: [String]?,
    systemModule: ModuleInfo?
  ) {
    self.name = name
    self.containerName = containerName
    self.usr = usr
    self.bestLocalDeclaration = bestLocalDeclaration
    self.kind = kind
    self.isDynamic = isDynamic
    self.isSystem = isSystem
    self.receiverUsrs = receiverUsrs
    self.systemModule = systemModule
  }
}