File: Action.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 (196 lines) | stat: -rw-r--r-- 7,533 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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
//
//===----------------------------------------------------------------------===//

public enum Action: Hashable {
  case cursorInfo(offset: Int)

  /// If expectedResult is non-nil, the results should contain expectedResult, otherwise the results should
  /// not be checked.
  case codeComplete(offset: Int, expectedResult: ExpectedResult?)
  case rangeInfo(offset: Int, length: Int)
  case replaceText(offset: Int, length: Int, text: String)
  case format(offset: Int)
  case typeContextInfo(offset: Int)
  case conformingMethodList(offset: Int)
  case collectExpressionType
  case testModule

  /// If the action is anchored at a specific offset, return that offset, otherwise `nil`.
  var offset: Int? {
    switch self {
    case .cursorInfo(offset: let offset):
      return offset
    case .codeComplete(offset: let offset, expectedResult: _):
      return offset
    case .rangeInfo(offset: let offset, length: _):
      return offset
    case .replaceText(offset: let offset, length: _, text: _):
      return offset
    case .format(offset: let offset):
      return offset
    case .typeContextInfo(offset: let offset):
      return offset
    case .conformingMethodList(offset: let offset):
      return offset
    case .collectExpressionType:
      return nil
    case .testModule:
      return nil
    }
  }
}

extension Action: CustomStringConvertible {
  public var description: String {
    switch self {
    case .cursorInfo(let offset):
      return "CursorInfo at offset \(offset)"
    case .codeComplete(let offset, let expectedResult):
      return "CodeComplete at offset \(offset) with expectedResult \(expectedResult?.name.name ?? "None")"
    case .rangeInfo(let from, let length):
      return "RangeInfo from offset \(from) for length \(length)"
    case .replaceText(let from, let length, let text):
      return "ReplaceText from offset \(from) for length \(length) with \(text.debugDescription)"
    case .format(let offset):
      return "Format line containing offset \(offset)"
    case .typeContextInfo(let offset):
      return "TypeContextInfo at offset \(offset)"
    case .conformingMethodList(let offset):
      return "ConformingMethodList at offset \(offset)"
    case .collectExpressionType:
      return "CollectExpressionType"
    case .testModule:
      return "TestModule"
    }
  }
}

extension Action: Codable {
  enum CodingKeys: String, CodingKey {
    case action, offset, length, text, expectedResult
  }
  public enum BaseAction: String, Codable {
    case cursorInfo, codeComplete, rangeInfo, replaceText, format,
      typeContextInfo, conformingMethodList, collectExpressionType,
      testModule
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    switch try container.decode(BaseAction.self, forKey: .action) {
    case .cursorInfo:
      let offset = try container.decode(Int.self, forKey: .offset)
      self = .cursorInfo(offset: offset)
    case .codeComplete:
      let offset = try container.decode(Int.self, forKey: .offset)
      let expectedResult = try container.decodeIfPresent(ExpectedResult.self, forKey: .expectedResult)
      self = .codeComplete(offset: offset, expectedResult: expectedResult)
    case .rangeInfo:
      let offset = try container.decode(Int.self, forKey: .offset)
      let length = try container.decode(Int.self, forKey: .length)
      self = .rangeInfo(offset: offset, length: length)
    case .replaceText:
      let offset = try container.decode(Int.self, forKey: .offset)
      let length = try container.decode(Int.self, forKey: .length)
      let text = try container.decode(String.self, forKey: .text)
      self = .replaceText(offset: offset, length: length, text: text)
    case .format:
      let offset = try container.decode(Int.self, forKey: .offset)
      self = .format(offset: offset)
    case .typeContextInfo:
      let offset = try container.decode(Int.self, forKey: .offset)
      self = .typeContextInfo(offset: offset)
    case .conformingMethodList:
      let offset = try container.decode(Int.self, forKey: .offset)
      self = .conformingMethodList(offset: offset)
    case .collectExpressionType:
      self = .collectExpressionType
    case .testModule:
      self = .testModule
    }
  }

  public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    switch self {
    case .cursorInfo(let offset):
      try container.encode(BaseAction.cursorInfo, forKey: .action)
      try container.encode(offset, forKey: .offset)
    case .codeComplete(let offset, let expectedResult):
      try container.encode(BaseAction.codeComplete, forKey: .action)
      try container.encode(offset, forKey: .offset)
      try container.encodeIfPresent(expectedResult, forKey: .expectedResult)
    case .rangeInfo(let startOffset, let length):
      try container.encode(BaseAction.rangeInfo, forKey: .action)
      try container.encode(startOffset, forKey: .offset)
      try container.encode(length, forKey: .length)
    case .replaceText(let offset, let length, let text):
      try container.encode(BaseAction.replaceText, forKey: .action)
      try container.encode(offset, forKey: .offset)
      try container.encode(length, forKey: .length)
      try container.encode(text, forKey: .text)
    case .format(let offset):
      try container.encode(BaseAction.format, forKey: .action)
      try container.encode(offset, forKey: .offset)
    case .typeContextInfo(let offset):
      try container.encode(BaseAction.typeContextInfo, forKey: .action)
      try container.encode(offset, forKey: .offset)
    case .conformingMethodList(let offset):
      try container.encode(BaseAction.conformingMethodList, forKey: .action)
      try container.encode(offset, forKey: .offset)
    case .collectExpressionType:
      try container.encode(BaseAction.collectExpressionType, forKey: .action)
    case .testModule:
      try container.encode(BaseAction.testModule, forKey: .action)
    }
  }
}

public struct ExpectedResult: Codable, Hashable {
  public enum Kind: String, Codable { case reference, call, pattern }
  public let name: SwiftName
  public let kind: Kind

  public init(name: SwiftName, kind: Kind) {
    self.name = name
    self.kind = kind
  }
}

public struct SwiftName: Codable, Hashable {
  public let base: String
  public let argLabels: [String]
  public var name: String {
    argLabels.isEmpty
        ? base
        : "\(base)(\(argLabels.map{ "\($0.isEmpty ? "_" : $0):" }.joined()))"
  }

  public init?(_ name: String) {
    if let argStart = name.firstIndex(of: "(") {
      let argEnd = name.firstIndex(of: ")") ?? name.endIndex
      base = String(name[..<argStart])
      argLabels = name[name.index(after: argStart)..<argEnd]
        .split(separator: ":", omittingEmptySubsequences: false)
        .dropLast()
        .map{ $0 == "_" ? "" : String($0)}
    } else {
      base = name
      argLabels = []
    }
  }

  init(base: String, labels: [String]) {
    self.base = base
    self.argLabels = labels.map{ $0 == "_" ? "" : $0 }
  }
}