12
12
13
13
import ArgumentParser
14
14
import Foundation
15
+ import SKSupport
15
16
import SourceKitD
16
17
17
18
import struct TSCBasic. AbsolutePath
@@ -35,15 +36,32 @@ public struct SourceKitdRequestCommand: AsyncParsableCommand {
35
36
)
36
37
var sourcekitdRequestPath : String
37
38
39
+ @Option ( help: " line:column override for key.offset " )
40
+ var position : String ?
41
+
38
42
public init ( ) { }
39
43
40
44
public func run( ) async throws {
41
- let requestString = try String ( contentsOf: URL ( fileURLWithPath: sourcekitdRequestPath) )
45
+ var requestString = try String ( contentsOf: URL ( fileURLWithPath: sourcekitdRequestPath) )
42
46
43
47
let sourcekitd = try DynamicallyLoadedSourceKitD . getOrCreate (
44
48
dylibPath: try ! AbsolutePath ( validating: sourcekitdPath)
45
49
)
46
50
51
+ if let lineColumn = position? . split ( separator: " : " , maxSplits: 2 ) . map ( Int . init) ,
52
+ lineColumn. count == 2 ,
53
+ let line = lineColumn [ 0 ] ,
54
+ let column = lineColumn [ 1 ]
55
+ {
56
+ let requestInfo = try RequestInfo ( request: requestString)
57
+
58
+ let lineTable = LineTable ( requestInfo. fileContents)
59
+ if let offset = lineTable. utf8OffsetOf ( line: line - 1 , utf8Column: column - 1 ) {
60
+ print ( " Adjusting request offset to \( offset) " )
61
+ requestString. replace ( #/key.offset: [0-9]+/# , with: " key.offset: \( offset) " )
62
+ }
63
+ }
64
+
47
65
let request = try requestString. cString ( using: . utf8) !. withUnsafeBufferPointer { buffer in
48
66
var error : UnsafeMutablePointer < CChar > ?
49
67
let req = sourcekitd. api. request_create_from_yaml ( buffer. baseAddress!, & error) !
@@ -60,7 +78,17 @@ public struct SourceKitdRequestCommand: AsyncParsableCommand {
60
78
}
61
79
62
80
switch response. error {
63
- case . requestFailed, . requestInvalid, . requestCancelled, . missingRequiredSymbol:
81
+ case . requestFailed( let message) :
82
+ print ( message)
83
+ throw ExitCode ( 1 )
84
+ case . requestInvalid( let message) :
85
+ print ( message)
86
+ throw ExitCode ( 1 )
87
+ case . requestCancelled:
88
+ print ( " request cancelled " )
89
+ throw ExitCode ( 1 )
90
+ case . missingRequiredSymbol:
91
+ print ( " missing required symbol " )
64
92
throw ExitCode ( 1 )
65
93
case . connectionInterrupted:
66
94
throw ExitCode ( 255 )
0 commit comments