File: SourceKitDocument.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 (776 lines) | stat: -rw-r--r-- 33,647 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

import SwiftSourceKit
import SwiftDiagnostics
import SwiftParser
import SwiftParserDiagnostics
import SwiftSyntax
import SwiftOperators
import Common
import Foundation
import InstructionCount

let SOURCEKIT_REQUEST_TIMEOUT = 300 // seconds

class SourceKitDocument {
  let swiftc: String
  let args: CompilerArgs
  /// Extra code completion options to pass to sourcekitd for each code completion request in the `key.codecomplete.options` dictionary.
  /// `key.codecomplete.` will automatically be prepended to these options.
  /// If the value is convertible to an integer, it will be passed to sourcekitd as an integer.
  let extraCodeCompleteOptions: [String: String]
  let tempDir: URL
  let connection: SourceKitdService
  let containsErrors: Bool

  private var sourceState: SourceState? = nil
  private var tree: SourceFileSyntax? = nil
  private var lookaheadRanges: LookaheadRanges? = nil
  private var converter: SourceLocationConverter? = nil

  private var tempModulePath: URL {
    tempDir.appendingPathComponent("TestModForStressTester.swiftmodule")
  }

  private var documentInfo: DocumentInfo {
    var modification: DocumentModification? = nil
    if let state = sourceState, state.wasModified {
      modification = DocumentModification(mode: state.mode, content: state.source)
    }
    return DocumentInfo(path: args.forFile.path, modification: modification)
  }

  init(swiftc: String, args: CompilerArgs,
       tempDir: URL, connection: SourceKitdService,
       extraCodeCompleteOptions: [String: String],
       containsErrors: Bool = false) {
    self.swiftc = swiftc
    self.args = args
    self.extraCodeCompleteOptions = extraCodeCompleteOptions
    self.tempDir = tempDir
    self.connection = connection
    self.containsErrors = containsErrors
  }

  func open(rewriteMode: RewriteMode) throws -> (SourceFileSyntax, SourceKitdResponse) {
    let source = try! String(contentsOf: args.forFile, encoding: .utf8)
    sourceState = SourceState(rewriteMode: rewriteMode, content: source)
    return try openOrUpdate(path: args.forFile.path)
  }

  func update(updateSource: (inout SourceState) -> Void) throws -> (SourceFileSyntax, SourceKitdResponse) {
    var sourceState = self.sourceState!
    try close()
    updateSource(&sourceState)
    self.sourceState = sourceState
    return try openOrUpdate(source: sourceState.source)
  }

  private func openOrUpdate(path: String? = nil, source: String? = nil)
  throws -> (SourceFileSyntax, SourceKitdResponse) {
    let request = SourceKitdRequest(uid: .request_EditorOpen)
    if let path = path {
      request.addParameter(.key_SourceFile, value: path)
    } else if let source = source {
      request.addParameter(.key_SourceText, value: source)
    }
    request.addParameter(.key_Name, value: args.forFile.path)
    request.addParameter(.key_EnableSyntaxMap, value: 0)
    request.addParameter(.key_EnableStructure, value: 0)
    request.addParameter(.key_SyntacticOnly, value: 1)
    request.addCompilerArgs(args.sourcekitdArgs)

    let info = RequestInfo.editorOpen(document: documentInfo)
    let response = try sendWithTimeout(request, info: info)

    try updateSyntaxTree(request: info)

    return (tree!, response)
  }

  @discardableResult
  func close() throws -> SourceKitdResponse {
    sourceState = nil

    let request = SourceKitdRequest(uid: .request_EditorClose)
    request.addParameter(.key_SourceFile, value: args.forFile.path)
    request.addParameter(.key_Name, value: args.forFile.path)

    let info = RequestInfo.editorClose(document: documentInfo)
    let response = try sendWithTimeout(request, info: info)
    return response
  }

  func rangeInfo(offset: Int, length: Int) throws -> (RequestInfo, SourceKitdResponse) {
    let request = SourceKitdRequest(uid: .request_RangeInfo)

    request.addParameter(.key_SourceFile, value: args.forFile.path)
    request.addParameter(.key_Offset, value: offset)
    request.addParameter(.key_Length, value: length)
    request.addParameter(.key_RetrieveRefactorActions, value: 1)
    request.addCompilerArgs(args.sourcekitdArgs)

    let info = RequestInfo.rangeInfo(document: documentInfo, offset: offset,
                                     length: length, args: args.sourcekitdArgs)
    let response = try sendWithTimeout(request, info: info)

    if let actions = response.value.getOptional(.key_RefactorActions)?.getArray() {
      for i in 0 ..< actions.count {
        let action = actions.getDictionary(i)
        guard action.getOptional(.key_ActionUnavailableReason) == nil else { continue }
        let actionName = action.getString(.key_ActionName)
        let kind = action.getUID(.key_ActionUID)
        _ = try semanticRefactoring(actionKind: kind, actionName: actionName,
                                    offset: offset)
      }
    }

    return (info, response)
  }

  func cursorInfo(offset: Int) throws -> (request: RequestInfo, response: SourceKitdResponse, instructions: Int, reusingASTContext: Bool) {
    let request = SourceKitdRequest(uid: .request_CursorInfo)

    request.addParameter(.key_SourceFile, value: args.forFile.path)
    request.addParameter(.key_Offset, value: offset)
    request.addParameter(.key_RetrieveRefactorActions, value: 1)
    request.addParameter(.key_RetrieveSymbolGraph, value: 1)
    request.addParameter(.key_VerifySolverBasedCursorInfo, value: 1)
    request.addCompilerArgs(args.sourcekitdArgs)

    let info = RequestInfo.cursorInfo(document: documentInfo, offset: offset,
                                      args: args.sourcekitdArgs)
    let (response, instructions) = try sendWithTimeoutMeasuringInstructions(request, info: info) { response in
      if !containsErrors {
        if let typeName = response.value.getOptional(.key_TypeName)?.getString(), typeName.contains("<<error type>>") {
          throw SourceKitError.failed(.errorTypeInResponse, request: info, response: response.value.description)
        }
      }
    }

    let reusingASTContext = response.value.getBool(.key_ReusingASTContext)

    let symbolName = response.value.getOptional(.key_Name)?.getString()
    if let actions = response.value.getOptional(.key_RefactorActions)?.getArray() {
      for i in 0 ..< actions.count {
        let action = actions.getDictionary(i)
        guard action.getOptional(.key_ActionUnavailableReason) == nil else { continue }
        let actionName = action.getString(.key_ActionName)
        switch actionName {
        case "Global Rename", "Local Rename":
          continue
        default:
          let kind = action.getUID(.key_ActionUID)
          _ = try semanticRefactoring(actionKind: kind, actionName: actionName,
                                      offset: offset)
        }
      }
    }

    return (info, response, instructions, reusingASTContext)
  }

  func format(offset: Int) throws -> (RequestInfo, SourceKitdResponse) {
    let request = SourceKitdRequest(uid: .request_EditorFormatText)
    guard let converter = self.converter else { fatalError("didn't call open?") }

    request.addParameter(.key_SourceFile, value: args.forFile.path)
    request.addParameter(.key_Name, value: args.forFile.path)
    request.addParameter(.key_SourceText, value: "")

    let options = request.addDictionaryParameter(.key_FormatOptions)
    options.add(.key_IndentSwitchCase, value: 0)
    options.add(.key_IndentWidth, value: 2)
    options.add(.key_TabWidth, value: 2)
    options.add(.key_UseTabs, value: 0)

    let location = converter.location(for: AbsolutePosition(utf8Offset: offset))
    request.addParameter(.key_Line, value: location.line)
    request.addParameter(.key_Length, value: 1)

    let info = RequestInfo.format(document: documentInfo, offset: offset)
    let response = try sendWithTimeout(request, info: info)

    return (info, response)
  }

  func semanticRefactoring(actionKind: SourceKitdUID, actionName: String,
                           offset: Int) throws -> (RequestInfo, SourceKitdResponse) {
    let request = SourceKitdRequest(uid: .request_SemanticRefactoring)
    guard let converter = self.converter else { fatalError("didn't call open?") }

    request.addParameter(.key_ActionUID, value: actionKind)
    request.addParameter(.key_SourceFile, value: args.forFile.path)
    let location = converter.location(for: AbsolutePosition(utf8Offset: offset))
    request.addParameter(.key_Line, value: location.line)
    request.addParameter(.key_Column, value: location.column)
    request.addCompilerArgs(args.sourcekitdArgs)

    let info = RequestInfo.semanticRefactoring(document: documentInfo,
                                               offset: offset,
                                               kind: actionName,
                                               args: args.sourcekitdArgs)
    let response = try sendWithTimeout(request, info: info)

    return (info, response)
  }

  /// Retrieves the number of instructions the SourceKit process has executed
  /// since it was launched. Returns 0 if the number of executed instructions
  /// could not be determined.
  private func getSourceKitInstructionCount() throws -> Int {
    let request = SourceKitdRequest(uid: .request_Statistics)
    let response = try sendWithTimeout(request, info: .statistics)
    let results = response.value.getArray(.key_Results)
    for i in 0..<results.count {
      let stat = results.getDictionary(i)
      if stat.getUID(.key_Kind) == .kind_StatInstructionCount {
        return stat.getInt(.key_Value)
      }
    }
    return 0
  }

  /// Build the codecomplete.open or codecomplete.close request.
  /// The two requests share the exact same options except for the request name.
  private func buildCompletionRequest(request: SourceKitdUID, offset: Int, extraOptions: [String: String]) -> SourceKitdRequest {
    let location = converter!.location(for: AbsolutePosition(utf8Offset: offset))

    let request = SourceKitdRequest(uid: request)
    request.addParameter(.key_SourceFile, value: args.forFile.path)
    request.addParameter(.key_Name, value: args.forFile.path)
    if let sourceState = sourceState {
      request.addParameter(.key_SourceText, value: sourceState.source)
    }
    request.addParameter(.key_Offset, value: offset)
    request.addParameter(.key_Line, value: location.line)
    request.addParameter(.key_Column, value: location.column)
    request.addCompilerArgs(args.sourcekitdArgs)

    let completionOptions = request.addDictionaryParameter(.key_CodeCompleteOptions)
    // Don't hide any results because otherwise checking if the expected result is contained will fail
    completionOptions.add(.key_HideLowPriority, value: 0)
    completionOptions.add(.key_HideByName, value: 0)
    completionOptions.add(.key_HideUnderscores, value: 0)
    // Disable inner results because they might cause a second completion to occur in SourceKit, slowing down the measurements.
    completionOptions.add(.key_AddInnerResults, value: 0)
    completionOptions.add(.key_AddInnerOperators, value: 0)

    // Set expected results to 200 to avoid inflating the time measurements by serialization time.
    // To make sure that the expected result is in the results, filter by its base name.
    completionOptions.add(.key_RequestLimit, value: 200)

    for (option, value) in extraOptions {
      if let intValue = Int(value) {
        completionOptions.add(SourceKitdUID(string: "key.codecomplete.\(option)"), value: intValue)
      } else {
        completionOptions.add(SourceKitdUID(string: "key.codecomplete.\(option)"), value: value)
      }
    }
    return request
  }

  func codeComplete(offset: Int, expectedResult: ExpectedResult?) throws -> (request: RequestInfo, response: SourceKitdResponse, instructions: Int, reusingASTContext: Bool) {
    var responseToReport: SourceKitdResponse
    let openRequest = buildCompletionRequest(request: .request_CodeCompleteOpen, offset: offset, extraOptions: extraCodeCompleteOptions)
    let openInfo = RequestInfo.codeCompleteOpen(document: documentInfo, offset: offset,
                                                args: args.sourcekitdArgs)
    defer {
      let closeRequest = buildCompletionRequest(request: .request_CodeCompleteClose, offset: offset, extraOptions: extraCodeCompleteOptions)
      let closeInfo = RequestInfo.codeCompleteClose(document: documentInfo, offset: offset)
      _ = try? sendWithTimeout(closeRequest, info: closeInfo)
    }
    let (openResponse, openInstructions) = try sendWithTimeoutMeasuringInstructions(openRequest, info: openInfo)

    let reusingASTContext = openResponse.value.getBool(.key_ReusingASTContext)

    if let expectedResult = expectedResult {
      var updateOptions = extraCodeCompleteOptions

      updateOptions["filtertext"] = expectedResult.name.base

      let updateRequest = buildCompletionRequest(request: .request_CodeCompleteUpdate, offset: offset, extraOptions: updateOptions)
      let updateInfo = RequestInfo.codeCompleteUpdate(document: documentInfo, offset: offset, args: args.sourcekitdArgs)
      let updateResponse = try sendWithTimeout(updateRequest, info: updateInfo) { response in
        try checkExpectedCompletionResult(expectedResult, in: response, info: openInfo)
      }
      responseToReport = updateResponse
    } else {
      responseToReport = openResponse
    }

    return (openInfo, responseToReport, openInstructions, reusingASTContext)
  }

  func typeContextInfo(offset: Int) throws -> (RequestInfo, SourceKitdResponse) {
    let request = SourceKitdRequest(uid: .request_TypeContextInfo)

    request.addParameter(.key_SourceFile, value: args.forFile.path)
    if let sourceState = sourceState {
      request.addParameter(.key_SourceText, value: sourceState.source)
    }
    request.addParameter(.key_Offset, value: offset)
    request.addCompilerArgs(args.sourcekitdArgs)

    let info = RequestInfo.typeContextInfo(document: documentInfo,
                                           offset: offset,
                                           args: args.sourcekitdArgs)
    let response = try sendWithTimeout(request, info: info)

    return (info, response)
  }

  func conformingMethodList(offset: Int, typeList: [String]) throws -> (RequestInfo, SourceKitdResponse) {
    let request = SourceKitdRequest(uid: .request_ConformingMethodList)

    request.addParameter(.key_SourceFile, value: args.forFile.path)
    if let sourceState = sourceState {
      request.addParameter(.key_SourceText, value: sourceState.source)
    }
    request.addParameter(.key_Offset, value: offset)

    let expressionTypeList = request.addArrayParameter(.key_ExpressionTypeList)
    for type in typeList { expressionTypeList.add(type) }

    request.addCompilerArgs(args.sourcekitdArgs)

    let info = RequestInfo.conformingMethodList(document: documentInfo,
                                                offset: offset,
                                                typeList: typeList,
                                                args: args.sourcekitdArgs)
    let response = try sendWithTimeout(request, info: info)

    return (info, response)
  }

  func collectExpressionType() throws -> (RequestInfo, SourceKitdResponse) {
    let request = SourceKitdRequest(uid: .request_CollectExpressionType)

    request.addParameter(.key_SourceFile, value: args.forFile.path)
    request.addCompilerArgs(args.sourcekitdArgs)

    let info = RequestInfo.collectExpressionType(document: documentInfo,
                                                 args: args.sourcekitdArgs)
    let response = try sendWithTimeout(request, info: info)

    return (info, response)
  }

  private func emitModule() throws {
    guard let sourceState = sourceState else { return }

    let moduleName = tempModulePath.deletingPathExtension().lastPathComponent
    let compilerArgs = self.args.processArgs + [
      // Merge modules ends up about the same speed as WMO when skipping
      // function bodies
      "-whole-module-optimization",
      "-Xfrontend", "-experimental-allow-module-with-compiler-errors",
      "-Xfrontend", "-experimental-skip-all-function-bodies",
      "-emit-module", "-module-name", moduleName, "-emit-module-path",
      tempModulePath.path,
      "-"
    ]

    let swiftcResult = ProcessRunner(launchPath: swiftc,
                                     arguments: compilerArgs)
      .run(input: sourceState.source)
    if swiftcResult.status != EXIT_SUCCESS {
      throw SourceKitError.failed(
        .errorWritingModule,
        request: .writeModule(document: documentInfo, args: compilerArgs),
        response: swiftcResult.stderrStr ?? "<could not decode stderr>")
    }
  }

  func moduleInterfaceGen() throws -> (RequestInfo, SourceKitdResponse) {
    try emitModule()

    let moduleDir = tempModulePath.deletingLastPathComponent()
    let moduleName = tempModulePath.deletingPathExtension().lastPathComponent
    let interfaceFile = moduleDir.appendingPathComponent("<interface-gen>")
    let compilerArgs = self.args.sourcekitdArgs + [
      "-I\(moduleDir.path)",
       "-Xfrontend", "-experimental-allow-module-with-compiler-errors"
    ]

    let request = SourceKitdRequest(uid: .request_EditorOpenInterface)
    request.addParameter(.key_SourceFile, value: args.forFile.path)
    request.addParameter(.key_Name, value: interfaceFile.path)
    request.addParameter(.key_ModuleName, value: moduleName)
    request.addCompilerArgs(compilerArgs)

    let info = RequestInfo.interfaceGen(document: documentInfo,
                                        moduleName: moduleName,
                                        args: compilerArgs)
    let response = try sendWithTimeout(request, info: info)
    return (info, response)
  }

  func replaceText(offset: Int, length: Int, text: String) throws -> (request: RequestInfo, tree: SourceFileSyntax, response: SourceKitdResponse, instructions: Int) {
    let request = SourceKitdRequest(uid: .request_EditorReplaceText)
    request.addParameter(.key_Name, value: args.forFile.path)
    request.addParameter(.key_Offset, value: offset)
    request.addParameter(.key_Length, value: length)
    request.addParameter(.key_SourceText, value: text)

    request.addParameter(.key_EnableSyntaxMap, value: 0)
    request.addParameter(.key_EnableStructure, value: 0)
    request.addParameter(.key_SyntacticOnly, value: 1)

    let info = RequestInfo.editorReplaceText(document: documentInfo, offset: offset, length: length, text: text)
    let response = try sendWithTimeout(request, info: info)

    // update expected source content and syntax tree
    sourceState?.replace(offset: offset, length: length, with: text)
    let instructions = try updateSyntaxTree(request: info).instructions

    return (request: info, tree: tree!, response: response, instructions)
  }

  private func checkExpectedCompletionResult(_ expected: ExpectedResult, in response: SourceKitdResponse, info: RequestInfo) throws {
    let matcher = CompletionMatcher(for: expected)
    var found = false
    response.value.getArray(.key_Results).enumerate { (_, item) -> Bool in
      let result = item.getDictionary()
      found = matcher.match(result.getString(.key_Name), ignoreArgLabels: shouldIgnoreArgs(of: expected, for: result))
      return !found
    }
    if !found {
      // FIXME: code completion responses can be huge, truncate them for now.
      let maxSize = 25_000
      var responseText = response.description
      if responseText.count > maxSize {
        responseText = responseText.prefix(maxSize) + "[truncated]"
      }
      throw SourceKitError.failed(.missingExpectedResult, request: info, response: responseText.trimmingCharacters(in: .newlines))
    }
  }

  private func shouldIgnoreArgs(of expected: ExpectedResult, for result: SourceKitdResponse.Dictionary) -> Bool {
    switch result.getUID(.key_Kind) {
    case .kind_DeclStruct, .kind_DeclClass, .kind_DeclEnum, .kind_DeclTypeAlias, .kind_DeclGenericTypeParam:
      // Initializer calls look like function calls syntactically, but the
      // completion results only include the type name. Allow for that by
      // matching on the base name only.
      return expected.kind == .call
    case .kind_DeclVarGlobal, .kind_DeclVarStatic, .kind_DeclVarClass, .kind_DeclVarInstance, .kind_DeclVarParam, .kind_DeclVarLocal:
      // Any variable/property of function type can be called, and looks the
      // same as a function call as far as the expected result is concerned,
      // but it's name won't have any argument labels.
      // If the expected result is a call that only has empty argument labels
      // (if any), it *may* be in this category, so match on the base name only.
      return expected.kind == .call && expected.name.argLabels.allSatisfy{ $0.isEmpty }
    default:
      return false
    }
  }

  /// Same as `sendWithTimeout` but also report the number of instructions
  /// executed by SourceKit to fulfill the request.
  /// The number of instructions is 0 if SourceKit crashed during the request.
  private func sendWithTimeoutMeasuringInstructions(_ request: SourceKitdRequest, info: RequestInfo, validateResponse: (SourceKitdResponse) throws -> Void = { _ in }) throws -> (response: SourceKitdResponse, instructions: Int) {
    let startInstructions = try getSourceKitInstructionCount()

    func getInstructionCount() throws -> Int {
      let endInstructions = try getSourceKitInstructionCount()
      if endInstructions < startInstructions {
        // sourcekitd crashed and was restarted for the request, which resets its instruction counter.
        // Report 0 to indicate that we were unable to measure the instructions.
        return 0
      } else {
        return endInstructions - startInstructions
      }
    }

    do {
      let response = try sendWithTimeout(request, info: info, validateResponse: validateResponse)
      return (response, try getInstructionCount())
    } catch SourceKitError.softTimeout(request: let request, duration: let duration, instructions: _) {
      throw SourceKitError.softTimeout(request: request, duration: duration, instructions: try getInstructionCount())
    } catch {
      throw error
    }
  }

  /// Send the `request` synchronously, timing out after
  /// `SOURCEKIT_REQUEST_TIMEOUT`.
  /// An error is thrown if either the response is invalid (see `throwIfInvalid`)
  /// or `validateResponse` throws. If the request took longer than half the
  /// time allowed but no other error is thrown, a `SourceKitError.softTimeout`
  /// will be thrown.
  private func sendWithTimeout(_ request: SourceKitdRequest, info: RequestInfo, validateResponse: (SourceKitdResponse) throws -> Void = { _ in }, reopenDocumentIfNeeded: Bool = true) throws -> SourceKitdResponse {
    var response: SourceKitdResponse? = nil
    let completed = DispatchSemaphore(value: 0)
    let startDate = Date()
    connection.send(request: request) {
      response = $0
      completed.signal()
    }

    switch completed.wait(timeout: .now() + DispatchTimeInterval.seconds(SOURCEKIT_REQUEST_TIMEOUT)) {
    case .success:
      guard let response = response else {
        fatalError("nil response without timout being hit?")
      }

      let requestDuration = Date().timeIntervalSince(startDate)

      if response.isError, response.description.contains("No associated Editor Document"), reopenDocumentIfNeeded {
        _ = try self.openOrUpdate(source: sourceState?.source)
        return try sendWithTimeout(request, info: info, validateResponse: validateResponse, reopenDocumentIfNeeded: false)
      } else {
        // Check if there was an error in the response
        try throwIfInvalid(response, request: info)
        try validateResponse(response)

        if requestDuration > TimeInterval(SOURCEKIT_REQUEST_TIMEOUT) / 10 {
          // There was no error in the response, but the request took too long
          // throw a soft timeout.
          throw SourceKitError.softTimeout(request: info, duration: requestDuration, instructions: nil)
        } else {
          return response
        }
      }
    case .timedOut:
      /// We timed out waiting for the response. Any following requests would
      /// not be executed by SourceKit until this one finishes. To avoid this,
      /// and since we don't have cancellation support in SourceKit, crash the
      /// current SourceKitService so we get a new instance that isn't
      /// processing any requests.
      connection.crash()
      throw SourceKitError.timedOut(request: info)
    }
  }

  private func isErrorAllowed(_ errorDescription: String, request: RequestInfo) -> Bool {
    let asyncErrorsToBlock: [String] = [
      "cannot refactor as callback closure argument missing",
      "cannot refactor as callback arguments do not match declaration"
    ]

    // The "Convert Call to Async Alternative" refactoring produces some error
    // responses that are expected and intended to be communicated to users
    // even though CursorInfo reports the refactoring as being applicable. These
    // aren't considered errors in the implementation so ignore them.
    if case .semanticRefactoring(_, _, "Convert Call to Async Alternative", _) = request {
      return !asyncErrorsToBlock.contains { errorDescription.contains($0) }
    }

    // FIXME: We don't supply a valid new name for initializer calls for local
    // rename requests. Ignore these errors for now.
    return errorDescription.contains("does not match the arity of the old name")
  }

  private func throwIfInvalid(_ response: SourceKitdResponse, request: RequestInfo) throws {
    if response.isError && !isErrorAllowed(response.description, request: request) {
      throw SourceKitError.failed(.errorResponse, request: request,
                                  response: response.description.trimmingCharacters(in: .newlines))
    }

    if response.isConnectionInterruptionError || response.isCompilerCrash {
      throw SourceKitError.crashed(request: request)
    }
  }

  private func parseSyntax(request: RequestInfo) throws -> SourceFileSyntax {
    let reparseTransition: IncrementalParseTransition?
    switch request {
    case .editorReplaceText(_, let offset, let length, let text):
      let edit = IncrementalEdit(range: ByteSourceRange(offset: offset, length: length), replacementLength: text.utf8.count)
      reparseTransition = IncrementalParseTransition(previousTree: self.tree!, edits: ConcurrentEdits(edit), lookaheadRanges: self.lookaheadRanges!)
    default:
      reparseTransition = nil
    }

    var tree: SourceFileSyntax
    if let state = sourceState {
      (tree, lookaheadRanges) = Parser.parseIncrementally(
        source: state.source,
        parseTransition: reparseTransition
      )
    } else {
      let fileData = try Data(contentsOf: args.forFile)
      let source = fileData.withUnsafeBytes { buf in
        return String(decoding: buf.bindMemory(to: UInt8.self), as: UTF8.self)
      }
      tree = Parser.parse(source: source)
    }
    if let foldedTree = OperatorTable.standardOperators.foldAll(tree, errorHandler: { _ in }).as(SourceFileSyntax.self) {
      tree = foldedTree
    }
    _ = ParseDiagnosticsGenerator.diagnostics(for: tree)
    return tree
  }

  @discardableResult
  private func updateSyntaxTree(request: RequestInfo) throws -> (tree: SourceFileSyntax, instructions: Int) {
    let tree: SourceFileSyntax
    let executedInstruction: Int
    do {
      let previousInstructionCount = Int(get_current_instruction_count())
      tree = try parseSyntax(request: request)
      executedInstruction = Int(get_current_instruction_count()) - previousInstructionCount
    } catch let error {
      throw SourceKitError.failed(.errorResponse, request: request, response: error.localizedDescription)
    }
    self.tree = tree
    self.converter = SourceLocationConverter(file: "", tree: tree)

    if let state = sourceState, state.source != tree.description {
      // FIXME: add state and tree descriptions in their own field
      let comparison = """
        --source-state------
        \(state.source)
        --tree-description--
        \(tree.description)
        --end---------------
        """
      throw SourceKitError.failed(.sourceAndSyntaxTreeMismatch, request: request, response: comparison)
    }

    return (tree, executedInstruction)
  }
}

/// Tracks the current state of a source file
public struct SourceState {
  public let mode: RewriteMode
  public var source: String
  public var wasModified: Bool

  public init(rewriteMode: RewriteMode, content source: String, wasModified: Bool = false) {
    self.mode = rewriteMode
    self.source = source
    self.wasModified = wasModified
  }

  /// - returns: true if source state changed
  @discardableResult
  public mutating func replace(offset: Int, length: Int, with text: String) -> Bool {
    let bytes = source.utf8
    let prefix = bytes.prefix(upTo: bytes.index(bytes.startIndex, offsetBy: offset))
    let suffix = bytes.suffix(from: bytes.index(bytes.startIndex, offsetBy: offset + length))
    source = String(prefix)! + text + String(suffix)!
    let changed = length > 0 || !text.isEmpty
    wasModified = wasModified || changed
    return changed
  }
}

public struct CompletionMatcher {
  private let expected: ExpectedResult

  public init(for expected: ExpectedResult) {
    self.expected = expected
  }

  /// - returns: true if a match was found
  public func match(_ result: String, ignoreArgLabels: Bool) -> Bool {
    if ignoreArgLabels {
      let name = SwiftName(result)!
      return name.base == expected.name.base
    }
    // Check if the base name and/or argument labels match based on the expected
    // result kind.
    return matches(name: result)
  }

  private func matches(name: String) -> Bool {
    let resultName = SwiftName(name)!
    guard resultName.base == expected.name.base else { return false }
    switch expected.kind {
    case .call:
      return name.last == ")" && matchesCall(paramLabels: resultName.argLabels)
    case .reference:
      return expected.name.argLabels.isEmpty || expected.name.argLabels == resultName.argLabels
    case .pattern:
      // If the expected result didn't match on the associated value: it matches
      if expected.name.argLabels.isEmpty {
        return true
      }

      // Result names for enum cases work differently to functions in that
      // unlabelled items in the associated values aren't represented, e.g.:
      //   case foo              // name: foo
      //   case foo(Int, Int)    // name: foo()
      //   case foo(x: Int, Int) // name: foo(x:)
      //   case foo(Int, y: Int) // name: foo(y:)

      // If the result name doesn't have an associated value, but the expected
      // name does: it doesn't match
      if name.last != ")" && !expected.name.argLabels.isEmpty {
        return false
      }

      // If the expected result bound the entire associated value to a single
      // unlabelled variable, we're done
      if expected.name.argLabels == [""] {
        return true
      }

      // Otherwise the expected argument labels must either match the
      // corresponding result arg labels or be "" since they don't have to be
      // specified when pattern matching.
      var unmatched = resultName.argLabels[...]
      return expected.name.argLabels.allSatisfy{ label in
        if label.isEmpty { return true }
        if let labelIndex = unmatched.firstIndex(of: label) {
          unmatched = unmatched.dropFirst(labelIndex - unmatched.startIndex + 1)
          return true
        }
        return false
      }
    }
  }

  private func matchesCall(paramLabels: [String]) -> Bool {
    var remainingArgLabels = expected.name.argLabels[...]

    guard !paramLabels.isEmpty else {
      return remainingArgLabels.allSatisfy { $0.isEmpty }
    }
    for nextParamLabel in paramLabels {
      if nextParamLabel.isEmpty {
        // No label
        if let first = remainingArgLabels.first, first.isEmpty {
          // Matched - consume the argument
          _ = remainingArgLabels.removeFirst()
        } else {
          // Assume this was defaulted and skip over it
          continue
        }
      } else {
        // Has param label
        if remainingArgLabels.count < expected.name.argLabels.count {
          // A previous param was matched, so assume it was variadic and consume
          // any leading unlabelled args so the next arg is labelled
          remainingArgLabels = remainingArgLabels.drop{ $0.isEmpty }
        }
        guard let nextArgLabel = remainingArgLabels.first else {
          // Assume any unprocessed parameters are defaulted
          return true
        }
        if nextArgLabel == nextParamLabel {
          // Matched - consume the argument
          _ = remainingArgLabels.removeFirst()
          continue
        }
        // Else assume this param was defaulted and skip it.
      }
    }
    // If at least one arglabel was matched, allow for it being variadic
    let hadMatch = remainingArgLabels.count < expected.name.argLabels.count
    return  remainingArgLabels.isEmpty || hadMatch &&
      remainingArgLabels.allSatisfy { $0.isEmpty }
  }
}